public void Run(string deployStateId, IDeployTaskStatusManager statusManager, List<IDeployTaskDefinition> taskDefinitionList, DeployComponent component, DeployEnvironmentConfiguration environmentComponent, DeployMachine machine, DeployBuild build, RuntimeSystemSettings runtimeSystemSettings) { int stepCounter = 0; foreach(var taskDefinition in taskDefinitionList) { stepCounter++; statusManager.Info(deployStateId, string.Format("Step {0}: Starting {1}", stepCounter, taskDefinition.TaskDefintionName)); DeployTaskExecutionResult result; //using (var impersontator = BeginImpersonation(deployStateId, statusManager, environmentComponent)) //{ var executor = _deployTaskFactory.CreateTaskExecutor(taskDefinition.GetTaskExecutorType()); result = executor.Execute(deployStateId, statusManager, taskDefinition, component, environmentComponent, machine, build, runtimeSystemSettings); //} switch(result.Status) { case EnumDeployTaskExecutionResultStatus.Success: statusManager.Info(deployStateId, string.Format("Step {0}: End {1}, completed successfully", stepCounter, taskDefinition.TaskDefintionName)); break; case EnumDeployTaskExecutionResultStatus.Error: statusManager.Info(deployStateId, string.Format("Step {0}: End {1}, failed", stepCounter, taskDefinition.TaskDefintionName)); return; //error error eject! //break; case EnumDeployTaskExecutionResultStatus.Warning: statusManager.Info(deployStateId, string.Format("Step {0}: End {1}, completed with warnings", stepCounter, taskDefinition.TaskDefintionName)); break; default: throw new UnknownEnumValueException(result.Status); } } }
public string EvaluateBuildParameter(string parameterName, DeployBuild build) { if(string.IsNullOrEmpty(parameterName)) { throw new ArgumentNullException("Missing parameterName"); } if(build == null) { throw new ArgumentNullException("Missing build"); } switch(parameterName.ToLower()) { case "version": return build.Version; case "majorversion": return GetVersionPosition(parameterName, build.Version, 0); case "minorversion": return GetVersionPosition(parameterName, build.Version, 1); case "buildversion": return GetVersionPosition(parameterName, build.Version, 2); case "revisionversion": return GetVersionPosition(parameterName, build.Version, 3); } throw new ArgumentException(string.Format("Unrecognized build parameter \"{0}\"", parameterName)); }
public void MinorVersion() { var fixture = new Fixture(); var build = new DeployBuild { Version = "123.456.789.012" }; var sut = new ParameterEvaluator(new ParameterParser()); var result = sut.EvaluateBuildParameter("MinorVersion", build); Assert.AreEqual("456", result); }
public void Version() { var fixture = new Fixture(); var build = new DeployBuild { Version = fixture.Create<string>() }; var sut = new ParameterEvaluator(new ParameterParser()); var result = sut.EvaluateBuildParameter("Version", build); Assert.AreEqual(build.Version, result); }
public void PurgeBuildIfNecessary(DeployBuild build) { int? rentionMinutes = _buildPurgeRuleManager.CalculateRentionMinutes(build); if(rentionMinutes.HasValue) { DateTime cutoffDate = DateTime.UtcNow.AddMinutes(0 - rentionMinutes.Value); if(build.UpdatedDateTimeUtc < cutoffDate) { _logger.Info("Build \"{0}\" updated date {1} is less than cutoff date {2}, purging build", build.DisplayValue, build.UpdatedDateTimeUtc, cutoffDate); this.PurgeBuild(build); } else { _logger.Trace("Build \"{0}\" updated date {1} is greater than cutoff date {2}, leaving as is", build.DisplayValue, build.UpdatedDateTimeUtc, cutoffDate); } } }
public DeployState CreateDeployState(DeployBuild build, DeployProjectBranch branch, DeployEnvironment environment, DeployComponent component, IEnumerable<DeployMachine> machineList, string deployBatchRequestItemId) { if (build == null) { throw new ArgumentNullException("Missing build"); } if (branch == null) { throw new ArgumentNullException("Missing branch"); } if (component == null) { throw new ArgumentNullException("Missing component"); } if (environment == null) { throw new ArgumentNullException("Missing environment"); } if (machineList == null) { throw new ArgumentNullException("Missing machineList"); } if (deployBatchRequestItemId == null) { throw new ArgumentNullException("Missing deployBatchRequestItemId"); } var deployState = new DeployState { Id = Guid.NewGuid().ToString(), ProjectId = environment.ProjectId, Build = build, Branch = branch, Environment = environment, Component = component, MachineList = machineList.ToList(), Status = EnumDeployStatus.NotStarted, SubmittedDateTimeUtc = DateTime.UtcNow, DeployBatchRequestItemId = deployBatchRequestItemId, CreatedDateTimeUtc = DateTime.UtcNow, CreatedByUserName = _userIdentity.UserName, UpdatedDateTimeUtc = DateTime.UtcNow, UpdatedByUserName = _userIdentity.UserName }; _documentSession.StoreSaveEvict(deployState); return deployState; }
public DeployState CreateDeployState(DeployBuild build, DeployProjectBranch branch, DeployEnvironment environment, DeployComponent component, IEnumerable<DeployMachine> machineList, string deployBatchRequestItemId) { var deployState = new DeployState { Id = Guid.NewGuid().ToString(), ProjectId = environment.ProjectId, Build = build, Branch = branch, Environment = environment, Component = component, MachineList = machineList.ToList(), Status = EnumDeployStatus.NotStarted, SubmittedDateTimeUtc = DateTime.UtcNow, DeployBatchRequestItemId = deployBatchRequestItemId, CreatedDateTimeUtc = DateTime.UtcNow, CreatedByUserName = _userIdentity.UserName, UpdatedDateTimeUtc = DateTime.UtcNow, UpdatedByUserName = _userIdentity.UserName }; _offlineDataProvider.SaveDeployState(deployState); return deployState; }
public void SendBuildPublishedNotification(DeployProject project, DeployBuild build) { var emailAddresseList = GetNotificationEmailAddresses(project.Id, i=>i.BuildPublished); if(emailAddresseList != null && emailAddresseList.Count > 0) { var dataObject = new { Project = project, Build = build, ViewBuildUrl = _urlGenerator.ViewBuildUrl(build.Id) }; var template = _razorTemplateRepository.GetTemplate("BuildPublishEmail", _notificationResourceViews.BuildPublishEmailView); _emailQueue.QueueMessage("New Build Published", emailAddresseList, dataObject, template.ViewData); } }
public void NoDots_RevisionVersion_ThrowsArgumentException() { var fixture = new Fixture(); var build = new DeployBuild { Version = "123456789012" }; var sut = new ParameterEvaluator(new ParameterParser()); Assert.Throws<ArgumentException>(() => sut.EvaluateBuildParameter("RevisionVersion", build)); }
public void NoDots_MajorVersion_EntireString() { var fixture = new Fixture(); var build = new DeployBuild { Version = "123456789012" }; var sut = new ParameterEvaluator(new ParameterParser()); var result = sut.EvaluateBuildParameter("MajorVersion", build); Assert.AreEqual("123456789012", result); }
public abstract bool MatchesRule(DeployBuild build, IDIFactory diFactory);
public static string GetDisplayValue(DeployBuild build) { return GetDisplayValue(build.ProjectName, build.ProjectBranchName, build.ProjectComponentName, build.Version); }
public DeployBuild CreateBuild(string projectId, string projectName, string projectComponentId, string projectComponentName, string projectBranchId, string projectBranchName, string fileId, string version) { if (string.IsNullOrEmpty(projectId)) { throw new ArgumentNullException("Missing project ID"); } if (string.IsNullOrEmpty(projectName)) { throw new ArgumentNullException("Missing project name"); } if (string.IsNullOrEmpty(projectComponentId)) { throw new ArgumentNullException("Missing component ID"); } if (string.IsNullOrEmpty(projectComponentName)) { throw new ArgumentNullException("Missing component name"); } if (string.IsNullOrEmpty(projectBranchId)) { throw new ArgumentNullException("Missing branch ID"); } if (string.IsNullOrEmpty(projectBranchName)) { throw new ArgumentNullException("Missing branch name"); } if (string.IsNullOrEmpty(fileId)) { throw new ArgumentNullException("Missing file ID"); } if (string.IsNullOrEmpty(version)) { throw new ArgumentNullException("Missing version"); } using(var db = _sqlConnectionInfo.GetDB()) { var sql = GetBaseQuery() .Append("WHERE ProjectID=@0", projectId) .Append("AND ProjectBranchID=@0", projectBranchId) .Append("AND ProjectComponentID=@0", projectComponentId) .Append("AND Version=@0", version); var existingItem = db.SingleOrDefault<DeployBuild>(sql); if(existingItem != null) { throw new DuplicateObjectException<DeployBuild>(existingItem); } } var build = new DeployBuild { Id = Guid.NewGuid().ToString(), ProjectId = projectId, ProjectName = projectName, ProjectBranchId = projectBranchId, ProjectBranchName = projectBranchName, ProjectComponentId = projectComponentId, ProjectComponentName = projectComponentName, FileId = fileId, Version = version, CreatedDateTimeUtc = DateTime.UtcNow, CreatedByUserName = _userIdentity.UserName, UpdatedDateTimeUtc = DateTime.UtcNow, UpdatedByUserName = _userIdentity.UserName }; using(var db = _sqlConnectionInfo.GetDB()) { var sql = PetaPoco.Sql.Builder .Append("INSERT INTO DeployBuild(ID, ProjectID, ProjectName, ProjectBranchID, ProjectBranchName, ProjectComponentID, ProjectComponentName, FileID, Version, CreatedDateTimeUtc, CreatedByUserName, UpdatedDateTimeUtc, UpdatedByUserName)") .Append("VALUES (@Id, @ProjectId, @ProjectName, @ProjectBranchId, @ProjectBranchName, @ProjectComponentId, @ProjectComponentName, @FileId, @Version, @CreatedDateTimeUtc, @CreatedByUserName, @UpdatedDateTimeUtc, @UpdatedByUserName)", build); db.Execute(sql); } return build; }
private DeployBuild PublishZip(string apiUrl, string projectId, string componentId, string branchId, string version, string zipPath, string userName, string password) { string url = apiUrl; if (!url.EndsWith("/")) { url += "/"; } if (!url.EndsWith("/api/", StringComparison.CurrentCultureIgnoreCase)) { url += "api/"; } var deployFile = new DeployFileDto { FileData = File.ReadAllBytes(zipPath), FileName = Path.GetFileName(zipPath) }; branchId = FormatBranch(branchId, version); Cookie authCookie = null; if(!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) { authCookie = GetAuthCookie(apiUrl, userName, password); } using (var client = new JsonServiceClient(url)) { _logger.Debug("Posting file {0} to URL {1}", zipPath, url); //var x = client.Send<DeployFileDto>(deployFile); var fileToUpload = new FileInfo(zipPath); string fileUrl = url + "file"; client.Credentials = System.Net.CredentialCache.DefaultCredentials; client.Timeout = TimeSpan.FromMinutes(5); client.ReadWriteTimeout = TimeSpan.FromMinutes(5); if(authCookie != null) { client.CookieContainer.Add(authCookie); } var fileResponse = client.PostFile<DeployFileDto>(fileUrl, fileToUpload, MimeTypes.GetMimeType(fileToUpload.Name)); _logger.Debug("Done posting file {0} to URL {1}, returned fileId {2} and fileStorageId {3}", zipPath, url, fileResponse.Id, fileResponse.FileStorageId); var buildRequest = new DeployBuild { FileId = fileResponse.Id, ProjectId = projectId, ProjectBranchId = branchId, ProjectComponentId = componentId, Version = version }; _logger.Debug("Posting DeployBuild object to URL {0}, sending{1}", url, buildRequest.ToJson()); string buildUrl = url + "build"; try { var buildResponse = client.Post<DeployBuild>(buildUrl, buildRequest); _logger.Debug("Posting DeployBuild object to URL {0}, returned {1}", url, buildRequest.ToJson()); return buildResponse; } catch(Exception err) { _logger.WarnException(string.Format("Error posting DeployBuild object to URL {0}: {1}, ERROR: {2}", url, buildRequest.ToJson(), err.ToString()), err); throw; } } }
public bool MatchesRule(DeployBuild build, IDIFactory diFactory) { var deployStateRepository = diFactory.CreateInjectedObject<IDeployStateRepository>(); var projectRepository = diFactory.CreateInjectedObject<IProjectRepository>(); if (this.EnvironmentIdList != null) { foreach(var environmentId in this.EnvironmentIdList) { var list = deployStateRepository.FindDeployStateListForEnvironment(build.Id, environmentId); if (list != null && list.Count > 0) { return true; } } } var project = projectRepository.GetProject(build.ProjectId); if (this.EnvironmentNameList != null) { foreach(var environmentName in this.EnvironmentNameList) { var environmentList = project.EnvironmentList.Where(i=>i.EnvironmentName == environmentName); if(environmentList != null) { foreach(var environment in environmentList) { var list = deployStateRepository.FindDeployStateListForEnvironment(build.Id, environment.Id); if(list != null && list.Count > 0) { return true; } } } } } if(this.MachineIdList != null) { foreach(var machineId in this.MachineIdList) { var machine = project.GetMachine(machineId); var list = deployStateRepository.FindDeployStateListForMachine(build.Id, machine.EnvironmentId, machine.Id); if(list != null) { foreach(var item in list) { if(item.MachineList.Any(i=>i.Id == machine.Id)) { return true; } } } } } if(this.MachineNameList != null) { foreach(var machineName in this.MachineNameList) { var machineList = project.GetMachineListForName(machineName); if(machineList != null) { foreach(var machine in machineList) { var list = deployStateRepository.FindDeployStateListForMachine(build.Id, machine.EnvironmentId, machine.Id); if(list != null) { foreach(var item in list) { if(item.MachineList.Any(i=>i.Id == machine.Id)) { return true; } } } } } } } return false; }
public void PurgeBuild(DeployBuild build) { _logger.Info("Purging build {0}", build.DisplayValue); _buildManager.DeleteBuild(build.Id); _logger.Info("Done purging build {0}", build.DisplayValue); }
public DeployState CreateDeployState(DeployBuild build, DeployProjectBranch branch, DeployEnvironment environment, DeployComponent component, IEnumerable<DeployMachine> machineList, string deployBatchRequestItemId) { if (build == null) { throw new ArgumentNullException("Missing build"); } if (branch == null) { throw new ArgumentNullException("Missing branch"); } if (component == null) { throw new ArgumentNullException("Missing component"); } if (environment == null) { throw new ArgumentNullException("Missing environment"); } if (machineList == null) { throw new ArgumentNullException("Missing machineList"); } if (deployBatchRequestItemId == null) { throw new ArgumentNullException("Missing deployBatchRequestItemId"); } var sqlDeployState = new SqlDeployState { ID = Guid.NewGuid().ToString(), ProjectID = build.ProjectId, BranchID = branch.Id, BranchJson = branch.ToJson(), BuildID = build.Id, BuildJson = build.ToJson(), ComponentID = component.Id, ComponentJson = component.ToJson(), EnvironmentID = environment.Id, EnvironmentName = environment.EnvironmentName, EnvironmentJson = environment.ToJson(), DeployBatchRequestItemID = deployBatchRequestItemId, DeploymentCompleteDateTimeUtc = null, DeploymentStartedDateTimeUtc = null, ErrorDetails = null, SortableVersion = build.SortableVersion, EnumDeployStatusID = (int)EnumDeployStatus.NotStarted, CreatedByUserName = _userIdentity.UserName, CreatedDateTimeUtc = DateTime.UtcNow, UpdatedByUserName = _userIdentity.UserName, UpdatedDateTimeUtc = DateTime.UtcNow, SubmittedDateTimeUtc = DateTime.UtcNow }; var branchJson = branch.ToJson(); var buildJson = build.ToJson(); var componentJson = component.ToJson(); var environmentJson = environment.ToJson(); using(var db = _sqlConnectionInfo.GetDB()) { var deployStateSql = PetaPoco.Sql.Builder .Append("INSERT INTO DeployState (ID, DeployBatchRequestItemID, EnumDeployStatusID, ProjectID, BranchID, BuildID, EnvironmentID, EnvironmentName, ComponentID,") .Append("BranchJson, BuildJson, EnvironmentJson, ComponentJson, SubmittedDateTimeUtc, DeploymentStartedDateTimeUtc, DeploymentCompleteDateTimeUtc, ") .Append("ErrorDetails, SortableVersion, CreatedByUserName, CreatedDateTimeUtc, UpdatedByUserName, UpdatedDateTimeUtc)") .Append("VALUES (@ID, @DeployBatchRequestItemID, @EnumDeployStatusID, @ProjectID, @BranchID, @BuildID, @EnvironmentID, @EnvironmentName, @ComponentID,", sqlDeployState) .Append("@BranchJson, @BuildJson, @EnvironmentJson, @ComponentJson, @SubmittedDateTimeUtc, @DeploymentStartedDateTimeUtc, @DeploymentCompleteDateTimeUtc,", sqlDeployState) .Append("@ErrorDetails, @SortableVersion, @CreatedByUserName, @CreatedDateTimeUtc, @UpdatedByUserName, @UpdatedDateTimeUtc)", sqlDeployState); db.Execute(deployStateSql); foreach(var machine in machineList) { var sqlMachine = new SqlDeployStateMachine { ID = Guid.NewGuid().ToString(), DeployStateID = sqlDeployState.ID, MachineID = machine.Id, MachineName = machine.MachineName, MachineJson = machine.ToJson(), CreatedByUserName = _userIdentity.UserName, CreatedDateTimeUtc = DateTime.UtcNow, UpdatedByUserName = _userIdentity.UserName, UpdatedDateTimeUtc = DateTime.UtcNow }; var machineSql = PetaPoco.Sql.Builder .Append("INSERT INTO DeployStateMachine (ID, DeployStateID, MachineID, MachineName, MachineJson, CreatedByUserName, CreatedDateTimeUtc, UpdatedByUserName, UpdatedDateTimeUtc)") .Append("VALUES (@ID, @DeployStateID, @MachineID, @MachineName, @MachineJson, @CreatedByUserName, @CreatedDateTimeUtc, @UpdatedByUserName, @UpdatedDateTimeUtc)", sqlMachine); db.Execute(machineSql); } } return GetDeployState(sqlDeployState.ID); }
public void SendBuildPublishedNotification(DeployProject project, DeployBuild build) { }
public int? CalculateRentionMinutes(DeployBuild build) { var systemRules = _buildPurgeRuleRepository.GetSystemBuildPurgeRuleList(); var projectRules = _buildPurgeRuleRepository.GetProjectBuildPurgeRuleList(build.ProjectId); int defaultRetentionMinutes = _systemSettings.DefaultBuildRetentionMinutes; var ruleList = systemRules.Union(projectRules); var matchingRules = (from i in ruleList where i.MatchesRule(build, _diFactory) select i).ToList(); if(matchingRules.Any(i=>!i.BuildRetentionMinutes.HasValue)) { //keep forever return null; } else { return matchingRules.Max(i=>i.BuildRetentionMinutes).GetValueOrDefault(defaultRetentionMinutes); } }