Esempio n. 1
0
            private AmazonEC2Client CreateClient(EC2Settings settings)
            {
                if (settings == null)
                {
                    throw new ArgumentNullException("settings");
                }
                
                if (settings.Region == null)
                {
                    throw new ArgumentNullException("settings.Region");
                }

                if (settings.Credentials == null)
                {
                    if (String.IsNullOrEmpty(settings.AccessKey))
                    {
                        throw new ArgumentNullException("settings.AccessKey");
                    }
                    if (String.IsNullOrEmpty(settings.SecretKey))
                    {
                        throw new ArgumentNullException("settings.SecretKey");
                    }

                    return new AmazonEC2Client(settings.AccessKey, settings.SecretKey, settings.Region);
                }
                else
                {
                    return new AmazonEC2Client(settings.Credentials, settings.Region);
                }
            }
        /// <summary>
        /// Helper method to get the AWS Credentials from environment variables
        /// </summary>
        /// <param name="environment">The cake environment.</param>
        /// <returns>A new <see cref="EC2Settings"/> instance to be used in calls to the <see cref="IEC2Manager"/>.</returns>
        public static EC2Settings CreateEC2Settings(this ICakeEnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }

            EC2Settings settings = new EC2Settings();

            //AWS Fallback
            AWSCredentials creds = FallbackCredentialsFactory.GetCredentials();
            if (creds != null)
            {
                settings.Credentials = creds;
            }

            //Environment Variables
            string region = environment.GetEnvironmentVariable("AWS_REGION");
            if (!String.IsNullOrEmpty(region))
            {
                settings.Region = RegionEndpoint.GetBySystemName(region);
            }

            return settings;
        }
Esempio n. 3
0
        public static InstanceStatus DescribeInstance(this ICakeContext context, string instance, EC2Settings settings)
        {
            IList<InstanceStatus> status = context.CreateManager().DescribeInstances(new List<string>() { instance }, settings);

            if (status.Count > 0)
            {
                return status[0];
            }
            else
            {
                return null;
            }
        }
Esempio n. 4
0
 public static IList<InstanceStatus> DescribeInstances(this ICakeContext context, IList<string> instances, EC2Settings settings)
 {
     return context.CreateManager().DescribeInstances(instances, settings);
 }
Esempio n. 5
0
 public static bool TerminateEC2Instance(this ICakeContext context, EC2Settings settings)
 {
     return context.CreateManager().TerminateInstances(EC2Metadata.InstanceId.Split(','), settings);
 }
Esempio n. 6
0
 public static InstanceStatus DescribeInstance(this ICakeContext context, EC2Settings settings)
 {
     return context.DescribeInstance(EC2Metadata.InstanceId, settings);
 }
Esempio n. 7
0
        public static string GetTagValue(this ICakeContext context, string instance, string key, EC2Settings settings)
        {
            IList<TagDescription> tags = context.CreateManager().DescribeTags(new List<string>() { instance }, settings);

            return tags.Where(t => t.Key == key).Select(x => x.Value).FirstOrDefault();
        }
Esempio n. 8
0
 public static async Task <InstanceStatus> DescribeInstance(this ICakeContext context, EC2Settings settings)
 {
     return(await context.DescribeInstance(EC2InstanceMetadata.InstanceId, settings));
 }
Esempio n. 9
0
            /// <summary>
            /// Describes the status of one or more instances. Instance status includes the following components:
            /// Status checks - Amazon EC2 performs status checks on running EC2 instances to identify hardware and software issues. For more information, see Status Checks
            /// for Your Instances and Troubleshooting Instances with Failed Status Checks in the Amazon Elastic Compute Cloud User Guide.
            /// Scheduled events - Amazon EC2 can schedule events (such as reboot, stop, or terminate) for your instances related to hardware issues, software updates, or system maintenance.
            /// For more information, see Scheduled Events for Your Instances in the Amazon Elastic Compute Cloud User Guide.
            /// Instance state - You can manage your instances from the moment you launch them through their termination. For more information, see Instance Lifecycle in the Amazon Elastic Compute Cloud User Guide.
            /// </summary>
            /// <param name="instances">A list of instance IDs to be stopped.</param>
            /// <param name="settings">The <see cref="EC2Settings"/> used during the request to AWS.</param>
            public IList<InstanceStatus> DescribeInstances(IList<string> instances, EC2Settings settings)
            {
                if ((instances == null) || (instances.Count == 0))
                {
                    throw new ArgumentNullException("instances");
                }



                //Create Request
                AmazonEC2Client client = this.CreateClient(settings);
                DescribeInstanceStatusRequest request = new DescribeInstanceStatusRequest();

                foreach (string instance in instances)
                {
                    request.InstanceIds.Add(instance);
                }



                //Check Response
                DescribeInstanceStatusResponse response = client.DescribeInstanceStatus(request);

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    _Log.Verbose("Successfully terminated instances '{0}'", string.Join(",", instances));
                    return response.InstanceStatuses;
                }
                else
                {
                    _Log.Error("Failed to terminate instances '{0}'", string.Join(",", instances));
                    return new List<InstanceStatus>();
                }
            }
Esempio n. 10
0
 public static IList<TagDescription> DescribeTags(this ICakeContext context, IList<string> instances, EC2Settings settings)
 {
     return context.CreateManager().DescribeTags(instances, settings);
 }
Esempio n. 11
0
 public static async Task <IList <TagDescription> > DescribeTags(this ICakeContext context, string instance, EC2Settings settings)
 {
     return(await context.CreateManager().DescribeTags(new List <string>()
     {
         instance
     }, settings));
 }
Esempio n. 12
0
            /// <summary>
            /// Stops an Amazon EBS-backed instance. Each time you transition an instance from stopped to started, Amazon EC2 charges a full instance hour, even if transitions
            /// happen multiple times within a single hour. You can't start or stop Spot Instances.
            /// Instances that use Amazon EBS volumes as their root devices can be quickly stopped and started. When an instance is stopped, the compute resources are released
            /// and you are not billed for hourly instance usage. However, your root partition Amazon EBS volume remains, continues to persist your data, and you are charged
            /// for Amazon EBS volume usage. You can restart your instance at any time. Before stopping an instance, make sure it is in a state from which it can be
            /// restarted. Stopping an instance does not preserve data stored in RAM. Performing this operation on an instance that uses an instance store as its root device returns an error.
            /// </summary>
            /// <param name="instances">A list of instance IDs to be stopped.</param>
            /// <param name="settings">The <see cref="EC2Settings"/> used during the request to AWS.</param>
            public bool StopInstances(IList<string> instances, EC2Settings settings)
            {
                if ((instances == null) || (instances.Count == 0))
                {
                    throw new ArgumentNullException("instances");
                }

                

                //Create Request
                AmazonEC2Client client = this.CreateClient(settings);
                StopInstancesRequest request = new StopInstancesRequest();

                foreach (string instance in instances)
                {
                    request.InstanceIds.Add(instance);
                }



                //Check Response
                StopInstancesResponse response = client.StopInstances(request);

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    _Log.Verbose("Successfully stopped instances '{0}'", string.Join(",", instances));
                    return true;
                }
                else
                {
                    _Log.Error("Failed to stop instances '{0}'", string.Join(",", instances));
                    return false;
                }
            }
Esempio n. 13
0
 public static async Task <IList <TagDescription> > DescribeTags(this ICakeContext context, EC2Settings settings)
 {
     return(await context.DescribeTags(EC2InstanceMetadata.InstanceId, settings));
 }
Esempio n. 14
0
 public static async Task <IList <InstanceStatus> > DescribeInstances(this ICakeContext context, IList <string> instances, EC2Settings settings)
 {
     return(await context.CreateManager().DescribeInstances(instances, settings));
 }
Esempio n. 15
0
        public static async Task <InstanceStatus> DescribeInstance(this ICakeContext context, string instance, EC2Settings settings)
        {
            IList <InstanceStatus> status = await context.CreateManager().DescribeInstances(new List <string>()
            {
                instance
            }, settings);

            if (status.Count > 0)
            {
                return(status[0]);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 16
0
 public static IList<TagDescription> DescribeTags(this ICakeContext context, EC2Settings settings)
 {
     return context.DescribeTags(EC2Metadata.InstanceId, settings);
 }
Esempio n. 17
0
            /// <summary>
            /// Describes one or more of the tags for your EC2 resources.
            /// </summary>
            /// <param name="instances">A list of instance IDs to be stopped.</param>
            /// <param name="settings">The <see cref="EC2Settings"/> used during the request to AWS.</param>
            public IList<TagDescription> DescribeTags(IList<string> instances, EC2Settings settings)
            {
                if ((instances == null) || (instances.Count == 0))
                {
                    throw new ArgumentNullException("instances");
                }



                //Create Request
                AmazonEC2Client client = this.CreateClient(settings);
                DescribeTagsRequest request = new DescribeTagsRequest();

                List<string> list = new List<string>();
                list.AddRange(instances);

                request.Filters.Add(new Filter("resource-id", list));



                //Check Response
                DescribeTagsResponse response = client.DescribeTags(request);

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    _Log.Verbose("Successfully terminated instances '{0}'", string.Join(",", instances));
                    return response.Tags;
                }
                else
                {
                    _Log.Error("Failed to terminate instances '{0}'", string.Join(",", instances));
                    return new List<TagDescription>();
                }
            }
Esempio n. 18
0
 public static IList<TagDescription> DescribeTags(this ICakeContext context, string instance, EC2Settings settings)
 {
     return context.CreateManager().DescribeTags(new List<string>() { instance }, settings);
 }
Esempio n. 19
0
 public static bool StartEC2Instances(this ICakeContext context, string instances, EC2Settings settings)
 {
     return context.CreateManager().StartInstances(instances.Split(','), settings);
 }
Esempio n. 20
0
 public static string GetTagValue(this ICakeContext context, string key, EC2Settings settings)
 {
     return  context.GetTagValue(EC2Metadata.InstanceId, key, settings);
 }
Esempio n. 21
0
 public static async Task <bool> TerminateEC2Instance(this ICakeContext context, EC2Settings settings)
 {
     return(await context.CreateManager().TerminateInstances(EC2InstanceMetadata.InstanceId.Split(','), settings));
 }
Esempio n. 22
0
        public static bool IsInstanceTerminated(this ICakeContext context, string instance, EC2Settings settings)
        {
            InstanceStatus status = context.DescribeInstance(instance, settings);

            return ((status != null) && (status.InstanceState.Name.Value == InstanceStateName.Terminated.Value));
        }
Esempio n. 23
0
 public static async Task <bool> TerminateEC2Instances(this ICakeContext context, IList <string> instances, EC2Settings settings)
 {
     return(await context.CreateManager().TerminateInstances(instances, settings));
 }
Esempio n. 24
0
 public static bool StartEC2Instances(this ICakeContext context, IList<string> instances, EC2Settings settings)
 {
     return context.CreateManager().StartInstances(instances, settings);
 }
Esempio n. 25
0
 public static async Task <bool> StopEC2Instances(this ICakeContext context, string instances, EC2Settings settings)
 {
     return(await context.CreateManager().StopInstances(instances.Split(','), settings));
 }