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);
				}
			}
		}
Example #2
0
        public void TestDeployDatabase()
        {
            var definition = new DeployRoundhouseDatabaseTaskDefinition(new ParameterParser());
            definition.Options = new DeployRoundhouseDatabaseTaskOptions
            {
                EnvironmentName = "LOCAL",
                DatabaseType = EnumRoundhouseDatabaseType.SqlServer,
                SqlFilesDirectory = @"C:\Projects\Sriracha.Deploy\Repository\SqlServer\Sriracha.Deploy.SqlServer.RoundhousE\db",
                ConnectionString = "Data Source=(local); Initial Catalog=TestRoundhouseTask; Integrated Security=SSPI;"
            };
            var executor = new DeployRoundhouseDatabaseExecutor(new ParameterEvaluator(new ParameterParser()), new DeploymentValidator(new DeployTaskFactory(new Mock<IDIFactory>().Object, new ModuleInspector())));
            var fixture = new Fixture();

            string deployStateId = fixture.Create<string>("DeployStateId");
            var deployTaskStatusManager = new DeployTaskStatusManager(new Mock<ILog>().Object, new Mock<IDeployStateManager>().Object, new Mock<IDeployStatusNotifier>().Object);
            var component = fixture.Create<DeployComponent>();
            var environmentComponent = fixture.Create<DeployEnvironmentConfiguration>();
            var machine = fixture.Create<DeployMachine>();
            var build = fixture.Create<DeployBuild>();
            var systemSettings = new RuntimeSystemSettings();
            var result = executor.Execute(deployStateId, deployTaskStatusManager, definition, component, environmentComponent, machine, build, systemSettings);

            Assert.IsNotNull(result);
            Assert.AreEqual(EnumDeployTaskExecutionResultStatus.Success, result.Status);
        }
		private void RunDeployment(DeployBatchRequest deployBatchRequest)
		{
			string subDirName = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + deployBatchRequest.Id;
			string deployDirectory = Path.Combine(_systemSettings.DeployWorkingDirectory, subDirName);
			if (Directory.Exists(deployDirectory))
			{
				//if directory already exists, start adding "_1", "_2", until we get a directory that does not exist
				int counter = 1;
				string newDeployDirectory = deployDirectory;
				while (Directory.Exists(newDeployDirectory))
				{
					newDeployDirectory = deployDirectory + "_" + counter.ToString();
					counter++;
				}
				deployDirectory = newDeployDirectory;
			}

			var runtimeSettings = new RuntimeSystemSettings
			{
				LocalDeployDirectory = deployDirectory
			};
			Directory.CreateDirectory(runtimeSettings.LocalDeployDirectory);
			_cleanupManager.QueueFolderForCleanup(runtimeSettings.LocalMachineName, runtimeSettings.LocalDeployDirectory, _systemSettings.DeploymentFolderCleanupMinutes);
			foreach (var item in deployBatchRequest.ItemList)
			{
				foreach (var machine in item.MachineList)
				{
					if (_deployRequestManager.HasCancelRequested(deployBatchRequest.Id))
					{
						_deployStateManager.MarkBatchDeploymentCancelled(deployBatchRequest.Id, deployBatchRequest.CancelMessage);
						return;
					}
					var deployState = _deployStateManager.GetOrCreateDeployState(item.Build.ProjectId, item.Build.Id, machine.EnvironmentId, machine.Id, deployBatchRequest.Id);
					if (deployState.Status != EnumDeployStatus.Success)
					{
						try
						{
							_deployStateManager.MarkDeploymentInProcess(deployState.Id);
							var machineIdList = new List<string> { machine.Id };
							_deployRunner.Deploy(deployState.Id, machine.EnvironmentId, item.Build.Id, machineIdList, runtimeSettings);
							_deployStateManager.MarkDeploymentSuccess(deployState.Id);
						}
						catch (Exception err)
						{
							_deployStateManager.MarkDeploymentFailed(deployState.Id, err);
							throw;
						}
					}
				}
			}

			_deployStateManager.MarkBatchDeploymentSuccess(deployBatchRequest.Id);

			this._logger.Info("Deployment complete: " + deployBatchRequest.Id);
		}
		private void RunDeployment(DeployBatchRequest deployBatchRequest)
		{
			string subDirName = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + deployBatchRequest.Id;
			string deployDirectory = Path.Combine(_systemSettings.DeployWorkingDirectory, subDirName);
			if (Directory.Exists(deployDirectory))
			{
				//if directory already exists, start adding "_1", "_2", until we get a directory that does not exist
				int counter = 1;
				string newDeployDirectory = deployDirectory;
				while (Directory.Exists(newDeployDirectory))
				{
					newDeployDirectory = deployDirectory + "_" + counter.ToString();
					counter++;
				}
				deployDirectory = newDeployDirectory;
			}
			var runtimeSettings = new RuntimeSystemSettings
			{
				LocalDeployDirectory = deployDirectory,
				LocalMachineName = Environment.MachineName
			};
			Directory.CreateDirectory(runtimeSettings.LocalDeployDirectory);
			_cleanupManager.QueueFolderForCleanup(runtimeSettings.LocalMachineName, runtimeSettings.LocalDeployDirectory, _systemSettings.DeploymentFolderCleanupMinutes);
			var plan = _deploymentPlanBuilder.Build(deployBatchRequest);
			//_deployStateManager.SaveDeploymentPlan(plan);
			foreach (var parallelBatchList in plan.ParallelBatchList)
			{
				var taskList = new List<Task>();
				foreach (var machineQueue in parallelBatchList.MachineQueueList)
				{
					string machineQueueId = machineQueue.Id;
					var task = Task.Factory.StartNew(() => DeployMachineQueue(plan, machineQueueId, runtimeSettings));
					taskList.Add(task);
				}
				var taskArray = taskList.ToArray();
				Task.WaitAll(taskArray);
				if (_deployRequestManager.IsStopped(deployBatchRequest.Id))
				{
					break;
				}
			}
			_deployStateManager.MarkBatchDeploymentSuccess(deployBatchRequest.Id);

			this._logger.Info("Deployment complete: " + deployBatchRequest.Id);
		}
		public string EvaluateDeployParameter(string parameterName, RuntimeSystemSettings runtimeSettings, DeployMachine machine, DeployComponent component)
		{
			if (string.IsNullOrEmpty(parameterName))
			{
				throw new ArgumentNullException("Missing parameterName");
			}
			if (runtimeSettings == null)
			{
				throw new ArgumentNullException("Missing runtimeSettings");
			}
			switch (parameterName.ToLower())
			{
				case "directory":
					return runtimeSettings.GetLocalMachineComponentDirectory(machine.MachineName, component.Id);
				default:
					throw new ArgumentException(string.Format("Unrecognized deploy parameter \"{0}\"", parameterName));
			}
		}
		public void Deploy(string deployStateId, string environmentId, string buildId, List<string> machineIdList, RuntimeSystemSettings systemSettings)
		{
			var build = _buildRepository.GetBuild(buildId);
			var project = _projectRepository.GetProject(build.ProjectId);
			var environment = project.GetEnvironment(environmentId);
			var component = project.GetComponent(build.ProjectComponentId);
			DeployEnvironmentConfiguration environmentConfiguration;
			if(component.UseConfigurationGroup)
			{
				environmentConfiguration = environment.GetConfigurationItem(component.ConfigurationId);
			}
			else
			{
				environmentConfiguration = environment.GetComponentItem(component.Id);
			}

			_statusManager.Info(deployStateId, "Building task definition objects");
			var taskDefinitionList = new List<IDeployTaskDefinition>();
			List<DeployStep> deploymentStepList;
			if(component.UseConfigurationGroup)
			{
				if(string.IsNullOrEmpty(component.ConfigurationId))
				{
					throw new Exception(string.Format("Component {0} should use configuration group, but no configuration specified", component.Id));
				}
				var configuration = project.GetConfiguration(component.ConfigurationId);
				deploymentStepList = configuration.DeploymentStepList;
			}
			else
			{
				deploymentStepList = component.DeploymentStepList;
			}
            deploymentStepList = deploymentStepList.OrderBy(i=>i.OrderNumber).ToList();
			foreach(var step in deploymentStepList)
			{
				var taskDefinition = _taskFactory.CreateTaskDefinition(step.TaskTypeName, step.TaskOptionsJson);
				taskDefinitionList.Add(taskDefinition);
			}
			
			var fileData = _fileManager.GetFile(build.FileId);
			string compressedFilePath = Path.Combine(systemSettings.GetLocalCompressedPackageDirectory(component.Id), fileData.FileName);
			_statusManager.Info(deployStateId, string.Format("Extracting deployment package {0} to {1}", fileData.Id, compressedFilePath));
			_fileManager.ExportFile(fileData.Id, compressedFilePath);
			_statusManager.Info(deployStateId, string.Format("Deployment extracted package {0} to {1}", fileData.Id, compressedFilePath));

			string extractedDirectory = systemSettings.GetLocalExtractedDirectory(component.Id);
			_statusManager.Info(deployStateId, string.Format("Decompressing deployment package {0} to directory {1}", compressedFilePath, extractedDirectory));
			_zipper.ExtractFile(compressedFilePath, extractedDirectory);
			_statusManager.Info(deployStateId, string.Format("Done decompressing deployment package {0} to directory {1}", compressedFilePath, extractedDirectory));

			foreach(var machineId in machineIdList)
			{
				var machine = environmentConfiguration.GetMachine(machineId);
				//string machineDirectory = systemSettings.GetLocalMachineDirectory(machine.MachineName);
				//if(!Directory.Exists(machineDirectory))
				//{
				//	Directory.CreateDirectory(machineDirectory);
				//}
				string machineComponentDirectory = systemSettings.GetLocalMachineComponentDirectory(machine.MachineName, component.Id);
				CopyAllFiles(extractedDirectory, machineComponentDirectory);
				_statusManager.Info(deployStateId, string.Format("Copying deployment files from {0} to machine/component directory {1}", extractedDirectory, machineComponentDirectory));

				_statusManager.Info(deployStateId, string.Format("Done copying deployment files from {0} to machine/component  directory {1}", extractedDirectory, machineComponentDirectory));

				_componentRunner.Run(deployStateId, _statusManager, taskDefinitionList, component, environmentConfiguration, machine, build, systemSettings);
			}
		}
				public void Execute(DeployEnvironmentConfiguration deployEnvironmentComponent, RuntimeSystemSettings runtimeSystemSettings)
				{
					throw new NotImplementedException();
				}
		public void DeployMachineQueue(DeploymentPlan plan, string machineQueueId, RuntimeSystemSettings runtimeSettings)
		{
			string deployBatchRequestId = plan.DeployBatchRequestId;
			var machineQueue = plan.ParallelBatchList.SelectMany(i=>i.MachineQueueList.Where(j=>j.Id == machineQueueId)).SingleOrDefault();
			if(machineQueue == null)
			{
				throw new RecordNotFoundException(typeof(DeploymentPlanMachineQueue), "Id", machineQueueId);
			}
			//Sooooo, we're in some threads now.  And some of our repository types (Raven) doesn't like sharing sessions between threads.
			//	So let's create a new instance now for this method
			var localDeployRequestManager = _diFactory.CreateInjectedObject<IDeployRequestManager>();
			var localDeployRunner = _diFactory.CreateInjectedObject<IDeployRunner>();
			var localDeployStateManager = _diFactory.CreateInjectedObject<IDeployStateManager>();

			foreach(var machineQueueItem in machineQueue.MachineQueueItemList)
			{
				if (localDeployRequestManager.HasCancelRequested(deployBatchRequestId))
				{
					localDeployStateManager.MarkBatchDeploymentCancelled(deployBatchRequestId, null);
					return;
				}
				else if (localDeployRequestManager.IsStopped(deployBatchRequestId))
				{
					return;
				}
				var machine = machineQueueItem.DeployBatchRequestItem.MachineList.FirstOrDefault(i=>i.Id == machineQueueItem.MachineId);
				if(machine == null)
				{
					throw new Exception("Failed to find machine " + machineQueueItem.MachineId);
				}
				var deployState = localDeployStateManager.GetOrCreateDeployState(machineQueueItem.DeployBatchRequestItem.Build.ProjectId, machineQueueItem.DeployBatchRequestItem.Build.Id, machine.EnvironmentId, machine.Id, machineQueueItem.DeployBatchRequestItem.Id);
				if (deployState.Status != EnumDeployStatus.Success)
				{
					try
					{
						localDeployStateManager.MarkDeploymentInProcess(deployState.Id);
						var machineIdList = new List<string> { machine.Id };
						localDeployRunner.Deploy(deployState.Id, machine.EnvironmentId, machineQueueItem.DeployBatchRequestItem.Build.Id, machineIdList, runtimeSettings);
						localDeployStateManager.MarkDeploymentSuccess(deployState.Id);
					}
					catch (Exception err)
					{
						localDeployStateManager.MarkDeploymentFailed(deployState.Id, err);
						throw;
					}
				}
			}
		}
        public void Test1()
        {
            var testData = TestData.Create();
            var systemSettings = new RuntimeSystemSettings()
            {
                LocalDeployDirectory = "C:\\Temp\\DeployTest",
                LocalMachineName = Environment.MachineName
            };

            var result = testData.Sut.Execute(testData.DeployStateId, testData.DeployTaskStatusManager, testData.TaskDefinition, testData.Component, testData.EnvironmentComponent, testData.Machine, testData.Build, systemSettings);

            Assert.AreEqual(EnumDeployTaskExecutionResultStatus.Success, result.Status);
        }
Example #10
0
        //private static void ConfigureMachine(string machineId, string configName, string configValue)
        //{
        //    var pm = _diFactory.CreateInjectedObject<IProjectManager>();
        //    pm.UpdateMachineConfig(machineId, configName, configValue);
        //}

        //private static void ConfigureEnvironment(string environmentId, string componentId, string configName, string configValue)
        //{
        //    var pm = _diFactory.CreateInjectedObject<IProjectManager>();
        //    pm.UpdateEnvironmentComponentConfig(environmentId, componentId, configName, configValue);
        //}

		private static void Deploy(string environmentID, string buildID, string machineId)
		{
			Program._logger.Info("Executing deployment request for build " + buildID + " to environment " + environmentID);

			var deploymentRunner = _diFactory.CreateInjectedObject<IDeployRunner>();
			var runtimeSystemSettings = new RuntimeSystemSettings
			{
				LocalDeployDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Guid.NewGuid().ToString())
			};
			Directory.CreateDirectory(runtimeSystemSettings.LocalDeployDirectory);
			Program._logger.Info("\tUsing path: " + runtimeSystemSettings.LocalDeployDirectory);

			var machineIdList = new List<string> { machineId };
			deploymentRunner.Deploy(null, environmentID, buildID, machineIdList, runtimeSystemSettings);

			Program._logger.Info("Done executing deployment request for build " + buildID + " to environment " + environmentID);
		}