예제 #1
0
        static async Task <int> Main(string[] args)
        {
            var git = new BackupToGit.Git();

            BackupToGit.SecureLogger.Logfile = Path.Combine(Directory.GetCurrentDirectory(), "BackupRunscope.log");

            var parsedArgs = args.TakeWhile(a => a != "--").ToArray();
            var usage      = "Usage: BackupRunscope <access_token>";

            if (parsedArgs.Length != 1)
            {
                Log(usage);
                return(1);
            }

            var access_token = parsedArgs[0];
            var folder       = "buckets";
            await Runscope.BackupAsync(access_token, folder);

            var gitsourcefolder = folder;
            var gitbinary       = Environment.GetEnvironmentVariable("gitbinary");
            var gitserver       = Environment.GetEnvironmentVariable("gitserver");
            var gitrepopath     = Environment.GetEnvironmentVariable("gitrepopath");
            var gitreposubpath  = Environment.GetEnvironmentVariable("gitreposubpath");
            var gitusername     = Environment.GetEnvironmentVariable("gitusername");
            var gitpassword     = Environment.GetEnvironmentVariable("gitpassword");
            var gitemail        = Environment.GetEnvironmentVariable("gitemail");
            var gitsimulatepush = ParseBooleanEnvironmentVariable("gitsimulatepush", false);

            if (string.IsNullOrEmpty(gitbinary) && File.Exists("/usr/bin/git"))
            {
                gitbinary = "/usr/bin/git";
            }
            if (string.IsNullOrEmpty(gitbinary) && File.Exists(@"C:\Program Files\Git\bin\git.exe"))
            {
                gitbinary = @"C:\Program Files\Git\bin\git.exe";
            }

            if (string.IsNullOrEmpty(gitbinary) || string.IsNullOrEmpty(gitserver) || string.IsNullOrEmpty(gitrepopath) || string.IsNullOrEmpty(gitreposubpath) ||
                string.IsNullOrEmpty(gitusername) || string.IsNullOrEmpty(gitpassword) || string.IsNullOrEmpty(gitemail))
            {
                var missing = new StringBuilder();
                if (string.IsNullOrEmpty(gitbinary))
                {
                    missing.AppendLine("Missing gitbinary.");
                }

                if (string.IsNullOrEmpty(gitserver))
                {
                    missing.AppendLine("Missing gitserver.");
                }

                if (string.IsNullOrEmpty(gitrepopath))
                {
                    missing.AppendLine("Missing gitrepopath.");
                }

                if (string.IsNullOrEmpty(gitreposubpath))
                {
                    missing.AppendLine("Missing gitreposubpath.");
                }

                if (string.IsNullOrEmpty(gitusername))
                {
                    missing.AppendLine("Missing gitusername.");
                }

                if (string.IsNullOrEmpty(gitpassword))
                {
                    missing.AppendLine("Missing gitpassword.");
                }

                if (string.IsNullOrEmpty(gitemail))
                {
                    missing.AppendLine("Missing gitemail.");
                }

                Log($"Missing git environment variables, will not push Runscope bucket files to Git.{Environment.NewLine}{missing}");
            }
            else
            {
                git.GitBinary    = gitbinary;
                git.SourceFolder = gitsourcefolder;
                git.Server       = gitserver;
                git.RepoPath     = gitrepopath;
                git.RepoSubPath  = gitreposubpath;
                git.Username     = gitusername;
                git.Password     = gitpassword;
                git.Email        = gitemail;
                git.SimulatePush = gitsimulatepush;

                var result = false;
                for (var tries = 0; tries < 5 && !result; tries++)
                {
                    result = git.Push();
                }

                if (!result)
                {
                    return(1);
                }
            }

            return(0);
        }
예제 #2
0
        static async Task <int> Main(string[] args)
        {
            var git = new BackupToGit.Git();

            BackupToGit.SecureLogger.Logfile = Path.Combine(Directory.GetCurrentDirectory(), "backuparm.log");

            var parsedArgs = args.TakeWhile(a => a != "--").ToArray();
            var usage      = "Usage: backuparm <folder>";

            if (parsedArgs.Length != 1)
            {
                Log(usage);
                return(1);
            }

            var folder = parsedArgs[0];

            ServicePrincipal[] servicePrincipals;

            string[] files = Directory.GetFiles(".", "*BearerToken*.txt")
                             .Select(f => f.StartsWith($".{Path.DirectorySeparatorChar}") || f.StartsWith($".{Path.AltDirectorySeparatorChar}") ? f.Substring(2) : f)
                             .ToArray();
            if (files.Length >= 1)
            {
                servicePrincipals = new ServicePrincipal[files.Length];
                Log($"Found {files.Length} token files.");
                for (int i = 0; i < files.Length; i++)
                {
                    var    filename = files[i];
                    string content  = File.ReadAllText(filename);
                    servicePrincipals[i] = new ServicePrincipal
                    {
                        AccessToken  = content,
                        FriendlyName = filename
                    };
                }
            }
            else
            {
                servicePrincipals = GetServicePrincipals();
                if (servicePrincipals.Length == 0)
                {
                    Log("Missing environment variables: AzureTenantId, AzureSubscriptionId, AzureClientId, AzureClientSecret");
                    return(1);
                }

                Log($"Got {servicePrincipals.Length} service principals: '{string.Join("', '", servicePrincipals.Select(sp => sp.FriendlyName))}'");

                await Arm.GetAzureAccessTokensAsync(servicePrincipals);

                servicePrincipals = servicePrincipals.Where(sp => !string.IsNullOrEmpty(sp.AccessToken)).ToArray();
                Log($"Got {servicePrincipals.Length} access tokens.");
                if (servicePrincipals.Length == 0)
                {
                    return(1);
                }
            }


            var tasks = servicePrincipals.Select(servicePrincipal => Arm.SaveArmTemplatesAsync(servicePrincipal, folder));
            await Task.WhenAll(tasks);


            var gitsourcefolder = folder;
            var gitbinary       = Environment.GetEnvironmentVariable("gitbinary");
            var gitserver       = Environment.GetEnvironmentVariable("gitserver");
            var gitrepopath     = Environment.GetEnvironmentVariable("gitrepopath");
            var gitreposubpath  = Environment.GetEnvironmentVariable("gitreposubpath");
            var gitusername     = Environment.GetEnvironmentVariable("gitusername");
            var gitpassword     = Environment.GetEnvironmentVariable("gitpassword");
            var gitemail        = Environment.GetEnvironmentVariable("gitemail");
            var gitsimulatepush = ParseBooleanEnvironmentVariable("gitsimulatepush", false);
            var gitzipbinary    = Environment.GetEnvironmentVariable("gitzipbinary");
            var gitzippassword  = Environment.GetEnvironmentVariable("gitzippassword");

            if (string.IsNullOrEmpty(gitbinary) && File.Exists("/usr/bin/git"))
            {
                gitbinary = "/usr/bin/git";
            }
            if (string.IsNullOrEmpty(gitbinary) && File.Exists(@"C:\Program Files\Git\bin\git.exe"))
            {
                gitbinary = @"C:\Program Files\Git\bin\git.exe";
            }

            if (string.IsNullOrEmpty(gitbinary) || string.IsNullOrEmpty(gitserver) || string.IsNullOrEmpty(gitrepopath) || string.IsNullOrEmpty(gitreposubpath) ||
                string.IsNullOrEmpty(gitusername) || string.IsNullOrEmpty(gitpassword) || string.IsNullOrEmpty(gitemail))
            {
                var missing = new StringBuilder();
                if (string.IsNullOrEmpty(gitbinary))
                {
                    missing.AppendLine("Missing gitbinary.");
                }

                if (string.IsNullOrEmpty(gitserver))
                {
                    missing.AppendLine("Missing gitserver.");
                }

                if (string.IsNullOrEmpty(gitrepopath))
                {
                    missing.AppendLine("Missing gitrepopath.");
                }

                if (string.IsNullOrEmpty(gitreposubpath))
                {
                    missing.AppendLine("Missing gitreposubpath.");
                }

                if (string.IsNullOrEmpty(gitusername))
                {
                    missing.AppendLine("Missing gitusername.");
                }

                if (string.IsNullOrEmpty(gitpassword))
                {
                    missing.AppendLine("Missing gitpassword.");
                }

                if (string.IsNullOrEmpty(gitemail))
                {
                    missing.AppendLine("Missing gitemail.");
                }

                if (string.IsNullOrEmpty(gitzipbinary))
                {
                    missing.AppendLine("Missing gitzipbinary.");
                }

                if (string.IsNullOrEmpty(gitzippassword))
                {
                    missing.AppendLine("Missing gitzippassword.");
                }

                Log($"Missing git environment variables, will not push Arm template files to Git.{Environment.NewLine}{missing}");
            }
            else
            {
                git.GitBinary    = gitbinary;
                git.SourceFolder = gitsourcefolder;
                git.Server       = gitserver;
                git.RepoPath     = gitrepopath;
                git.RepoSubPath  = gitreposubpath;
                git.Username     = gitusername;
                git.Password     = gitpassword;
                git.Email        = gitemail;
                git.SimulatePush = gitsimulatepush;
                git.ZipBinary    = gitzipbinary ?? string.Empty;
                git.ZipPassword  = gitzippassword ?? string.Empty;

                var result = false;
                for (var tries = 0; tries < 5 && !result; tries++)
                {
                    result = git.Push();
                }

                if (!result)
                {
                    return(1);
                }
            }

            return(0);
        }