예제 #1
0
		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 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);
        }