Example #1
0
        private static string GetNuGetExePath(BuildMasterAgent agent, string fileName)
        {
            var    executer    = agent.GetService <IRemoteMethodExecuter>();
            string assemblyDir = executer.InvokeFunc(GetNugetExeDirectory);

            return(PathEx.Combine(assemblyDir, fileName));
        }
        protected override void Execute()
        {
            var addresses = this.GetToAddresses().ToList();

            if (!addresses.Any())
            {
                this.LogWarning("The \"To address(es)\" field is empty, therefore no emails will be sent.");
                return;
            }

            this.LogInformation("Preparing to send email message to {0}...", addresses.Count > 1 ? addresses.Count + " addresses" : addresses.First());

            using (var smtp = new BuildMasterSmtpClient())
                using (var message = BuildMasterSmtpClient.CreateMailMessage(addresses))
                {
                    message.IsBodyHtml = this.IsBodyHtml;
                    message.Subject    = this.Subject;
                    message.Body       = this.Message;

                    if (this.Timeout > 0)
                    {
                        smtp.Timeout = this.Timeout;
                    }

                    if (!string.IsNullOrEmpty(this.Attachment))
                    {
                        using (var agent = BuildMasterAgent.Create(this.ServerId.Value))
                        {
                            var fileOps        = agent.GetService <IFileOperationsExecuter>();
                            var sourceDir      = fileOps.GetLegacyWorkingDirectory((IGenericBuildMasterContext)this.Context, PathEx.GetDirectoryName(this.Attachment));
                            var attachmentPath = fileOps.CombinePath(sourceDir, PathEx.GetFileName(this.Attachment));
                            this.LogDebug("Adding attachment at file path \"{0}\"...", attachmentPath);

                            if (!fileOps.FileExists(attachmentPath))
                            {
                                this.LogWarning("Could not attach \"{0}\" to the email message because the file was not found on the selected server.", this.Attachment);
                                smtp.Send(message);
                            }
                            else
                            {
                                using (var fileStream = fileOps.OpenFile(attachmentPath, FileMode.Open, FileAccess.Read))
                                {
                                    message.Attachments.Add(new Attachment(fileStream, PathEx.GetFileName(attachmentPath)));
                                    smtp.Send(message);
                                }
                            }
                        }
                    }
                    else
                    {
                        smtp.Send(message);
                    }
                }

            this.LogInformation("Email message{0} sent.", addresses.Count > 1 ? "s" : "");
        }
Example #3
0
        internal TfsBuildInfo GetBuildInfo(string teamProject, string buildDefinition, string buildNumber, bool includeUnsuccessful)
        {
            var agent = this.ServerId != null?BuildMasterAgent.Create((int)this.ServerId) : BuildMasterAgent.CreateLocalAgent();

            using (agent)
            {
                var methodExecuter = agent.GetService <IRemoteMethodExecuter>();
                return(methodExecuter.InvokeFunc(this.GetBuildInfoInternal, teamProject, buildDefinition, buildNumber, includeUnsuccessful));
            }
        }
Example #4
0
 private BuildMasterAgent CreateAgent()
 {
     if (this.ServerId == null)
     {
         return(BuildMasterAgent.CreateLocalAgent());
     }
     else
     {
         return(BuildMasterAgent.Create((int)this.ServerId));
     }
 }
Example #5
0
            public BuildMasterAgent CreateAgent()
            {
                var vars = DB.Variables_GetVariablesAccessibleFromScope(
                    Variable_Name: "TfsDefaultServerName",
                    Application_Id: this.ApplicationId,
                    IncludeSystemVariables_Indicator: true
                    );

                var variable = (from v in vars
                                orderby v.Application_Id != null ? 0 : 1
                                select v).FirstOrDefault();

                if (variable == null)
                {
                    return(BuildMasterAgent.CreateLocalAgent());
                }
                else
                {
                    return(BuildMasterAgent.Create(InedoLib.UTF8Encoding.GetString(variable.Variable_Value)));
                }
            }
Example #6
0
        private void TransferFiles(IFileOperationsExecuter sourceAgentHelper, char srcSeparator, string srcPath, DirectoryEntryInfo sourceDir, int targetServerId)
        {
            using (var targetAgent = BuildMasterAgent.Create(targetServerId))
            {
                var  fileOps         = targetAgent.GetService <IFileOperationsExecuter>();
                char targetSeparator = fileOps.DirectorySeparator;

                var tarPath = fileOps.GetLegacyWorkingDirectory(
                    (IGenericBuildMasterContext)this.Context,
                    this.TargetDirectory
                    );

                this.LogDebug("Full target directory: {0}", tarPath);
                fileOps.CreateDirectory(tarPath);

                this.LogDebug("Loading target file list...");
                var targetDir = fileOps.GetDirectoryEntry(
                    new GetDirectoryEntryCommand
                {
                    Path            = tarPath,
                    IncludeRootPath = true,
                    Recurse         = true
                }
                    ).Entry;

                this.LogDebug("Performing directory comparison...");
                List <string> filesToCopy     = new List <string>(),
                              foldersToCopy   = new List <string>(),
                              filesToDelete   = new List <string>(),
                              foldersToDelete = new List <string>();
                Util.Files.Comparison.CompareDirectories(
                    sourceDir, targetDir,
                    filesToCopy, foldersToCopy,
                    filesToDelete, foldersToDelete);

                // Make sure target files and folders to delete are canonical paths
                for (int i = 0; i < filesToDelete.Count; i++)
                {
                    filesToDelete[i] = filesToDelete[i].Replace(srcSeparator, targetSeparator);
                }
                for (int i = 0; i < foldersToDelete.Count; i++)
                {
                    foldersToDelete[i] = foldersToDelete[i].Replace(srcSeparator, targetSeparator);
                }

                // Run Filters
                if (this.IncludeFileMasks.Length != 0 &&
                    !(this.IncludeFileMasks.Length == 1 &&
                      string.IsNullOrEmpty(this.IncludeFileMasks[0])))
                {
                    filesToCopy = new List <string>(Util.Files.Comparison
                                                    .GetMatches(srcPath, filesToCopy.ToArray(), IncludeFileMasks));
                    foldersToCopy = new List <string>(Util.Files.Comparison
                                                      .GetMatches(srcPath, foldersToCopy.ToArray(), IncludeFileMasks));
                    filesToDelete = new List <string>(Util.Files.Comparison
                                                      .GetMatches(tarPath, filesToDelete.ToArray(), IncludeFileMasks));
                    foldersToDelete = new List <string>(Util.Files.Comparison
                                                        .GetMatches(tarPath, foldersToDelete.ToArray(), IncludeFileMasks));
                }

                if (this.DeleteTarget)
                {
                    this.LogInformation("Deleting files and directories that are not present in the source directory...");

                    if (filesToDelete.Count == 0)
                    {
                        this.LogDebug("No files to delete in target directory.");
                    }
                    else
                    {
                        this.LogDebug("Deleting {0} files from target directory...", filesToDelete.Count);
                    }

                    foreach (string path in filesToDelete)
                    {
                        this.LogDebug("\t" + path.Substring(tarPath.Length));
                    }

                    fileOps.DeleteFiles(filesToDelete.ToArray());

                    if (foldersToDelete.Count == 0)
                    {
                        this.LogDebug("No directories to delete in target directory.");
                    }
                    else
                    {
                        this.LogDebug("Deleting {0} directories from target directory...", foldersToDelete.Count);
                    }

                    foreach (string path in foldersToDelete)
                    {
                        this.LogDebug("\t" + path.Substring(tarPath.Length));
                    }

                    fileOps.DeleteDirectories(foldersToDelete.ToArray());
                }
                else
                {
                    this.LogDebug("Files and directories not present in source directory will not be deleted from target directory.");
                }

                this.LogInformation("Creating missing directories in target directory...");
                if (foldersToCopy.Count == 0)
                {
                    this.LogDebug("No directories missing in target directory.");
                }
                else
                {
                    this.LogDebug("Creating {0} directories in target directory...", foldersToCopy.Count);
                }

                foreach (string directoryToCopy in foldersToCopy)
                {
                    string relativeTargetPath = directoryToCopy.Substring(srcPath.Length)
                                                .Replace(srcSeparator, targetSeparator);

                    if (relativeTargetPath.StartsWith(targetSeparator.ToString()))
                    {
                        relativeTargetPath = relativeTargetPath.Substring(1);
                    }

                    this.LogDebug("\t" + relativeTargetPath);
                    fileOps.CreateDirectory(fileOps.CombinePath(tarPath, relativeTargetPath));
                }

                this.LogInformation("Copying or transferring modified files from source directory to target directory...");

                if (filesToCopy.Count == 0)
                {
                    this.LogDebug("No files to copy to the target directory.");
                }
                else
                {
                    this.LogDebug("Copying {0} files to the target directory...", filesToCopy.Count);
                }

                // Build list of source and target files to copy.
                var sourceFilesToCopy = new List <string>(filesToCopy.Count);
                var destFilesToCreate = new List <string>(filesToCopy.Count);
                foreach (var fileToCopy in filesToCopy)
                {
                    var relativeSourcePath = fileToCopy.Substring(srcPath.Length)
                                             .TrimStart(srcSeparator);
                    sourceFilesToCopy.Add(relativeSourcePath);

                    var relativeDestPath = relativeSourcePath.Replace(srcSeparator, targetSeparator);
                    destFilesToCreate.Add(relativeDestPath);

                    // These are copied in a batch, so can't get logged all at once.
                    if (this.Context.SourceServerId == targetServerId)
                    {
                        this.LogDebug("Copying all files from: {0}", relativeDestPath);
                    }
                }

                if (this.Context.SourceServerId == targetServerId)
                {
                    this.LogDebug("Both source and target servers are the same, performing batch copy of files...");
                    fileOps.FileCopyBatch(srcPath, sourceFilesToCopy.ToArray(), tarPath, destFilesToCreate.ToArray(), true, true);
                }
                else
                {
                    this.LogDebug("Transferring files from source server to target server...");
                    for (int i = 0; i < sourceFilesToCopy.Count; i++)
                    {
                        this.LogDebug("\t" + destFilesToCreate[i]);

                        var fullSourcePath = CombinePath(srcPath, sourceFilesToCopy[i], srcSeparator);
                        var fullTargetPath = CombinePath(tarPath, destFilesToCreate[i], targetSeparator);

                        string targetDirectory = PathEx.GetDirectoryName(fullTargetPath);
                        if (!string.IsNullOrEmpty(targetDirectory))
                        {
                            try
                            {
                                fileOps.CreateDirectory(targetDirectory);
                            }
                            catch (Exception ex)
                            {
                                this.LogDebug("Could not create directory at \"{0}\"; error was: {1}", targetDirectory, ex.Message);
                            }
                        }

                        Util.Files.TransferFile(sourceAgentHelper, fullSourcePath, fileOps, fullTargetPath);

                        var lastModified = sourceAgentHelper.GetFileInfo(fullSourcePath).LastWriteTimeUtc;
                        fileOps.SetLastWriteTime(fullTargetPath, lastModified);
                    }
                }
            }
        }
Example #7
0
        public override void Import(IBuildImporterContext context)
        {
            var configurer = (TfsConfigurer)this.GetExtensionConfigurer();

            this.LogDebug($"Searching for build {this.TfsBuildNumber} for team project {this.TeamProject} definition {this.BuildDefinition}...");
            var tfsBuild = configurer.GetBuildInfo(this.TeamProject, this.BuildDefinition, this.TfsBuildNumber, this.IncludeUnsuccessful);

            if (tfsBuild == null)
            {
                this.LogError("Query did not return any builds.");
                return;
            }

            this.LogInformation("TFS Build Number: " + tfsBuild.BuildNumber);

            if (string.IsNullOrWhiteSpace(tfsBuild.DropLocation))
            {
                this.LogError("TFS configuration error: the selected build definition does not have a drop location specified.");
                return;
            }

            this.LogInformation("Drop location: " + tfsBuild.DropLocation);

            var agent = configurer.ServerId != null?BuildMasterAgent.Create((int)configurer.ServerId) : BuildMasterAgent.CreateLocalAgent();

            using (agent)
            {
                var fileOps = agent.GetService <IFileOperationsExecuter>();

                this.LogDebug("Querying drop location...");

                var directoryResult = fileOps.GetDirectoryEntry(
                    new GetDirectoryEntryCommand
                {
                    Path            = tfsBuild.DropLocation,
                    Recurse         = true,
                    IncludeRootPath = true
                }
                    );
                var exception = directoryResult.Exceptions.FirstOrDefault();
                if (exception != null)
                {
                    throw exception;
                }

                var matches = Util.Files.Comparison.GetMatches(tfsBuild.DropLocation, directoryResult.Entry, new[] { "*" })
                              .Where(e => !IsSamePath(e.Path, tfsBuild.DropLocation))
                              .ToList();

                if (!matches.Any())
                {
                    this.LogWarning("No files were found in the drop folder.");
                    return;
                }

                this.LogDebug($"Creating {this.ArtifactName} artifact...");
                var artifactId = new ArtifactIdentifier(context.ApplicationId, context.ReleaseNumber, context.BuildNumber, context.DeployableId, this.ArtifactName);
                using (var artifact = new ArtifactBuilder(artifactId))
                {
                    artifact.RootPath = tfsBuild.DropLocation;

                    foreach (var match in matches)
                    {
                        artifact.Add(match, fileOps);
                    }

                    artifact.Commit();
                }
            }

            if (this.CreateBuildNumberVariable)
            {
                this.LogDebug($"Setting $TfsBuildNumber build variable to {tfsBuild.BuildNumber}...");
                DB.Variables_CreateOrUpdateVariableDefinition(
                    Variable_Name: "TfsBuildNumber",
                    Environment_Id: null,
                    ServerRole_Id: null,
                    Server_Id: null,
                    ApplicationGroup_Id: null,
                    Application_Id: context.ApplicationId,
                    Deployable_Id: null,
                    Release_Number: context.ReleaseNumber,
                    Build_Number: context.BuildNumber,
                    Execution_Id: null,
                    Promotion_Id: null,
                    Value_Text: tfsBuild.BuildNumber,
                    Sensitive_Indicator: YNIndicator.No
                    );

                this.LogInformation("$TfsBuildNumber build variable set to: " + tfsBuild.BuildNumber);
            }
        }
Example #8
0
        public override void Import(IBuildImporterContext context)
        {
            var configurer = (NuGetConfigurer)this.GetExtensionConfigurer();

            if (configurer.AlwaysClearNuGetCache)
            {
                this.LogDebug("Clearing NuGet cache...");
                if (NuGetConfigurer.ClearCache())
                {
                    this.LogDebug("Cache cleared!");
                }
                else
                {
                    this.LogWarning("Error clearing cache; a file may be locked.");
                }
            }

            var nugetExe = configurer != null ? configurer.NuGetExe : null;

            if (string.IsNullOrEmpty(nugetExe))
            {
                nugetExe = Path.Combine(Path.GetDirectoryName(typeof(NuGetBuildImporter).Assembly.Location), "nuget.exe");
            }

            var packageSource = Util.CoalesceStr(this.PackageSource, configurer != null ? configurer.PackageSource : null);

            var args = "install \"" + this.PackageId + "\" -ExcludeVersion -NoCache";

            if (!string.IsNullOrEmpty(this.PackageVersion))
            {
                args += " -Version \"" + this.PackageVersion + "\"";
            }
            if (this.IncludePrerelease)
            {
                args += " -Prerelease";
            }
            if (!string.IsNullOrEmpty(packageSource))
            {
                args += " -Source \"" + packageSource + "\"";
            }

            var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            try
            {
                Directory.CreateDirectory(tempPath);

                args += " -OutputDirectory \"" + tempPath + "\"";

                if (!string.IsNullOrWhiteSpace(this.AdditionalArguments))
                {
                    args += " " + this.AdditionalArguments;
                }

                this.LogDebug($"Executing {nugetExe} {args}");
                this.LogInformation("Executing NuGet...");

                using (var process = new LocalProcess(new RemoteProcessStartInfo {
                    FileName = nugetExe, Arguments = args
                }))
                {
                    process.OutputDataReceived += this.Process_OutputDataReceived;
                    process.ErrorDataReceived  += this.Process_ErrorDataReceived;
                    process.Start();
                    process.WaitForExit();

                    if (process.ExitCode != 0)
                    {
                        this.LogError($"NuGet failed with exit code {process.ExitCode}.");
                        return;
                    }
                }

                this.LogInformation("NuGet indicated successful package install.");

                var packageRootPath = Path.Combine(tempPath, this.PackageId);
                var artifactName    = this.PackageId;

                if (this.CaptureIdAndVersion || this.IncludeVersionInArtifactName)
                {
                    try
                    {
                        var nupkgPath = Path.Combine(packageRootPath, this.PackageId + ".nupkg");
                        this.LogDebug($"Attempting to gather metadata from {nupkgPath}...");
                        var nuspec = NuGetPackage.ReadFromNupkgFile(nupkgPath);

                        var packageId      = nuspec.Id;
                        var packageVersion = nuspec.Version.OriginalString;

                        if (this.CaptureIdAndVersion)
                        {
                            this.LogDebug("Setting $ImportedPackageId = " + packageId);
                            SetBuildVariable(context, "ImportedPackageId", packageId);

                            this.LogDebug("Setting $ImportedPackageVersion = " + packageVersion);
                            SetBuildVariable(context, "ImportedPackageVersion", packageVersion);
                        }

                        if (this.IncludeVersionInArtifactName)
                        {
                            artifactName = packageId + "." + packageVersion;
                        }
                    }
                    catch (Exception ex)
                    {
                        this.LogError("Could not read package metadata: " + ex.ToString());
                        return;
                    }
                }

                var rootCapturePath = Path.Combine(packageRootPath, (this.PackageArtifactRoot ?? string.Empty).Replace('/', '\\').TrimStart('/', '\\'));
                this.LogDebug($"Capturing files in {rootCapturePath}...");

                var rootEntry = Util.Files.GetDirectoryEntry(
                    new GetDirectoryEntryCommand
                {
                    Path            = rootCapturePath,
                    IncludeRootPath = true,
                    Recurse         = true
                }
                    ).Entry;

                using (var agent = BuildMasterAgent.CreateLocalAgent())
                {
                    var fileOps    = agent.GetService <IFileOperationsExecuter>();
                    var matches    = Util.Files.Comparison.GetMatches(rootCapturePath, rootEntry, new[] { "!\\" + this.PackageId + ".nupkg", "*" });
                    var artifactId = new ArtifactIdentifier(context.ApplicationId, context.ReleaseNumber, context.BuildNumber, context.DeployableId, artifactName);

                    this.LogInformation($"Creating artifact {artifactName}...");
                    using (var artifact = new ArtifactBuilder(artifactId))
                    {
                        artifact.RootPath = rootCapturePath;

                        foreach (var match in matches)
                        {
                            artifact.Add(match, fileOps);
                        }

                        artifact.Commit();
                    }

                    this.LogInformation("Artifact created.");
                }
            }
            finally
            {
                try
                {
                    DirectoryEx.Delete(tempPath);
                }
                catch
                {
                }
            }
        }