Beispiel #1
0
        private static async Task <bool> executeSNS(string[] args, Credentials credentials)
        {
            var nArgs  = CLIHelper.GetNamedArguments(args);
            var helper = new SNSHelper();

            switch (args[1])
            {
            case "send":
            {
                return(true);
            }

            case "help":
            {
                HelpPrinter($"{args[0]}", "Simple Notificaiton Service",
                            ("send", "Accepts params: topic, data"));
                return(true);
            }

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown SNS command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #2
0
        private static async Task executeCURL(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1])
            {
            case "curl-get":
                await TestHelper.AwaitSuccessCurlGET(
                    uri : nArgs["uri"],
                    timeout : nArgs["timeout"].ToInt32(),
                    intensity : nArgs.FirstOrDefault(x => x.Key == "intensity").Value.ToIntOrDefault(1000));

                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Asmodat Tests",
                            ("curl-get", "Accepts params: uri, timeout, intensity (default: 1000 [ms])"));
                break;

            default: throw new Exception($"Unknown test command: '{args[1]}'");
            }
        }
Beispiel #3
0
        private static async Task <bool> executeSM(string[] args, Credentials credentials)
        {
            var nArgs  = CLIHelper.GetNamedArguments(args);
            var helper = new SMHelper(credentials);

            switch (args[1])
            {
            case "get-secret":
            {
                var name   = nArgs["name"];
                var key    = nArgs.GetValueOrDefault("key", null);
                var output = nArgs["output"]?.ToFileInfo();
                var force  = nArgs["force"].ToBoolOrDefault(false);

                if (!(output?.Directory).TryCreate())
                {
                    throw new Exception($"Failed to create output directory '{output?.Directory?.FullName ?? "undefined"}'");
                }

                if (output.Exists && !force)
                {
                    throw new Exception($"Failed to create secret, output '{output.FullName}' already exists.");
                }

                var result = await helper.GetSecret(name : name, key : key);

                output.WriteAllText(result);

                WriteLine($"{result ?? "undefined"}");

                return(true);
            }

            case "show-secret":
            {
                var name   = nArgs["name"];
                var key    = nArgs.GetValueOrDefault("key", null);
                var result = await helper.GetSecret(name : name, key : key);

                Console.Write($"{result ?? "undefined"}");
                return(true);
            }

            case "help":
            {
                HelpPrinter($"{args[0]}", "Secrets Manager",
                            ("get-secret", "Accepts params: name, key (optional), output, silent, force"),
                            ("show-secret", "Accepts params: name, key (optional)"));
                return(true);
            }

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown IAM command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #4
0
        private static void executeECS(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            var helper = new ECSHelper();

            switch (args[1])
            {
            case "destroy-cluster":
                helper.DeleteClusterAsync(nArgs["name"]).Wait();
                ; break;

            case "destroy-service":
                helper.DestroyService(
                    nArgs.FirstOrDefault(x => x.Key == "cluster").Value,     //optional
                    nArgs["service"]).Wait();
                ; break;

            case "destroy-task-definitions":
                helper.DestroyTaskDefinitions(nArgs["family"]).Wait();
                ; break;

            case "await-service-start":
                helper.WaitForServiceToStart(
                    nArgs.FirstOrDefault(x => x.Key == "cluster").Value,     //optional
                    nArgs["service"],
                    nArgs["timeout"].ToInt32()).Wait();
                ; break;

            case "describe-tasks":
            {
                var result = helper.DescribeTasksAsync(nArgs["cluster"], nArgs["tasks"].Split(',')).Result;
                Console.WriteLine(result.JsonSerialize(Newtonsoft.Json.Formatting.Indented));
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Amazon S3",
                            ("destroy-cluster", "Accepts params: name"),
                            ("destroy-service", "Accepts params: cluster, service"),
                            ("destroy-task-definitions", "Accepts params: family"),
                            ("await-service-start", "Accepts params: cluster, service, timeout"),
                            ("describe-tasks", "Accepts params: tasks (array, comma separated guid's)"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown ECS command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #5
0
        private static async Task executeKMS(string[] args, Credentials credentials)
        {
            var nArgs  = CLIHelper.GetNamedArguments(args);
            var helper = new KMSHelper(credentials);

            switch (args[1])
            {
            case "list-grants":
            {
                var result = await helper.GetGrantsByKeyNameAsync(
                    keyName : nArgs["key"],
                    grantName : nArgs["name"]);

                Console.WriteLine($"{result.JsonSerialize(Newtonsoft.Json.Formatting.Indented)}");
            }
                ; break;

            case "create-grant":
            {
                var grants = nArgs["grants"].Split(',').Where(x => !x.IsNullOrWhitespace()).ToEnum <KMSHelper.GrantType>();
                var grant  = grants.Aggregate((a, b) => a | b);

                var result = await helper.CreateRoleGrantByName(
                    keyName : nArgs["key"],
                    grantName : nArgs["name"],
                    roleName : nArgs["role"],
                    grant : grant);

                Console.WriteLine($"SUCCESS, grant '{nArgs["name"]}' of key '{nArgs["key"]}' for role '{nArgs["role"]}' was created with privileges: '{grants.Select(x => x.ToString()).JsonSerialize()}'.");
            }
                ; break;

            case "remove-grant":
            {
                var result = await helper.RemoveGrantsByName(
                    keyName : nArgs["key"],
                    grantName : nArgs["name"]);

                Console.WriteLine($"SUCCESS, {result?.Length ?? 0} grant/s with name '{nArgs["name"]}' of key '{nArgs["key"]}' were removed.");
            }
                ; break;

            case "help": HelpPrinter($"{args[0]}", "Amazon Identity and Access Management",
                                     ("list-grants", "Accepts params: key, name"),
                                     ("create-grant", "Accepts params: key, name, role, grants (',' separated: Encrypt, Decrypt)"),
                                     ("remove-grant", "Accepts params: key, name"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown IAM command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #6
0
        private static void executeECR(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            var helper = new ECRHelper();

            switch (args[1])
            {
            case "list-images-by-tag":
                Console.WriteLine($"{(helper.GetImagesByTag(imageTag: nArgs["imageTag"], registryId: nArgs["registryId"], repositoryName: nArgs["repositoryName"]).Result.Select(x => x.ImageId).JsonSerialize(Newtonsoft.Json.Formatting.Indented))}");
                ; break;

            case "list-tagged-images":
                Console.WriteLine($"{(helper.ListTaggedImages(registryId: nArgs["registryId"], repositoryName: nArgs["repositoryName"]).Result.JsonSerialize(Newtonsoft.Json.Formatting.Indented))}");
                ; break;

            case "list-untagged-images":
                Console.WriteLine($"{(helper.ListUntaggedImages(registryId: nArgs["registryId"], repositoryName: nArgs["repositoryName"]).Result.JsonSerialize(Newtonsoft.Json.Formatting.Indented))}");
                ; break;

            case "retag":
                helper.RetagImageAsync(
                    imageTag: nArgs["imageTag"],
                    imageTagNew: nArgs["imageTagNew"],
                    registryId: nArgs["registryId"],
                    repositoryName: nArgs["repositoryName"]).Wait();
                ; break;

            case "delete":
                helper.BatchDeleteImagesByTag(
                    imageTag: nArgs["imageTag"],
                    registryId: nArgs["registryId"],
                    repositoryName: nArgs["repositoryName"]).Wait();
                ; break;

            case "help": HelpPrinter($"{args[0]}", "Amazon Elastic Container Registry",
                                     ("list-images-by-tag", "Accepts params: imageTag, imageTagNew, registryId, repositoryName"),
                                     ("list-tagged-images", "Accepts params: imageTagNew, registryId, repositoryName"),
                                     ("list-untagged-images", "Accepts params: imageTagNew, registryId, repositoryName"),
                                     ("retag", "Accepts params: imageTag, imageTagNew, registryId, repositoryName"),
                                     ("delete", "Accepts params: imageTag, registryId, repositoryName"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown ECS command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Print help for using this executable, including switch usage
        /// information and descriptions.
        /// </summary>
        static void PrintHelp()
        {
            HelpPrinter helpPrinter = new HelpPrinter(ExecutableName, Switches,
                                                      GetHeader());

            helpPrinter.Print();
        }
Beispiel #8
0
        /// <summary>
        /// Finally construct Engine instance when all items are ready
        /// </summary>
        /// <param name="console"></param>
        /// <returns></returns>
        public ICommandEngine ConstructForConsole(IConsole console)
        {
            if (console == null)
            {
                throw new ArgumentNullException(nameof(console));
            }

            if (this._options == null)
            {
                throw new InvalidOperationException("Could not construct engine without providing Options object.");
            }

            if (this._styles == null)
            {
                throw new InvalidOperationException("Could not construct engine without providing Styles object.");
            }

            var printHelper    = new HelpPrinter(this._options, this._styles.HelpStyles, console);
            var commandManager = new CommandManager(this._commands.Distinct().ToArray());

            var keywords = printHelper.GetCommandKeyWords();

            // TODO: here eventually construct other subsystems with keywords, and then merge them. Also validate different keywords from different subsystems...

            ValidateKeywords(keywords, commandManager);
            // TODO: switches too?

            return(new CommandEngine(commandManager, this._options, this._styles, printHelper, console));
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            var parameters = new Parameters(new Randomizer(), args);

            if (parameters.ShowHelp)
            {
                var helpPrinter = new HelpPrinter();
                helpPrinter.Print();
            }
            else
            {
                var verbosePrinter = new VerbosePrinter(parameters.IsVerboseEnabled);
                var calculator     = new Calculator(verbosePrinter, parameters.Target, parameters.Numbers);

                if (parameters.IsUsingRandomTarget)
                {
                    Console.WriteLine("No target number specified. Will use random number.");
                    Console.WriteLine($"The random target is {calculator.Target}");
                    Console.WriteLine();
                }

                if (parameters.IsUsingRandomNumbers)
                {
                    Console.WriteLine("No numbers specified. Will use random numbers between 1 and 20.");
                    var numbers = string.Join(", ", calculator.InputNumbers.Select(o => o.Integer.ToString()));
                    Console.WriteLine($"The numbers are {numbers}");
                    Console.WriteLine();
                }

                var result  = calculator.FindSolutions();
                var printer = new SolutionPrinter(parameters.ShowAllResults, result);
                printer.Print();
            }
        }
Beispiel #10
0
        private static void executeCLI(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "command":
            {
                var file       = nArgs["file"];
                var argsuments = nArgs["args"];
                var directory  = nArgs["dir"];
                var timeout    = nArgs.GetValueOrDefault("timeout").ToIntOrDefault(0);
                var output     = CLIHelper.Command(
                    fileName: file,
                    args: argsuments,
                    workingDirectory: directory,
                    timeout: timeout);

                Console.WriteLine(output.JsonSerialize(Newtonsoft.Json.Formatting.Indented));

                Console.WriteLine($"Success, command was executed.");
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Execute command",
                            ("command", "Accepts params: file, args, dir, timeout)")
                            );
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown CLI command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #11
0
        private static void executeGithub(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "on-change-process":
            {
                GithubOnChangeProcess(
                    branch: nArgs["branch"],
                    user: nArgs["user"],
                    repository: nArgs["repository"],
                    file: nArgs["file"],
                    arguments: nArgs["arguments"],
                    wait: nArgs["wait"].ToInt32(),
                    intensity: nArgs.GetValueOrDefault("intensity").ToIntOrDefault(60000),
                    userAgent: nArgs.GetValueOrDefault("user-agent", "Asmodat Deployment Toolkit"),
                    accessToken: nArgs.GetValueOrDefault("access_token"));
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Github API Control",
                            ("on-change-process", "Accepts params: branch, user, repository, file, arguments, wait, intensity"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown AES command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #12
0
        private static void executeConsole(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "prompt-read-line":
            {
                var prompts = nArgs.GetValueOrDefault("prompts", "").Split(",");
                var output  = new List <string>();
                foreach (var prompt in prompts)
                {
                    Console.Write(prompt);
                    output.Add(Console.ReadLine());
                }
                Console.WriteLine(output.ToArray().JsonSerialize());
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Execute command",
                            ("prompt-read-line", "Accepts params: input)")
                            );
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown console command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #13
0
        private static void executeBitbucket(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "pull-approve":
            {
                var key         = nArgs["key"];
                var secret      = nArgs["secret"];
                var username    = nArgs["username"];
                var slug        = nArgs["slug"];
                var source      = nArgs.GetValueOrDefault("source", "develop");
                var destination = nArgs.GetValueOrDefault("destination", "master");
                var state       = nArgs.GetValueOrDefault("state", "OPEN");

                var bc          = new BitbucketClient(key, secret);
                var pullRequest = bc.GetPullRequest(username, slug, source, destination, state).Result;

                if (pullRequest == null)
                {
                    Console.WriteLine($"Warning, could not pull-approve because no pull request was found for source: {source}, destination: {destination}, state: {state} in {username}/{slug} repository.");
                }

                if (bc.PullRequestApprove(pullRequest).Result)
                {
                    Console.WriteLine($"Success, Pull request {source} => {destination} in {username}/{slug} repository was approved.");
                }

                Console.WriteLine($"Failure, Pull request {source} => {destination} in {username}/{slug} repository was NOT approved.");
            }
                ; break;

            case "pull-unapprove":
            {
                var key         = nArgs["key"];
                var secret      = nArgs["secret"];
                var username    = nArgs["username"];
                var slug        = nArgs["slug"];
                var source      = nArgs.GetValueOrDefault("source", "develop");
                var destination = nArgs.GetValueOrDefault("destination", "master");
                var state       = nArgs.GetValueOrDefault("state", "OPEN");

                var bc          = new BitbucketClient(key, secret);
                var pullRequest = bc.GetPullRequest(username, slug, source, destination, state).Result;

                if (pullRequest == null)
                {
                    Console.WriteLine($"Warning, could not pull-unapprove because no pull request was found for source: {source}, destination: {destination}, state: {state} in {username}/{slug} repository.");
                }

                if (bc.PullRequestUnApprove(pullRequest).Result)
                {
                    Console.WriteLine($"Success, Pull request {source} => {destination} in {username}/{slug} repository was unpproved.");
                    return;
                }

                Console.WriteLine($"Failure, Pull request {source} => {destination} in {username}/{slug} repository was NOT unapproved.");
            }
                ; break;

            case "pull-comment":
            {
                var key         = nArgs["key"];
                var secret      = nArgs["secret"];
                var username    = nArgs["username"];
                var slug        = nArgs["slug"];
                var source      = nArgs.GetValueOrDefault("source", "develop");
                var destination = nArgs.GetValueOrDefault("destination", "master");
                var state       = nArgs.GetValueOrDefault("state", "OPEN");
                var content     = nArgs["content"];

                var bc          = new BitbucketClient(key, secret);
                var pullRequest = bc.GetPullRequest(username, slug, source, destination, state).Result;

                if (pullRequest == null)
                {
                    Console.WriteLine($"Warning, could not comment because no pull request was found for source: {source}, destination: {destination}, state: {state} in {username}/{slug} repository.");
                }

                bc.PullRequestComment(pullRequest, content).Await();
                Console.WriteLine($"Success, Commented Pull request {source} => {destination} in {username}/{slug} repository with text: '{content}'.");
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Bitbucket API Control",
                            ("pull-approve", "Accepts params: key, secret, username, slug, source (default: develop), destination (default: master), state (default: OPEN)"),
                            ("pull-unapprove", "Accepts params: key, secret, username, slug, source (default: develop), destination (default: master), state (default: OPEN)"),
                            ("pull-comment", "Accepts params: key, secret, username, slug, source (default: develop), destination (default: master), state (default: OPEN), content"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown AES command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #14
0
        private static void executeRSA(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "create-pem-key":
            {
                var dir  = nArgs["output"].ToDirectoryInfo();
                var size = nArgs["size"].ToInt32();

                if (!dir.Exists)
                {
                    dir.Create();
                }

                var keys = RSA.GeneratePemKeyPair(size);

                Console.WriteLine($"Public Key:\n{keys.Public}");

                var pubFile = Path.Combine(dir.FullName, $"{dir.Name}.pub.pem").ToFileInfo();
                var prvFile = Path.Combine(dir.FullName, $"{dir.Name}.prv.pem").ToFileInfo();

                Console.WriteLine($"Saving RSA Public Key to: '{pubFile.FullName}'.");
                pubFile.WriteAllText(keys.Public);
                Console.WriteLine($"Saving RSA private Key to: '{prvFile.FullName}'.");
                prvFile.WriteAllText(keys.Private);
            }
                ; break;

            case "sign":
            {
                var key    = nArgs["key"].ToFileInfo();
                var input  = nArgs["input"].ToFileInfo();
                var output = nArgs["output"].ToFileInfo();
                var algo   = EnumEx.ToEnum <RSA.Algorithm>(nArgs["algorithm"]);

                Console.WriteLine($"Signing: '{input.FullName}' with Algorithm: '{algo.ToString()}'.");
                var sig = RSA.SignWithPemKey(input: input, pem: key, algorithm: algo);

                Console.WriteLine($"Signature:\n{sig.ToHexString()}");
                Console.WriteLine($"Saving Signature to: '{output.FullName}'.");
                output.WriteAllBytes(sig);
            }
                ; break;

            case "verify":
            {
                var key       = nArgs["key"].ToFileInfo();
                var input     = nArgs["input"].ToFileInfo();
                var signature = nArgs["signature"].ToFileInfo();
                var algo      = EnumEx.ToEnum <RSA.Algorithm>(nArgs["algorithm"]);
                var @throw    = nArgs.GetValueOrDefault("throw").ToBoolOrDefault(true);

                Console.WriteLine($"Veryfying: '{input.FullName}' with Algorithm: '{algo.ToString()}' and Public Key: {key.FullName}.");
                var success = RSA.VerifyWithPemKey(input: input, signature: signature, pem: key, algorithm: algo);

                if (success)
                {
                    Console.WriteLine("Veryfication Suceeded.");
                }
                else
                {
                    Console.WriteLine("Veryfication Failed.");

                    if (@throw)
                    {
                        throw new Exception($"Signature verification failed, input: '{input?.FullName}'");
                    }
                }
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", $"Rivest–Shamir–Adleman Algoritm, with following allowed algorithms: '{EnumEx.ToStringArray<RSA.Algorithm>().JsonSerialize()}'.",
                            ("create-key", "Accepts params: output, size"),
                            ("sign", "Accepts params: key, input, output, algorithm"),
                            ("verify", "Accepts params: key, input, signature, algorithm, throw (optional, default: true)"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown RSA command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #15
0
        private static void executeDocker(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "gen":
            {
                var tmpOutp = nArgs.GetValueOrDefault("output");
                if (tmpOutp.IsNullOrWhitespace() || tmpOutp == "./" || tmpOutp == "\\" || tmpOutp == "/")
                {
                    tmpOutp = Environment.CurrentDirectory;
                    Console.WriteLine($"Output is set to be the current console directory: {tmpOutp}");
                }

                var output = tmpOutp.ToDirectoryInfo();
                Console.WriteLine($"Directory output is set to: {output.FullName}");

                var configInfo = nArgs["config"].ToFileInfo();

                if (!configInfo.Exists)
                {
                    throw new Exception($"Can't find config file: '{configInfo.FullName}'.");
                }
                else
                {
                    Console.WriteLine($"Config file set to: {configInfo.FullName}");
                }

                var config = configInfo.ReadAllText().JsonDeserialize <DockergenConfig>();
                config.workingDirectory = config.workingDirectory.CoalesceNullOrWhitespace($"/{Guid.NewGuid()}");
                config.relativePath     = config.relativePath.CoalesceNullOrWhitespace("/");

                if (config.version != 1)
                {
                    throw new Exception($"Incompatible config version, was: {config.version}, but expected 1");
                }

                var imageName   = nArgs["image-name"];
                var force       = nArgs.GetValueOrDefault("force", "false").ToBoolOrDefault() || config.force;
                var exposedPort = nArgs.GetValueOrDefault("exposed-port", "80").ToIntOrDefault(80);

                if (force)
                {
                    output.Create();
                }

                if (!output.Exists)
                {
                    throw new Exception($"Directory not found: '{output.FullName}'.");
                }
                else
                {
                    Console.WriteLine($"Output directory: {output.FullName}");
                }

                var nginxConfigInfo = PathEx.RuntimeCombine(
                    output.FullName,
                    config.relativePath,
                    "nginx.conf").ToFileInfo();

                if (force)
                {
                    nginxConfigInfo.TryDelete();
                }
                else
                {
                    Console.WriteLine($"Nginx config directory: {nginxConfigInfo.FullName}");
                }

                var useEnginx = config.port != exposedPort && !nginxConfigInfo.Exists;
                if (useEnginx)
                {
                    Console.WriteLine($"Generating nginx configuration in: '{nginxConfigInfo.FullName}'");

                    var nginxConfig = NginxConfigGenerator.GetSinglePortProxy(
                        sourcePort: exposedPort, destinationPort: config.port);

                    nginxConfigInfo.WriteAllText(nginxConfig);
                    Console.WriteLine($"Nginx Config: \n'{nginxConfig}'");
                }
                else
                {
                    Console.WriteLine($"Nginx configuration will not be set because defined application port ({config.port}) is the same as exposed port ({exposedPort}) and request proxy is not needed or nginx config is already defined ({nginxConfigInfo.Exists}).");
                }

                var dockerComposeInfo = PathEx.RuntimeCombine(
                    output.FullName,
                    config.relativePath,
                    "docker-compose.app.yml").ToFileInfo();

                if (force)
                {
                    dockerComposeInfo.TryDelete();
                }

                if (!dockerComposeInfo.Exists)
                {
                    Console.WriteLine($"Generating docker compose file in: '{dockerComposeInfo.FullName}'");

                    var dockerCompose = DockerfileConfigGenerator.GetDockerCompose(
                        imageName: imageName,
                        port: exposedPort,
                        exposedPorts: config.exposedPorts,
                        portsMap: config.portsMap);

                    dockerComposeInfo.WriteAllText(dockerCompose);
                    Console.WriteLine($"Docker Compose: \n'{dockerCompose}'");
                }
                else
                {
                    Console.WriteLine($"Docker Compose file was not creaed because it already existed.");
                }


                var envFileInfo = PathEx.RuntimeCombine(
                    output.FullName,
                    config.relativePath,
                    ".env").ToFileInfo();

                if (force)
                {
                    envFileInfo.TryDelete();
                }

                if (!envFileInfo.Exists)
                {
                    Console.WriteLine($"Generating Env file: '{envFileInfo.FullName}'");

                    if (!envFileInfo.Exists)
                    {
                        envFileInfo.Create().Close();
                    }

                    if (!config.env.IsNullOrEmpty())
                    {
                        var text = envFileInfo.ReadAllText() + "\r\n";

                        foreach (var line in config.env)
                        {
                            text += line.Trim() + "\r\n";
                        }

                        text = text.Trim("\r\n");

                        envFileInfo.WriteAllText(text, System.IO.Compression.CompressionLevel.NoCompression, mode: FileMode.Create);
                    }

                    Console.WriteLine($"Env file: \n'{envFileInfo.ReadAllText()}'");
                }
                else
                {
                    Console.WriteLine($"Env file was not creaed because it already existed.");
                }


                var dockerfileInfo = PathEx.RuntimeCombine(
                    output.FullName,
                    config.relativePath,
                    "Dockerfile").ToFileInfo();

                if (force)
                {
                    dockerfileInfo.TryDelete();
                }

                if (!dockerfileInfo.Exists)
                {
                    Console.WriteLine($"Generating dockerfile in: '{dockerfileInfo.FullName}'");

                    var dockerfile = DockerfileConfigGenerator.GetDockerfile(
                        baseImage: config.baseImage,
                        port: config.port,
                        exposedPorts: config.exposedPorts,
                        workingDirectory: config.workingDirectory,
                        buildpackId: config.buildpackId,
                        customBuildCommand: config.customBuildCommand,
                        customStartCommand: config.customStartCommand,
                        enginx: useEnginx,
                        env: config.env);

                    dockerfileInfo.WriteAllText(dockerfile);
                    Console.WriteLine($"Dockerfile: \n'{dockerfile}'");
                }
                else
                {
                    Console.WriteLine($"Docker Compose file was not creaed because it already existed.");
                }

                Console.WriteLine("SUCCESS, dockergen finished running.");
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Docker Helper",
                            ("gen", "Accepts params: config, output, image-name, force (optional/false), exposed-port (optional/80)"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown docker command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #16
0
        private static void executeCopy(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "local":
            {
                var config = nArgs["config"].ToFileInfo();

                Console.WriteLine($"Local Copy Execution according to '{config.FullName}'.");

                var configs = config.DeserialiseJson <CopyConfig[]>();

                foreach (var cfg in configs)
                {
                    string src, dst;

                    if (!RuntimeEx.IsWindows())
                    {
                        Console.WriteLine($"Detected OS is NOT Windows, paths containing '\\' and ':' will be replaced with '/'");
                        src = cfg.Source.ToLinuxPath();
                        dst = cfg.Destination.ToLinuxPath();
                    }
                    else
                    {
                        src = cfg.Source;
                        dst = cfg.Destination;
                    }

                    if (src.IsFile())
                    {
                        Console.WriteLine($"Detected, that Source is a FILE");

                        var srcInfo = src.ToFileInfo();
                        var dstInfo = dst.ToFileInfo();

                        if (!srcInfo.Exists)
                        {
                            throw new Exception($"Source file: '{srcInfo.FullName}' does not exit.");
                        }

                        if (!dstInfo.Directory.Exists)
                        {
                            Console.WriteLine($"Destination directory '{dstInfo.Directory.FullName}' does not exist, creating...");
                            dstInfo.Directory.Create();
                        }

                        Console.WriteLine($"Copying Files '{srcInfo.FullName}' => '{dstInfo.FullName}' (Override: {cfg.Override}).");
                        srcInfo.Copy(dstInfo, cfg.Override);
                    }
                    else if (src.IsDirectory())
                    {
                        Console.WriteLine($"Detected, that Source is a DIRECTORY");

                        var srcInfo = src.ToDirectoryInfo();
                        var dstInfo = dst.ToDirectoryInfo();

                        if (!srcInfo.Exists)
                        {
                            throw new Exception($"Source directory: '{srcInfo.FullName}' does not exit.");
                        }

                        if (!dstInfo.Exists)
                        {
                            Console.WriteLine($"Destination directory '{dstInfo.FullName}' does not exist, creating...");
                            dstInfo.Create();
                        }

                        if (!dst.IsDirectory())
                        {
                            throw new Exception("If source is a directory then destination also must be a directory.");
                        }

                        Console.WriteLine($"Recursive Copy: '{cfg.Recursive}'");
                        foreach (string newPath in Directory.GetFiles(srcInfo.FullName, "*.*", cfg.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
                        {
                            Console.WriteLine($"Copying Files '{srcInfo.FullName}' => '{dstInfo.FullName}' (Override: {cfg.Override}).");
                            File.Copy(newPath, newPath.Replace(srcInfo.FullName, dstInfo.FullName), cfg.Override);
                        }
                    }
                    else
                    {
                        throw new Exception($"Source '{src}' is neither a file or a directory.");
                    }
                }
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Copy",
                            ("local", "Accepts params: config, override"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown Copy command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #17
0
        private static void executeSSH(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "execute-shell":
            {
                string host;
                var    ec2InstanceName = nArgs.GetValueOrDefault("ec2-instance-name");
                if (!ec2InstanceName.IsNullOrEmpty())
                {
                    var ec2Helper = new EC2Helper();
                    var instance  = ec2Helper.ListInstancesByName(name: ec2InstanceName).Result
                                    .SingleOrDefault(x => (x.State.Name != Amazon.EC2.InstanceStateName.Terminated) && (x.State.Name != Amazon.EC2.InstanceStateName.ShuttingDown));

                    host = instance.PublicIpAddress;
                }
                else
                {
                    host = nArgs["host"];
                }

                var user = nArgs["user"];
                var key  = nArgs["key"].ToFileInfo();


                var commands = nArgs.ContainsKey("cmd[]") ?
                               nArgs["cmd[]"].JsonDeserialize <string[]>() :
                               new string[] { nArgs["cmd"] };

                var ssh = new SSHManaged(host, user, key);

                if (commands.IsNullOrEmpty())
                {
                    throw new Exception("Command/s parameter/s 'cmd'/'cmd-<nr.>' was/were not specified, coudn't execute.");
                }

                var maxConnectionRetry      = nArgs.GetValueOrDefault("max-reconnections").ToIntOrDefault(24);
                var maxConnectionRetryDelay = nArgs.GetValueOrDefault("max-reconnection-delay").ToIntOrDefault(2500);
                Console.WriteLine($"Connecting to '{host}'... (Max Retries: {maxConnectionRetry}, Max Retry Delay: {maxConnectionRetryDelay})");
                ssh.Connect(maxConnectionRetry: maxConnectionRetry,
                            maxConnectionRetryDelay: maxConnectionRetryDelay);

                var checkDelay = nArgs.GetValueOrDefault("check-delay").ToIntOrDefault(500);
                Console.WriteLine($"Sucessfully connected, executing {commands.Length} command/s...");
                var results = ssh.ExecuteShellCommands(commands,
                                                       commandTimeout_ms: nArgs.GetValueOrDefault("command-timeout").ToIntOrDefault(60 * 1000),
                                                       outputStream: Console.OpenStandardOutput(),
                                                       checkDelay: checkDelay);

                Console.WriteLine($"SUCCESS");
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Secure Shell",
                            ("execute-shell", "Accepts params: host, user, key, cmd"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown AES command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #18
0
        private static async Task executeEmail(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "send":
            {
                var           to          = nArgs["to"];
                var           body        = nArgs.GetValueOrDefault("body", @default: "");
                var           subject     = nArgs.GetValueOrDefault("subject", @default: "");
                var           attachments = nArgs.GetValueOrDefault("attachments")?.Split(",");
                var           secret      = nArgs.GetValueOrDefault("aws-secret");
                var           envSecret   = nArgs.GetValueOrDefault("env-secret", @default: "SMTP_SECRET");
                var           html        = nArgs.GetValueOrDefault("html").ToBoolOrDefault(false);
                var           recursive   = nArgs.GetValueOrDefault("recursive").ToBoolOrDefault(false);
                var           throwIfAttachementNotFound = nArgs.GetValueOrDefault("throwIfAttachementNotFound").ToBoolOrDefault(false);
                var           throwIfAttachementTooBig   = nArgs.GetValueOrDefault("throwIfAttachementTooBig").ToBoolOrDefault(false);
                var           attachementMaxSize         = nArgs.GetValueOrDefault("throwIfAttachementTooBig").ToIntOrDefault(25 * 1024 * 1024);
                SmtpMailSetup smtpSecret = null;

                if (secret.IsNullOrEmpty())
                {
                    secret = envSecret.ContainsAll("{", "}", "host", "port") ? envSecret : Environment.GetEnvironmentVariable(envSecret);
                    if (!secret.IsNullOrEmpty())
                    {
                        smtpSecret = secret.JsonDeserialize <SmtpMailSetup>();
                    }
                }
                else
                {
                    var sm  = new SMHelper();
                    var sgs = await sm.GetSecret(name : secret);

                    smtpSecret = sgs.JsonDeserialize <SmtpMailSetup>();
                }

                if (smtpSecret == null)
                {
                    throw new Exception("SMTP secret (aws-secret or env-secret) was NOT specified");
                }

                var from = nArgs.GetValueOrDefault("from", @default: null) ?? smtpSecret.login;

                if (!from.IsValidEmailAddress())
                {
                    throw new Exception($"From property ({from ?? "null"}) is not a valid email addess");
                }

                var smtpm = new SmtpMail(smtpSecret);

                if (!body.IsNullOrEmpty())
                {
                    var file = PathEx.ToRuntimePath(body);
                    if (File.Exists(file))
                    {
                        var fileBody = File.ReadAllText(file);
                        if (!fileBody.IsNullOrEmpty())
                        {
                            body = fileBody;
                        }
                    }
                }

                await smtpm.Send(
                    from,
                    to,
                    body,
                    subject,
                    isBodyHTML : html,
                    attachments : attachments,
                    recursive : recursive,
                    throwIfAttachementNotFound : throwIfAttachementNotFound,
                    throwIfAttachementTooBig : throwIfAttachementTooBig,
                    attachementMaxSize : attachementMaxSize);

                WriteLine($"SUCCESS, Email was send to {to ?? "undefined"}. ");
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Emailing",
                            ("sent", "Accepts params: aws-secret, from, to, subject, body, html (default: false), recursive (default: false), throwIfAttachementNotFound (default: false), throwIfAttachementTooBig (default: false), attachementMaxSize (default: 26214400)"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown Email command: '{args[0]} {args[1]}'");
            }
            }
        }
        private static void ExecuteBatchMode(CommandLineArgs args, BenchmarkRunner runner)
        {
            try
            {
                if (args.ShowHelp)
                {
                    HelpPrinter.Print();
                    return;
                }

                // Load assemblies
                foreach (string assemblyFile in args.Assemblies)
                {
                    runner.Benchmarks.AddSuitesFromAssembly(assemblyFile);
                }

                // Load configuration
                if (args.ConfigurationFile != null)
                {
                    runner.Parameters.LoadFromFile(args.ConfigurationFile);
                }

                // Set parameters
                if (args.Parameters.Count > 0)
                {
                    runner.Parameters.Set(args.Parameters);
                }

                if (args.ShowBenchmarks)
                {
                    PrintBenchmarks(runner);
                    return;
                }

                if (args.ShowParameters)
                {
                    PrintParameters(runner);
                    return;
                }

                // Benchmark the environment
                if (args.BenchmarkEnvironment)
                {
                    System.Console.Out.WriteLine("Benchmarking Environment (wait up to 2 mins)...");
                    runner.Environment.Measure(true, true, true);
                    System.Console.Out.WriteLine("CPU: {0}, Video: {1}, Disk: {2}",
                                                 runner.Environment.CpuMeasurement.ToString("0.##"),
                                                 runner.Environment.VideoMeasurement.ToString("0.##"),
                                                 runner.Environment.DiskMeasurement.ToString("0.##")
                                                 );
                }

                // Configure benchmarking
                runner.Configuration.ForceContinue   = true;//args.IsForceContinue;
                runner.Configuration.MeasurementType = args.MeasurementType;
                runner.Configuration.NominalRate     = args.NominalRate;
                runner.Configuration.ExecutionType   = args.ExecutionType;
                runner.Configuration.NumberOfThreads = 1;
                runner.Configuration.Duration        = 600000;

                // Enable benchmarks
                if (args.Benchmarks.Count == 0)
                {
                    runner.Benchmarks.SelectAll();
                }
                else
                {
                    foreach (var benchmark in args.Benchmarks)
                    {
                        runner.Benchmarks.SelectByName(new string[] { benchmark });
                    }
                }

                // Perform benchmarking
                runner.Start();

                if (runner.Configuration.ExecutionType == ExecutionType.Proportional)
                {
                    Thread.Sleep(runner.Configuration.Duration);
                    runner.Stop();
                }

                if (runner.Results.All.Count > 0)
                {
                    System.Console.Out.WriteLine("Average Perfomance: {0}", runner.Results.All[0].PerformanceMeasurement.AverageValue);
                    System.Console.Out.WriteLine("Average Memory Usage: {0}", runner.Results.All[0].MemoryUsageMeasurement.AverageValue);
                }

                // Generate report
                if (args.ReportFile != null)
                {
                    using (FileStream stream = File.OpenWrite(args.ReportFile))
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                            writer.Write(runner.Report.Generate());
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.Out.WriteLine("Error: {0}", ex.Message);
            }
        }
Beispiel #20
0
        private static void executeText(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "vereq":     // this function allows to check if software versions are equal
            {
                var old  = nArgs["old"].RemoveNonDigits(exceptions: new char[] { '.' });
                var @new = nArgs["new"].RemoveNonDigits(exceptions: new char[] { '.' });

                int[] Split(string s)
                {
                    var  values          = new List <int>();
                    var  splits          = s.Split('.');
                    bool firstDigitFound = false;

                    foreach (var split in splits)
                    {
                        var i = split.ToIntOrDefault(-1);
                        if (firstDigitFound && i <= 0)
                        {
                            continue;
                        }

                        if (!firstDigitFound && i > 0)
                        {
                            firstDigitFound = true;
                        }

                        values.Add(i);
                    }
                    return(values.ToArray());
                }

                var ovar = Split(old);
                var nvar = Split(@new);

                WriteLine($"INFO: Comparing 'v{ovar.StringJoin(".")}' and 'v{nvar.StringJoin(".")}' ...");

                if (ovar.Length <= 0 && ovar.Length <= 0)
                {
                    throw new Exception($"Both '{old}' and '{@new}' are NOT valid versions");
                }

                for (int i = 0; i < Math.Max(ovar.Length, nvar.Length); i++)
                {
                    var o = ovar.Length > i ? ovar[i] : 0;
                    var n = nvar.Length > i ? nvar[i] : 0;

                    if (o == n)
                    {
                        continue;
                    }

                    Console.WriteLine(o > n ? "1" : "-1");
                    return;
                }

                Console.WriteLine("0");
            }
                ; break;

            case "lineswap":
            {
                var insert            = nArgs["insert"];
                var appendIfFoundNot  = nArgs.GetValueOrDefault("append-if-found-not").ToBoolOrDefault(false);
                var prependIfFoundNot = nArgs.GetValueOrDefault("prepend-if-found-not").ToBoolOrDefault(false);
                var dryRun            = nArgs.GetValueOrDefault("dry-run").ToBoolOrDefault(false);

                var prefix   = nArgs.GetValueOrDefault("prefix", "");
                var suffix   = nArgs.GetValueOrDefault("suffix", "");
                var regex    = nArgs.GetValueOrDefault("regex", "");
                var contains = nArgs.GetValueOrDefault("contains", "");

                // mach only after given regex sequence is found
                var after = nArgs.GetValueOrDefault("after-regex", "");
                // mach only before given regex sequence is found
                var before = nArgs.GetValueOrDefault("before-regex", "");

                var prefixNot   = nArgs.GetValueOrDefault("prefix-not", "");
                var suffixNot   = nArgs.GetValueOrDefault("suffix-not", "");
                var regexNot    = nArgs.GetValueOrDefault("regex-not", "");
                var containsNot = nArgs.GetValueOrDefault("contains-not", "");

                var andPrefix   = nArgs.GetValueOrDefault("and-prefix", "");
                var andSuffix   = nArgs.GetValueOrDefault("and-suffix", "");
                var andRegex    = nArgs.GetValueOrDefault("and-regex", "");
                var andContains = nArgs.GetValueOrDefault("and-contains", "");

                var andPrefixNot   = nArgs.GetValueOrDefault("and-prefix-not", "");
                var andSuffixNot   = nArgs.GetValueOrDefault("and-suffix-not", "");
                var andRegexNot    = nArgs.GetValueOrDefault("and-regex-not", "");
                var andContainsNot = nArgs.GetValueOrDefault("and-contains-not", "");

                var path  = nArgs["path"];
                var files = FileHelper.GetFiles(path,
                                                pattern: nArgs.GetValueOrDefault("path-pattern") ?? "*",
                                                recursive: nArgs.GetValueOrDefault("path-recursive").ToBoolOrDefault(false),
                                                split: ',');

                var rx       = regex.IsNullOrEmpty() ? null : new Regex(regex);
                var rxNot    = regexNot.IsNullOrEmpty() ? null : new Regex(regexNot);
                var rxAnd    = andRegex.IsNullOrEmpty() ? null : new Regex(andRegex);
                var rxAndNot = andRegexNot.IsNullOrEmpty() ? null : new Regex(andRegexNot);

                var rxAfter  = after.IsNullOrEmpty() ? null : new Regex(after);
                var rxBefore = before.IsNullOrEmpty() ? null : new Regex(before);

                if (!after.IsNullOrEmpty())
                {
                    WriteLine($"Matching AFTER regex: '{after}'");
                }
                if (!before.IsNullOrEmpty())
                {
                    WriteLine($"Matching BEFORE regex: '{before}'");
                }

                if (!regex.IsNullOrEmpty())
                {
                    WriteLine($"Matching OR regex: '{regex}'");
                }
                if (!prefix.IsNullOrEmpty())
                {
                    WriteLine($"Matching OR prefix: '{prefix}'");
                }
                if (!suffix.IsNullOrEmpty())
                {
                    WriteLine($"Matching OR suffix: '{suffix}'");
                }
                if (!contains.IsNullOrEmpty())
                {
                    WriteLine($"Matching OR contains: '{contains}'");
                }

                if (!regexNot.IsNullOrEmpty())
                {
                    WriteLine($"Matching OR NOT regex: '{regexNot}'");
                }
                if (!prefixNot.IsNullOrEmpty())
                {
                    WriteLine($"Matching OR NOT prefix: '{prefixNot}'");
                }
                if (!suffixNot.IsNullOrEmpty())
                {
                    WriteLine($"Matching OR NOT suffix: '{suffixNot}'");
                }
                if (!containsNot.IsNullOrEmpty())
                {
                    WriteLine($"Matching OR NOT contains: '{containsNot}'");
                }

                if (!andPrefix.IsNullOrEmpty())
                {
                    WriteLine($"Matching AND prefix: '{andPrefix}'");
                }
                if (!andSuffix.IsNullOrEmpty())
                {
                    WriteLine($"Matching AND suffix: '{andSuffix}'");
                }
                if (!andRegex.IsNullOrEmpty())
                {
                    WriteLine($"Matching AND regex: '{andRegex}'");
                }
                if (!andContains.IsNullOrEmpty())
                {
                    WriteLine($"Matching AND contains: '{andContains}'");
                }

                if (!andPrefixNot.IsNullOrEmpty())
                {
                    WriteLine($"Matching AND NOT prefix: '{andPrefixNot}'");
                }
                if (!andSuffixNot.IsNullOrEmpty())
                {
                    WriteLine($"Matching AND NOT suffix: '{andSuffixNot}'");
                }
                if (!andRegexNot.IsNullOrEmpty())
                {
                    WriteLine($"Matching AND NOT regex: '{andRegexNot}'");
                }
                if (!andContainsNot.IsNullOrEmpty())
                {
                    WriteLine($"Matching AND NOT conatins: '{andContainsNot}'");
                }

                if (appendIfFoundNot)
                {
                    WriteLine($"Appending if NOT matching");
                }
                if (prependIfFoundNot)
                {
                    WriteLine($"Prepending if NOT matching");
                }

                WriteLine($"Swapping matched lines with: '{insert}'");

                int count = 0;
                files.ParallelForEach(file =>
                    {
                        if (!file.Exists)
                        {
                            throw new Exception($"Input file does not exists '{file.FullName}'");
                        }

                        WriteLine($"Reading Text File '{file.FullName}' ...");
                        var lines = new List <string>();
                        int cntr  = 0;
                        int nr    = 0;
                        using (var s = new StreamReader(file.FullName))
                        {
                            string line;
                            bool match;
                            bool afterMatch  = false;
                            bool beforeMatch = false;
                            while ((line = s.ReadLine()) != null)
                            {
                                ++nr;
                                if (!afterMatch && !after.IsNullOrEmpty())
                                {
                                    if (rxAfter.Match(line).Success)
                                    {
                                        afterMatch = true;     // after match found
                                    }
                                    lines.Add(line);
                                    continue;
                                }

                                if (!beforeMatch && !before.IsNullOrEmpty() && rxBefore.Match(line).Success)
                                {
                                    beforeMatch = true;         // before match found
                                }
                                if (beforeMatch)
                                {       // do not match, nothing left to process as before regex was hit
                                    lines.Add(line);
                                    continue;
                                }

                                match = !prefix.IsNullOrEmpty() && line.StartsWith(prefix);
                                match = match || (!suffix.IsNullOrEmpty() && line.EndsWith(suffix));
                                match = match || (!regex.IsNullOrEmpty() && rx.Match(line).Success);
                                match = match || (!contains.IsNullOrEmpty() && line.Contains(contains));

                                match = match || (!prefixNot.IsNullOrEmpty() && !line.StartsWith(prefixNot));
                                match = match || (!suffixNot.IsNullOrEmpty() && !line.EndsWith(suffixNot));
                                match = match || (!regexNot.IsNullOrEmpty() && !rxNot.Match(line).Success);
                                match = match || (!containsNot.IsNullOrEmpty() && !line.Contains(containsNot));

                                if (match)
                                {
                                    match = match && !andPrefix.IsNullOrEmpty() ? line.StartsWith(andPrefix) : match;
                                    match = match && !andSuffix.IsNullOrEmpty() ? line.EndsWith(andSuffix) : match;
                                    match = match && !andRegex.IsNullOrEmpty() ? rxAnd.Match(line).Success : match;
                                    match = match && !andContains.IsNullOrEmpty() ? line.Contains(andContains) : match;

                                    match = match && !andPrefixNot.IsNullOrEmpty() ? !line.StartsWith(andPrefixNot) : match;
                                    match = match && !andSuffixNot.IsNullOrEmpty() ? !line.EndsWith(andSuffixNot) : match;
                                    match = match && !andRegexNot.IsNullOrEmpty() ? !rxAndNot.Match(line).Success : match;
                                    match = match && !andContainsNot.IsNullOrEmpty() ? !line.Contains(andContainsNot) : match;
                                }

                                if (match)
                                {
                                    ++cntr;
                                    WriteLine($"Match {cntr} found for line {nr}: '{line}' in '{file.FullName}'");
                                    lines.Add(insert);
                                }
                                else
                                {
                                    lines.Add(line);
                                }
                            }
                            s.Close();
                        }

                        appendIfFoundNot  = cntr <= 0 && appendIfFoundNot;
                        prependIfFoundNot = cntr <= 0 && prependIfFoundNot;

                        if (cntr > 0 || appendIfFoundNot || prependIfFoundNot)
                        {
                            WriteLine($"Writing changes to '{file.FullName}' ...");
                            if (!_dryRun)     // used for command testing
                            {
                                using (var s = new StreamWriter(file.FullName, false))
                                {
                                    if (prependIfFoundNot)
                                    {
                                        s.WriteLine(insert);
                                    }
                                    foreach (var line in lines)
                                    {
                                        s.WriteLine(line);
                                    }
                                    if (appendIfFoundNot)
                                    {
                                        s.WriteLine(insert);
                                    }
                                    s.Close();
                                }
                            }

                            Interlocked.Increment(ref count);
                        }
                    });

                if (count > 0)
                {
                    WriteLine($"SUCCESS: Replaced or Added text in {count} file/s. ");
                }
                else
                {
                    WriteLine($"WAFNING: No lines were matched in any of the files specified by path {path}");
                }
            }
                ; break;

            case "replace":
            {
                var @new  = nArgs["new"];
                var old   = nArgs["old"];
                var files = FileHelper.GetFiles(nArgs["input"],
                                                pattern: nArgs.GetValueOrDefault("pattern") ?? "*",
                                                recursive: nArgs.GetValueOrDefault("recursive").ToBoolOrDefault(false));

                int count = 0;
                files.ParallelForEach(file =>
                    {
                        if (!file.Exists)
                        {
                            throw new Exception($"Input file does not exists '{file.FullName}'");
                        }

                        WriteLine($"Reading Text File '{file.FullName}' ...");
                        var text = file.ReadAllText();

                        if (text?.Contains(old) == true)
                        {
                            WriteLine($"Replacing text '{old}' with '{@new}' in file '{file.FullName}' ...");
                            text = text.Replace(old, @new);
                            file.WriteAllText(text);
                            Interlocked.Increment(ref count);
                            WriteLine($"Success, Replaced text in file '{file.FullName}' [{file?.Length ?? 0}]");
                        }
                        else
                        {
                            WriteLine($"File '{file.FullName}' does NOT contain text '{old}', skipping replacement.");
                        }
                    });

                WriteLine($"SUCCESS, Replaced text in {count} files. ");
            }
                ; break;

            case "dos2unix":
            {
                var files = FileHelper.GetFiles(nArgs["input"],
                                                pattern: nArgs.GetValueOrDefault("pattern") ?? "*",
                                                recursive: nArgs.GetValueOrDefault("recursive").ToBoolOrDefault(false));

                files.ParallelForEach(file =>
                    {
                        WriteLine($"Converting '{file?.FullName}' [{file?.Length ?? 0}] from dos to unix format...");
                        file.ConvertDosToUnix();
                        file.Refresh();
                        WriteLine($"Success, Converted '{file?.FullName}' [{file?.Length ?? 0}]");
                    });

                WriteLine($"SUCCESS, Converted {files?.Length ?? 0} files to unix format. ");
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "String Manipulation",
                            ("lineswap", "Accepts: insert, path, (and-)prefix(-not), (and-)suffix(-not), (and-)regex(-not), (after-/before-)regex, (and-)contains(-not), (append-/prepend-)if-found-not (optiona: False)"),
                            ("replace", "Accepts: old, new, input"),
                            ("dos2unix", "Accepts: input"),
                            ("vereq", "Accepts: old, new | Returns: 0 if equal, -1 if old is smaller, 1 if old is greater"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown String command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #21
0
        private static void executeAES(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "create-key":
            {
                var path = nArgs["path"].ToFileInfo();

                if (path.Exists)
                {
                    throw new Exception($"Can't create key file: '{path.FullName}' because it already exists.");
                }

                path.WriteAllText(AES.CreateAesSecret().JsonSerialize());
            }
                ; break;

            case "encrypt":
            {
                var key       = FileHelper.DeserialiseJson <AesSecret>(nArgs["key"]);
                var path      = nArgs.GetValueOrDefault("path");
                var @override = nArgs.GetValueOrDefault("override").ToBoolOrDefault();

                void Encrypt(FileInfo src, FileInfo dst)
                {
                    if (!dst.Directory.Exists)
                    {
                        Console.WriteLine($"Creating directory '{dst.Directory.FullName}' for secret located at '{src.FullName}'.");
                        dst.Directory.Create();
                    }
                    else if (dst.Exists)
                    {
                        if (@override)
                        {
                            dst.Delete();
                        }
                        else
                        {
                            throw new Exception($"Could NOT encrypt file '{src.FullName}', because destination '{dst.FullName}' already exists and override flag is not set.");
                        }
                    }

                    Console.WriteLine($"Encrypting '{src.FullName}' => '{dst.FullName}'.");
                    src.EncryptAsync(key, dst).Await();
                }

                if (path != null)
                {
                    var src    = path.ToFileInfo();
                    var output = nArgs.GetValueOrDefault("output");
                    var dst    = (output.IsNullOrEmpty() ?
                                  (path + ".aes") :
                                  Path.Combine(output.ToDirectoryInfo().FullName, src.FullName + ".aes")).ToFileInfo();

                    Encrypt(src, dst);
                }
                else
                {
                    var config  = nArgs["config"].ToFileInfo();
                    var configs = config.DeserialiseJson <SecretConfig[]>();

                    foreach (var cfg in configs)
                    {
                        var src = cfg.Source.ToFileInfo();
                        var dst = cfg.Destination.ToFileInfo();
                        Encrypt(src, dst);
                    }
                }

                Console.WriteLine("SUCCESS");
            }
                ; break;

            case "decrypt":
            {
                var key  = FileHelper.DeserialiseJson <AesSecret>(nArgs["key"]);
                var path = nArgs["path"];

                var files = FileHelper.GetFiles(path, pattern: "*.aes", recursive: true);

                if (files.IsNullOrEmpty())
                {
                    Console.WriteLine($"No '.aes' files were found for path: '{path}'");
                }

                foreach (var f in files)
                {
                    var newFile = Path.Combine(f.Directory.FullName, f.NameWithoutExtension()).ToFileInfo();
                    if (newFile.Exists)
                    {
                        Console.WriteLine($"Decrypted file '{newFile}' was already present, removing.");
                        newFile.Delete();
                    }

                    Console.WriteLine($"Decrypting '{f.FullName}' => '{newFile.FullName}'");
                    f.DecryptAsync(key, newFile).Await();
                }
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "AES Encryption",
                            ("create-key", "Accepts params: path"),
                            ("encrypt", "Accepts params: key, config, path, override"),
                            ("decrypt", "Accepts params: key, path"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown AES command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #22
0
        private static async Task Execute(string[] args)
        {
            switch (args[0]?.ToLower().TrimStart("-"))
            {
            case "ssh":
                executeSSH(args);
                break;

            case "curl":
                executeCURL(args);
                break;

            case "hash":
                executeHash(args);
                break;

            case "aes":
                executeAES(args);
                break;

            case "rsa":
                executeRSA(args);
                break;

            case "bitbucket":
                executeBitbucket(args);
                break;

            case "github":
                executeGithub(args);
                break;

            case "email":
                await executeEmail(args);

                break;

            case "scheduler":
                await executeScheduler(args);

                break;

            case "console":
                executeConsole(args);
                break;

            case "copy":
                executeCopy(args);
                break;

            case "cli":
                executeCLI(args);
                break;

            case "text":
                executeText(args);
                break;

            case "time":
                executeTime(args);
                break;

            case "docker":
                executeDocker(args);
                break;

            case "version":
            case "ver":
            case "v":
                Console.WriteLine($"v{_version}");
                break;

            case "help":
            case "h":
                HelpPrinter($"{args[0]}", "CDHelper List of available commands",
                            ("ssh", "Accepts params: command"),
                            ("cli", "Accepts params: command"),
                            ("console", "Accepts params: prompt-read-line"),
                            ("copy", "Accepts params: local"),
                            ("curl", "Accepts params: GET, GET-FILE"),
                            ("docker", "Accepts params: gen"),
                            ("email", "Accepts params: send"),
                            ("github", "Accepts params: on-change-process"),
                            ("hash", "Accepts params: SHA256"),
                            ("scheduler", "Accepts params: github"),
                            ("text", "Accepts params: lineswap, replace, dos2unix"),
                            ("time", "Accepts params: add, unix2timestamp"),
                            ("AES", "Accepts params: create-key, encrypt, decrypt"),
                            ("RSA", "Accepts params: create-key, sign, verify"),
                            ("bitbucket", "Accepts params: pull-approve, pull-unapprove, pull-comment"),
                            ("[flags]", "Allowed Syntax: key=value, --key=value, -key='v1 v2 v3', -k, --key"),
                            ("--execution-mode=silent-errors", "[All commands] Don't throw errors, only displays exception message."),
                            ("--execution-mode=debug", "[All commands] Throw instantly without reporting a failure."),
                            ("--silent=<bool>", "[All commands] quiets console output"),
                            ("--dry-run=<bool>", "[All commands] comand testing"));
                break;

            default:
            {
                Console.WriteLine("Try 'help' to find out list of available commands.");
                throw new Exception($"Unknown command: '{args[0]}'.");
            }
            }
        }
Beispiel #23
0
        private static void executeTime(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "now":
            {
                Console.WriteLine(DateTimeEx.UnixTimestampNow());
            }
                ; break;

            case "add":
            {
                var time  = nArgs.GetValueOrDefault("unix").ToLongOrDefault(0);
                var unit  = nArgs.GetValueOrDefault("unit", "s").Trim();
                var value = nArgs.GetValueOrDefault("value").ToDoubleOrDefault(0);

                if (time <= 0)
                {
                    time = DateTimeEx.UnixTimestampNow();
                }

                var dt = time.ToDateTimeFromUnixTimestamp();

                var unitLow = unit.ToLower();
                if (unitLow.EquailsAny <string>("f", "ms", "milisecond", "milisec", "milisecs", "miliseconds"))
                {
                    dt = dt.AddMilliseconds(value);
                }
                else if (unitLow.EquailsAny <string>("s", "second", "sec", "secs", "seconds"))
                {
                    dt = dt.AddSeconds(value);
                }
                else if (unit == "m" || unitLow.EquailsAny <string>("min", "minut", "minute", "minutes"))
                {
                    dt = dt.AddMinutes(value);
                }
                else if (unitLow.EquailsAny <string>("h", "hour", "hours"))
                {
                    dt = dt.AddHours(value);
                }
                else if (unitLow.EquailsAny <string>("d", "day", "days"))
                {
                    dt = dt.AddDays(value);
                }
                else if (unit == "M" || unitLow.EquailsAny <string>("month", "months"))
                {
                    dt = dt.AddMonths((int)value);
                }
                else if (unitLow.EquailsAny <string>("y", "year", "years", "annum", "annums"))
                {
                    dt = dt.AddYears((int)value);
                }
                else if (unitLow.EquailsAny <string>("tick", "ticks"))
                {
                    dt = dt.AddTicks((long)value);
                }
                else
                {
                    throw new Exception($"Unknown unit '{unit ?? "undefined"}'");
                }

                Console.WriteLine(dt.ToUnixTimestamp());
            }
                ; break;

            case "unix2timestamp":
            {
                var time = nArgs.GetValueOrDefault("unix").ToLongOrDefault(0);
                Console.WriteLine(time.ToDateTimeFromUnixTimestamp().ToTimestamp());
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Date Time Manipulation",
                            ("now", "No Params, returns unix UTC timestamp"),
                            ("add", "Accepts params: unix, value, unit (f,s,m,h,d,w,M,y)"),
                            ("unix2timestamp", "Accepts params: unix"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown String command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #24
0
        private static void executeCURL(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "get":
            {
                var uri            = nArgs.FirstOrDefault(x => x.Key == "uri").Value.CoalesceNullOrWhitespace(args[2]);
                var intensity      = nArgs.FirstOrDefault(x => x.Key == "intensity").Value.ToIntOrDefault(1000);
                var requestTimeout = nArgs.FirstOrDefault(x => x.Key == "request-timeout").Value.ToIntOrDefault(5 * 1000);

                var timeout = Math.Max(
                    requestTimeout,
                    nArgs.FirstOrDefault(x => x.Key == "timeout").Value.ToIntOrDefault(15 * 1000));

                var showResponse = nArgs.GetValueOrDefault("show-response", "false").ToBoolOrDefault();
                var sw           = Stopwatch.StartNew();

                Console.WriteLine($"Executing Curl GET command... timeout will occur if execution takes longer then {timeout} [ms]");

                var response = CurlHelper.AwaitSuccessCurlGET(
                    uri: uri,
                    timeout: timeout,
                    intensity: intensity,
                    requestTimeout: requestTimeout).Result;

                Console.WriteLine($"Curl GET commend executed sucessfully, elapsed {sw.ElapsedMilliseconds} [ms]");

                if (showResponse)
                {
                    Console.WriteLine("Response: " + (response?.JsonSerialize(Newtonsoft.Json.Formatting.Indented) ?? "null"));
                }
            }
                ; break;

            case "get-file":
            {
                var uri            = nArgs.FirstOrDefault(x => x.Key == "uri").Value.CoalesceNullOrWhitespace(args[2]);
                var intensity      = nArgs.FirstOrDefault(x => x.Key == "intensity").Value.ToIntOrDefault(1000);
                var requestTimeout = nArgs.FirstOrDefault(x => x.Key == "request-timeout").Value.ToIntOrDefault(5 * 1000);
                var output         = nArgs["output"].ToFileInfo();
                var @override      = nArgs["override"].ToBool();
                var basicAuth      = nArgs.FirstOrDefault(x => x.Key == "basic-auth").Value;

                if (!output.Directory.Exists)
                {
                    throw new Exception($"Output directory was NOT found: {output.Directory}");
                }

                if (!@override && output.Exists)
                {
                    Console.WriteLine($"File will not be downloaded because it already exists at: {output.FullName}");
                    return;
                }

                var timeout = Math.Max(
                    requestTimeout,
                    nArgs.FirstOrDefault(x => x.Key == "timeout").Value.ToIntOrDefault(15 * 1000));

                var sw = Stopwatch.StartNew();

                List <(string, string)> headers         = new List <(string, string)>();
                BasicAuthSecret         basicAuthSecret = null;
                if (!basicAuth.IsNullOrEmpty() && File.Exists(basicAuth) && basicAuth.ToFileInfo().Extension == ".json")
                {
                    Console.WriteLine($"Basic Auth file was found at: {basicAuth}");
                    var secret = FileHelper.ReadAllAsString(basicAuth.ToFileInfo().FullName);
                    basicAuthSecret = secret.JsonDeserialize <BasicAuthSecret>();

                    if (basicAuthSecret.login == null || basicAuthSecret.password == null)
                    {
                        throw new Exception("Login or password was not defined within BasicAuthSecret json config file.");
                    }

                    headers.Add(("Authorization", $"Bearer {($"{basicAuthSecret.login}:{basicAuthSecret.password}").Base64Encode()}"));
                }
                else if (!basicAuth.IsNullOrEmpty())
                {
                    throw new NotSupportedException("Basic Auth Config file was not found, and no other authorization methods are supported.");
                }

                Console.WriteLine($"Executing Curl GET command... timeout will occur if execution takes longer then {timeout} [ms]");


                var response = CurlHelper.AwaitSuccessCurlGET(
                    uri: uri,
                    timeout: timeout,
                    intensity: intensity,
                    requestTimeout: requestTimeout,
                    headers: headers.ToArray()).Result;

                var fileContent = response.Content.ReadAsByteArrayAsync().Result;

                Console.WriteLine($"Curl GET commend executed sucessfully, elapsed {sw.ElapsedMilliseconds} [ms], read: {fileContent.Length} bytes, writing result to: '{output.FullName}'.");

                if (!output.Exists)
                {
                    var stream = output.Create();
                    stream.Write(fileContent, 0, fileContent.Length);
                    stream.Close();
                }
                else
                {
                    output.WriteAllBytes(fileContent);
                }

                Console.WriteLine($"Success, fileContent was saved.");
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Curl Like Web Requests",
                            ("GET", "Accepts params: uri, timeout (optional [ms], 15s default), intensity (default 1000 [ms]), request-timeout (optional [ms], 5s default)"),
                            ("GET-FILE", "Accepts params: uri, timeout (15s), intensity (1s), request-timeout (5s), output, override, basic-auth (.json file with login & password props)")
                            );
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown ECS command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #25
0
        private static void executeFargate(string[] args, Credentials credentials)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            var elb = new ELBHelper();
            var r53 = new Route53Helper();
            var ecs = new ECSHelper();
            var cw  = new CloudWatchHelper();
            var kms = new KMSHelper(credentials);
            var iam = new IAMHelper(credentials);
            var acm = new ACMHelper();

            switch (args[1])
            {
            case "create-resources":
            {
                bool catchDisable          = nArgs.GetValueOrDefault("catch-disable", "false").ToBool();
                int  resourceCreateTimeout = nArgs["resource-create-timeout"].ToInt32();

                var resource = new FargateResourceV2(nArgs);

                string prefix_new = "a-";
                string prefix_old = "b-";
                bool   setRoutes  = true;

                Console.WriteLine("Determining Temporary Resource Naming Conventions...");
                var record = r53.GetCNameRecordSet(resource.IsPublic ? resource.ZonePublic : resource.ZonePrivate, resource.DNSCName,
                                                   failover: "PRIMARY",
                                                   throwIfNotFound: false).Result;

                if (record?.ResourceRecords.IsNullOrEmpty() == false)
                {
                    var a_alb = elb.GetLoadBalancersByName(loadBalancerName: $"a-{resource.LoadBalancerName}", throwIfNotFound: false).Result.SingleOrDefault();
                    var b_alb = elb.GetLoadBalancersByName(loadBalancerName: $"b-{resource.LoadBalancerName}", throwIfNotFound: false).Result.SingleOrDefault();

                    if (a_alb != null && record.ResourceRecords.Any(r => r.Value == a_alb.DNSName))
                    {
                        prefix_new = "b-";
                        prefix_old = "a-";
                        setRoutes  = false;
                    }
                    else if (b_alb != null && record.ResourceRecords.Any(r => r.Value == b_alb.DNSName))
                    {
                        prefix_new = "a-";
                        prefix_old = "b-";
                        setRoutes  = false;
                    }
                    else
                    {
                        Console.WriteLine("WARNING!!! Record was present, but could NOT find any associated loadbalancers.");
                    }
                }

                var resourceNew = resource.DeepCopy();
                resourceNew.SetName($"{prefix_new}{resource.Name}");
                resourceNew.SetDNSCName($"{prefix_new}{resource.DNSCName}");

                var resourceOld = resource.DeepCopy();
                resourceOld.SetName($"{prefix_old}{resource.Name}");
                resourceOld.SetDNSCName($"{prefix_old}{resource.DNSCName}");

                Console.WriteLine("Destroying Temporary Resources...");
                FargateResourceHelperV2.Destroy(resourceNew, elb, r53, ecs, cw, kms, iam, throwOnFailure: true, catchDisable: catchDisable).Await();

                try
                {
                    Console.WriteLine("Creating New Resources...");
                    FargateResourceHelperV2.Create(resourceNew, elb, r53, ecs, cw, kms, iam, acm);

                    Console.WriteLine($"Awaiting up to {resourceCreateTimeout} [s] for Tasks Desired Status...");
                    ecs.WaitForServiceToStart(resourceNew.ClusterName, resourceNew.ServiceName, resourceCreateTimeout).Await();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Failed New Resource Deployment with exception: {ex.JsonSerializeAsPrettyException(Formatting.Indented)}");

                    Console.WriteLine("Destroying New Resources...");
                    FargateResourceHelperV2.Destroy(resourceNew, elb, r53, ecs, cw, kms, iam, throwOnFailure: true, catchDisable: catchDisable).Await();

                    throw new Exception("New Resource Deployment Failure", ex);
                }

                if (setRoutes ||
                    record?.HealthCheckId == null ||
                    record.HealthCheckId != r53.GetHealthCheckAsync(resource.HealthCheckName, throwIfNotFound: false).Result?.Id)
                {
                    Console.WriteLine("DNS Route Initialization...");
                    FargateResourceHelperV2.SetRoutes(resource, resourceNew, elb, r53, cw);
                }
                else
                {
                    Console.WriteLine("DNS Route Swap...");
                    FargateResourceHelperV2.SwapRoutes(resource, resourceNew, elb, r53, cw);
                }

                Console.WriteLine("Destroying Old Resources...");
                FargateResourceHelperV2.Destroy(resourceOld, elb, r53, ecs, cw, kms, iam, throwOnFailure: true, catchDisable: catchDisable).Await();
            }
                ; break;

            case "destroy-resources":
            {
                bool catchDisable = nArgs.GetValueOrDefault("catch-disable", "false").ToBool();

                var resource = new FargateResourceV2(nArgs);

                var resourceA = resource.DeepCopy();
                resourceA.SetName($"a-{resource.Name}");
                resourceA.SetDNSCName($"a-{resource.DNSCName}");

                var resourceB = resource.DeepCopy();
                resourceB.SetName($"b-{resource.Name}");
                resourceB.SetDNSCName($"b-{resource.DNSCName}");

                var t0 = FargateResourceHelperV2.Destroy(resource, elb, r53, ecs, cw, kms, iam, throwOnFailure: true, catchDisable: catchDisable);
                var t1 = FargateResourceHelperV2.Destroy(resourceA, elb, r53, ecs, cw, kms, iam, throwOnFailure: true, catchDisable: catchDisable);
                var t2 = FargateResourceHelperV2.Destroy(resourceB, elb, r53, ecs, cw, kms, iam, throwOnFailure: true, catchDisable: catchDisable);

                var result = Task.WhenAll(t0, t1, t2).Result;

                Console.WriteLine($"Destroying Health Check'{resource.ELBHealthyMetricAlarmName}'...");
                r53.DeleteHealthCheckByNameAsync(resource.HealthCheckName, throwIfNotFound: false)
                .CatchExceptionAsync(catchDisable: catchDisable).Result.PrintResult();
            }
            break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Amazon Fargate",
                            ("create-service", "Accepts params:"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown Fargate command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #26
0
        private static async Task <bool> executeScheduler(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            switch (args[1]?.ToLower())
            {
            case "github":
            {
                if (Environment.GetEnvironmentVariable("test_connection").ToBoolOrDefault(false))
                {
                    Console.WriteLine($"Your Internet Connection is {(SilyWebClientEx.CheckInternetAccess(timeout: 5000) ? "" : "NOT")} available.");
                }

                var userWhitelist         = nArgs.GetValueOrDefault("users")?.Split(',');
                var repositoriesWhitelist = nArgs.GetValueOrDefault("repositories")?.Split(',');

                Console.WriteLine($"Fetching scheduler info.");
                var workingDirectory = (await GetVariableByKey("working_directory", nArgs: nArgs)).ToDirectoryInfo();
                var githubSchedule   = await GetVariableByKey("github_schedule", nArgs : nArgs);

                var user = GITWrapper.GitHubHelperEx.GetUserFromUrl(githubSchedule);

                if (!userWhitelist.IsNullOrEmpty() && !userWhitelist.Any(x => x == user))
                {
                    throw new Exception($"User was specified but, user '{user ?? "undefined"}' was not present among whitelisted users: {userWhitelist.JsonSerialize()}");
                }

                var accessToken = await GetSecretHexToken("github_token", nArgs);

                var repo = GITWrapper.GitHubHelperEx.GetRepoFromUrl(githubSchedule);


                if (!repositoriesWhitelist.IsNullOrEmpty() && !repositoriesWhitelist.Any(x => x == user))
                {
                    throw new Exception($"Repository was specified but, repo '{repo ?? "undefined"}' was not present among whitelisted repositories: {repositoriesWhitelist.JsonSerialize()}");
                }

                var branch           = GITWrapper.GitHubHelperEx.GetBranchFromUrl(githubSchedule);
                var scheduleLocation = GITWrapper.GitHubHelperEx.GetFileFromUrl(githubSchedule);

                var git = new GITWrapper.GitHubHelper(new GITWrapper.Models.GitHubRepoConfig()
                    {
                        accessToken = accessToken,
                        user        = user,
                        repository  = repo,
                        branch      = branch
                    });

                var contentDirectory = PathEx.RuntimeCombine(workingDirectory.FullName, repo).ToDirectoryInfo();
                var statusDirectory  = PathEx.RuntimeCombine(workingDirectory.FullName, "status").ToDirectoryInfo();
                var logsDirectory    = PathEx.RuntimeCombine(workingDirectory.FullName, "logs").ToDirectoryInfo();
                var scheduleFileInfo = PathEx.RuntimeCombine(contentDirectory.FullName, scheduleLocation).ToFileInfo();

                contentDirectory.TryDelete(recursive: true, exception: out var contentDirectoryException);
                Console.WriteLine($"Removing git directory '{contentDirectory.FullName}' {(contentDirectory.Exists ? $"did NOT suceeded, error: {contentDirectoryException.JsonSerializeAsPrettyException()}" : "succeded")}.");

                statusDirectory.TryCreate();
                CommandOutput result;

                var pullCommand = $"git clone https://{accessToken}@github.com/{user}/{repo}.git --branch {branch}";
                result = CLIHelper.Console(pullCommand, workingDirectory: workingDirectory.FullName);
                Console.WriteLine(result.JsonSerialize());

                var gitDirectory = PathEx.RuntimeCombine(contentDirectory.FullName, ".git").ToDirectoryInfo();
                gitDirectory.TryDelete(recursive: true);
                Console.WriteLine($"Removing git directory '{gitDirectory.FullName}' {(gitDirectory.Exists ? "did NOT" : "")} succeded.");

                if (!RuntimeEx.IsWindows())
                {
                    result = CLIHelper.Console($"chmod 777 -R ./{repo}", workingDirectory: workingDirectory.FullName);
                    Console.WriteLine(result.JsonSerialize());
                }

                if (!scheduleFileInfo.Exists)
                {
                    Console.WriteLine($"FAILURE, schedule file '{scheduleFileInfo.FullName}' does not exist or was not defined.");
                    return(false);
                }

                var deploymentConfig    = scheduleFileInfo.DeserialiseJson <DeploymentConfig>();
                var deploymentConfigOld = deploymentConfig.LoadDeploymentConfig(statusDirectory);

                if (deploymentConfig?.enable != true || deploymentConfig.schedules.IsNullOrEmpty())
                {
                    Console.WriteLine($"Deployment config '{scheduleFileInfo.FullName}' was not enabled or schedules were not defined.");
                    return(false);
                }

                //Defines if schedule executuions should be triggered
                var masterTrigger = deploymentConfig.IsTriggered(deploymentConfigOld);

                var serialSchedules = deploymentConfig.schedules
                                      .Where(x => !(x?.id).IsNullOrEmpty() && x.parallelizable == false)
                                      ?.OrderBy(x => x.priority)?.DistinctBy(x => x.id)?.ToArray();

                var parallelSchedules = deploymentConfig.schedules
                                        .Where(x => !(x?.id).IsNullOrEmpty() && x.parallelizable == true)
                                        ?.OrderBy(x => x.priority)?.DistinctBy(x => x.id)?.ToArray();

                var breakAll = false;
                async Task TryCatchExecute(ExecutionSchedule s)
                {
                    var sOld = s.LoadExecutionSchedule(statusDirectory);

                    if (s == null || sOld == null)
                    {
                        Console.WriteLine($"New or old schedule could not be found.");
                        return;
                    }

                    if (!s.IsTriggered(sOld, masterTrigger))
                    {
                        Console.WriteLine($"WARNING, schedule '{s?.id ?? "undefined"}' execution was not triggered.");
                        return;
                    }


                    Console.WriteLine($"Processing executioon schedule '{s.id}', parralelized: {s.parallelizable}, cron: {s.cron ?? "null"}, trigger: {s.trigger}/{sOld.trigger}.");

                    if (s.delay > 0)
                    {
                        await Task.Delay(s.delay);
                    }

                    if (_debug)
                    {
                        Console.WriteLine($"WARNING! github schedule will be processed in DEBUG mode");
                        await ProcessSchedule(s, sOld, contentDirectory, statusDirectory, logsDirectory, masterTrigger : masterTrigger);

                        return;
                    }

                    try
                    {
                        await ProcessSchedule(s, sOld, contentDirectory, statusDirectory, logsDirectory, masterTrigger : masterTrigger);

                        breakAll = s.breakAllOnFinalize;

                        if (s.sleep > 0)
                        {
                            await Task.Delay(s.sleep);
                        }
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            if (deploymentConfig.throwOnFailure == true)
                            {
                                if (deploymentConfig.finalizeOnFailure)
                                {
                                    deploymentConfig.UpdateDeploymentConfig(statusDirectory);
                                    breakAll = s.breakAllOnFinalize;
                                }

                                throw;
                            }

                            Console.WriteLine($"FAILED! execution of schedule '{s.id}', parralelized: {s.parallelizable}, error: {ex.JsonSerializeAsPrettyException()}.");
                        }
                        finally
                        {
                            var logPath = PathEx.Combine(logsDirectory.FullName, $"{s.GetFileSafeId() ?? "tmp.log"}.log").ToFileInfo();
                            if (logPath.TryCreate())
                            {
                                logPath.AppendAllText(ex.JsonSerializeAsPrettyException());
                            }
                        }
                    }
                }

                if (deploymentConfig.delay > 0)
                {
                    await Task.Delay(deploymentConfig.delay);
                }

                var sum = 0;
                if (!serialSchedules.IsNullOrEmpty())
                {
                    sum += serialSchedules.Length;
                    foreach (var s in serialSchedules)
                    {
                        await TryCatchExecute(s);
                    }
                }

                if (!parallelSchedules.IsNullOrEmpty())
                {
                    sum += serialSchedules.Length;
                    await ParallelEx.ForEachAsync(parallelSchedules,
                                                  s => TryCatchExecute(s), maxDegreeOfParallelism : parallelSchedules.Count());
                }

                deploymentConfig.UpdateDeploymentConfig(statusDirectory);

                Console.WriteLine($"SUCCESS, {sum} github schedule/s was/were executed out of {deploymentConfig.schedules.Length}.");

                if (deploymentConfig.sleep > 0)
                {
                    await Task.Delay(deploymentConfig.sleep);
                }

                return(true);
            }

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
            {
                HelpPrinter($"{args[0]}", "Command Deployment",
                            ("github", "Accepts params: working_directory, github_schedule, github_token"));
                return(true);
            }

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown String command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #27
0
        private static async Task executeR53(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            var helper = new Route53Helper();

            switch (args[1])
            {
            case "destroy-record":
                await helper.DestroyRecord(
                    zoneId : nArgs["zone"],
                    recordName : nArgs["name"],
                    recordType : nArgs["type"],
                    throwIfNotFound : nArgs.GetValueOrDefault("throw-if-not-foud").ToBoolOrDefault(true));

                ; break;

            case "upsert-cname-record":
            {
                var result = await helper.UpsertCNameRecordAsync(
                    zoneId : nArgs["zone"],
                    name : nArgs["name"],
                    value : nArgs["value"],
                    ttl : nArgs.GetValueOrDefault("ttl").ToIntOrDefault(60),
                    failover : nArgs.GetValueOrDefault("failover"),
                    healthCheckId : nArgs.GetValueOrDefault("health-check-id"),
                    setIdentifier : nArgs.GetValueOrDefault("set-identifier"));

                WriteLine($"SUCCESS, Result: '{result}'");
            }
                ; break;

            case "upsert-a-record":
            {
                var result = await helper.UpsertARecordAsync(
                    zoneId : nArgs["zone"],
                    name : nArgs["name"],
                    value : nArgs["value"],
                    ttl : nArgs.GetValueOrDefault("ttl").ToIntOrDefault(60),
                    failover : nArgs.GetValueOrDefault("failover"),
                    healthCheckId : nArgs.GetValueOrDefault("health-check-id"),
                    setIdentifier : nArgs.GetValueOrDefault("set-identifier"));

                WriteLine($"SUCCESS, Result: '{result}'");
            }
                ; break;

            case "get-record-sets":
            {
                WriteLine("Loading Route53 Record Sets...");
                var result = await helper.GetRecordSets();

                WriteLine("SUCCESS, Result:");
                Console.WriteLine(result.Select(x => (x.Key.Name, x.Value.Select(y => y))).JsonSerialize(Newtonsoft.Json.Formatting.Indented));
            }
                ; break;

            case "list-resource-record-sets":
            {
                WriteLine("Loading Route53 Resource Record Sets...");
                var result = await helper.ListResourceRecordSetsAsync(nArgs["zone"]);

                WriteLine("SUCCESS, Result:");
                Console.WriteLine(result.JsonSerialize(Newtonsoft.Json.Formatting.Indented));
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Amazon Route53",
                            ("destroy-record", "Accepts params: zone, name, type, throw-if-not-foud (optional)"),
                            ("get-record-sets", "Accepts params: no params"),
                            ("list-resource-record-sets", "Accepts params: zone"),
                            ("upsert-cname-record", "Accepts: zone, name, value, ttl (optional:60), failover (optional), health-check-id (optional), set-identifier (optional)"),
                            ("upsert-a-record", "Accepts: zone, name, value, ttl (optional:60), failover (optional), health-check-id (optional), set-identifier (optional)"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown Route53 command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #28
0
        private static async Task Execute(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            Credentials credentials = null;

            if (nArgs.ContainsKey("assume-role"))
            {
                var role   = await(new IAMHelper(null)).GetRoleByNameAsync(name: nArgs["assume-role"]);
                var result = await(new STHelper(null)).AssumeRoleAsync(role.Arn);
                credentials = result.Credentials;
            }

            switch (args[0]?.ToLower()?.TrimStart("-"))
            {
            case "ec2":
                await executeEC2(args, credentials);

                break;

            case "ecs":
                executeECS(args);
                break;

            case "ecr":
                executeECR(args);
                break;

            case "elb":
                executeELB(args);
                break;

            case "cloud-watch":
                executeCW(args);
                break;

            case "route53":
                await executeR53(args);

                break;

            case "iam":
                executeIAM(args, credentials);
                break;

            case "s3":
                await executeS3(args, credentials);

                break;

            case "kms":
                await executeKMS(args, credentials);

                break;

            case "sm":
                await executeSM(args, credentials);

                break;

            case "fargate":
                executeFargate(args, credentials);
                break;

            case "test":
                await executeCURL(args);

                break;

            case "version":
            case "ver":
            case "v":
                Console.Write($"v{_version}");
                break;

            case "help":
            case "h":
                HelpPrinter($"{args[0]}", "AWSHelper List of available commands",
                            ("ec2", "Accepts params: create-instance, help"),
                            ("ecs", "Accepts params: destroy-cluster, destroy-service, destroy-task-definitions, await-service-start"),
                            ("ecr", "Accepts params: retag, delete, help"),
                            ("elb", "Accepts params: destroy-load-balancer, register-target-instance, deregister-target-instance"),
                            ("cloud-watch", "Accepts params: destroy-log-group"),
                            ("route53", "Accepts params: destroy-record, get-record-sets, list-resource-record-sets, upsert-cname-record"),
                            ("iam", "Accepts params: create-policy, create-role, delete-policy, delete-role, help"),
                            ("s3", "Accepts params: upload-text, hash-upload, hash-download, help"),
                            ("kms", "Accepts params: create-grant, remove-grant, help"),
                            ("sm", "Accepts params: get-secret, show-secret"),
                            ("fargate", "Accepts params: "),
                            ("test", "Accepts params: curl-get"),
                            ("version", "Accepts params: none"),
                            ("[flags]", "Allowed Syntax: key=value, --key=value, -key='v1 v2 v3', -k, --key"),
                            ("--execution-mode=silent-errors", "[All commands] Don't throw errors, only displays exception message."),
                            ("--execution-mode=debug", "[All commands] Throw instantly without reporting a failure."),
                            ("--execution-mode=retry", "[All commands] Repeats command at least once in case it fails. [Sub Flags] retry-times (default 1), retry-delay (default 1000 ms), retry-throws (default true)"));
                break;

            default:
            {
                Console.WriteLine("Try 'help' to find out list of available commands.");
                throw new Exception($"Unknown command: '{args[0]}'.");
            }
            }
        }
Beispiel #29
0
        private static void executeIAM(string[] args, Credentials credentials)
        {
            var nArgs  = CLIHelper.GetNamedArguments(args);
            var helper = new IAMHelper(credentials);

            switch (args[1])
            {
            case "create-policy":
            {
                if (!nArgs.ContainsKey("permissions"))
                {
                    throw new NotSupportedException("permissions parameter was not specified");
                }

                if (!nArgs.ContainsKey("paths"))
                {
                    throw new NotSupportedException("paths parameter was not specified");
                }

                var paths = nArgs["paths"].Split(',').Where(x => !x.IsNullOrWhitespace()).ToArray();

                if (paths.IsNullOrEmpty())
                {
                    throw new Exception("At least one path must be specified in the paths parameter.");
                }

                var permissions = nArgs["permissions"].Split(',').Where(x => !x.IsNullOrWhitespace())
                                  .ToArray().ToEnum <AWSWrapper.S3.S3Helper.Permissions>();

                if (permissions.IsNullOrEmpty())
                {
                    throw new Exception("No permissions were found!");
                }

                var result = helper.CreatePolicyS3Async(
                    name: nArgs["name"],
                    paths: paths,
                    permissions: permissions,
                    description: nArgs.FirstOrDefault(x => x.Key == "description").Value ?? $"Admin S3 Access Policy to '{nArgs["paths"].JsonSerialize()}' auto generated by AWSHelper").Result;

                Console.WriteLine($"SUCCESS, {nArgs["name"]} policy was created for path {nArgs["paths"].JsonSerialize()}, Arn: {result.Arn}.");
            }
                ; break;

            case "delete-policy":
            {
                var result = helper.DeletePolicyByNameAsync(
                    name: nArgs["name"]).Result;

                Console.WriteLine($"SUCCESS, {nArgs["name"]} policy was removed.");
            }
                ; break;

            case "create-role":
            {
                if (!nArgs.ContainsKey("policies"))
                {
                    throw new NotSupportedException("policies parameter was not specified");
                }

                var policies = nArgs["policies"].Split(',').Where(x => !x.IsNullOrWhitespace()).ToArray();

                if (policies.IsNullOrEmpty())
                {
                    throw new Exception($"No policies were found although flag was present, {nArgs["policies"]}, verify that names are separated by ','");
                }

                var result = helper.CreateRoleWithPoliciesAsync(
                    roleName: nArgs["name"],
                    policies: policies,
                    createInstanceProfile: nArgs.GetValueOrDefault("create-instance-profile").ToBoolOrDefault(false),
                    roleDescription: nArgs.FirstOrDefault(x => x.Key == "description").Value ?? $"Role '{nArgs["name"]}' auto generated by AWSHelper").Result;

                Console.WriteLine($"SUCCESS, {nArgs["name"]} role was created, Result:\n{result.JsonSerialize(Newtonsoft.Json.Formatting.Indented)}.");
            }
                ; break;

            case "delete-role":
            {
                var result = helper.DeleteRoleAsync(
                    roleName: nArgs["name"],
                    detachPolicies: true,
                    deleteInstanceProfiles: nArgs.GetValueOrDefault("delete-instance-profiles").ToBoolOrDefault(false)
                    ).Result;

                Console.WriteLine($"SUCCESS, {nArgs["name"]} role was removed and all policies detatched.");
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Amazon Identity and Access Management",
                            ("create-policy", "Accepts params: permissions (',' separated, allowed: Read,Write,Delete,All,<s3-specyfic without 's3:', case sensitive>), name, paths (',' separated), description (optional)"),
                            ("create-role", "Accepts params: name, policies (',' separated), description (optional)"),
                            ("delete-policy", "Accepts params: name"),
                            ("delete-role", "Accepts params: name"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown IAM command: '{args[0]} {args[1]}'");
            }
            }
        }
Beispiel #30
0
        private static void executeELB(string[] args)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            var elb = new ELBHelper();
            var ec2 = new EC2Helper();

            switch (args[1])
            {
            case "destroy-load-balancer":
                elb.DestroyLoadBalancer(nArgs["name"], throwIfNotFound: true).Wait();
                ; break;

            case "register-target-instance":
            {
                var tgName       = nArgs["tg-name"];
                var instanceName = nArgs["instance"];
                var port         = nArgs["port"].ToInt32();
                var tg           = elb.GetTargetGroupByNameAsync(
                    targetGroupName: tgName,
                    throwIfNotFound: true).Result;

                var instance = ec2.ListInstancesByName(name: instanceName,
                                                       stateExclude: new List <Amazon.EC2.InstanceStateName>()
                    {
                        Amazon.EC2.InstanceStateName.ShuttingDown,
                        Amazon.EC2.InstanceStateName.Terminated
                    }
                                                       ).Result.SingleOrDefault();

                if (instance == null)
                {
                    throw new Exception($"Could not find instance with name '{instanceName}' or found more then one.");
                }

                var result = elb.RegisterTargetAsync(tg, instance, port: port).Result;

                Console.WriteLine($"Successfully Registered Instance '{instance.InstanceId}' into Target Group '{tg.TargetGroupArn}', response metadata: {result.ResponseMetadata?.JsonSerialize() ?? "undefined"}");
            }
                ; break;

            case "deregister-target-instance":
            {
                var tgName       = nArgs["tg-name"];
                var instanceName = nArgs["instance"];
                var tg           = elb.GetTargetGroupByNameAsync(
                    targetGroupName: tgName,
                    throwIfNotFound: true).Result;

                var instance = ec2.ListInstancesByName(name: instanceName,
                                                       stateExclude: new List <Amazon.EC2.InstanceStateName>()
                    {
                        Amazon.EC2.InstanceStateName.ShuttingDown,
                        Amazon.EC2.InstanceStateName.Terminated
                    }
                                                       ).Result.SingleOrDefault();

                if (instance == null)
                {
                    throw new Exception($"Could not find instance with name '{instanceName}' or found more then one.");
                }

                var result = elb.DeregisterTargetAsync(tg, instance).Result;

                Console.WriteLine($"Successfully Deregistered Instance '{instance.InstanceId}' from Target Group '{tg.TargetGroupArn}', response metadata: {result.ResponseMetadata?.JsonSerialize() ?? "undefined"}");
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Amazon Elastic Load Balancer",
                            ("destroy-load-balancer", "Accepts params: name"),
                            ("register-target-instance", "Accepts params: tg-name, instance, port"),
                            ("deregister-target-instance", "Accepts params: tg-name, instance"));
                break;

            default: throw new Exception($"Unknown ELB command: '{args[1]}'");
            }
        }