Example #1
0
        private void ProcessMapping(BuildStatusChangeEvent statusChanged, BuildDetail buildDetail, Mapping mapping, IPostDeployAction postDeployAction)
        {
            lock (GetLockObject(mapping))
            {
                var deployAgent = _deployAgentProvider.GetDeployAgent(mapping);

                // default to "happy; did nothing" if there's no deployment agent.
                var deployResult = new DeployAgentResult {
                    HasErrors = false, Output = string.Empty
                };

                if (deployAgent != null)
                {
                    using (var workingDirectory = new WorkingDirectory())
                    {
                        var deployAgentDataFactory = new DeployAgentDataFactory();
                        var deployData             = deployAgentDataFactory.Create(workingDirectory.DirectoryInfo.FullName,
                                                                                   mapping, buildDetail, statusChanged);

                        _deploymentFolderSource.DownloadDeploymentFolder(deployData.TfsBuildDetail, workingDirectory.DirectoryInfo.FullName);
                        deployResult = deployAgent.Deploy(deployData);
                    }
                }

                postDeployAction.DeploymentFinished(mapping, deployResult);
            }
        }
Example #2
0
        private static void ApplyRetainBuild(Mapping mapping, DeployAgentResult deployAgentResult, IBuildDetail detail)
        {
            if (!mapping.RetainBuildSpecified)
            {
                return;
            }
            if (deployAgentResult.HasErrors)
            {
                return;
            }

            // no change to setting?
            if (detail.KeepForever == mapping.RetainBuild)
            {
                return;
            }

            detail.KeepForever = mapping.RetainBuild;
            try
            {
                detail.Save();
            }
            catch (AccessDeniedException ex)
            {
                deployAgentResult.Output = string.Format("{0}\n{1}", deployAgentResult.Output, ex);
            }
        }
Example #3
0
        public void PostDeployAction_should_set_KeepForever_and_save_build_when_RetainBuild_is_specified()
        {
            // Arrange
            var alert       = MockRepository.GenerateStub <IAlert>();
            var buildServer = MockRepository.GenerateStub <IBuildServer>();

            var buildDetail = new BuildDetail();

            var tfsBuildDetail = new StubBuildDetail();

            ((IBuildDetail)tfsBuildDetail).KeepForever = false;
            buildServer.Stub(o => o.GetBuild(null, null, null, QueryOptions.None))
            .IgnoreArguments()
            .Return(tfsBuildDetail);

            var mapping = new Mapping {
                RetainBuildSpecified = true, RetainBuild = true
            };

            var result = new DeployAgentResult {
                HasErrors = false, Output = string.Empty
            };

            var postDeployAction = new PostDeployAction(buildDetail, tfsBuildDetail, alert);

            // Act
            postDeployAction.DeploymentFinished(mapping, result);

            // Assert
            Assert.AreEqual(true, ((IBuildDetail)tfsBuildDetail).KeepForever, "KeepForever");
            Assert.AreEqual(1, tfsBuildDetail.SaveCount, "Save()");
        }
Example #4
0
        public void Execute(BuildStatusChangeEvent statusChanged, BuildDetail buildDetail, Mapping mapping, IPostDeployAction postDeployAction, int deploymentId)
        {
            lock (_namedLockSet.GetLockObject(mapping.Queue))
            {
                _deploymentEventRecorder.RecordStarted(deploymentId);

                var deployAgent = _deployAgentProvider.GetDeployAgent(mapping);

                // default to "happy; did nothing" if there's no deployment agent.
                var deployResult = new DeployAgentResult {
                    HasErrors = false, Output = string.Empty
                };

                if (deployAgent != null)
                {
                    using (var workingDirectory = new WorkingDirectory())
                    {
                        var deployAgentDataFactory = new DeployAgentDataFactory();
                        var deployData             = deployAgentDataFactory.Create(workingDirectory.DirectoryInfo.FullName,
                                                                                   mapping, buildDetail, statusChanged);
                        deployData.DeploymentId = deploymentId;

                        _deploymentFolderSource.DownloadDeploymentFolder(deployData.TfsBuildDetail, workingDirectory.DirectoryInfo.FullName);
                        deployResult = deployAgent.Deploy(deployData);
                    }
                }

                postDeployAction.DeploymentFinished(mapping, deployResult);

                _deploymentEventRecorder.RecordFinished(deploymentId, deployResult.HasErrors, deployResult.Output);
            }
        }
Example #5
0
        private static string GetSubject(Mapping map, IBuildData build, DeployAgentResult deployAgentResult)
        {
            var errorMessage = "Success: ";

            if (deployAgentResult.HasErrors)
            {
                errorMessage = "Failed: ";
            }

            return(string.Format("{0} TfsDeployer Ran Script {1} on Machine {2} for {3}/{4}/{5}", errorMessage, map.Script, map.Computer, build.TeamProject, build.BuildType, build.BuildNumber));
        }
Example #6
0
        private static string GetBody(Mapping map, IBuildData build, DeployAgentResult deployAgentResult)
        {
            var builder = new StringBuilder();

            builder.AppendLine(string.Format("Team Project/Build: {0} to {1}", build.TeamProject, build.BuildType));
            builder.AppendLine(string.Format("Quality Change: {0} to {1}", map.OriginalQuality, map.NewQuality));
            builder.AppendLine(string.Format("Drop Location: {0}", build.DropLocation));
            builder.AppendLine(string.Format("Build Uri: {0}", build.BuildUri));
            builder.AppendLine(string.Format("Script: {0}", map.Script));
            builder.AppendLine(string.Format("Executed on Machine: {0}", map.Computer));
            builder.AppendLine("Output:");
            builder.AppendLine(deployAgentResult.Output);
            return(builder.ToString());
        }
Example #7
0
        public void EmailAlerter_should_return_silently_if_mapping_notification_address_is_blank()
        {
            // Arrange
            var mapping           = new Mapping();
            var build             = new BuildDetail();
            var deployAgentResult = new DeployAgentResult();
            var emailAlerter      = new EmailAlerter();

            mapping.NotificationAddress = string.Empty;

            // Act
            emailAlerter.Alert(mapping, build, deployAgentResult);

            // Assert
            // no exception
        }
Example #8
0
        private static void ApplyRetainBuild(Mapping mapping, DeployAgentResult deployAgentResult, IBuildDetail detail)
        {
            if (!mapping.RetainBuildSpecified)
            {
                return;
            }
            if (deployAgentResult.HasErrors)
            {
                return;
            }
            if (detail.KeepForever == mapping.RetainBuild)
            {
                return;
            }

            detail.KeepForever = mapping.RetainBuild;
            detail.Save();
        }
Example #9
0
        public void MappingExecutor_should_record_finished_time_and_errors_and_final_output()
        {
            // Arrange
            const int deploymentId      = 23;
            var       deployAgentResult = new DeployAgentResult {
                HasErrors = true, Output = "Done!"
            };

            var deployAgent = MockRepository.GenerateStub <IDeployAgent>();

            deployAgent.Stub(o => o.Deploy(null))
            .IgnoreArguments()
            .Return(deployAgentResult);

            var deployAgentProvider = MockRepository.GenerateStub <IDeployAgentProvider>();

            deployAgentProvider.Stub(o => o.GetDeployAgent(null))
            .IgnoreArguments()
            .Return(deployAgent);

            var deploymentFolderSource = MockRepository.GenerateStub <IDeploymentFolderSource>();

            var mapping = new Mapping();

            var deploymentEventRecorder = MockRepository.GenerateStub <IDeploymentEventRecorder>();

            var namedLockSet = new NamedLockSet();

            var mappingExecutor  = new MappingExecutor(deploymentEventRecorder, deployAgentProvider, deploymentFolderSource, namedLockSet);
            var postDeployAction = MockRepository.GenerateStub <IPostDeployAction>();

            var buildDetail = new BuildDetail();

            var statusChanged = new BuildStatusChangeEvent {
                StatusChange = new Change()
            };

            // Act
            mappingExecutor.Execute(statusChanged, buildDetail, mapping, postDeployAction, deploymentId);

            // Assert
            deploymentEventRecorder.AssertWasCalled(o => o.RecordFinished(deploymentId, deployAgentResult.HasErrors, deployAgentResult.Output));
        }
Example #10
0
        public void Alert(Mapping mapping, BuildDetail build, DeployAgentResult deployAgentResult)
        {
            if (string.IsNullOrEmpty(mapping.NotificationAddress))
            {
                TraceHelper.TraceInformation(TraceSwitches.TfsDeployer, string.Format("Skipping email alert because NotificationAddress is not specified."));
                return;
            }

            try
            {
                var client    = new SmtpClient();
                var subject   = GetSubject(mapping, build, deployAgentResult);
                var body      = GetBody(mapping, build, deployAgentResult);
                var toAddress = mapping.NotificationAddress;

                var message = new MailMessage
                {
                    Subject = subject,
                    Body    = body
                };

                TraceHelper.TraceInformation(TraceSwitches.TfsDeployer, string.Format(TraceFormat,
                                                                                      client.Host,
                                                                                      message.From.Address,
                                                                                      toAddress,
                                                                                      message.Subject,
                                                                                      message.Body));

                // Allow multiple recipients separated by semi-colon
                foreach (var address in toAddress.Split(';'))
                {
                    message.To.Add(address);
                }

                client.Send(message);
            }
            catch (Exception ex)
            {
                TraceHelper.TraceError(TraceSwitches.TfsDeployer, ex);
            }
        }
Example #11
0
        public void DeploymentEventJournal_should_retrieve_output_from_output_delegate_on_request()
        {
            // Arrange
            const string expectedContent = "Updated output";
            var          journal         = new DeploymentEventJournal();
            var          eventId         = journal.RecordTriggered("Foobar_123.2", null, null, null, null, null);
            var          deploymentId    = journal.RecordQueued(eventId, "Foo.ps1", "QueueCumber");

            var outputContainer = new DeployAgentResult {
                Output = "Initial output"
            };

            journal.SetDeploymentOutputDelegate(deploymentId, () => outputContainer.Output);
            journal.GetDeploymentOutput(deploymentId); // get initial output and discard

            // Act
            outputContainer.Output = expectedContent; // update output
            var deploymentOutput = journal.GetDeploymentOutput(deploymentId);

            // Assert
            Assert.AreEqual(expectedContent, deploymentOutput.Content, "Content does not match expected.");
            Assert.IsFalse(deploymentOutput.IsFinal, "IsFinal is not false");
        }
Example #12
0
        public void Alert(Mapping mapping, IBuildData build, DeployAgentResult deployAgentResult)
        {
            try
            {
                var client    = new SmtpClient(Settings.Default.SmtpServer);
                var subject   = GetSubject(mapping, build, deployAgentResult);
                var body      = GetBody(mapping, build, deployAgentResult);
                var toAddress = mapping.NotificationAddress ?? Settings.Default.ToAddress;

                var message = new MailMessage
                {
                    From    = new MailAddress(Settings.Default.FromAddress),
                    Subject = subject,
                    Body    = body
                };

                TraceHelper.TraceInformation(TraceSwitches.TfsDeployer, string.Format(TraceFormat,
                                                                                      client.Host,
                                                                                      message.From.Address,
                                                                                      toAddress,
                                                                                      message.Subject,
                                                                                      message.Body));

                // Allow multiple recipients separated by semi-colon
                foreach (var address in toAddress.Split(';'))
                {
                    message.To.Add(address);
                }

                client.Send(message);
            }
            catch (Exception ex)
            {
                TraceHelper.TraceError(TraceSwitches.TfsDeployer, ex);
            }
        }
Example #13
0
 public void DeploymentFinished(Mapping mapping, DeployAgentResult result)
 {
     ApplyRetainBuild(mapping, result, _tfsBuildDetail);
     _alert.Alert(mapping, _buildDetail, result);
 }