/// <summary> /// Initializes the Amazon EC2 client object and then calls /// RebootInstancesAsync to reboot the instance(s) in the ectInstanceId /// list. /// </summary> public static async Task Main() { string ec2InstanceId = "i-0123456789abcdef0"; // If your EC2 instances are not in the same AWS Region as // the default users on your system, you need to supply // the AWS Region as a parameter to the client constructor. var client = new AmazonEC2Client(); var request = new RebootInstancesRequest { InstanceIds = new List <string> { ec2InstanceId }, }; var response = await client.RebootInstancesAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine("Instance(s) successfully rebooted."); } else { Console.WriteLine("Could not reboot one or more instances."); } }
private static async Task RebootInstances(List <InstanceResult> badInstances) { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Rebooting Instances!"); Console.WriteLine("----------------------"); badInstances.ForEach(i => Console.WriteLine(i.Name)); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; AmazonEC2Client client = new AmazonEC2Client(AccessKey, SecretKey, Region); //go sync so we can take it slower //if this list starts to get too large we could easily do async in batches int processed = 0; foreach (var instance in badInstances.Select(i => i.InstanceDescription.InstanceId)) { var request = new RebootInstancesRequest() { InstanceIds = new List <string> { instance }, }; var response = await client.RebootInstancesAsync(request); Console.Write("\rProcessed {0}/{1} instances.", ++processed, badInstances.Count); } Console.WriteLine(); Console.WriteLine("Complete"); }
internal static async Task <RebootInstancesResponse> RebootEc2InstanceAsync(IConfiguration Configuration, string User, string AccountName, string InstanceId) { try { var accountKey = LoadAwsAccounts(Configuration).SingleOrDefault(x => x.AccountName == AccountName); var accountRegion = RegionEndpoint.GetBySystemName(accountKey.Region); var stsClient = new AmazonSecurityTokenServiceClient(); var sessionName = string.Format(ResourceStrings.RebootAction, User, accountKey.AccountName, DateTime.Now.Ticks.ToString()); sessionName = sessionName.Length > 63 ? sessionName.Substring(0, 63) : sessionName; var assumeRoleRequest = new AssumeRoleRequest { RoleArn = accountKey.RoleArn, RoleSessionName = sessionName, DurationSeconds = 900 }; var stsResponse = await stsClient.AssumeRoleAsync(assumeRoleRequest); var instanceIdAsList = new List <string> { InstanceId }; var rebootRequest = new RebootInstancesRequest(instanceIdAsList); var ec2Client = new AmazonEC2Client(stsResponse.Credentials, accountRegion); var response = ec2Client.RebootInstancesAsync(rebootRequest); ec2Client.Dispose(); stsClient.Dispose(); return(await response); } catch (Exception e) { throw new Exception(string.Format(ErrorStrings.RebootEc2InstanceError, InstanceId, e.Message), e.InnerException); } }
/// <summary> /// Reboots an EC2 instance /// </summary> /// <param name="instanceIds"></param> public void RebootInstance(IEnumerable <string> instanceIds) { var request = new RebootInstancesRequest { InstanceId = new List <string>(instanceIds) }; Client.RebootInstances(request); }
public void Serialize1() { var a = new RebootInstancesRequest(new[] { "i-1234567890abcdef0" }) { DryRun = true }; var result = string.Join('&', a.ToParams().Select(pair => pair.Key + "=" + pair.Value)); Assert.Equal("Action=RebootInstances&DryRun=True&InstanceId.1=i-1234567890abcdef0", result); }
public void reboot() { try { RebootInstancesRequest request = new RebootInstancesRequest(); request.InstanceId.Add(_instanceId); RebootInstancesResponse response = _service.RebootInstances(request); } catch (AmazonEC2Exception ex) { throw new Exception("Caught Exception: " + ex.XML); } }
/// <summary> /// 只有状态为RUNNING的实例才可以进行此操作;接口调用成功时,实例会进入REBOOTING状态;重启实例成功时,实例会进入RUNNING状态;支持强制重启,强制重启的效果等同于关闭物理计算机的电源开关再重新启动。强制重启可能会导致数据丢失或文件系统损坏,请仅在服务器不能正常重启时使用。 /// </summary> /// <param name="req"><see cref="RebootInstancesRequest"/></param> /// <returns><see cref="RebootInstancesResponse"/></returns> public RebootInstancesResponse RebootInstancesSync(RebootInstancesRequest req) { JsonResponseModel <RebootInstancesResponse> rsp = null; try { var strResp = this.InternalRequestSync(req, "RebootInstances"); rsp = JsonConvert.DeserializeObject <JsonResponseModel <RebootInstancesResponse> >(strResp); } catch (JsonSerializationException e) { throw new TencentCloudSDKException(e.Message); } return(rsp.Response); }
public string RebbotInstance(IAmazonEC2 ec2, string instidstr) //stop ec2 instance { RebootInstancesRequest rebootreq; RebootInstancesResponse rebootInstancesResponse = null; try { rebootreq = new RebootInstancesRequest { InstanceIds = new List <string>() { instidstr } }; rebootInstancesResponse = ec2.RebootInstances(rebootreq); return("Done"); } catch (Exception ex) { return(ex.Message + "\n" + ex.StackTrace); } }
/// <summary> /// Reboot Instances /// </summary> /// <param name="request">Reboot Instances request</param> /// <returns>Reboot Instances Response from the service</returns> /// <remarks> /// The RebootInstances operation requests a reboot of one or more instances. This /// operation is asynchronous; it only queues a request to reboot the specified /// instance(s). The operation will succeed if the instances are valid and belong /// to the user. Requests to reboot terminated instances are ignored. /// /// </remarks> public RebootInstancesResponse RebootInstances(RebootInstancesRequest request) { return(Invoke <RebootInstancesResponse>("RebootInstancesResponse.xml")); }