Ejemplo n.º 1
0
        public static Application CreateDefault(OpulenceConfig config, FileInfo projectFile)
        {
            if (config is null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (projectFile is null)
            {
                throw new ArgumentNullException(nameof(projectFile));
            }

            var application = new Application()
            {
                Config          = config,
                Name            = Path.GetFileNameWithoutExtension(projectFile.FullName),
                ProjectFilePath = projectFile.FullName,
                Steps           =
                {
                    new ContainerStep(),
                    new HelmChartStep(),
                },
            };

            return(application);
        }
Ejemplo n.º 2
0
        private static async Task ExecuteAsync(OutputContext output, DirectoryInfo directory)
        {
            var opulenceFilePath = DirectorySearch.AscendingSearch(directory.FullName, "opulence.json");

            if (opulenceFilePath != null)
            {
                output.WriteInfoLine($"found 'opulence.json' at '{Path.GetDirectoryName(opulenceFilePath)}'");
                return;
            }

            output.WriteInfoLine("locating nearest sln file");
            var solutionFilePath = DirectorySearch.AscendingWildcardSearch(directory.FullName, "*.sln").FirstOrDefault()?.FullName;

            if (opulenceFilePath == null && solutionFilePath != null && Confirm(output, $"use '{Path.GetDirectoryName(solutionFilePath)}' as root?"))
            {
                opulenceFilePath = Path.Combine(Path.GetDirectoryName(solutionFilePath) !, "opulence.json");
            }

            if (opulenceFilePath == null && Confirm(output, "use project directory as root?"))
            {
                opulenceFilePath = Path.Combine(directory.FullName, "opulence.json");
            }

            if (opulenceFilePath == null)
            {
                throw new CommandException("cannot determine root directory");
            }

            var config = new OpulenceConfig()
            {
                Container = new ContainerConfig()
                {
                    Registry = new RegistryConfig(),
                }
            };

            while (true)
            {
                output.WriteAlways("entry the container registry hostname (ex: example.azurecr.io): ");
                var line = Console.ReadLine();
                output.WriteAlwaysLine(string.Empty);

                if (!string.IsNullOrEmpty(line))
                {
                    config.Container.Registry.Hostname = line.Trim();
                    break;
                }
            }

            using var stream = File.OpenWrite(opulenceFilePath);
            await JsonSerializer.SerializeAsync(stream, config, new JsonSerializerOptions()
            {
                WriteIndented = true,
            });

            output.WriteInfo($"initialized opulence config at '{opulenceFilePath}'");
        }
Ejemplo n.º 3
0
        private static async Task ExecuteAsync(OutputContext output, FileInfo projectFile)
        {
            var config = await OpulenceConfigFactory.ReadConfigAsync(output, projectFile.DirectoryName);

            if (config == null)
            {
                // Allow operating without config for now.
                output.WriteInfoLine("config was not found, using defaults");
                config = new OpulenceConfig()
                {
                    Container = new ContainerConfig()
                    {
                        Registry = new RegistryConfig(),
                    }
                };
            }

            var application = ApplicationFactory.CreateDefault(config, projectFile);
            await ProjectReader.InitializeAsync(output, application);

            await ScriptRunner.RunProjectScriptAsync(output, application);

            for (var i = 0; i < application.Steps.Count; i++)
            {
                var step = application.Steps[i];
                output.WriteInfoLine($"executing step: {step.DisplayName}");

                if (step is ContainerStep container)
                {
                    await DockerContainerBuilder.BuildContainerImageAsync(output, application, container);
                }
                else if (step is HelmChartStep chart)
                {
                    await HelmChartBuilder.BuildHelmChartAsync(output, application, application.Steps.Get <ContainerStep>() !, chart);
                }
            }
        }
Ejemplo n.º 4
0
        private static async Task ExecuteAsync(OutputContext output, FileInfo projectFile, List <string> outputs, bool force)
        {
            var config = await OpulenceConfigFactory.ReadConfigAsync(output, projectFile.DirectoryName);

            if (config == null)
            {
                // Allow operating without config for now.
                output.WriteInfoLine("config was not found, using defaults");
                config = new OpulenceConfig()
                {
                    Container = new ContainerConfig()
                    {
                        Registry = new RegistryConfig(),
                    }
                };
            }

            var application = ApplicationFactory.CreateDefault(config, projectFile);
            await ProjectReader.InitializeAsync(output, application);

            await ScriptRunner.RunProjectScriptAsync(output, application);

            for (var i = 0; i < application.Steps.Count; i++)
            {
                var step = application.Steps[i];

                if (step is ContainerStep container)
                {
                    if (!outputs.Contains("container"))
                    {
                        // We should still apply the defaults here because they'll be used by
                        // the helm step.
                        DockerfileGenerator.ApplyContainerDefaults(application, container);

                        output.WriteDebugLine("skipping container");
                        continue;
                    }

                    output.WriteInfoLine("generating dockerfile");

                    var dockerFilePath = Path.Combine(application.ProjectDirectory, "Dockerfile");
                    if (File.Exists(dockerFilePath) && !force)
                    {
                        throw new CommandException("'Dockerfile' already exists for project. use --force to overwrite");
                    }

                    // force multi-phase dockerfile - this makes much more sense in the workflow
                    // where you're going to maintain the dockerfile yourself.
                    container.UseMultiphaseDockerfile = true;

                    File.Delete(dockerFilePath);

                    await DockerfileGenerator.WriteDockerfileAsync(output, application, container, dockerFilePath);
                }
                else if (step is HelmChartStep chart)
                {
                    if (!outputs.Contains("chart"))
                    {
                        output.WriteDebugLine("skipping helm chart");
                        continue;
                    }

                    output.WriteInfoLine("generating helm charts");

                    var chartDirectory = Path.Combine(application.ProjectDirectory, "charts");
                    if (Directory.Exists(chartDirectory) && !force)
                    {
                        throw new CommandException("'charts' directory already exists for project. use --force to overwrite");
                    }

                    await HelmChartGenerator.GenerateAsync(
                        output,
                        application,
                        application.Steps.Get <ContainerStep>() !,
                        chart,
                        new DirectoryInfo(chartDirectory));
                }
            }
        }