Esempio n. 1
0
        private void ProcessWebSiteDeploymentStep(DeploymentStep deploymentStep)
        {
            dynamic bindings = JsonConvert.DeserializeObject(this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("IIS.Bindings")));

            string configuration = JsonConvert.SerializeObject(new
            {
                destination         = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("IIS.DestinationPath")),
                siteName            = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("IIS.SiteName")),
                applicationPoolName = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("IIS.SiteName")),
                projectId           = deploymentStep.GetIntProperty("ProjectId"),
                bindings            = ((IEnumerable <dynamic>)bindings)
            });

            this.deploymentClient.DeployWebSite(configuration);
        }
Esempio n. 2
0
        private void ProcessDacpacStep(DeploymentStep deploymentStep)
        {
            dynamic customConfig = JsonConvert.DeserializeObject(this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("CustomConfiguration")));

            string configuration = JsonConvert.SerializeObject(new
            {
                dacpacFileName              = "database.dacpac",
                connectionString            = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("ConnectionString")),
                targetDatabase              = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("TargetDatabase")),
                projectId                   = deploymentStep.GetIntProperty("ProjectId"),
                backupDatabaseBeforeChanges = (bool)customConfig.backupDatabaseBeforeChanges,
                blockOnPossibleDataLoss     = (bool)customConfig.blockOnPossibleDataLoss,
            });

            this.deploymentClient.ApplyDacpac(configuration);
        }
Esempio n. 3
0
        private void ProcessCopyFilesStep(DeploymentStep deploymentStep)
        {
            string mode = "replace";

            if (!string.IsNullOrWhiteSpace(deploymentStep.GetStringProperty("CustomConfiguration")))
            {
                dynamic customConfig = deploymentStep.GetDynamicProperty("CustomConfiguration");

                mode = customConfig.mode ?? "replace";
            }

            string configuration = JsonConvert.SerializeObject(new
            {
                destination = this.variableProcessor.ProcessValue(deploymentStep.GetStringProperty("DestinationPath")),
                projectId   = deploymentStep.GetIntProperty("ProjectId"),
                mode        = mode
            });

            this.deploymentClient.CopyFiles(configuration);
        }
Esempio n. 4
0
        public ActionResult EditStep(int id, int deploymentStepId)
        {
            this.CheckPermission(UserRoleAction.DeploymentChangeSteps);

            DeploymentStep deploymentStep = this.Entities.DeploymentStep
                                            .Include("Properties")
                                            .Include("MachineRoles")
                                            .Include("BundleVersion.Bundle")
                                            .First(ds => ds.Id == deploymentStepId && ds.BundleVersion.Id == id);

            List <MachineRole> machineRoles = this.Entities.MachineRole.ToList();

            this.ViewBag.DeploymentStep = deploymentStep;
            this.ViewBag.BundleVersion  = deploymentStep.BundleVersion;
            this.ViewBag.MachineRoles   = machineRoles;

            if (deploymentStep.Type == DeploymentStepType.DeployWebSite)
            {
                WebSiteDeploymentStepModel model = new WebSiteDeploymentStepModel
                {
                    OrderIndex       = deploymentStep.OrderIndex,
                    DeploymentStepId = deploymentStepId,
                    BundleVersionId  = deploymentStep.BundleVersionId,
                    SiteName         = deploymentStep.GetStringProperty("IIS.SiteName"),
                    ProjectId        = deploymentStep.GetIntProperty("ProjectId"),
                    Destination      = deploymentStep.GetStringProperty("IIS.DestinationPath"),
                    Roles            = string.Join(", ", deploymentStep.MachineRoles.Select(mr => mr.Name)),
                    BindingsJson     = deploymentStep.GetStringProperty("IIS.Bindings")
                };

                this.ViewBag.ProjectsSelect = this.Entities.SourceControlVersion
                                              .SelectMany(scv => scv.ProjectVersions)
                                              .Where(pv => pv.ProjectType.HasFlag(ProjectType.Web) && !pv.Project.Properties.Any(p => p.Key == "NotForDeployment" && p.Value == "true"))
                                              .Select(pv => new SelectListItem
                {
                    Text  = pv.SourceControlVersion.SourceControl.Name + " / " + pv.SourceControlVersion.Name + " / " + pv.Name,
                    Value = pv.Id.ToString()
                })
                                              .OrderBy(sli => sli.Text)
                                              .ToList();

                return(this.View("EditWebsiteStep", model));
            }
            if (deploymentStep.Type == DeploymentStepType.CopyFiles)
            {
                ZipArchiveDeploymentStepModel model = new ZipArchiveDeploymentStepModel
                {
                    OrderIndex              = deploymentStep.OrderIndex,
                    DeploymentStepId        = deploymentStepId,
                    BundleVersionId         = deploymentStep.BundleVersionId,
                    StepTitle               = deploymentStep.GetStringProperty("Step.Title"),
                    ProjectId               = deploymentStep.GetIntProperty("ProjectId"),
                    Destination             = deploymentStep.GetStringProperty("DestinationPath"),
                    Roles                   = string.Join(", ", deploymentStep.MachineRoles.Select(mr => mr.Name)),
                    CustomConfigurationJson = deploymentStep.GetStringProperty("CustomConfiguration")
                };

                this.ViewBag.ProjectsSelect = this.Entities.SourceControlVersion
                                              .SelectMany(scv => scv.ProjectVersions)
                                              .Where(pv => pv.ProjectType.HasFlag(ProjectType.ZipArchive) || pv.ProjectType.HasFlag(ProjectType.GulpFile))
                                              .Where(pv => !pv.Project.Properties.Any(p => p.Key == "NotForDeployment" && p.Value == "true"))
                                              .Select(pv => new SelectListItem
                {
                    Text  = pv.SourceControlVersion.SourceControl.Name + " / " + pv.SourceControlVersion.Name + " / " + pv.Name,
                    Value = pv.Id.ToString()
                })
                                              .OrderBy(sli => sli.Text)
                                              .ToList();

                return(this.View("EditZipArchiveStep", model));
            }

            if (deploymentStep.Type == DeploymentStepType.Configuration)
            {
                ConfigDeploymentStepModel model = new ConfigDeploymentStepModel
                {
                    OrderIndex       = deploymentStep.OrderIndex,
                    BundleVersionId  = deploymentStep.BundleVersionId,
                    DeploymentStepId = deploymentStepId,
                    ConfigJson       = deploymentStep.GetStringProperty("SetValues"),
                    StepTitle        = deploymentStep.GetStringProperty("Step.Title"),
                    File             = deploymentStep.GetStringProperty("File"),
                    Roles            = string.Join(", ", deploymentStep.MachineRoles.Select(mr => mr.Name))
                };

                return(this.View("EditConfigStep", model));
            }

            if (deploymentStep.Type == DeploymentStepType.RunSQLScript)
            {
                SqlScriptDeploymentStepModel model = new SqlScriptDeploymentStepModel
                {
                    OrderIndex       = deploymentStep.OrderIndex,
                    BundleVersionId  = deploymentStep.BundleVersionId,
                    DeploymentStepId = deploymentStepId,
                    Roles            = string.Join(", ", deploymentStep.MachineRoles.Select(mr => mr.Name)),
                    ConnectionString = deploymentStep.GetStringProperty("ConnectionString"),
                    StepTitle        = deploymentStep.GetStringProperty("Step.Title"),
                    Command          = deploymentStep.GetStringProperty("Command")
                };

                return(this.View("EditSqlScriptStep", model));
            }

            if (deploymentStep.Type == DeploymentStepType.UpdateHostsFile)
            {
                HostsDeploymentStepModel model = new HostsDeploymentStepModel
                {
                    OrderIndex       = deploymentStep.OrderIndex,
                    BundleVersionId  = deploymentStep.BundleVersionId,
                    DeploymentStepId = deploymentStepId,
                    Roles            = string.Join(", ", deploymentStep.MachineRoles.Select(mr => mr.Name)),
                    StepTitle        = deploymentStep.GetStringProperty("Step.Title"),
                    ConfigJson       = deploymentStep.GetStringProperty("ConfigurationJson")
                };

                return(this.View("EditHostsStep", model));
            }

            if (deploymentStep.Type == DeploymentStepType.DeployDacpac)
            {
                DacpacDeploymentStepModel model = new DacpacDeploymentStepModel
                {
                    OrderIndex          = deploymentStep.OrderIndex,
                    BundleVersionId     = deploymentStep.BundleVersionId,
                    DeploymentStepId    = deploymentStepId,
                    Roles               = string.Join(", ", deploymentStep.MachineRoles.Select(mr => mr.Name)),
                    StepTitle           = deploymentStep.GetStringProperty("Step.Title"),
                    ProjectId           = deploymentStep.GetIntProperty("ProjectId"),
                    ConnectionString    = deploymentStep.GetStringProperty("ConnectionString"),
                    TargetDatabase      = deploymentStep.GetStringProperty("TargetDatabase"),
                    CustomConfiguration = deploymentStep.GetStringProperty("CustomConfiguration")
                };

                this.ViewBag.ProjectsSelect = this.Entities.SourceControlVersion
                                              .SelectMany(scv => scv.ProjectVersions)
                                              .Where(pv => pv.ProjectType.HasFlag(ProjectType.Database) && !pv.Project.Properties.Any(p => p.Key == "NotForDeployment" && p.Value == "true"))
                                              .Select(pv => new SelectListItem
                {
                    Text  = pv.SourceControlVersion.SourceControl.Name + " / " + pv.SourceControlVersion.Name + " / " + pv.Name,
                    Value = pv.Id.ToString()
                })
                                              .OrderBy(sli => sli.Text)
                                              .ToList();

                return(this.View("EditDacpacStep", model));
            }

            if (deploymentStep.Type == DeploymentStepType.RunVsTests)
            {
                RunVsTestStepModel model = new RunVsTestStepModel
                {
                    OrderIndex       = deploymentStep.OrderIndex,
                    BundleVersionId  = deploymentStep.BundleVersionId,
                    DeploymentStepId = deploymentStepId,
                    Roles            = string.Join(", ", deploymentStep.MachineRoles.Select(mr => mr.Name)),
                    StepTitle        = deploymentStep.GetStringProperty("Step.Title"),
                    ProjectId        = deploymentStep.GetIntProperty("ProjectId"),
                    FiltersJson      = deploymentStep.GetStringProperty("FiltersJson"),
                    StopOnFailure    = deploymentStep.GetBoolProperty("StopOnFailure")
                };

                this.ViewBag.ProjectsSelect = this.Entities.SourceControlVersion
                                              .SelectMany(scv => scv.ProjectVersions)
                                              .Where(pv => pv.ProjectType.HasFlag(ProjectType.Test) && !pv.Project.Properties.Any(p => p.Key == "NotForDeployment" && p.Value == "true"))
                                              .Select(pv => new SelectListItem
                {
                    Text  = pv.SourceControlVersion.SourceControl.Name + " / " + pv.SourceControlVersion.Name + " / " + pv.Name,
                    Value = pv.Id.ToString()
                })
                                              .OrderBy(sli => sli.Text)
                                              .ToList();

                return(this.View("EditRunTestStep", model));
            }

            return(this.Content("Unsupported step type"));
        }
Esempio n. 5
0
        public void Deploy(int publicationId, Action <int> machineDeploymentStarted, Action <int, bool> machineDeploymentComplete)
        {
            AspNetDeployEntities entities = new AspNetDeployEntities();

            Publication publication = entities.Publication
                                      .Include("Package.BundleVersion.Properties")
                                      .Include("Package.BundleVersion.ProjectVersions")
                                      .Include("Package.BundleVersion.ProjectVersions.SourceControlVersion")
                                      .Include("Package.BundleVersion.DeploymentSteps.Properties")
                                      .Include("Package.BundleVersion.DeploymentSteps.MachineRoles")
                                      .Include("Environment.Properties")
                                      .Include("Environment.Machines.MachineRoles")
                                      .First(p => p.Id == publicationId);

            IList <Machine> affectedMachines = this.GetAffectedMachines(publication);
            IDictionary <Machine, IDeploymentAgent>   agents = this.CreateDeploymentAgents(affectedMachines, publication.Package);
            IDictionary <Machine, MachinePublication> machinePublications = this.CreateMachinePublications(affectedMachines, publication, entities);

            if (!this.ValidateDeploymentAgents(agents))
            {
                return;
            }

            string bundlePackagePath = pathServices.GetBundlePackagePath(publication.Package.BundleVersionId, publication.Package.Id);

            if (!this.ValidatePackage(bundlePackagePath))
            {
                return;
            }

            this.ChangePublicationResult(publication, PublicationState.InProgress, entities);

            foreach (MachinePublication machinePublication in machinePublications.Values)
            {
                this.ChangeMachinePublication(machinePublication, MachinePublicationState.Queued, entities);
            }

            foreach (KeyValuePair <Machine, IDeploymentAgent> pair in agents)
            {
                Machine          machine         = pair.Key;
                IDeploymentAgent deploymentAgent = pair.Value;

                MachinePublication machinePublication = machinePublications[machine];

                if (!deploymentAgent.IsReady())
                {
                    this.RecordException(entities, null, new AspNetDeployException("Deployment agent not ready"));
                    this.ChangePublicationResult(publication, PublicationState.Error, entities);
                    return;
                }

                machineDeploymentStarted(machine.Id);

                try
                {
                    deploymentAgent.BeginPublication(publication.Id);
                    deploymentAgent.ResetPackage();

                    this.ChangeMachinePublication(machinePublication, MachinePublicationState.Uploading, entities);
                    deploymentAgent.UploadPackage(bundlePackagePath);

                    this.ChangeMachinePublication(machinePublication, MachinePublicationState.Configuring, entities);

                    IList <DeploymentStep> machineDeploymentSteps = this.GetMachineDeploymentSteps(publication.Package, machine);

                    foreach (DeploymentStep deploymentStep in machineDeploymentSteps)
                    {
                        if (deploymentStep.Type == DeploymentStepType.RunVsTests)
                        {
                            this.LogMachinePublicationStep(machinePublication, deploymentStep, entities, MachinePublicationLogEvent.DeploymentStepConfiguringComplete);
                            continue;
                        }

                        try
                        {
                            this.LogMachinePublicationStep(machinePublication, deploymentStep, entities, MachinePublicationLogEvent.DeploymentStepConfiguring);
                            deploymentAgent.ProcessDeploymentStep(deploymentStep);
                            this.LogMachinePublicationStep(machinePublication, deploymentStep, entities, MachinePublicationLogEvent.DeploymentStepConfiguringComplete);
                        }
                        catch (Exception e)
                        {
                            this.RecordException(entities, null, e);
                            this.LogMachinePublicationStep(machinePublication, deploymentStep, entities, MachinePublicationLogEvent.DeploymentStepConfiguringError, this.GetLastExceptionSafe(deploymentAgent));
                            throw;
                        }
                    }

                    this.ChangeMachinePublication(machinePublication, MachinePublicationState.Running, entities);

                    for (var i = 0; i < machineDeploymentSteps.Count; i++)
                    {
                        DeploymentStep deploymentStep = machineDeploymentSteps[i];

                        if (deploymentStep.Type == DeploymentStepType.RunVsTests)
                        {
                            this.LogMachinePublicationStep(machinePublication, deploymentStep, entities, MachinePublicationLogEvent.DeploymentStepExecuting);

                            IVariableProcessor variableProcessor = this.variableProcessorFactory.Create(publication.Package.BundleVersionId, machine.Id);
                            ProjectVersion     projectVersion    = publication.Package.BundleVersion.ProjectVersions.First(pv => pv.Id == deploymentStep.GetIntProperty("ProjectId"));

                            IProjectTestRunner projectTestRunner = this.projectTestRunnerFactory.Create(projectVersion.ProjectType, variableProcessor);

                            IList <TestResult> testResults = projectTestRunner.Run(projectVersion);

                            if (testResults.All(t => t.IsPass))
                            {
                                this.LogMachinePublicationStep(machinePublication, deploymentStep, entities, MachinePublicationLogEvent.DeploymentStepExecutingComplete);
                            }
                            else
                            {
                                this.LogMachinePublicationStep(
                                    machinePublication,
                                    deploymentStep,
                                    entities,
                                    MachinePublicationLogEvent.DeploymentStepExecutingError,
                                    new ExceptionInfo()
                                {
                                    Message       = string.Join(", ", testResults.Where(t => !t.IsPass).Select(t => t.TestClassName + "." + t.TestName)),
                                    ExceptionData = testResults
                                                    .Where(t => !t.IsPass)
                                                    .Select((t, index) => new ExceptionDataInfo()
                                    {
                                        Name       = index + ". " + t.TestClassName + "." + t.TestName,
                                        Value      = t.Message,
                                        IsProperty = true
                                    })
                                                    .Cast <IExceptionDataInfo>()
                                                    .ToList()
                                });
                            }

                            continue;
                        }

                        try
                        {
                            this.LogMachinePublicationStep(machinePublication, deploymentStep, entities, MachinePublicationLogEvent.DeploymentStepExecuting);

                            if (deploymentAgent.ExecuteNextOperation())
                            {
                                this.LogMachinePublicationStep(machinePublication, deploymentStep, entities, MachinePublicationLogEvent.DeploymentStepExecutingComplete);
                            }
                            else
                            {
                                throw new AspNetDeployException("Error executing deploymentStep");
                            }
                        }
                        catch (Exception e)
                        {
                            e.Data.Add("Deployment step id", deploymentStep.Id);
                            e.Data.Add("Deployment ttep index", i + 1);
                            e.Data.Add("Machine name", machine.Name);

                            this.RecordException(entities, null, e);

                            this.LogMachinePublicationStep(
                                machinePublication,
                                deploymentStep,
                                entities,
                                MachinePublicationLogEvent.DeploymentStepExecutingError,
                                this.GetLastExceptionSafe(deploymentAgent));

                            throw;
                        }
                    }

                    deploymentAgent.Complete();
                }
                catch (Exception e)
                {
                    e.Data.Add("Publication.Id", publication.Id);
                    e.Data.Add("Publication.Package.Id", publication.Package.Id);

                    /*deploymentAgent.Rollback();*/
                    machineDeploymentComplete(machine.Id, false);
                    this.ChangeMachinePublication(machinePublication, MachinePublicationState.Error, entities);
                    this.ChangePublicationResult(publication, PublicationState.Error, entities);
                    this.RecordException(entities, null, e);

                    return;
                }

                this.ChangeMachinePublication(machinePublication, MachinePublicationState.Complete, entities);
                machineDeploymentComplete(machine.Id, true);
            }

            this.ChangePublicationResult(publication, PublicationState.Complete, entities);
        }