示例#1
0
        public List <SA_AsgEc2Map> GetAsgEc2Maps(string region = null)
        {
            var rets = new List <SA_AsgEc2Map>();

            if (region != null)
            {
                client = new AmazonAutoScalingClient(
                    CredentiaslManager.GetCredential(profile),
                    AwsCommon.GetRetionEndpoint(region));
            }

            var lstSaGroups = new List <SA_AutoScalingGroup>();
            var response    = client.DescribeAutoScalingGroups();
            var lstGroups   = response.AutoScalingGroups.FindAll(o => o.Tags[0].Value.IndexOf(environment.ToString()) >= 0);

            foreach (var group in lstGroups)
            {
                if (group.Instances != null)
                {
                    foreach (var instance in group.Instances)
                    {
                        rets.Add(new SA_AsgEc2Map()
                        {
                            AsgName = group.Tags[0].Value,
                            AutoScalingGroupName = group.AutoScalingGroupName,
                            InstanceId           = instance.InstanceId
                        });
                    }
                }
            }
            return(rets);
        }
示例#2
0
        /****************************************** Scaling Group  ******************************************/
        public async Task <List <SA_Ec2Instance> > GetScalingGroupList(string region = null)
        {
            if (region != null)
            {
                client = new AmazonEC2Client(
                    CredentiaslManager.GetCredential(profile),
                    AwsCommon.GetRetionEndpoint(region));
            }
            var request  = new DescribeInstancesRequest();
            var response = await client.DescribeInstancesAsync(request);

            var ret = new List <SA_Ec2Instance>();

            foreach (var reservation in response.Reservations)
            {
                foreach (var instance in reservation.Instances)
                {
                    ret.Add(new SA_Ec2Instance()
                    {
                        Name         = instance.KeyName,
                        InstanceId   = instance.InstanceId,
                        InstanceType = instance.InstanceType,
                        VpcId        = instance.VpcId,
                        State        = instance.State.Name
                    });
                }
            }

            //DescribeInstanceStatusRequest requst = new DescribeInstanceStatusRequest();
            //var rr = client.DescribeInstanceStatus(requst);
            //    rr.InstanceStatuses[0].
            return(ret);
        }
示例#3
0
 public AutoScalingHelper(string profile, string region, string color) : base(profile, region, color)
 {
     this.profile = profile;
     client       = new AmazonAutoScalingClient(
         CredentiaslManager.GetCredential(profile),
         AwsCommon.GetRetionEndpoint(region));
 }
示例#4
0
 public ALBHelper(string profile, string region, string color) : base(profile, region, color)
 {
     //client = new AmazonRDSClient(credentials, AwsCommon.GetRetionEndpoint(region));
     client = new AmazonElasticLoadBalancingV2Client(
         CredentiaslManager.GetCredential(profile),
         AwsCommon.GetRetionEndpoint(region));
 }
示例#5
0
 public CloudFormationHelper(string profile, string region, string color) : base(profile, region, color)
 {
     //client = new AmazonRDSClient(credentials, AwsCommon.GetRetionEndpoint(region));
     client = new AmazonCloudFormationClient(
         CredentiaslManager.GetCredential(profile),
         AwsCommon.GetRetionEndpoint(region));
 }
示例#6
0
 public EC2Helper(string profile, string region, string color) : base(profile, region, color)
 {
     //client = new AmazonRDSClient(credentials, AwsCommon.GetRetionEndpoint(region));
     this.profile = profile;
     client       = new AmazonEC2Client(
         CredentiaslManager.GetCredential(profile),
         AwsCommon.GetRetionEndpoint(region));
 }
示例#7
0
        //public S3Helper(string awsAccessKeyId, string awsSecretAccessKey, string serviceUrl, string bucketName)
        //{
        //    //AwsAccessKeyId = awsAccessKeyId;
        //    //AwsSecretAccessKey = awsSecretAccessKey;
        //    //ServiceUrl = serviceUrl;
        //    //BucketName = bucketName;

        //    if (string.IsNullOrEmpty(serviceUrl))
        //    {
        //        client = new AmazonS3Client();
        //    }
        //    else
        //    {
        //        var s3Config = new AmazonS3Config { ServiceURL = serviceUrl };
        //        //client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, s3Config);
        //        client = new AmazonS3Client(
        //            CredentiaslManager.GetDynamoDbCredential(), s3Config);
        //    }
        //    BucketName = bucketName;
        //}

        public S3Helper(string profile, string region, string color, string bucketName) : base(profile, region, color)
        {
            //Amazon.Runtime.AWSCredentials credentials = new Amazon.Runtime.StoredProfileAWSCredentials(profile.ToString());
            //client = new AmazonS3Client(credentials, AwsCommon.GetRetionEndpoint(region));
            client = new AmazonS3Client(
                CredentiaslManager.GetCredential(profile),
                AwsCommon.GetRetionEndpoint(region));
            BucketName = bucketName;
        }
示例#8
0
        public async Task StopEc2Instances(List <string> InstanceIds, string region = null)
        {
            if (region != null)
            {
                client = new AmazonEC2Client(
                    CredentiaslManager.GetCredential(profile),
                    AwsCommon.GetRetionEndpoint(region));
            }

            var request = new StopInstancesRequest()
            {
                InstanceIds = InstanceIds
            };
            await client.StopInstancesAsync(request);
        }
示例#9
0
        public async Task <string> TakeSnapshot()
        {
            var rdsInstance = await GetRDSInstance();

            var instanceId = rdsInstance.DBInstanceIdentifier;
            //don't wrap in using block or it will be disposed before you are done with it.
            var rdsClient = new AmazonRDSClient(
                CredentiaslManager.GetCredential(environment),
                AwsCommon.GetRetionEndpoint(region));
            var request = new CreateDBSnapshotRequest($"{environment}-{DateTime.Today.ToShortDateString()}", instanceId);
            //don't await this long running task
            var response = await rdsClient.CreateDBSnapshotAsync(request);

            return(response.DBSnapshot.DBInstanceIdentifier);
        }
示例#10
0
        public async Task ChangeScalingGroup(string groupName, AutoScalingGroupSettings settings, string region = null)
        {
            if (region != null)
            {
                client = new AmazonAutoScalingClient(
                    CredentiaslManager.GetCredential(profile),
                    AwsCommon.GetRetionEndpoint(region));
            }
            UpdateAutoScalingGroupRequest request = new UpdateAutoScalingGroupRequest();

            request.MaxSize                = settings.MaxSize;
            request.MinSize                = settings.MinSize;
            request.DesiredCapacity        = settings.DesiredCapacity;
            request.HealthCheckType        = settings.HealthCheckType;
            request.HealthCheckGracePeriod = settings.HealthCheckGracePeriod;
            request.AutoScalingGroupName   = groupName;
            await client.UpdateAutoScalingGroupAsync(request);
        }
示例#11
0
        }                                                                   //List of accounts like "125237747044"

        public static AssumeRoleAWSCredentials GetCredential(string env)
        {
            if (GlobalRoleCredentialsCache == null)
            {
                GlobalRoleCredentialsCache = new Dictionary <string, AssumeRoleAWSCredentials>();
                foreach (var strEnv in AwsCommon.GetEnvironmentList())
                {
                    GlobalRoleCredentialsCache.Add(strEnv, null);
                }
            }
            if (GlobalRoleArnCache == null)
            {
                GlobalRoleArnCache  = new Dictionary <string, string>();
                GlobalAccountsCache = new List <string>();
                var client   = new AmazonDynamoDBClient(GetDynamoDbCredential(), AwsCommon.GetRetionEndpoint("us-east-2"));
                var request  = new ScanRequest("sa_env_accounts");
                var response = client.Scan(request);
                foreach (var item in response.Items)
                {
                    GlobalRoleArnCache.Add(item["Environment"].S, item["RoleArn"].S + "/" + ConfigurationSettings.AppSettings["Role"]);

                    var account = item["Account"].S;
                    if (!GlobalAccountsCache.Exists(o => o == account))
                    {
                        GlobalAccountsCache.Add(account);
                    }
                }
            }

            if (GlobalRoleCredentialsCache[env.ToString()] == null)
            {
                GlobalRoleCredentialsCache[env.ToString()] =
                    new AssumeRoleAWSCredentials(
                        new StoredProfileAWSCredentials("schoolmessenger"),
                        GlobalRoleArnCache[env.ToString()],
                        "AssumeRole");
            }

            return(GlobalRoleCredentialsCache[env.ToString()]);
        }
示例#12
0
        public async Task <List <SA_AutoScalingGroup> > GetAllAutoScalingGroupList(string region = null)
        {
            if (region != null)
            {
                client = new AmazonAutoScalingClient(
                    CredentiaslManager.GetCredential(profile),
                    AwsCommon.GetRetionEndpoint(region));
            }

            var lstSaGroups = new List <SA_AutoScalingGroup>();
            var response    = await client.DescribeAutoScalingGroupsAsync();

            var lstGroups = response.AutoScalingGroups.FindAll(o => o.Tags[0].Value.IndexOf(environment.ToString()) >= 0);

            foreach (var group in lstGroups)
            {
                var saGroup = new SA_AutoScalingGroup()
                {
                    AutoScalingGroupName = group.AutoScalingGroupName,
                    AutoScalingGroupARN  = group.AutoScalingGroupARN,
                    Name = group.Tags[0].Value,
                    //Status = group.Status,
                    MaxSize                = group.MaxSize,
                    MinSize                = group.MinSize,
                    DesiredCapacity        = group.DesiredCapacity,
                    CreatedTime            = group.CreatedTime,
                    RunningInstances       = group.Instances.Count,
                    HealthCheckType        = group.HealthCheckType,
                    HealthCheckGracePeriod = group.HealthCheckGracePeriod
                };
                lstSaGroups.Add(saGroup);
            }
            var results = lstSaGroups.OrderBy(o => o.Name).ToList();

            return(results);
        }
示例#13
0
        /****************************************** EC2  ******************************************/
        public async Task <List <SA_Ec2Instance> > GetEc2InsatancesList(string region = null)
        {
            var lstRegions = new List <string>();
            var lstImages  = new List <Image>();
            var ret        = new List <SA_Ec2Instance>();

            if (region == null)
            {
                foreach (var objRegion in Regions.GetRegionList())
                {
                    lstRegions.Add(objRegion.Key);
                }
            }
            else
            {
                lstRegions.Add(region);
            }

            foreach (var itemRegion in lstRegions)
            {
                client = new AmazonEC2Client(
                    CredentiaslManager.GetCredential(profile),
                    AwsCommon.GetRetionEndpoint(itemRegion));



                //hello, world
                var request  = new DescribeInstancesRequest();
                var response = await client.DescribeInstancesAsync(request);

                var imageIds   = new HashSet <string>();
                var asgHelper  = new AutoScalingHelper(profile, itemRegion, "green");
                var asgEc2Maps = asgHelper.GetAsgEc2Maps(itemRegion);
                foreach (var reservation in response.Reservations)
                {
                    foreach (var instance in reservation.Instances)
                    {
                        var saInstance = new SA_Ec2Instance()
                        {
                            Name           = instance.Tags.Find(o => o.Key == "Name") == null ? string.Empty : instance.Tags.Find(o => o.Key == "Name").Value,
                            Keyname        = instance.KeyName,
                            InstanceId     = instance.InstanceId,
                            InstanceType   = instance.InstanceType,
                            VpcId          = instance.VpcId,
                            State          = instance.State.Name,
                            PrivateDnsName = instance.PrivateDnsName,
                            ImageId        = instance.ImageId,
                            Zone           = instance.Placement.AvailabilityZone
                        };
                        //imageIds.Add(instance.ImageId);

                        DescribeImagesRequest requestImage = new DescribeImagesRequest();
                        requestImage.ImageIds.Add(instance.ImageId);
                        //imageIds.Add(instance.ImageId);
                        var responseImages = await client.DescribeImagesAsync(requestImage);

                        if (responseImages.Images.Count > 0)
                        {
                            saInstance.OS = responseImages.Images[0].ImageLocation;
                        }

                        var searchedAsgEc2Map = asgEc2Maps.Find(o => o.InstanceId == instance.InstanceId);
                        if (searchedAsgEc2Map != null)
                        {
                            saInstance.AsgName = searchedAsgEc2Map.AutoScalingGroupName;
                        }
                        else
                        {
                            saInstance.AsgName = string.Empty;
                        }
                        ret.Add(saInstance);
                    }
                }

                //Get all Images in EC2 instances
                //DescribeImagesRequest requestImage = new DescribeImagesRequest();
                //requestImage.ImageIds = imageIds.ToList();
                //var responseImages = await client.DescribeImagesAsync(requestImage);
                //lstImages.AddRange(responseImages.Images);
                //foreach (var imageId in imageIds)
                //{
                //    DescribeImageAttributeRequest arq = new DescribeImageAttributeRequest();
                //    arq.ImageId = imageId;
                //    //var arp = client.DescribeImageAttribute(arq);
                //}
            }

            var results = ret.OrderBy(o => o.State).ToList();

            return(results);
        }
示例#14
0
 public Route53Helper(string profile, string region, string color) : base(profile, region, color)
 {
     client = new AmazonRoute53Client(
         CredentiaslManager.GetCredential(profile),
         AwsCommon.GetRetionEndpoint(region));
 }
示例#15
0
 public RDSHelper(string profile, string region, string color) : base(profile, region, color)
 {
     client = new AmazonRDSClient(
         CredentiaslManager.GetCredential(environment),
         AwsCommon.GetRetionEndpoint(region));
 }
示例#16
0
 public DynamoDBHelper()
 {
     client = new AmazonDynamoDBClient(
         CredentiaslManager.GetDynamoDbCredential(),
         AwsCommon.GetRetionEndpoint("us-east-2"));
 }