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); } }
private async Task ShutdownInstanceAsync(Instance instance, ILambdaContext context) { // It's also possible to hibernate - not supported at this time. bool terminate = TerminateOnShutdown(instance); if (terminate) { context.Logger.LogLine($"*** Terminating instance!!! {instance.InstanceId}"); TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest(new List <string> { instance.InstanceId }); await _client.TerminateInstancesAsync(terminateRequest); } else { context.Logger.LogLine($"*** Stopping instance {instance.InstanceId}"); StopInstancesRequest request = new StopInstancesRequest(new List <string> { instance.InstanceId }); await _client.StopInstancesAsync(request); } await PublishSnsMessageAsync(instance, terminate, context); context.Logger.LogLine($"Shutdown for instance {instance.InstanceId} requested."); }
private void StopInstances() { var request = new StopInstancesRequest(instances); var response = client.StopInstances(request); response.StoppingInstances.ForEach(x => Console.WriteLine($"STOPPING INSTANCE ID:{x.InstanceId}")); }
public void StopInstances(List <string> instanceIds) { var request = new StopInstancesRequest { InstanceIds = instanceIds }; _ec2Client.StopInstances(request); }
public void Request() { var a = new StopInstancesRequest(new[] { "i-1234567890abcdef0" }); var result = string.Join('&', a.ToParams().Select(pair => pair.Key + "=" + pair.Value)); Assert.Equal("Action=StopInstances&InstanceId.1=i-1234567890abcdef0", result); }
static async Task StopInstanceAsync() { StopInstancesRequest req = new StopInstancesRequest { InstanceIds = _InstanceId }; await _Client.StopInstancesAsync(req); }
public static async Task StopInstance(AmazonEC2Client ec2Client, string instanceId) { var request = new StopInstancesRequest { InstanceIds = new List <string> { instanceId } }; var response = await ec2Client.StopInstancesAsync(request); }
public void StopInstance(string InstanceID) { var mStopInstancesRequest = new StopInstancesRequest(); mStopInstancesRequest.InstanceIds = new List <string> { InstanceID }; ec2Client.StopInstances(mStopInstancesRequest); }
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); }
InstanceState Stop(RunningInstance instance) { using (var client = CreateClient()) { var request = new StopInstancesRequest(); request.InstanceId.Add(instance.InstanceId); var response = client.StopInstances(request); return(response.StopInstancesResult.StoppingInstances[0].CurrentState); } }
internal void Stop(string bootstrapId) { Logger.Info("Stopping instances"); IEnumerable <string> instanceIds = FindInstanceIdsFromBootstrapId(bootstrapId); var stopRequest = new StopInstancesRequest(); stopRequest.InstanceIds.AddRange(instanceIds); _client.StopInstances(stopRequest); Logger.WithLogSection("Waiting for instances to stop", () => WaitForInstancesToStop(instanceIds)); }
public async Task <StopInstancesResponse> StopAsync(StopInstancesRequest request) { using (var ec2 = new AmazonEC2Client(_config)) { var response = await ec2.StopInstancesAsync(request); if (response.HttpStatusCode != HttpStatusCode.OK) { throw new AwsResponseException <StopInstancesRequest, StopInstancesResponse>(request, response); } return(response); } }
private void StopInstance(List <string> ids) { if (ids.Count <= 0) { return; } var sir = new StopInstancesRequest { InstanceIds = ids }; ec2Client.StopInstances(sir); }
public async Task StopServer() { while (true) { await Task.Delay((int)BotConfig.GetCachedConfig().PollingDelay); MineStat ms = new MineStat(BotConfig.GetCachedConfig().Minecraft.MinecraftServerIP, BotConfig.GetCachedConfig().Minecraft.MinecraftServerPort); Console.WriteLine("Current player count: " + ms.CurrentPlayers); if (ms.CurrentPlayers == "0") { Console.WriteLine("Server player is count is zero. Sleeping, and checking again."); await Task.Delay((int)BotConfig.GetCachedConfig().StopServerDelay); ms = new MineStat(BotConfig.GetCachedConfig().Minecraft.MinecraftServerIP, BotConfig.GetCachedConfig().Minecraft.MinecraftServerPort); if (ms.ServerUp && ms.CurrentPlayers == "0") { Console.WriteLine("Player count is still zero. Stopping."); await Embeds.EditMessage(Embeds.ServerStopping()); var stopRequest = new StopInstancesRequest(); var request = new StopInstancesRequest(); request.InstanceIds = new List <string>() { BotConfig.GetCachedConfig().Aws.EC2InstanceId }; request.Force = false; var response = await client.StopInstancesAsync(request); ushort pollCounter = 0; while (pollCounter < BotConfig.GetCachedConfig().Aws.MaxInstanceStartAttempts) { pollCounter++; await Task.Delay(5000); var serverState = await this.GetState(); if (serverState.InstanceState.Code == 80) { break; } } await Embeds.EditMessage(Embeds.ServerStopped()); break; } } } IsWorking = false; }
/// <summary> /// 本接口(StopInstances)用于关闭一个或多个实例。 /// * 只有状态为 RUNNING 的实例才可以进行此操作。 /// * 接口调用成功时,实例会进入 STOPPING 状态;关闭实例成功时,实例会进入 STOPPED 状态。 /// * 支持批量操作。每次请求批量实例的上限为 100。 /// * 本接口为异步接口,请求发送成功后会返回一个 RequestId,此时操作并未立即完成。实例操作结果可以通过调用 DescribeInstances 接口查询,如果实例的最新操作状态(LatestOperationState)为“SUCCESS”,则代表操作成功。 /// </summary> /// <param name="req"><see cref="StopInstancesRequest"/></param> /// <returns><see cref="StopInstancesResponse"/></returns> public StopInstancesResponse StopInstancesSync(StopInstancesRequest req) { JsonResponseModel <StopInstancesResponse> rsp = null; try { var strResp = this.InternalRequestSync(req, "StopInstances"); rsp = JsonConvert.DeserializeObject <JsonResponseModel <StopInstancesResponse> >(strResp); } catch (JsonSerializationException e) { throw new TencentCloudSDKException(e.Message); } return(rsp.Response); }
/// <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)); } } }
public async Task StopEc2Instances(List <string> InstanceIds, string region = null) { if (region != null) { client = new AmazonEC2Client( CredentiaslManager.GetCredential(profile), AwsCommon.GetRetionEndpoint(region)); } var request = new StopInstancesRequest() { InstanceIds = InstanceIds }; await client.StopInstancesAsync(request); }
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); } }
private static void StopInstance() { try { AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client(_awsKey, _awsSecretKey); StopInstancesRequest request = new StopInstancesRequest(); request.WithInstanceId(new string[] { _instanceId }); ec2.StopInstances(request); Mail(string.Format("Successfully stopped EC2 instance {0}", _instanceId)); } catch (Exception e) { MailError("Error stopping instance", e); } }
static void Main(string[] args) { try { var argsDic = GetArgsDic(args); var itemsList = argsDic["--instances"]; if (itemsList.StartsWith("\"") && itemsList.EndsWith("\"")) { itemsList = itemsList.Substring(1, itemsList.Length - 2); } var items = itemsList.Split(','); var action = argsDic["--action"]; using (var client = AwsUtil.CreateClient(AwsUtil.Region)) { if (action == "start") { var request = new StartInstancesRequest(); request.InstanceId.AddRange(items); client.StartInstances(request); } else if (action == "stop") { var request = new StopInstancesRequest(); request.InstanceId.AddRange(items); client.StopInstances(request); } } } catch (ApplicationException exc) { Console.WriteLine(exc.Message); } catch (AmazonEC2Exception exc) { Console.WriteLine(exc.Message); } catch (Exception exc) { Console.WriteLine(exc); } }
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 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); }
public bool StopVM(string instanceId) { try { AWSAuthDetails detail; string[] id = instanceId.Split(','); detail = SqlHelper.GetAWSAuth(id[0], "VM"); var region = RegionEndpoint.GetBySystemName(detail.Region); //Amazon.Runtime.AWSCredentials credentials = new Amazon.Runtime.StoredProfileAWSCredentials(detail.ProfileName); AmazonEC2Client ec2 = new AmazonEC2Client(detail.AccessKey, detail.SecretKey, region); StopInstancesRequest stopRequest = new StopInstancesRequest(); StopInstancesResponse stopResponse; foreach (var instance in id) { stopRequest.InstanceIds.Add(instance); } stopResponse = ec2.StopInstances(stopRequest); string response = stopResponse.HttpStatusCode.ToString(); if (response == "OK") { //set all to starting SqlHelper.UpdateInstanceStatus(id, "stopping");; return(true); } return(false); } catch (Exception e) { this.log.Error(e); return(false); } }
/// <summary> /// Initializes the Amazon EC2 client object and uses it to call the /// StartInstancesAsync method to stop the listed Amazon EC2 instances. /// </summary> public static async Task Main() { string ec2InstanceId = "i-0123456789abcdef0"; // If your EC2 instances are not in the same AWS Region as // the default user on your system, you need to supply // the AWS Region as a parameter to the client constructor. var client = new AmazonEC2Client(); // In addition to the list of instance Ids, the // request can als include the following properties: // Force When true forces the instances to // stop but you have to check the integrity // of the file system. (Not recommended on // Windows instances. // Hibernate When true, hibernates the instance if the // instance was enabled for hibernation when // it was launched. var request = new StopInstancesRequest { InstanceIds = new List <string> { ec2InstanceId }, }; var response = await client.StopInstancesAsync(request); if (response.StoppingInstances.Count > 0) { var instances = response.StoppingInstances; instances.ForEach(i => { Console.WriteLine($"Successfully stopped the EC2 Instance with InstanceID: {i.InstanceId}."); }); } }
public static Task <StopInstancesResponse> StopInstancesAsyncByIds(this IAmazonEC2 ec2Client, IEnumerable <string> instanceIds) { var stopRequest = new StopInstancesRequest(new List <string>(instanceIds)); return(ec2Client.StopInstancesAsync(stopRequest)); }
public static Instance StopInstance(string instanceId, string regionName, string profileName) { if (string.IsNullOrWhiteSpace(instanceId)) { throw new Exception("Instance id is not specified."); } AWSCredentials creds = GetAWSCredentials(profileName); if (creds == null) { throw new Exception("AWS credentials are not specified"); } RegionEndpoint endpoint = RegionEndpoint.GetBySystemName(regionName); if (endpoint.DisplayName.Contains("Unknown")) { throw new Exception("AWS region endpoint is not valid."); } List <Instance> instances = new List <Instance>(); Instance foundInstance; try { using (AmazonEC2Client client = new AmazonEC2Client(creds, endpoint)) { StopInstancesRequest req = new StopInstancesRequest { InstanceIds = new List <string>() { instanceId } }; // do // { // StopInstancesResponse resp = client.StopInstances(req); // if (resp != null) // { // instances.AddRange(resp.Reservations.SelectMany(reservation => reservation.Instances).Where(x => x.InstanceId == instanceId)); // req.NextToken = resp.NextToken; // } // } while (!string.IsNullOrWhiteSpace(req.NextToken)); } if (instances.Count == 1) { foundInstance = instances[0]; } else { throw new Exception("Error finding the specified instance."); } } catch (Exception ex) { throw new Exception($"Encountered exception while describing EC2 instances: {ex.Message}"); } return(foundInstance); }
/// <summary> /// Stops instances /// </summary> /// <param name="request"></param> /// <returns></returns> public StopInstancesResponse StopInstances(StopInstancesRequest request) { return(EC2.StopInstances(request)); }
public StopInstancesResponse StopInstances(StopInstancesRequest request) { throw new NotImplementedException(); }