Beispiel #1
0
        public void StopInstance(ref bool actionSucceeded, ref string actionMessage)
        {
            if (InstanceRequestObj.InstanceState.ToLower() != "running")
            {
                actionSucceeded = false;
                actionMessage   = $"The instance {InstanceRequestObj.InstanceName} is currently in {InstanceRequestObj.InstanceState} state, and cannot be stopped at this time.";
                return;
            }
            var request = new StopInstancesRequest
            {
                InstanceIds = new List <string>()
                {
                    InstanceRequestObj.InstanceID
                }
            };

            try
            {
                StopInstancesResponse response = Ec2Client.StopInstancesAsync(request).GetAwaiter().GetResult();
                foreach (InstanceStateChange item in response.StoppingInstances)
                {
                    Console.WriteLine("Stopped instance: " + item.InstanceId);
                    Console.WriteLine("Instance state: " + item.CurrentState.Name);
                }
                actionSucceeded = true;
                actionMessage   = $"The instance {InstanceRequestObj.InstanceName} is being stopped. Please check the AWS Console to verify.";
            }
            catch (Exception ex)
            {
                context.Logger.LogLine($"ServerOperationsHelper::StopInstance {ex.Message}");
                context.Logger.LogLine($"ServerOperationsHelper::StopInstance {ex.StackTrace}");
                actionSucceeded = false;
                actionMessage   = $"Could not stop {InstanceRequestObj.InstanceName} . Please contact your administrator.";
            }
        }
Beispiel #2
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>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        public async Task <bool> StopInstances(IList <string> instances, EC2Settings settings, CancellationToken cancellationToken = default(CancellationToken))
        {
            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 = await client.StopInstancesAsync(request, cancellationToken);

            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);
            }
        }
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            StopInstancesResponse response = new StopInstancesResponse();

            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth = 2;
            }

            while (context.ReadAtDepth(originalDepth))
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("instancesSet/item", targetDepth))
                    {
                        var unmarshaller = InstanceStateChangeUnmarshaller.Instance;
                        var item         = unmarshaller.Unmarshall(context);
                        response.StoppingInstances.Add(item);
                        continue;
                    }
                }
            }

            return(response);
        }
        public string StopInstance(IAmazonEC2 ec2, string instidstr)
        //stop ec2 instance
        {
            StopInstancesRequest  stopreq;
            StopInstancesResponse stopInstancesResponse = null;



            try
            {
                stopreq = new StopInstancesRequest
                {
                    InstanceIds = new List <string>()
                    {
                        instidstr
                    }
                };

                stopInstancesResponse = ec2.StopInstances(stopreq);


                return("Done");
            }
            catch (Exception ex)
            {
                return(ex.Message + "\n" + ex.StackTrace);
            }
        }
Beispiel #5
0
        public static StopInstancesResponse Unmarshall(UnmarshallerContext context)
        {
            StopInstancesResponse stopInstancesResponse = new StopInstancesResponse();

            stopInstancesResponse.HttpResponse = context.HttpResponse;
            stopInstancesResponse.RequestId    = context.StringValue("StopInstances.RequestId");

            return(stopInstancesResponse);
        }
Beispiel #6
0
        public static void StopInstance(string id, AmazonEC2Client client)
        {
            var instanceIds = new List <String>();

            instanceIds.Add(id);

            var request = new StopInstancesRequest(instanceIds);

            StopInstancesResponse response = client.StopInstances(request);
        }
Beispiel #7
0
 public void StopInstance(string InstanceID)
 {
     StopInstancesResponse response = _client.StopInstances(new StopInstancesRequest
     {
         InstanceIds = new List <string> {
             InstanceID
         }
     });
     //Can also do something with the response object too
 }
Beispiel #8
0
        /// <summary>
        /// Stop Amazon EC2 instances.
        /// </summary>
        /// <param name="instances"></param>
        public void StopInstances(string[] instances)
        {
            var request = new StopInstancesRequest {
                InstanceId = new List <string>(instances)
            };
            StopInstancesResponse response = Client.StopInstances(request);

            if (response.IsSetStopInstancesResult())
            {
                foreach (InstanceStateChange instanceStateChange in response.StopInstancesResult.StoppingInstances)
                {
                    Trace.WriteLine(string.Format("Stopping instance {0}", instanceStateChange.InstanceId));
                }
            }
        }
Beispiel #9
0
 public void StopAutomatedTestingEC2()
 {
     try
     {
         List <string> instanceIds = new List <string>()
         {
             _configuration.GetConnectionString("EC2ID")
         };
         StopInstancesRequest  stopInstanceRequest = new StopInstancesRequest(instanceIds);
         StopInstancesResponse x = _ec2Client.StopInstancesAsync(stopInstanceRequest).Result;
     }
     catch (Exception e)
     {
         _logger.LogError(e.Message, e);
     }
 }
Beispiel #10
0
    public IDictionary <string, string> StopInstance(List <string> instancesIds)
    {
        IDictionary <string, string> result;

        // Para uma instância.
        try
        {
            StopInstancesRequest  stopRequest  = new StopInstancesRequest(instancesIds);
            StopInstancesResponse stopResponse = Ec2.StopInstances(stopRequest);
            result = stopResponse.ResponseMetadata.Metadata;
            result.Add("STATUS_CODE", stopResponse.HttpStatusCode.ToString());
            result.Add("RESPONSE", stopResponse.ToString());
        }
        catch (AmazonEC2Exception ex)
        {
            throw new AwsException(ex);
        }

        return(result);
    }
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            StopInstancesResponse response = new StopInstancesResponse();

            int targetDepth = 2;

            while (context.Read())
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("instancesSet/item", targetDepth))
                    {
                        response.StoppingInstances.Add(InstanceStateChangeUnmarshaller.GetInstance().Unmarshall(context));

                        continue;
                    }
                }
            }


            return(response);
        }
Beispiel #12
0
        public async Task <OperationDetails> StopEC2InstancesByInstanceIds(List <string> instanceIds)
        {
            OperationDetails operationDetails = new OperationDetails();

            try
            {
                using (AmazonEC2Client ec2Client = new AmazonEC2Client())
                {
                    StopInstancesRequest stopRequest = new StopInstancesRequest(instanceIds);

                    StopInstancesResponse stopResponse = await ec2Client.StopInstancesAsync(stopRequest);

                    operationDetails.StatusMessage = stopResponse != null?stopResponse.HttpStatusCode.ToString() : "null response";
                }
            }
            catch (Exception ex)
            {
                operationDetails.StatusMessage = ex.Message;
            }

            return(operationDetails);
        }
Beispiel #13
0
        public static StopInstancesResponse Unmarshall(UnmarshallerContext _ctx)
        {
            StopInstancesResponse stopInstancesResponse = new StopInstancesResponse();

            stopInstancesResponse.HttpResponse = _ctx.HttpResponse;
            stopInstancesResponse.RequestId    = _ctx.StringValue("StopInstances.RequestId");

            List <StopInstancesResponse.StopInstances_InstanceResponse> stopInstancesResponse_instanceResponses = new List <StopInstancesResponse.StopInstances_InstanceResponse>();

            for (int i = 0; i < _ctx.Length("StopInstances.InstanceResponses.Length"); i++)
            {
                StopInstancesResponse.StopInstances_InstanceResponse instanceResponse = new StopInstancesResponse.StopInstances_InstanceResponse();
                instanceResponse.InstanceId     = _ctx.StringValue("StopInstances.InstanceResponses[" + i + "].InstanceId");
                instanceResponse.PreviousStatus = _ctx.StringValue("StopInstances.InstanceResponses[" + i + "].PreviousStatus");
                instanceResponse.CurrentStatus  = _ctx.StringValue("StopInstances.InstanceResponses[" + i + "].CurrentStatus");
                instanceResponse.Code           = _ctx.StringValue("StopInstances.InstanceResponses[" + i + "].Code");
                instanceResponse.Message        = _ctx.StringValue("StopInstances.InstanceResponses[" + i + "].Message");

                stopInstancesResponse_instanceResponses.Add(instanceResponse);
            }
            stopInstancesResponse.InstanceResponses = stopInstancesResponse_instanceResponses;

            return(stopInstancesResponse);
        }
Beispiel #14
0
 internal void Stop()
 {
     CheckProtected(ProductionOptions.NonStop);
     StopInstancesResponse resp = myController.ec2.StopInstances(new StopInstancesRequest()
                                                                 .WithInstanceId(InstanceId));
 }