Ejemplo n.º 1
0
        public static T DeserializeYaml <T>(Stream s) where T : class
        {
            T retVal = null;

            s.Position = 0;
            using (StreamReader reader = new StreamReader(s))
            {
                string yaml = reader.ReadToEnd();
                retVal = _yamlDeserializer.Deserialize <T>(yaml);
            }

            return(retVal);
        }
Ejemplo n.º 2
0
        public static Dictionary <string, string[]> LoadCommandNames(string aliasesFilePath = "data/aliases.yml")
        {
            var text = File.ReadAllText(aliasesFilePath);

            return(_deserializer.Deserialize <Dictionary <string, string[]> >(text));
        }
Ejemplo n.º 3
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);
        }