public DeploymentMetadataLogNotification( [NotNull] DeploymentTask deploymentTask, [NotNull] DeploymentTaskResult result) { if (result == null) { throw new ArgumentNullException(nameof(result)); } if (string.IsNullOrWhiteSpace(result.Metadata)) { throw new ArgumentException("Value cannot be null or whitespace.", nameof(result)); } DeploymentTask = deploymentTask ?? throw new ArgumentNullException(nameof(deploymentTask)); Result = result; }
private static string GetTargetDirectoryPath( DeploymentTarget deploymentTarget, string jobId, DeploymentTask deploymentTask) { var targetTempDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "MDep", jobId)); string targetDirectoryPath = deploymentTarget.TargetDirectory .WithDefault(targetTempDirectory.FullName); if (!Directory.Exists(targetDirectoryPath)) { Directory.CreateDirectory(targetDirectoryPath); } deploymentTask.TempDirectories.Add(targetTempDirectory); return(targetDirectoryPath); }
private static void ClearTemporaryDirectoriesAndFiles(DeploymentTask deploymentTask) { foreach (FileInfo temporaryFile in deploymentTask.TempFiles) { temporaryFile.Refresh(); if (temporaryFile.Exists) { temporaryFile.Delete(); } } foreach (DirectoryInfo deploymentTaskTempDirectory in deploymentTask.TempDirectories) { deploymentTaskTempDirectory.Refresh(); if (deploymentTaskTempDirectory.Exists) { deploymentTaskTempDirectory.Delete(true); } } }
public async Task <ExitCode> ExecuteAsync( DeploymentTask deploymentTask, ILogger logger, CancellationToken cancellationToken = default) { string jobId = "MDep_" + Guid.NewGuid(); logger.Information("Starting job {JobId}", jobId); DeploymentTarget deploymentTarget = await GetDeploymentTarget(deploymentTask.DeploymentTargetId, cancellationToken); Environment.SetEnvironmentVariable(ConfigurationConstants.AllowPreReleaseEnabled, "true"); // TODO try to remove SetLogging(); string targetDirectoryPath = GetTargetDirectoryPath(deploymentTarget, jobId, deploymentTask); string targetEnvironmentConfigName = deploymentTarget.EnvironmentConfiguration; var arguments = new List <string>(); logger.Information("Using manifest file for job {JobId}", jobId); string publishSettingsFile = deploymentTarget.PublishSettingFile; string deploymentTargetParametersFile = deploymentTarget.ParameterFile; var tempManifestFile = new FileInfo(Path.Combine(Path.GetTempPath(), $"{jobId}.manifest")); deploymentTask.TempFiles.Add(tempManifestFile); ImmutableDictionary <string, string[]> parameterDictionary; if (!string.IsNullOrWhiteSpace(deploymentTargetParametersFile) && !Path.IsPathRooted(deploymentTargetParametersFile)) { throw new DeployerAppException( $"The deployment target {deploymentTarget} parameter file '{deploymentTargetParametersFile}' is not a rooted path"); } if (!string.IsNullOrWhiteSpace(deploymentTargetParametersFile) && File.Exists(deploymentTargetParametersFile)) { string parametersJson = File.ReadAllText(deploymentTargetParametersFile, Encoding.UTF8); parameterDictionary = JsonConvert .DeserializeObject <Dictionary <string, string[]> >(parametersJson).ToImmutableDictionary(); logger.Information("Using WebDeploy parameters from file {DeploymentTargetParametersFile}", deploymentTargetParametersFile); } else { logger.Information("No WebDeploy parameters file exists ('{DeploymentTargetParametersFile}')", deploymentTargetParametersFile); parameterDictionary = deploymentTarget.Parameters; } ImmutableDictionary <string, string[]> parameters = parameterDictionary; if (deploymentTarget.PublishSettingsXml.HasValue()) { string tempFileName = Path.GetTempFileName(); await File.WriteAllTextAsync(tempFileName, deploymentTarget.PublishSettingsXml, Encoding.UTF8, cancellationToken); deploymentTask.TempFiles.Add(new FileInfo(tempFileName)); publishSettingsFile = tempFileName; } if (!File.Exists(publishSettingsFile)) { const string secretKeyPrefix = "publish-settings"; string id = deploymentTarget.Id; const string usernameKey = secretKeyPrefix + ":username"; const string passwordKey = secretKeyPrefix + ":password"; const string publishUrlKey = secretKeyPrefix + ":publish-url"; const string msdeploySiteKey = secretKeyPrefix + ":msdeploySite"; string username = _credentialReadService.GetSecret(id, usernameKey); string password = _credentialReadService.GetSecret(id, passwordKey); string publishUrl = _credentialReadService.GetSecret(id, publishUrlKey); string msdeploySite = _credentialReadService.GetSecret(id, msdeploySiteKey); if (StringUtils.AllHaveValues(username, password, publishUrl, msdeploySite)) { FileInfo fileInfo = CreateTempPublishFile(deploymentTarget, username, password, publishUrl); deploymentTask.TempFiles.Add(fileInfo); publishSettingsFile = fileInfo.FullName; } else { Log.Warning("Could not get secrets for deployment target id {DeploymentTargetId}", id); } } var definitions = new { definitions = new object[] { new { deploymentTask.PackageId, targetDirectoryPath, isPreRelease = deploymentTask.SemanticVersion.IsPrerelease, environmentConfig = targetEnvironmentConfigName, publishSettingsFile, parameters, deploymentTarget.NuGetConfigFile, deploymentTarget.NuGetPackageSource, semanticVersion = deploymentTask.SemanticVersion.ToNormalizedString(), iisSiteName = deploymentTarget.IisSiteName, webConfigTransform = deploymentTarget.WebConfigTransform } } }; string json = JsonConvert.SerializeObject(definitions, Formatting.Indented); logger.Information("Using definitions JSON: {Json}", json); logger.Information("Using temp manifest file '{ManifestFile}'", tempManifestFile.FullName); await File.WriteAllTextAsync(tempManifestFile.FullName, json, Encoding.UTF8, cancellationToken); arguments.Add($"\"{tempManifestFile.FullName}\""); //TODO propagate properties by direct command or default Environment.SetEnvironmentVariable("urn:milou-deployer:tools:nuget:exe-path", _keyValueConfiguration["urn:milou-deployer:tools:nuget:exe-path"]); arguments.Add(Bootstrapper.Common.Constants.AllowPreRelease); arguments.Add(Milou.Deployer.Core.LoggingConstants.PlainOutputFormatEnabled); string[] deployerArgs = arguments.ToArray(); logger.Verbose("Running Milou Deployer bootstrapper"); HttpClient httpClient = _clientFactory.CreateClient("Bootstrapper"); using (Bootstrapper.Common.App deployerApp = await Bootstrapper.Common.App.CreateAsync(deployerArgs, logger, httpClient, false)) { NuGetPackageInstallResult result = await deployerApp.ExecuteAsync(deployerArgs.ToImmutableArray(), cancellationToken); if (result.PackageDirectory is null || result.SemanticVersion is null) { logger.Warning("Milou.Deployer failed"); return(ExitCode.Failure); } } ClearTemporaryDirectoriesAndFiles(deploymentTask); return(ExitCode.Success); }