Example #1
0
        /// <summary>
        /// Wait for the instances to become in the desired state.
        /// </summary>
        /// <param name="instanceIds"></param>
        /// <param name="desiredState"></param>
        /// <param name="timeOutSeconds"></param>
        /// <param name="pollIntervalSeconds"></param>
        public void WaitForInstances(string[] instanceIds, string desiredState, int timeOutSeconds, int pollIntervalSeconds)
        {
            DateTime waitUntil = DateTime.Now.AddSeconds(timeOutSeconds);
            var      request   = new DescribeInstancesRequest {
                InstanceId = new List <string>(instanceIds)
            };

            do
            {
                DescribeInstancesResponse response = Client.DescribeInstances(request);

                if (response.IsSetDescribeInstancesResult())
                {
                    // Are All instances in the desired state?
                    if (response.DescribeInstancesResult.Reservation.All(
                            reservation => reservation.RunningInstance.All(runningInstnace => runningInstnace.InstanceState.Name == desiredState))
                        )
                    {
                        return;
                    }
                }

                Thread.Sleep(new TimeSpan(0, 0, pollIntervalSeconds));
            } while (DateTime.Now <= waitUntil);

            throw new TimeoutException(string.Format("Timeout waiting for EC2 Instances state change."));
        }
Example #2
0
        public RunningInstance DescribeInstance(string instanceId)
        {
            var request = new DescribeInstancesRequest {
                InstanceId = new List <string> {
                    instanceId
                }
            };

            DescribeInstancesResponse response = Client.DescribeInstances(request);

            if (response.IsSetDescribeInstancesResult())
            {
                Reservation reservation = response.DescribeInstancesResult.Reservation.FirstOrDefault();
                if (reservation != null)
                {
                    return(reservation.RunningInstance.FirstOrDefault());
                }
            }
            throw new Exception("No details found of instance.");
        }
Example #3
0
        public List <CEc2Instance> describeInstances()
        {
            List <CEc2Instance> myInstances = new List <CEc2Instance>();

            try
            {
                DescribeInstancesRequest  request  = new DescribeInstancesRequest();
                DescribeInstancesResponse response = _service.DescribeInstances(request);
                if (response.IsSetDescribeInstancesResult())
                {
                    DescribeInstancesResult describeInstancesResult = response.DescribeInstancesResult;
                    List <Reservation>      reservationList         = describeInstancesResult.Reservation;


                    foreach (Reservation reservation in reservationList)
                    {
                        List <RunningInstance> runningInstanceList = reservation.RunningInstance;
                        foreach (RunningInstance runningInstance in runningInstanceList)
                        {
                            CEc2Instance inst = new CEc2Instance();

                            if (runningInstance.IsSetInstanceId())
                            {
                                inst.instanceId = runningInstance.InstanceId;
                            }
                            if (runningInstance.IsSetImageId())
                            {
                                inst.imageId = runningInstance.ImageId;
                            }
                            if (runningInstance.IsSetInstanceState())
                            {
                                InstanceState instanceState = runningInstance.InstanceState;
                                if (instanceState.IsSetName())
                                {
                                    inst.status = instanceState.Name;
                                }
                            }
                            if (runningInstance.IsSetPublicDnsName())
                            {
                                inst.publicDns = runningInstance.PublicDnsName;
                            }
                            if (runningInstance.IsSetKeyName())
                            {
                                inst.keyPairName = runningInstance.KeyName;
                            }
                            if (runningInstance.IsSetInstanceType())
                            {
                                inst.type = runningInstance.InstanceType;
                            }
                            if (runningInstance.IsSetLaunchTime())
                            {
                                inst.launchTime = runningInstance.LaunchTime;
                            }
                            if (runningInstance.IsSetPlacement())
                            {
                                if (runningInstance.Placement.IsSetAvailabilityZone())
                                {
                                    inst.zone = runningInstance.Placement.AvailabilityZone;
                                }
                            }
                            if (runningInstance.IsSetPlatform())
                            {
                                inst.platform = runningInstance.Platform;
                            }

                            myInstances.Add(inst);
                        }
                    }
                }
            }
            catch (AmazonEC2Exception ex)
            {
                throw new Exception("Caught Exception: " + ex.XML);
            }
            return(myInstances);
        }