public async Task <int> Execute(CommandDefinition definition, ChangeObservable observable)
        {
            if (definition.BaseDefinition == newCommand)
            {
                Entity dataModel = entityFactory.Create(definition.Name, definition);
                IEnumerable <VirtualFile> files = await templateFileGenerator.InitalizeTemplate(dataModel, observable).ConfigureAwait(false);

                userInterface.WriteInformation($"Successfully created template '{dataModel.Template().name}' in {GetCommonPath(files,dataModel.Path)}.");
            }

            if (generateCommands.Contains(definition))
            {
                Entity dataModel = entityFactory.Create(definition.Name, definition);
                userInterface.WriteInformation(definition.Name == "all"
                                                   ? $"Generating all files for {dataModel.Root.Path}."
                                                   : $"Generating all files with the '{definition.Name}' " +
                                               $"generator for {dataModel.Root.Path}.");

                SingleValueArgument singleValueArgument = definition.Argument <SingleValueArgument>(Constants.OutputArgumentName);
                await templateFileGenerator.GenerateFiles(dataModel.Root, definition.Name, singleValueArgument.Value, singleValueArgument.IsDefined, observable)
                .ConfigureAwait(false);

                userInterface.WriteInformation(definition.Name == "all"
                                                   ? $"Successfully generated all files for {dataModel.Root.Path}."
                                                   : $"Successfully generated all files with the '{definition.Name}' " +
                                               $"generator for {dataModel.Root.Path}.");
            }

            if (definition == deployCommand)
            {
                Entity dataModel = entityFactory.Create(definition.Name, definition);

                deployService.DeployFiles(dataModel);

                Entity root = dataModel.Root;
                TemplateDescription template = TemplateEntity.Decorate(root).Template;
                foreach (templateDeployPostStep deployPostStep in template.DeployPostStep ?? Enumerable.Empty <templateDeployPostStep>())
                {
                    IDeployStep step = deploySteps.FirstOrDefault(s => s.Identifier == deployPostStep.identifier);
                    if (step != null)
                    {
                        step.Execute(dataModel, observable);
                    }
                    else
                    {
                        executionContext.WriteWarning(
                            $"Deploy post step '{deployPostStep.identifier}' could not be executed because there is no implementation. " +
                            $"Available implementations are:{Environment.NewLine}" +
                            $"{string.Join(Environment.NewLine, deploySteps.Select(d => d.Identifier))}");
                    }
                }

                userInterface.WriteInformation($"Successfully deployed all files for {dataModel.Root.Path}.");
            }

            return(0);

            string GetCommonPath(IEnumerable <VirtualFile> generatedFiles, string fallback)
            {
                IEnumerable <IEnumerable <VirtualDirectory> > paths = generatedFiles.Select(GetPath);
                VirtualDirectory commonDirectory = paths.Transpose()
                                                   .TakeWhile(d => d.Distinct().Count() == 1)
                                                   .FirstOrDefault()
                                                   ?.First();

                return(commonDirectory?.FullName ?? fallback);

                IEnumerable <VirtualDirectory> GetPath(VirtualFile file)
                {
                    VirtualDirectory current = file.Parent;

                    while (current != null)
                    {
                        yield return(current);

                        current = current.Parent;
                    }
                }
            }
        }
 public void AddDeployStep(IDeployStep step)
 {
     _steps.Add(() => step);
 }
예제 #3
0
        static int Main(string[] args)
        {
            Console.WriteLine("Deploy running...");

            try
            {
                var instanceDeploy = new InstanceDeploy()
                {
                    Branch = "",
                    Release = ""
                };

                if (args.Length > 0)
                {
                    instanceDeploy.Branch = args[0];
                    instanceDeploy.Release = args[1];
                }

                var deployStatus = true;
                var error = "";

                var awsClient = new AwsClient();
                var nugetClient = new NugetClient();

                var stepsUp = new IDeployStep[]
                {
                    new OctopusManagerProjectStep(OctopusConnector.GetOrCreate(), instanceDeploy.Branch),
                    new AwsLaunchNewInstanceStep(awsClient, instanceDeploy),
                    new AwsCheckTheStateStep(awsClient, instanceDeploy),
                    new AwsAssociateElasticIpStep(awsClient, awsClient.GetIpAvailable(), instanceDeploy),
                    new OctopusCreateMachineStep(OctopusConnector.GetOrCreate(), instanceDeploy),
                    new OctopusCreateReleaseStep(OctopusConnector.GetOrCreate(), nugetClient, instanceDeploy),
                    new OctopusDeploytoAwsStep(OctopusConnector.GetOrCreate(), instanceDeploy),
                };

                var stepsDown = new IDeployStep[]
                {
                    new AwsTerminateInstanceStep(awsClient, instanceDeploy),
                };

                foreach (var deployStep in stepsUp)
                {
                    Console.WriteLine("--Step {0}", deployStep.Name);

                    deployStep.Run();

                    deployStatus = deployStep.FinishedSuccessfully;

                    if (deployStep.FinishedSuccessfully)
                        Console.WriteLine("----Finished Successfully!");
                    else
                    {
                        error = deployStep.Error;
                        Console.WriteLine("----Finished with Error {0}", deployStep.Error);
                        break;
                    }
                }

                foreach (var step in stepsDown)
                {
                    Console.WriteLine("--Step {0}", step.Name);

                    step.Run();

                    if (step.FinishedSuccessfully)
                        Console.WriteLine("----Finished Successfully!");
                    else
                    {
                        Console.WriteLine("----Finished with Error {0}", step.Error);
                    }
                }

                var slackNotification = new SlackNotificationStep(instanceDeploy, deployStatus, error);
                slackNotification.Run();

                Console.WriteLine("");
                Console.WriteLine("Deploy:");
                Console.WriteLine("Branch: {0}", instanceDeploy.Branch);
                Console.WriteLine("Release: {0}, Id: {1}", instanceDeploy.Release, instanceDeploy.ReleaseId);
                Console.WriteLine("Ip: {0}", instanceDeploy.Ip);
                Console.WriteLine("Instance: {0}", instanceDeploy.InstanceId);
                Console.WriteLine("Previous Status: {0}", instanceDeploy.PreviousStatus);
                Console.WriteLine("Last Status: {0}", instanceDeploy.CurrentStatus);

                Console.WriteLine("Deploy finished!");

                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: ");
                Console.WriteLine(ex.Message);

                return -1;
            }
        }