Esempio n. 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.";
            }
        }
Esempio n. 2
0
        public async Task StopAsync(HostInfo host, TimeSpan cooldown)
        {
            Validate.NotNull(host, nameof(host));

            if (host.ClusterId > 0)
            {
                var cluster = await clusterService.GetAsync(host.ClusterId);

                await clusterManager.DeregisterHostAsync(cluster, host);

                await Task.Delay(cooldown); // wait to allow the connections to drain from the load balancer
            }

            await ec2.StopInstancesAsync(new StopInstancesRequest(host.ResourceId));

            await TransitionStateAsync(host, HostStatus.Stopped);
        }