Exemple #1
0
        private int Run(string[] args)
        {
            System.Text.Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            if (args.Length < 2 || args.Length > 3)
            {
                return(Usage());
            }

            // Determine Backup/Restore
            CopyAction action;

            if (args[0].ToLower() == "backup")
            {
                action = CopyAction.Backup;
            }
            else if (args[0].ToLower() == "restore")
            {
                action = CopyAction.Restore;
            }
            else
            {
                return(Usage());
            }

            // Read Yaml input file
            string input = args[1];

            try {
                YamlRawContent = File.ReadAllText(input);
            } catch (Exception) {
                Console.WriteLine($"Input file {input} not found");
                throw;
            }

            // Find out what to do

            string content;

            // Determine Blue/Green
            if (args.Length == 3)
            {
                string blueGreen = args[2].ToLower();
                if (blueGreen == "blue")
                {
                    BlueGreenDeploy = BlueGreenDeployEnum.Blue;
                }
                else if (blueGreen == "green")
                {
                    BlueGreenDeploy = BlueGreenDeployEnum.Green;
                }
                else
                {
                    return(Usage());
                }

                if (action == CopyAction.Restore)
                {
                    throw new Error("Can't specify Blue/Green when using Restore");
                }
            }
            else
            {
                if (action == CopyAction.Backup)
                {
                    // find out whether Blue or Green is running

                    YamlData yamlData;
                    try {
                        YamlDotNet.Serialization.IDeserializer deserializer = Yaml.GetDeserializer();
                        yamlData = deserializer.Deserialize <YamlData>(YamlRawContent);
                    } catch (Exception exc) {
                        Console.WriteLine($"Input file {input} is invalid");
                        string msg = Error.FormatExceptionMessage(exc);
                        Console.WriteLine(msg);
                        throw;
                    }
                    // note that yamlData still has Blue/Green variables
                    if (!string.IsNullOrWhiteSpace(yamlData.Deploy.DetermineBlueGreen))
                    {
                        if (string.IsNullOrWhiteSpace(yamlData.Deploy.BlueRegex))
                        {
                            throw new Error($"Can't determine Blue/Green - Yaml file doesn't define {nameof(yamlData.Deploy)}:{nameof(yamlData.Deploy.BlueRegex)}");
                        }
                        if (string.IsNullOrWhiteSpace(yamlData.Deploy.GreenRegex))
                        {
                            throw new Error($"Can't determine Blue/Green - Yaml file doesn't define {nameof(yamlData.Deploy)}:{nameof(yamlData.Deploy.GreenRegex)}");
                        }

                        BlueGreenDeploy = DetermineNewBlueGreen(yamlData);
                    }
                }
            }

            content = ReplaceBlueGreen(YamlRawContent);

            // deserialize yamldata - yamlData no longer has Blue/Green variables
            try {
                YamlDotNet.Serialization.IDeserializer deserializer = Yaml.GetDeserializer();
                YamlData = deserializer.Deserialize <YamlData>(content);
            } catch (Exception exc) {
                Console.WriteLine($"Input file {input} is invalid");
                string msg = Error.FormatExceptionMessage(exc);
                Console.WriteLine(msg);
                throw;
            }

            try {
                if (action == CopyAction.Backup)
                {
                    Backup backup = new Backup();
                    backup.PerformBackup();
                }
                else
                {
                    Restore restore = new Restore();
                    restore.PerformRestore();
                }
            } catch (Exception exc) {
                string msg = Error.FormatExceptionMessage(exc);
                Console.WriteLine(msg);
                throw;
            }

            return(0);
        }
Exemple #2
0
 /// <summary>
 /// Initializes a new <see cref="YamlDotNetSerializer"/>
 /// </summary>
 /// <param name="serializer">The underlying <see cref="IYamlDotNetSerializer"/></param>
 /// <param name="deserializer">The underlying <see cref="IYamlDotNetDeserializer"/></param>
 public YamlDotNetSerializer(IYamlDotNetSerializer serializer, IYamlDotNetDeserializer deserializer)
 {
     this.Serializer   = serializer;
     this.Deserializer = deserializer;
 }