Ejemplo n.º 1
0
        private void TerminateInstance(ref bool actionSucceeded, ref string actionMessage)
        {
            if (InstanceRequestObj.InstanceState.ToLower() != "running" && InstanceRequestObj.InstanceState.ToLower() != "stopped")
            {
                actionSucceeded = false;
                actionMessage   = $"The instance {InstanceRequestObj.InstanceName} is currently in {InstanceRequestObj.InstanceState} state , and cannot be terminated at this time.";
                return;
            }
            var request = new TerminateInstancesRequest
            {
                InstanceIds = new List <string>()
                {
                    InstanceRequestObj.InstanceID
                }
            };

            try
            {
                TerminateInstancesResponse response = Ec2Client.TerminateInstancesAsync(request).GetAwaiter().GetResult();
                foreach (InstanceStateChange item in response.TerminatingInstances)
                {
                    Console.WriteLine("Stopped instance: " + item.InstanceId);
                    Console.WriteLine("Instance state: " + item.CurrentState.Name);
                }
                actionSucceeded = true;
                actionMessage   = $"The instance {InstanceRequestObj.InstanceName} is being terminated. Please check the AWS Console to verify.";
            }
            catch (Exception ex)
            {
                context.Logger.LogLine($"ServerOperationsHelper::TerminateInstance {ex.Message}");
                context.Logger.LogLine($"ServerOperationsHelper::TerminateInstance {ex.StackTrace}");
                actionSucceeded = false;
                actionMessage   = $"Could not terminate {InstanceRequestObj.InstanceName} . Please contact your administrator.";
            }
        }
Ejemplo n.º 2
0
        public async Task TerminateAsync(
            HostInfo host,
            TimeSpan cooldown,
            ISecurityContext context)
        {
            Validate.NotNull(host, nameof(host));
            Validate.NotNull(context, nameof(context));

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

                await clusterManager.DeregisterHostAsync(cluster, host);

                await Task.Delay(cooldown); // allow any active connections to drain before issuing a termination command
            }

            await ec2.TerminateInstancesAsync(
                new TerminateInstancesRequest(host.ResourceId)
                );

            // Mark the host as terminated
            await db.Hosts.PatchAsync(host.Id, new[] {
                Change.Replace("terminated", Func("NOW")),
                Change.Replace("status", HostStatus.Terminated)
            }, IsNull("terminated"));

            #region Logging

            await eventLog.CreateAsync(new Event(
                                           action   : "terminate",
                                           resource : "borg:host/" + host.Id,
                                           userId   : context.UserId
                                           ));

            #endregion
        }