示例#1
0
文件: ssh.cs 项目: asmodat/CDHelper
        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]}'");
            }
            }
        }
示例#2
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]}'");
            }
        }
示例#3
0
        private static async Task executeEC2(string[] args, Credentials credentials)
        {
            var nArgs = CLIHelper.GetNamedArguments(args);

            var ec2 = new EC2Helper();
            var iam = new IAMHelper(credentials);

            switch (args[1])
            {
            case "create-instance":
            {
                var imageId             = nArgs["image"];
                var keyName             = nArgs["key"];
                var instanceType        = nArgs["instance-type"].ToEnum <InstanceModel>().ToInstanceType();
                var securityGroupId     = nArgs["security-group"];
                var subnet              = nArgs["subnet"];
                var role                = nArgs["role"];
                var shutdownTermination = nArgs.GetValueOrDefault("on-shutdown-termination").ToBoolOrDefault(false);
                var publicIp            = nArgs.GetValueOrDefault("public-ip").ToBoolOrDefault(true);
                var name                = nArgs["name"];
                var autoKill            = nArgs.GetValueOrDefault("auto-kill").ToIntOrDefault(100 * 365 * 24 * 60);

                var tt   = DateTime.UtcNow.AddMinutes(autoKill);
                var tt2  = tt.AddMinutes(15);
                var tags = new System.Collections.Generic.Dictionary <string, string>()
                {
                    { "Name", name },
                    { "Auto Kill", $"{tt.Minute}-{tt2.Minute} {tt.Hour}-{tt2.Hour} {tt.Day}-{tt2.Day} {tt.Month}-{tt2.Month} * {tt.Year}-{tt2.Year}" },
                };

                var cname = nArgs.GetValueOrDefault("cname");
                var zones = nArgs.GetValueOrDefault("zones")?.Split(',')?.ToArray();

                if (cname != null && zones != null)
                {
                    for (int i = 0; i < zones.Length; i++)
                    {
                        var suffix = i > 0 ? $" {i + 1}" : "";
                        tags.Add($"Route53 Enable{suffix}", "true");
                        tags.Add($"Route53 Name{suffix}", cname);
                        tags.Add($"Route53 Zone{suffix}", zones[i]);
                    }
                }

                string instanceId;
                var    ebsOptymalized = nArgs.GetValueOrDefault("ebs-optymalized").ToBoolOrDefault();

                if (nArgs.Any(x => x.Key.IsWildcardMatch("ebs-root-")))
                {
                    Console.WriteLine("Advanced Instance Creation Initiated...");
                    var rootDeviceName = nArgs["ebs-root-dev-name"];
                    var rootSnapshotId = nArgs.GetValueOrDefault("ebs-root-snapshot-id");
                    var rootVolumeSize = nArgs["ebs-root-volume-size"].ToInt32();
                    var rootIOPS       = nArgs["ebs-root-iops"].ToIntOrDefault(0);
                    var rootVolumeType = nArgs["ebs-root-volume-type"];

                    instanceId = ec2.CreateInstanceAsync(
                        imageId: imageId,
                        instanceType: instanceType,
                        keyName: keyName,
                        securityGroupIDs: new string[] { securityGroupId },
                        subnetId: subnet,
                        roleName: role,
                        shutdownBehavior: shutdownTermination ? ShutdownBehavior.Terminate : ShutdownBehavior.Stop,
                        associatePublicIpAddress: publicIp,
                        ebsOptymalized: ebsOptymalized,
                        rootDeviceName: rootDeviceName,
                        rootSnapshotId: rootSnapshotId,
                        rootVolumeSize: rootVolumeSize,
                        rootIOPS: rootIOPS,
                        rootVolumeType: rootVolumeType,
                        tags: tags
                        ).Result.Reservation.Instances.Single().InstanceId;
                }
                else
                {
                    Console.WriteLine("Basic Instance Creation Initiated...");
                    instanceId = ec2.CreateInstanceAsync(
                        imageId: imageId,
                        instanceType: instanceType,
                        keyName: keyName,
                        securityGroupId: securityGroupId,
                        subnetId: subnet,
                        roleName: role,
                        shutdownBehavior: shutdownTermination ? ShutdownBehavior.Terminate : ShutdownBehavior.Stop,
                        associatePublicIpAddress: publicIp,
                        ebsOptymalized: ebsOptymalized,
                        tags: tags).Result.Reservation.Instances.Single().InstanceId;
                }

                if (nArgs.GetValueOrDefault("await-start").ToBoolOrDefault(false))
                {
                    var timeout_ms = nArgs.GetValueOrDefault("await-start-timeout").ToIntOrDefault(5 * 60 * 1000);

                    Console.WriteLine($"Awaiting up to {timeout_ms} [ms] for instance '{instanceId}' to start...");

                    ec2.AwaitInstanceStateCode(instanceId,
                                               EC2Helper.InstanceStateCode.running, timeout_ms: timeout_ms).Wait();
                }

                if (nArgs.GetValueOrDefault("await-system-start").ToBoolOrDefault(false))
                {
                    var timeout_ms = nArgs.GetValueOrDefault("await-system-start-timeout").ToIntOrDefault(5 * 60 * 1000);

                    Console.WriteLine($"Awaiting up to {timeout_ms} [ms] for instance '{instanceId}' OS to start...");

                    ec2.AwaitInstanceStatus(instanceId,
                                            EC2Helper.InstanceSummaryStatus.Ok, timeout_ms: timeout_ms).Wait();
                }

                Console.WriteLine($"SUCCESS, Instance '{instanceId}' was created.");
            }
                ; break;

            case "terminate-instance":
            {
                var        name      = nArgs.GetValueOrDefault("name");
                Instance[] instances = null;
                if (!name.IsNullOrEmpty())
                {
                    instances = ec2.ListInstancesByName(name).Result;
                    Console.WriteLine($"Found {instances?.Length ?? 0} instances with name: '{name}'.");
                }
                else
                {
                    throw new Exception("Not Supported Arguments");
                }

                instances.ParallelForEach(i =>
                    {
                        void TryRemoveTags()
                        {
                            if (!nArgs.GetValueOrDefault("try-delete-tags").ToBoolOrDefault(false))
                            {
                                return;
                            }

                            var err = ec2.DeleteAllInstanceTags(i.InstanceId).CatchExceptionAsync().Result;

                            if (err == null)
                            {
                                Console.WriteLine("Removed instance tags.");
                            }
                            else
                            {
                                Console.WriteLine($"Failed to remove instance tags, Error: {err.JsonSerializeAsPrettyException()}");
                            }
                        }

                        if (i.State.Code == (int)InstanceStateCode.terminating ||
                            i.State.Code == (int)InstanceStateCode.terminated)
                        {
                            Console.WriteLine($"Instance {i.InstanceId} is already terminating or terminated.");
                            TryRemoveTags();
                            return;
                        }

                        Console.WriteLine($"Terminating {i.InstanceId}...");
                        var result = ec2.TerminateInstance(i.InstanceId).Result;
                        Console.WriteLine($"Instance {i.InstanceId} state changed {result.PreviousState.Name} -> {result.CurrentState.Name}");
                        TryRemoveTags();
                    });

                Console.WriteLine($"SUCCESS, All Instances Are Terminated.");
            }
                ; break;

            case "describe-instance":
            {
                var      name     = nArgs.GetValueOrDefault("name");
                Instance instance = null;
                if (name != null)
                {
                    instance = ec2.ListInstancesByName(name: name).Result
                               .SingleOrDefault(x => (x.State.Name != InstanceStateName.Terminated) && (x.State.Name != InstanceStateName.ShuttingDown));

                    if (instance == null)
                    {
                        throw new Exception($"No non terminated instance with name '{name}' was found.");
                    }

                    var property = nArgs.GetValueOrDefault("property");
                    var output   = nArgs.GetValueOrDefault("output");

                    if (!property.IsNullOrEmpty())
                    {
                        var value    = instance.GetType().GetProperty(property).GetValue(instance, null);
                        var strValue = TypeEx.IsSimple(value.GetType().GetTypeInfo()) ?
                                       value.ToString() :
                                       value.JsonSerialize(Newtonsoft.Json.Formatting.Indented);

                        Console.WriteLine($"Instance '{instance.InstanceId}' Property '{property}', Value: '{strValue}'.");

                        if (!output.IsNullOrEmpty())
                        {
                            Console.WriteLine($"Saving Property Value into output file: '{output}'...");
                            output.ToFileInfo().WriteAllText(strValue);
                        }
                    }
                    else
                    {
                        Console.WriteLine($"Instance '{instance.InstanceId}' properties: {instance.JsonSerialize(Newtonsoft.Json.Formatting.Indented)}");
                        if (!output.IsNullOrEmpty())
                        {
                            Console.WriteLine($"Saving Properties into output file: '{output}'...");
                            output.ToFileInfo().WriteAllText(instance.JsonSerialize(Newtonsoft.Json.Formatting.Indented));
                        }
                    }
                }
                else
                {
                    throw new Exception("Only describe property by name option is available.");
                }

                Console.WriteLine($"SUCCESS, instance '{instance.InstanceId}' properties were found.");
            }
                ; break;

            case "help":
            case "--help":
            case "-help":
            case "-h":
            case "h":
                HelpPrinter($"{args[0]}", "Amazon Elastic Compute Cloud",
                            ("create-instance", "Accepts params: "),
                            ("terminate-instance", "Accepts params: name"));
                break;

            default:
            {
                Console.WriteLine($"Try '{args[0]} help' to find out list of available commands.");
                throw new Exception($"Unknown EC2 command: '{args[0]} {args[1]}'");
            }
            }
        }