Ejemplo n.º 1
0
        private async Task <bool> ImportDependencies(ModuleManifest manifest, bool allowImport = true)
        {
            // discover module dependencies
            var dependencies = await _loader.DiscoverAllDependenciesAsync(manifest, checkExisting : false, allowImport, allowDependencyUpgrades : false);

            if (HasErrors)
            {
                return(false);
            }

            // copy all dependencies to deployment bucket that are missing or have a pre-release version
            foreach (var dependency in dependencies.Where(dependency => dependency.ModuleLocation.SourceBucketName != Settings.DeploymentBucketName))
            {
                var imported = false;

                // copy check-summed module artifacts (guaranteed immutable)
                foreach (var artifact in dependency.Manifest.Artifacts)
                {
                    imported = imported | await ImportS3Object(dependency.ModuleLocation.ModuleInfo.Origin, artifact);
                }

                // copy version manifest
                imported = imported | await ImportS3Object(dependency.ModuleLocation.ModuleInfo.Origin, dependency.ModuleLocation.ModuleInfo.VersionPath, replace : dependency.ModuleLocation.ModuleInfo.Version.IsPreRelease());

                // show message if any artifacts were imported
                if (imported)
                {
                    Console.WriteLine($"=> Imported {dependency.ModuleLocation.ModuleInfo}");
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        //--- Methods ---
        public async Task <bool> DoAsync(
            DryRunLevel?dryRun,
            string moduleReference,
            string instanceName,
            bool allowDataLoos,
            bool protectStack,
            Dictionary <string, string> parameters,
            bool forceDeploy,
            bool promptAllParameters,
            XRayTracingLevel xRayTracingLevel,
            bool deployOnlyIfExists,
            bool allowDependencyUpgrades
            )
        {
            Console.WriteLine($"Resolving module reference: {moduleReference}");

            // determine location of cloudformation template from module key
            if (!ModuleInfo.TryParse(moduleReference, out var moduleInfo))
            {
                LogError($"invalid module reference: {moduleReference}");
                return(false);
            }
            var foundModuleLocation = await _loader.ResolveInfoToLocationAsync(moduleInfo, moduleInfo.Origin, ModuleManifestDependencyType.Root, allowImport : Settings.AllowImport, showError : !deployOnlyIfExists);

            if (foundModuleLocation == null)
            {
                // nothing to do; loader already emitted an error
                return(deployOnlyIfExists);
            }

            // download module manifest
            var(manifest, manifestErrorReason) = await _loader.LoadManifestFromLocationAsync(foundModuleLocation);

            if (manifest == null)
            {
                LogError(manifestErrorReason);
                return(false);
            }

            // deploy module
            if (dryRun == null)
            {
                var stackName = Settings.GetStackName(manifest.GetFullName(), instanceName);

                // check version of previously deployed module
                if (!deployOnlyIfExists)
                {
                    Console.WriteLine("=> Validating module for deployment tier");
                }
                var updateValidation = await IsValidModuleUpdateAsync(stackName, manifest, showError : !forceDeploy && !deployOnlyIfExists);

                if (!forceDeploy && !updateValidation.Success)
                {
                    return(false);
                }

                // check if a previous deployment was found
                if (deployOnlyIfExists && (updateValidation.ExistingStack == null))
                {
                    // nothing to do
                    return(true);
                }
                var existing = updateValidation.ExistingStack;

                // check if existing stack checksum matches template checksum
                if (!forceDeploy && !parameters.Any())
                {
                    var existingChecksum = existing?.Outputs.FirstOrDefault(output => output.OutputKey == "ModuleChecksum");
                    if (existingChecksum?.OutputValue == manifest.TemplateChecksum)
                    {
                        Console.WriteLine($"{Settings.LowContrastColor}=> No changes found to deploy{Settings.ResetColor}");
                        return(true);
                    }
                }

                // prompt for missing parameters
                var deployParameters = PromptModuleParameters(manifest, existing, parameters, promptAllParameters);
                if (HasErrors)
                {
                    return(false);
                }

                // check if module supports AWS X-Ray for tracing
                if (
                    manifest.GetAllParameters().Any(p => p.Name == "XRayTracing") &&
                    !deployParameters.Any(p => p.ParameterKey == "XRayTracing")
                    )
                {
                    deployParameters.Add(new CloudFormationParameter {
                        ParameterKey   = "XRayTracing",
                        ParameterValue = xRayTracingLevel.ToString()
                    });
                }

                // discover shared module dependencies and prompt for missing parameters
                var dependencies = (await _loader.DiscoverAllDependenciesAsync(manifest, checkExisting: true, allowImport: Settings.AllowImport, allowDependencyUpgrades: allowDependencyUpgrades))
                                   .Where(dependency => dependency.Type == ModuleManifestDependencyType.Shared)
                                   .ToList();
                if (HasErrors)
                {
                    return(false);
                }
                var dependenciesParameters = dependencies
                                             .Select(dependency => new {
                    ModuleFullName = dependency.Manifest.GetFullName(),
                    Parameters     = PromptModuleParameters(
                        dependency.Manifest,
                        promptAll: promptAllParameters
                        )
                })
                                             .ToDictionary(t => t.ModuleFullName, t => t.Parameters);
                if (HasErrors)
                {
                    return(false);
                }

                // deploy module dependencies
                foreach (var dependency in dependencies)
                {
                    var dependencyLocation = new ModuleLocation(Settings.DeploymentBucketName, dependency.ModuleLocation.ModuleInfo, dependency.ModuleLocation.Hash);
                    if (!await new ModelUpdater(Settings, SourceFilename).DeployChangeSetAsync(
                            dependency.Manifest,
                            await _loader.GetNameMappingsFromLocationAsync(dependencyLocation),
                            dependencyLocation,
                            Settings.GetStackName(dependency.Manifest.GetFullName()),
                            allowDataLoos,
                            protectStack,
                            dependenciesParameters[dependency.Manifest.GetFullName()]
                            ))
                    {
                        return(false);
                    }
                }

                // deploy module
                var moduleLocation = new ModuleLocation(Settings.DeploymentBucketName, manifest.ModuleInfo, manifest.TemplateChecksum);
                return(await new ModelUpdater(Settings, moduleReference).DeployChangeSetAsync(
                           manifest,
                           await _loader.GetNameMappingsFromLocationAsync(moduleLocation),
                           moduleLocation,
                           stackName,
                           allowDataLoos,
                           protectStack,
                           deployParameters
                           ));
            }
            return(true);
        }