示例#1
0
        public static bool WhatIfEnabled(
            [NotNull] this DeploymentExecutionDefinition deploymentExecutionDefinition,
            bool defaultValue = false)
        {
            if (deploymentExecutionDefinition == null)
            {
                throw new ArgumentNullException(nameof(deploymentExecutionDefinition));
            }

            return(GetBoolValue(deploymentExecutionDefinition,
                                defaultValue,
                                WebDeployRules.WhatIfEnabled));
        }
示例#2
0
        public static bool AppDataSkipDirectiveEnabled(
            [NotNull] this DeploymentExecutionDefinition deploymentExecutionDefinition,
            bool defaultValue = false)
        {
            if (deploymentExecutionDefinition is null)
            {
                throw new ArgumentNullException(nameof(deploymentExecutionDefinition));
            }

            return(GetBoolValue(deploymentExecutionDefinition,
                                defaultValue,
                                WebDeployRules.AppDataSkipDirectiveEnabled));
        }
示例#3
0
        public static bool ApplicationInsightsProfiler2SkipDirectiveEnabled(
            [NotNull] this DeploymentExecutionDefinition deploymentExecutionDefinition,
            bool defaultValue = true)
        {
            if (deploymentExecutionDefinition == null)
            {
                throw new ArgumentNullException(nameof(deploymentExecutionDefinition));
            }

            return(GetBoolValue(deploymentExecutionDefinition,
                                defaultValue,
                                WebDeployRules.ApplicationInsightsProfiler2SkipDirectiveEnabled));
        }
示例#4
0
        public static bool DoNotDeleteEnabled(
            [NotNull] this DeploymentExecutionDefinition deploymentExecutionDefinition,
            bool defaultValue = true)
        {
            if (deploymentExecutionDefinition is null)
            {
                throw new ArgumentNullException(nameof(deploymentExecutionDefinition));
            }

            return(GetBoolValue(deploymentExecutionDefinition,
                                defaultValue,
                                WebDeployRules.DoNotDeleteEnabled));
        }
示例#5
0
        private static bool GetBoolValue(
            DeploymentExecutionDefinition deploymentExecutionDefinition,
            bool defaultValue,
            string configurationKey)
        {
            deploymentExecutionDefinition.Parameters.TryGetValue(configurationKey,
                                                                 out StringValues values);

            if (values.Count == 1 && bool.TryParse(values[0], out bool flag))
            {
                return(flag);
            }

            return(defaultValue);
        }
        private static SemanticVersion GetSemanticVersionFromDefinition(
            DeploymentExecutionDefinition deploymentExecutionDefinition,
            DirectoryInfo packageDirectory,
            SemanticVersion fallback)
        {
            SemanticVersion version = deploymentExecutionDefinition.SemanticVersion
                                      ?? (SemanticVersion.TryParse(
                                              packageDirectory.Name.Replace(
                                                  deploymentExecutionDefinition.PackageId,
                                                  "").TrimStart('.'),
                                              out SemanticVersion semanticVersion)
                    ? semanticVersion
                    : fallback);

            return(version);
        }
        private static ImmutableArray <EnvironmentFile> GetEnvironmentFiles(
            DirectoryInfo configContentDirectory,
            DeploymentExecutionDefinition deploymentExecutionDefinition)
        {
            int patternLength = DeploymentConstants.EnvironmentPackagePattern.Split('.').Length;

            var files =
                configContentDirectory.GetFiles("*.*", SearchOption.AllDirectories)
                .Select(file => new EnvironmentFile(file, file.Name.Split('.')))
                .Where(file => file.FileNameParts.Length == patternLength &&
                       file.FileNameParts.Skip(1)
                       .First()
                       .Equals(DeploymentConstants.EnvironmentLiteral,
                               StringComparison.OrdinalIgnoreCase) &&
                       file.FileNameParts.Skip(2).First().Equals(
                           deploymentExecutionDefinition.EnvironmentConfig,
                           StringComparison.OrdinalIgnoreCase))
                .ToImmutableArray();

            return(files);
        }
        private async Task <EnvironmentPackageResult> AddEnvironmentPackageAsync(
            DeploymentExecutionDefinition deploymentExecutionDefinition,
            DirectoryInfo tempDirectoryInfo,
            List <FileMatch> possibleXmlTransformations,
            List <FileMatch> replaceFiles,
            List <DirectoryInfo> tempDirectoriesToClean,
            SemanticVersion version,
            CancellationToken cancellationToken = default)
        {
            _logger.Debug("Fetching environment configuration {EnvironmentConfig}",
                          deploymentExecutionDefinition.EnvironmentConfig);

            SemanticVersion expectedVersion   = version;
            string          expectedPackageId =
                $"{deploymentExecutionDefinition.PackageId}.{DeploymentConstants.EnvironmentLiteral}.{deploymentExecutionDefinition.EnvironmentConfig}";

            ImmutableArray <SemanticVersion> allVersions = await _nugetPackageInstaller.GetAllVersionsAsync(
                new NuGetPackageId(expectedPackageId),
                allowPreRelease : expectedVersion.IsPrerelease,
                nuGetSource : deploymentExecutionDefinition.NuGetPackageSource,
                nugetConfig : deploymentExecutionDefinition.NuGetConfigFile,
                nugetExePath : deploymentExecutionDefinition.NuGetExePath,
                timeoutInSeconds : 35,
                adaptiveEnabled : deploymentExecutionDefinition.PackageListPrefixEnabled,
                prefix : deploymentExecutionDefinition.PackageListPrefixEnabled.HasValue && deploymentExecutionDefinition.PackageListPrefixEnabled.Value?deploymentExecutionDefinition.PackageListPrefix : ""
                );

            var matchingFoundEnvironmentPackage = allVersions
                                                  .Where(currentVersion => currentVersion == expectedVersion)
                                                  .ToList();

            if (matchingFoundEnvironmentPackage.Count > 1)
            {
                _logger.Error("Found multiple environment packages matching '{ExpectedMatch}', {Found}",
                              expectedVersion.ToNormalizedString(),
                              string.Join(", ", matchingFoundEnvironmentPackage.Select(currentVersion => $"'{currentVersion.ToNormalizedString()}'")));
                return(new EnvironmentPackageResult(false));
            }

            const string environmentConfigPrefix = "EF_";

            if (matchingFoundEnvironmentPackage.Any())
            {
                var tempInstallDirectory =
                    new DirectoryInfo(
                        Path.Combine(
                            tempDirectoryInfo.FullName,
                            $"{environmentConfigPrefix}tmp",
                            deploymentExecutionDefinition.EnvironmentConfig));

                var deploymentDefinition =
                    new DeploymentExecutionDefinition(
                        expectedPackageId,
                        tempInstallDirectory.FullName,
                        expectedVersion,
                        nugetExePath: deploymentExecutionDefinition.NuGetExePath,
                        nuGetPackageSource: deploymentExecutionDefinition.NuGetPackageSource,
                        nuGetConfigFile: deploymentExecutionDefinition.NuGetConfigFile);

                var tempOutputDirectory =
                    new DirectoryInfo(
                        Path.Combine(
                            tempDirectoryInfo.FullName,
                            $"{environmentConfigPrefix}out",
                            deploymentExecutionDefinition.EnvironmentConfig));

                MayBe <InstalledPackage> installedEnvironmentPackage =
                    await
                    _packageInstaller.InstallPackageAsync(
                        deploymentDefinition,
                        tempOutputDirectory,
                        false,
                        null,
                        cancellationToken).ConfigureAwait(false);

                if (!installedEnvironmentPackage.HasValue)
                {
                    _logger.Error(
                        "No environment NuGet package was installed for deployment definition {DeploymentDefinition}",
                        deploymentDefinition);

                    return(new EnvironmentPackageResult(false));
                }

                var configContentDirectory =
                    new DirectoryInfo(
                        Path.Combine(tempOutputDirectory.FullName, expectedPackageId, "content"));

                if (!configContentDirectory.Exists)
                {
                    _logger.Debug("The content directory for the environment package does not exist");
                }
                else
                {
                    ImmutableArray <EnvironmentFile> environmentFiles = GetEnvironmentFiles(
                        configContentDirectory,
                        deploymentExecutionDefinition);

                    if (environmentFiles.Any())
                    {
                        foreach (EnvironmentFile item in environmentFiles)
                        {
                            FindMatches(item, possibleXmlTransformations, configContentDirectory, replaceFiles);
                        }
                    }
                    else
                    {
                        IEnumerable <string> fileNamesToConcat =
                            configContentDirectory.GetFiles().Select(file => $"'{file.Name}'");

                        string foundFiles = string.Join(", ", fileNamesToConcat);

                        _logger.Debug("Could not find any action files in package, all files {FoundFiles}",
                                      foundFiles);
                    }
                }

                _logger.Verbose("Deleting transformation package temp directory '{TempOutputDirectory}'",
                                tempOutputDirectory);

                tempDirectoriesToClean.Add(tempOutputDirectory);
            }
            else
            {
                if (deploymentExecutionDefinition.RequireEnvironmentConfig)
                {
                    _logger.Error(
                        "Environment config was set to {EnvironmentConfig} but no package was found with id {ExpectedPackageId} and version {Version}, deployment definition require the environment config",
                        deploymentExecutionDefinition.EnvironmentConfig,
                        expectedPackageId,
                        expectedVersion.ToNormalizedString());

                    return(new EnvironmentPackageResult(false));
                }

                _logger.Debug(
                    "Environment config was set to {EnvironmentConfig} but no package was found with id {ExpectedPackageId} and version {Version}",
                    deploymentExecutionDefinition.EnvironmentConfig,
                    expectedPackageId,
                    expectedVersion.ToNormalizedString());
            }

            var foundPackage = matchingFoundEnvironmentPackage.SingleOrDefault();

            return(new EnvironmentPackageResult(true, foundPackage));
        }
        public static RuleConfiguration Get(
            [NotNull] DeploymentExecutionDefinition deploymentExecutionDefinition,
            [NotNull] DeployerConfiguration deployerConfiguration,
            ILogger logger)
        {
            if (deploymentExecutionDefinition == null)
            {
                throw new ArgumentNullException(nameof(deploymentExecutionDefinition));
            }

            if (deployerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(deployerConfiguration));
            }

            bool doNotDeleteEnabled = deploymentExecutionDefinition.DoNotDeleteEnabled(deployerConfiguration
                                                                                       .WebDeploy.Rules.DoNotDeleteRuleEnabled);

            bool useChecksumEnabled = deploymentExecutionDefinition.UseChecksumEnabled(deployerConfiguration
                                                                                       .WebDeploy.Rules.UseChecksumRuleEnabled);

            bool appDataSkipDirectiveEnabled = deploymentExecutionDefinition.AppDataSkipDirectiveEnabled(
                deployerConfiguration
                .WebDeploy.Rules.AppDataSkipDirectiveEnabled);

            bool applicationInsightsProfiler2SkipDirectiveEnabled =
                deploymentExecutionDefinition.ApplicationInsightsProfiler2SkipDirectiveEnabled(
                    deployerConfiguration
                    .WebDeploy.Rules.ApplicationInsightsProfiler2SkipDirectiveEnabled);

            bool appOfflineEnabled = deploymentExecutionDefinition.AppOfflineEnabled(deployerConfiguration
                                                                                     .WebDeploy.Rules.AppOfflineRuleEnabled);

            bool whatIfEnabled = deploymentExecutionDefinition.WhatIfEnabled();

            logger?.Debug(
                "{RuleName}: {DoNotDeleteEnabled}",
                nameof(deployerConfiguration.WebDeploy.Rules.DoNotDeleteRuleEnabled),
                doNotDeleteEnabled);
            logger?.Debug(
                "{RuleName}: {AppOfflineEnabled}",
                nameof(deployerConfiguration.WebDeploy.Rules.AppOfflineRuleEnabled),
                appOfflineEnabled);
            logger?.Debug(
                "{RuleName}: {UseChecksumEnabled}",
                nameof(deployerConfiguration.WebDeploy.Rules.UseChecksumRuleEnabled),
                useChecksumEnabled);
            logger?.Debug(
                "{RuleName}: {AppDataSkipDirectiveEnabled}",
                nameof(deployerConfiguration.WebDeploy.Rules.AppDataSkipDirectiveEnabled),
                appDataSkipDirectiveEnabled);
            logger?.Debug(
                "{RuleName}: {ApplicationInsightsProfiler2SkipDirectiveEnabled}",
                nameof(deployerConfiguration.WebDeploy.Rules.ApplicationInsightsProfiler2SkipDirectiveEnabled),
                applicationInsightsProfiler2SkipDirectiveEnabled);
            logger?.Debug(
                "{RuleName}: {WhatIfEnabled}",
                nameof(DeploymentExecutionDefinitionExtensions.WhatIfEnabled),
                whatIfEnabled);

            return(new RuleConfiguration
            {
                UseChecksumEnabled = useChecksumEnabled,
                AppDataSkipDirectiveEnabled = appDataSkipDirectiveEnabled,
                ApplicationInsightsProfiler2SkipDirectiveEnabled = applicationInsightsProfiler2SkipDirectiveEnabled,
                WhatIfEnabled = whatIfEnabled,
                DoNotDeleteEnabled = doNotDeleteEnabled,
                AppOfflineEnabled = appOfflineEnabled,
                Excludes = DefaultExcludes
            });
        }