Beispiel #1
0
        // <summary>
        // This method initializes status,ssh,hooks & deployment locks used by Kudu to ensure
        // synchronized operations. This method creates a dictionary of locks which is injected
        // into various controllers to resolve the locks they need.
        // <list type="bullet">
        //     <listheader>
        //         <term>Locks used by Kudu:</term>
        //     </listheader>
        //     <item>
        //         <term>Status Lock</term>
        //         <description>Used by DeploymentStatusManager</description>
        //     </item>
        //     <item>
        //         <term>SSH Lock</term>
        //         <description>Used by SSHKeyController</description>
        //     </item>
        //     <item>
        //         <term>Hooks Lock</term>
        //         <description>Used by WebHooksManager</description>
        //     </item>
        //     <item>
        //         <term>Deployment Lock</term>
        //         <description>
        //             Used by DeploymentController, DeploymentManager, SettingsController,
        //             FetchDeploymentManager, LiveScmController, ReceivePackHandlerMiddleware
        //         </description>
        //     </item>
        // </list>
        // </summary>
        // <remarks>
        //     Uses File watcher.
        //     This originally used Ninject's "WhenInjectedInto" in .Net project for specific instances. IServiceCollection
        //     doesn't support this concept, or anything similar like named instances. There are a few possibilities,
        //     but the hack solution for now is just injecting a dictionary of locks and letting each dependent resolve
        //     the one it needs.
        // </remarks>
        private static void SetupLocks(ITraceFactory traceFactory, IEnvironment environment)
        {
            var lockPath           = Path.Combine(environment.SiteRootPath, Constants.LockPath);
            var deploymentLockPath = Path.Combine(lockPath, Constants.DeploymentLockFile);
            var statusLockPath     = Path.Combine(lockPath, Constants.StatusLockFile);
            var sshKeyLockPath     = Path.Combine(lockPath, Constants.SSHKeyLockFile);
            var hooksLockPath      = Path.Combine(lockPath, Constants.HooksLockFile);

            _deploymentLock = DeploymentLockFile.GetInstance(deploymentLockPath, traceFactory);
            _deploymentLock.InitializeAsyncLocks();

            var statusLock = new LockFile(statusLockPath, traceFactory);

            statusLock.InitializeAsyncLocks();
            var sshKeyLock = new LockFile(sshKeyLockPath, traceFactory);

            sshKeyLock.InitializeAsyncLocks();
            var hooksLock = new LockFile(hooksLockPath, traceFactory);

            hooksLock.InitializeAsyncLocks();

            _namedLocks = new Dictionary <string, IOperationLock>
            {
                { "status", statusLock },
                { "ssh", sshKeyLock },
                { "hooks", hooksLock },
                { "deployment", _deploymentLock }
            };
        }
        // key goal is to create background tracer that is independent of request.
        public static async Task <bool> PerformBackgroundDeployment(
            DeploymentInfoBase deployInfo,
            IEnvironment environment,
            IDeploymentSettingsManager settings,
            TraceLevel traceLevel,
            Uri uri,
            bool waitForTempDeploymentCreation)
        {
            var tracer       = traceLevel <= TraceLevel.Off ? NullTracer.Instance : new CascadeTracer(new XmlTracer(environment.TracePath, traceLevel), new ETWTracer(environment.RequestId, "POST"));
            var traceFactory = new TracerFactory(() => tracer);

            var backgroundTrace = tracer.Step(XmlTracer.BackgroundTrace, new Dictionary <string, string>
            {
                { "url", uri.AbsolutePath },
                { "method", "POST" }
            });

            // For waiting on creation of temp deployment
            var tempDeploymentCreatedTcs = new TaskCompletionSource <object>();

            // For determining whether or not we failed to create the deployment due to lock contention.
            // Needed for deployments where deferred deployment is not allowed. Will be set to false if
            // lock contention occurs and AllowDeferredDeployment is false, otherwise true.
            var deploymentWillOccurTcs = new TaskCompletionSource <bool>();

            // This task will be let out of scope intentionally
            var deploymentTask = Task.Run(() =>
            {
                try
                {
                    // lock related
                    string lockPath           = Path.Combine(environment.SiteRootPath, Constants.LockPath);
                    string deploymentLockPath = Path.Combine(lockPath, Constants.DeploymentLockFile);
                    string statusLockPath     = Path.Combine(lockPath, Constants.StatusLockFile);
                    string hooksLockPath      = Path.Combine(lockPath, Constants.HooksLockFile);
                    var statusLock            = new LockFile(statusLockPath, traceFactory);
                    var hooksLock             = new LockFile(hooksLockPath, traceFactory);
                    var deploymentLock        = DeploymentLockFile.GetInstance(deploymentLockPath, traceFactory);

                    var analytics = new Analytics(settings, new ServerConfiguration(), traceFactory);
                    var deploymentStatusManager = new DeploymentStatusManager(environment, analytics, statusLock);
                    var siteBuilderFactory      = new SiteBuilderFactory(new BuildPropertyProvider(), environment);
                    var webHooksManager         = new WebHooksManager(tracer, environment, hooksLock);
                    var deploymentManager       = new DeploymentManager(siteBuilderFactory, environment, traceFactory, analytics, settings, deploymentStatusManager, deploymentLock, NullLogger.Instance, webHooksManager);
                    var fetchDeploymentManager  = new FetchDeploymentManager(settings, environment, tracer, deploymentLock, deploymentManager, deploymentStatusManager);

                    IDisposable tempDeployment = null;

                    try
                    {
                        // Perform deployment
                        deploymentLock.LockOperation(() =>
                        {
                            deploymentWillOccurTcs.TrySetResult(true);

                            ChangeSet tempChangeSet = null;
                            if (waitForTempDeploymentCreation)
                            {
                                // create temporary deployment before the actual deployment item started
                                // this allows portal ui to readily display on-going deployment (not having to wait for fetch to complete).
                                // in addition, it captures any failure that may occur before the actual deployment item started
                                tempDeployment = deploymentManager.CreateTemporaryDeployment(
                                    Resources.ReceivingChanges,
                                    out tempChangeSet,
                                    deployInfo.TargetChangeset,
                                    deployInfo.Deployer);

                                tempDeploymentCreatedTcs.TrySetResult(null);
                            }

                            fetchDeploymentManager.PerformDeployment(deployInfo, tempDeployment, tempChangeSet).Wait();
                        }, "Performing continuous deployment", TimeSpan.Zero);
                    }
                    catch (LockOperationException)
                    {
                        if (tempDeployment != null)
                        {
                            tempDeployment.Dispose();
                        }

                        if (deployInfo.AllowDeferredDeployment)
                        {
                            deploymentWillOccurTcs.TrySetResult(true);

                            using (tracer.Step("Update pending deployment marker file"))
                            {
                                // REVIEW: This makes the assumption that the repository url is the same.
                                // If it isn't the result would be buggy either way.
                                FileSystemHelpers.SetLastWriteTimeUtc(fetchDeploymentManager._markerFilePath, DateTime.UtcNow);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    tracer.TraceError(ex);
                }
                finally
                {
                    // Will no-op if already set
                    deploymentWillOccurTcs.TrySetResult(false);
                    backgroundTrace.Dispose();
                }
            });

#pragma warning disable 4014
            // Run on BG task (Task.Run) to avoid ASP.NET Request thread terminated with request completion and
            // it doesn't get chance to clean up the pending marker.
            Task.Run(() => PostDeploymentHelper.TrackPendingOperation(deploymentTask, TimeSpan.Zero));
#pragma warning restore 4014

            // When the frontend/ARM calls /deploy with isAsync=true, it starts polling
            // the deployment status immediately, so it's important that the temp deployment
            // is created before we return.
            if (waitForTempDeploymentCreation)
            {
                // deploymentTask may return withoout creating the temp deployment (lock contention,
                // other exception), in which case just continue.
                await Task.WhenAny(tempDeploymentCreatedTcs.Task, deploymentTask);
            }

            // If deferred deployment is not permitted, we need to know whether or not the deployment was
            // successfully requested. Otherwise, to preserve existing behavior, we assume it was.
            if (!deployInfo.AllowDeferredDeployment)
            {
                return(await deploymentWillOccurTcs.Task);
            }
            else
            {
                return(true);
            }
        }
Beispiel #3
0
        private static int Main(string[] args)
        {
            var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());

            XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

            // Turn flag on in app.config to wait for debugger on launch
            if (ConfigurationManager.AppSettings["WaitForDebuggerOnStart"] == "true")
            {
                while (!Debugger.IsAttached)
                {
                    System.Threading.Thread.Sleep(100);
                }
            }

            if (System.Environment.GetEnvironmentVariable(SettingsKeys.DisableDeploymentOnPush) == "1")
            {
                return(0);
            }

            if (args.Length < 2)
            {
                System.Console.WriteLine("Usage: kudu.exe appRoot wapTargets [deployer]");
                return(1);
            }

            // The post receive hook launches the exe from sh and intereprets newline differently.
            // This fixes very wacky issues with how the output shows up in the console on push
            System.Console.Error.NewLine = "\n";
            System.Console.Out.NewLine   = "\n";

            string appRoot    = args[0];
            string wapTargets = args[1];
            string deployer   = args.Length == 2 ? null : args[2];
            string requestId  = System.Environment.GetEnvironmentVariable(Constants.RequestIdHeader);

            IEnvironment env      = GetEnvironment(appRoot, requestId);
            ISettings    settings = new XmlSettings.Settings(GetSettingsPath(env));
            IDeploymentSettingsManager settingsManager = new DeploymentSettingsManager(settings);

            // Setup the trace
            TraceLevel    level        = settingsManager.GetTraceLevel();
            ITracer       tracer       = GetTracer(env, level);
            ITraceFactory traceFactory = new TracerFactory(() => tracer);

            // Calculate the lock path
            string lockPath           = Path.Combine(env.SiteRootPath, Constants.LockPath);
            string deploymentLockPath = Path.Combine(lockPath, Constants.DeploymentLockFile);

            IOperationLock deploymentLock = DeploymentLockFile.GetInstance(deploymentLockPath, traceFactory);

            if (deploymentLock.IsHeld)
            {
                return(PerformDeploy(appRoot, wapTargets, deployer, lockPath, env, settingsManager, level, tracer, traceFactory, deploymentLock));
            }

            // Cross child process lock is not working on linux via mono.
            // When we reach here, deployment lock must be HELD! To solve above issue, we lock again before continue.
            try
            {
                return(deploymentLock.LockOperation(() =>
                {
                    return PerformDeploy(appRoot, wapTargets, deployer, lockPath, env, settingsManager, level, tracer, traceFactory, deploymentLock);
                }, "Performing deployment", TimeSpan.Zero));
            }
            catch (LockOperationException)
            {
                return(-1);
            }
        }