Esempio n. 1
0
        public HttpResponseMessage Post(ScriptJobDescriptor descriptor)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, this.GetValidationErrorMessages()));
            }

            var result = _scriptExecutionService.ExecuteScript(descriptor);

            return(Request.CreateResponse(HttpStatusCode.OK, result));
        }
        private void ExecuteDeploymentSteps(DeploymentJob job)
        {
            var planSteps = _deploymentPlanStepService.GetEnabledForDeploymentPlan(job.DeploymentPlanID)
                            .OrderBy(s => s.ExecutionOrder)
                            .ToList();

            if (planSteps.IsNullOrEmpty())
            {
                return;
            }

            var targetGroupIDs = _targetGroupService.GetAllForProject(job.ProjectID)
                                 .OrderBy(g => g.Name)
                                 .Select(g => g.TargetGroupID)
                                 .ToArray();

            var environmentName = _projectEnvironmentService.GetByKey(job.ProjectEnvironmentID)
                                  .Name;

            foreach (var planStep in planSteps)
            {
                var jobStep = new DeploymentJobStep
                {
                    DeploymentJobID      = job.DeploymentJobID,
                    DeploymentPlanStepID = planStep.DeploymentPlanStepID,
                    Status             = DeploymentStatus.Running,
                    StartTime          = DateTime.UtcNow,
                    ExecutionReference = Guid.NewGuid()
                };
                _deploymentJobStepService.Insert(jobStep);

                try
                {
                    var script = _scriptService.GetWithParameters(planStep.ScriptID);

                    if (planStep.AllTargetGroups)
                    {
                        this.ExecuteDeploymentStepOnTargets(script, planStep, job, environmentName,
                                                            jobStep.DeploymentJobStepID, targetGroupIDs);
                    }
                    else if (planStep.TargetGroupID.HasValue)
                    {
                        this.ExecuteDeploymentStepOnTargets(script, planStep, job, environmentName,
                                                            jobStep.DeploymentJobStepID, planStep.TargetGroupID.Value);
                    }
                    else  // Execute on deployment server
                    {
                        jobStep.ExecutedScript = _variableService.ResolveVariables(
                            script, planStep, job, environmentName);
                        jobStep.ExecutionOutput = "Waiting for output...";
                        _deploymentJobStepService.Update(jobStep);

                        var descriptor = new ScriptJobDescriptor
                        {
                            ScriptType      = script.ScriptType,
                            ScriptBody      = jobStep.ExecutedScript,
                            SuccessKeywords = script.SuccessKeywords,
                            FailureKeywords = script.FailureKeywords
                        };
                        var result = _scriptExecutionService.ExecuteScript(descriptor);

                        jobStep.ExecutionOutput = result.Output;
                        if (!result.IsSuccessful)
                        {
                            throw new Exception("Script execution on deployment server was not successful.");
                        }
                    }

                    jobStep.Status = DeploymentStatus.Finished;
                }
                catch (Exception ex)
                {
                    jobStep.Status = DeploymentStatus.Failed;
                    throw new Exception("Deployment step [{0}] failed.".FormatString(planStep.Name), ex);
                }
                finally
                {
                    jobStep.EndTime = DateTime.UtcNow;
                    _deploymentJobStepService.Update(jobStep);
                }
            }
        }