예제 #1
0
        public void SetPrivateKey()
        {
            string key;

            if (IsContentType("application/json"))
            {
                JObject result = GetJsonContent();
                key = result == null ? null : result.Value <string>(KeyParameterName);
            }
            else
            {
                // any other content-type assuming the content is key
                // curl http://server/sshkey -X PUT --upload-file /c/temp/id_rsa
                key = Request.Content.ReadAsStringAsync().Result;
            }

            if (String.IsNullOrEmpty(key))
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentNullException(KeyParameterName)));
            }

            using (_tracer.Step("SSHKeyController.SetPrivateKey"))
            {
                // This is not what we want
                bool success = _sshKeyLock.TryLockOperation(() =>
                {
                    try
                    {
                        _sshKeyManager.SetPrivateKey(key);
                    }
                    catch (ArgumentException ex)
                    {
                        throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                    }
                    catch (InvalidOperationException ex)
                    {
                        throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, ex));
                    }
                }, TimeSpan.FromSeconds(LockTimeoutSecs));

                if (!success)
                {
                    throw new HttpResponseException(Request.CreateErrorResponse(
                                                        HttpStatusCode.Conflict,
                                                        String.Format(CultureInfo.CurrentCulture, Resources.Error_OperationLockTimeout, LockTimeoutSecs)));
                }
            }
        }
예제 #2
0
        // acquire lock and then execute the operation
        public static void LockOperation(this IOperationLock lockObj, Action operation, TimeSpan timeout)
        {
            bool success = lockObj.TryLockOperation(operation, timeout);

            if (!success)
            {
                throw new InvalidOperationException("Unable to acquire lock within a given time.");
            }
        }
        public void Delete(int deleteWebRoot = 0, int ignoreErrors = 0)
        {
            // Fail if a deployment is in progress
            bool acquired = _deploymentLock.TryLockOperation(() =>
            {
                using (_tracer.Step("Deleting repository"))
                {
                    string repositoryPath = Path.Combine(_environment.SiteRootPath, Constants.RepositoryPath);
                    if (String.Equals(repositoryPath, _environment.RepositoryPath, StringComparison.OrdinalIgnoreCase))
                    {
                        // Delete the repository
                        FileSystemHelpers.DeleteDirectorySafe(_environment.RepositoryPath, ignoreErrors != 0);
                    }
                    else
                    {
                        // Just delete .git folder
                        FileSystemHelpers.DeleteDirectorySafe(Path.Combine(_environment.RepositoryPath, ".git"), ignoreErrors != 0);

                        FileSystemHelpers.DeleteDirectorySafe(Path.Combine(_environment.RepositoryPath, ".hg"), ignoreErrors != 0);
                    }
                }

                using (_tracer.Step("Deleting ssh key"))
                {
                    // Delete the ssh key
                    FileSystemHelpers.DeleteDirectorySafe(_environment.SSHKeyPath, ignoreErrors != 0);
                }

                if (deleteWebRoot != 0)
                {
                    using (_tracer.Step("Deleting web root"))
                    {
                        // Delete the wwwroot folder
                        FileSystemHelpers.DeleteDirectoryContentsSafe(_environment.WebRootPath, ignoreErrors != 0);
                    }

                    using (_tracer.Step("Deleting diagnostics"))
                    {
                        // Delete the diagnostic log. This is a slight abuse of deleteWebRoot, but the
                        // real semantic is more to reset the site to a fully clean state
                        FileSystemHelpers.DeleteDirectorySafe(_environment.DiagnosticsPath, ignoreErrors != 0);
                    }
                }

                using (_tracer.Step("Deleting deployment cache"))
                {
                    // Delete the deployment cache
                    FileSystemHelpers.DeleteDirectorySafe(_environment.DeploymentsPath, ignoreErrors != 0);
                }
            }, TimeSpan.Zero);

            if (!acquired)
            {
                HttpResponseMessage response = Request.CreateErrorResponse(HttpStatusCode.Conflict, Resources.Error_DeploymentInProgess);
                throw new HttpResponseException(response);
            }
        }
예제 #4
0
        // acquire lock and then execute the operation
        public static void LockOperation(this IOperationLock lockObj, Action operation, TimeSpan timeout)
        {
            bool success = lockObj.TryLockOperation(operation, timeout);

            if (!success)
            {
                throw new LockOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_OperationLockTimeout, timeout.TotalSeconds));
            }
        }
예제 #5
0
        // acquire lock and then execute the operation
        public static void LockOperation(this IOperationLock lockObj, Action operation, string operationName, TimeSpan timeout)
        {
            bool success = lockObj.TryLockOperation(operation, operationName, timeout);

            if (!success)
            {
                var lockInfo = lockObj.LockInfo;
                throw new LockOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_OperationLockTimeout, operationName, lockInfo.OperationName, lockInfo.AcquiredDateTime));
            }
        }
예제 #6
0
        public WebHook AddWebHook(WebHook webHook)
        {
            using (_tracer.Step("WebHooksManager.AddWebHook"))
            {
                if (!Uri.IsWellFormedUriString(webHook.HookAddress, UriKind.RelativeOrAbsolute))
                {
                    throw new FormatException(Resources.Error_InvalidHookAddress.FormatCurrentCulture(webHook.HookAddress));
                }

                WebHook createdWebHook = null;

                bool lockAcquired = _hooksLock.TryLockOperation(() =>
                {
                    var webHooks            = new List <WebHook>(ReadWebHooksFromFile());
                    WebHook existingWebHook = webHooks.FirstOrDefault(h => String.Equals(h.HookAddress, webHook.HookAddress, StringComparison.OrdinalIgnoreCase));

                    if (existingWebHook == null)
                    {
                        // if web hook doesn't exist (by address) then add it
                        createdWebHook = new WebHook(webHook.HookEventType, webHook.HookAddress, id: DateTime.UtcNow.Ticks.ToString(), insecureSsl: webHook.InsecureSsl);
                        webHooks.Add(createdWebHook);
                        SaveHooksToFile(webHooks);

                        _tracer.Trace("Added web hook: type - {0}, address - {1}", createdWebHook.HookEventType, createdWebHook.HookAddress);
                    }
                    else if (String.Equals(webHook.HookEventType, existingWebHook.HookEventType, StringComparison.OrdinalIgnoreCase))
                    {
                        // if web hook exist with the same hook event type, return the existing one
                        createdWebHook = existingWebHook;
                    }
                    else
                    {
                        // if web hook exists but with a different hook event type then throw a conflict exception
                        throw new ConflictException();
                    }
                }, LockTimeout);

                VerifyLockAcquired(lockAcquired);

                return(createdWebHook);
            }
        }
예제 #7
0
        public static void LockHttpOperation(this IOperationLock lockObj, Action action)
        {
            bool acquired = lockObj.TryLockOperation(action, TimeSpan.Zero);

            if (!acquired)
            {
                var response = new HttpResponseMessage(HttpStatusCode.Conflict);
                response.Content = new StringContent(Resources.Error_DeploymentInProgess);
                throw new HttpResponseException(response);
            }
        }
예제 #8
0
        // acquire lock and then execute the operation
        public static T LockOperation <T>(this IOperationLock lockObj, Func <T> operation, TimeSpan timeout)
        {
            T    result  = default(T);
            bool success = lockObj.TryLockOperation(() => result = operation(), timeout);

            if (!success)
            {
                throw new InvalidOperationException("Unable to acquire lock within a given time.");
            }

            return(result);
        }
예제 #9
0
        // acquire lock and then execute the operation
        public static T LockOperation <T>(this IOperationLock lockObj, Func <T> operation, TimeSpan timeout)
        {
            T    result  = default(T);
            bool success = lockObj.TryLockOperation(() => result = operation(), timeout);

            if (!success)
            {
                throw new LockOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_OperationLockTimeout, timeout.TotalSeconds));
            }

            return(result);
        }
예제 #10
0
        // acquire lock and then execute the operation
        public static T LockOperation <T>(this IOperationLock lockObj, Func <T> operation, string operationName, TimeSpan timeout)
        {
            T    result  = default(T);
            bool success = lockObj.TryLockOperation(() => result = operation(), operationName, timeout);

            if (!success)
            {
                var lockInfo = lockObj.LockInfo;
                throw new LockOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_OperationLockTimeout, operationName, lockInfo.OperationName, lockInfo.AcquiredDateTime));
            }

            return(result);
        }
예제 #11
0
        public void Delete(int deleteWebRoot = 0, int ignoreErrors = 0)
        {
            // Fail if a deployment is in progress
            bool acquired = _deploymentLock.TryLockOperation(() =>
            {
                using (_tracer.Step("Deleting repository"))
                {
                    string repositoryPath = Path.Combine(_environment.SiteRootPath, Constants.RepositoryPath);
                    if (String.Equals(repositoryPath, _environment.RepositoryPath, StringComparison.OrdinalIgnoreCase))
                    {
                        // Delete the repository
                        FileSystemHelpers.DeleteDirectorySafe(_environment.RepositoryPath, ignoreErrors != 0);
                    }
                    else
                    {
                        // Just delete .git folder
                        FileSystemHelpers.DeleteDirectorySafe(Path.Combine(_environment.RepositoryPath, ".git"), ignoreErrors != 0);

                        FileSystemHelpers.DeleteDirectorySafe(Path.Combine(_environment.RepositoryPath, ".hg"), ignoreErrors != 0);
                    }
                }

                using (_tracer.Step("Delete auto swap lock file"))
                {
                    FileSystemHelpers.DeleteFileSafe(Path.Combine(_environment.LocksPath, AutoSwapHandler.AutoSwapLockFile));
                }

                using (_tracer.Step("Deleting ssh key"))
                {
                    // Delete the ssh key
                    FileSystemHelpers.DeleteDirectorySafe(_environment.SSHKeyPath, ignoreErrors != 0);
                }

                if (deleteWebRoot != 0)
                {
                    // This logic is primarily used to help with site reuse during test.
                    // The flag is not documented for general use.

                    using (_tracer.Step("Deleting web root"))
                    {
                        // Delete the wwwroot folder
                        FileSystemHelpers.DeleteDirectoryContentsSafe(_environment.WebRootPath, ignoreErrors != 0);
                    }

                    using (_tracer.Step("Deleting diagnostics"))
                    {
                        // Delete the diagnostic log. This is a slight abuse of deleteWebRoot, but the
                        // real semantic is more to reset the site to a fully clean state
                        FileSystemHelpers.DeleteDirectorySafe(_environment.DiagnosticsPath, ignoreErrors != 0);
                    }

                    using (_tracer.Step("Deleting ASP.NET 5 approot"))
                    {
                        // Delete the approot folder used by ASP.NET 5 apps
                        FileSystemHelpers.DeleteDirectorySafe(Path.Combine(_environment.SiteRootPath, "approot"), ignoreErrors != 0);
                    }

                    // Delete first deployment manifest since it is no longer needed
                    FileSystemHelpers.DeleteFileSafe(Path.Combine(_environment.SiteRootPath, Constants.FirstDeploymentManifestFileName));
                }
                else
                {
                    using (_tracer.Step("Updating initial deployment manifest"))
                    {
                        // The active deployment manifest becomes the baseline initial deployment manifest
                        // When SCM is reconnected, the new deployment will use this manifest to clean the wwwroot
                        SaveInitialDeploymentManifest();
                    }
                }

                using (_tracer.Step("Deleting deployment cache"))
                {
                    // Delete the deployment cache
                    FileSystemHelpers.DeleteDirectorySafe(_environment.DeploymentsPath, ignoreErrors != 0);
                }
            }, TimeSpan.Zero);

            if (!acquired)
            {
                HttpResponseMessage response = Request.CreateErrorResponse(HttpStatusCode.Conflict, Resources.Error_DeploymentInProgess);
                throw new HttpResponseException(response);
            }
        }