List of usage examples for com.amazonaws.services.ec2.model TerminateInstancesRequest TerminateInstancesRequest
public TerminateInstancesRequest(java.util.List<String> instanceIds)
From source file:fr.xebia.cloud.amazon.aws.tools.AmazonAwsUtils.java
License:Apache License
/** * <p>/* w w w . ja v a2 s.c o m*/ * Create EC2 instances and ensure these instances are successfully started. * </p> * <p> * Successfully started means they reached the * {@link InstanceStateName#Running} state. * </p> * <p> * If the startup of an instance failed (e.g. * "Server.InternalError: Internal error on launch"), the instance is * terminated and another one is launched. * </p> * <p> * Max retry count: 3. * </p> * * @param runInstancesRequest * @param ec2 * @return list of "Running" created instances. List size is greater or * equals to given {@link RunInstancesRequest#getMinCount()} */ @Nonnull public static List<Instance> reliableEc2RunInstances(@Nonnull RunInstancesRequest runInstancesRequest, @Nonnull AmazonEC2 ec2) { int initialInstanceMinCount = runInstancesRequest.getMinCount(); int initialInstanceMaxCount = runInstancesRequest.getMaxCount(); try { int tryCount = 1; List<Instance> result = ec2.runInstances(runInstancesRequest).getReservation().getInstances(); result = AmazonAwsUtils.awaitForEc2Instances(result, ec2); //Check for instances state while (result.size() < initialInstanceMinCount && tryCount < 3) { runInstancesRequest.setMinCount(initialInstanceMinCount - result.size()); runInstancesRequest.setMaxCount(initialInstanceMinCount - result.size()); List<Instance> instances = ec2.runInstances(runInstancesRequest).getReservation().getInstances(); instances = AmazonAwsUtils.awaitForEc2Instances(instances, ec2); result.addAll(instances); tryCount++; } //Check for SSH availability for (Iterator<Instance> itInstance = result.iterator(); itInstance.hasNext();) { Instance instance = itInstance.next(); try { if (instance.getImageId().equals(InfrastructureCreationStep.GRAPHITE_IMAGE_ID)) { awaitForSshAvailability(instance, "root"); } else { awaitForSshAvailability(instance, "ec2-user"); } } catch (IllegalStateException e) { //Not available => terminate instance ec2.terminateInstances( new TerminateInstancesRequest(Lists.newArrayList(instance.getInstanceId()))); itInstance.remove(); } } if (result.size() < initialInstanceMinCount) { throw new IllegalStateException("Failure to create " + initialInstanceMinCount + " instances, only " + result.size() + " instances (" + Joiner.on(",").join( Collections2.transform(result, AmazonAwsFunctions.EC2_INSTANCE_TO_INSTANCE_ID)) + ") were started on request " + runInstancesRequest); } return result; } finally { // restore runInstancesRequest state runInstancesRequest.setMinCount(initialInstanceMinCount); runInstancesRequest.setMaxCount(initialInstanceMaxCount); } }
From source file:fr.xebia.cloud.amazon.aws.tools.AmazonAwsUtils.java
License:Apache License
/** * <p>/* w ww . j a v a 2s . c o m*/ * Wait for the ec2 instance startup and returns it up to date * </p> * <p> * Note: some information are missing of the {@link Instance} returned by * {@link AmazonEC2#describeInstances(DescribeInstancesRequest)} as long as * the instance is not "running" (e.g. {@link Instance#getPublicDnsName()}). * </p> * * @param instance * @return up to date instances or <code>null</code> if the instance crashed * at startup. */ @Nullable public static Instance awaitForEc2Instance(@Nonnull Instance instance, @Nonnull AmazonEC2 ec2) { logger.trace("Wait for startup of {}: {}", instance.getInstanceId(), instance); try { // initially wait for 3 secs to prevent "InvalidInstanceID.NotFound, AWS Error Message: The instance ID 'i-2f79c967' does not exist" Thread.sleep(3 * 1000); } catch (InterruptedException e) { throw Throwables.propagate(e); } int counter = 0; while (InstanceStateName.Pending.equals(instance.getState()) || (instance.getPublicIpAddress() == null) || (instance.getPublicDnsName() == null)) { logger.trace("Wait for startup of {}: {}", instance.getInstanceId(), instance); try { // 3s because ec2 instance creation < 10 seconds Thread.sleep(3 * 1000); } catch (InterruptedException e) { throw Throwables.propagate(e); } DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() .withInstanceIds(instance.getInstanceId()); DescribeInstancesResult describeInstances = ec2.describeInstances(describeInstancesRequest); instance = Iterables.getOnlyElement(toEc2Instances(describeInstances.getReservations())); counter++; if (counter >= 20) { logger.warn("Timeout waiting for startup of {}: {}", instance); return instance; } } if (InstanceStateName.ShuttingDown.equals(instance.getState()) || InstanceStateName.Terminated.equals(instance.getState())) { // typically a "Server.InternalError: Internal error on launch" logger.warn("Terminate and skip dying instance {} (stateReason={}, stateTransitionReason={}): {}", new Object[] { instance.getInstanceId(), instance.getStateReason(), instance.getStateTransitionReason(), instance }); try { ec2.terminateInstances(new TerminateInstancesRequest(Lists.newArrayList(instance.getInstanceId()))); } catch (Exception e) { logger.warn("Silently ignore exception terminating dying instance {}: {}", new Object[] { instance.getInstanceId(), instance, e }); } return null; } logger.debug("Instance {} is started: {}", instance.getInstanceId(), instance); return instance; }
From source file:fr.xebia.cloud.amazon.aws.tools.AmazonAwsUtils.java
License:Apache License
/** * Terminate EC2 instances matching all the filters given in parameters * * @param ec2 ec2 root object/*from w w w . j a v a 2 s .co m*/ * @param filters all filters to be matched by the searched instances */ private static void terminateInstancesByFilter(AmazonEC2 ec2, List<Filter> filters) { DescribeInstancesRequest describeInstancesWithRoleRequest = new DescribeInstancesRequest(). // withFilters(filters); DescribeInstancesResult describeInstancesResult = ec2.describeInstances(describeInstancesWithRoleRequest); Iterable<Instance> exstingInstances = AmazonAwsUtils .toEc2Instances(describeInstancesResult.getReservations()); List<String> instanceIds = Lists.newArrayList( Iterables.transform(exstingInstances, AmazonAwsFunctions.EC2_INSTANCE_TO_INSTANCE_ID)); if (instanceIds.isEmpty()) { logger.debug("No server tagged with filter '{}' to terminate", filters); } else { logger.info("Terminate servers tagged with filter '{}'", filters); ec2.terminateInstances(new TerminateInstancesRequest(instanceIds)); } }
From source file:fr.xebia.demo.amazon.aws.AmazonAwsShutdowner.java
License:Apache License
public void test() { String ownerId = "self"; boolean dryRun = true; // RETRIEVE TAGS Map<String, Map<String, String>> tagsByResourceId = new MapMaker() .makeComputingMap(new Function<String, Map<String, String>>() { @Override// w w w .jav a 2 s . c om public Map<String, String> apply(String input) { return Maps.newHashMap(); } }); for (TagDescription tagDescription : ec2.describeTags().getTags()) { tagsByResourceId.get(tagDescription.getResourceId()).put(tagDescription.getKey(), tagDescription.getValue()); } // RDS INSTANCEs for (DBInstance dbInstance : rds.describeDBInstances().getDBInstances()) { Map<String, String> instanceTags = tagsByResourceId.get(dbInstance.getDBInstanceIdentifier()); logger.debug("Tags for " + dbInstance + ": " + instanceTags); } // EC2 INSTANCES List<Instance> instancesAlreadyNotStarted = Lists.newArrayList(); List<Instance> instancesToStop = Lists.newArrayList(); List<Instance> instancesToTerminate = Lists.newArrayList(); List<Instance> instancesToKeepUnchanged = Lists.newArrayList(); for (Reservation reservation : ec2.describeInstances().getReservations()) { for (Instance instance : reservation.getInstances()) { Map<String, String> instanceTags = tagsByResourceId.get(instance.getInstanceId()); logger.debug("Tags for {}: {}", instance, instanceTags); if ("terminated".equals(instance.getState().getName())) { instancesToKeepUnchanged.add(instance); } else if (instanceTags.containsKey(TAG_DO_NOT_STOP)) { instancesToKeepUnchanged.add(instance); } else if (instanceTags.containsKey(TAG_DO_NOT_TERMINATE)) { if ("started".equals(instance.getState().getName())) { instancesToStop.add(instance); } else { instancesAlreadyNotStarted.add(instance); } } else { instancesToTerminate.add(instance); } } } System.out.println("EC2 INSTANCES"); if (dryRun) { System.out.println("DRY RUN Terminate:" + instancesToTerminate); System.out.println("DRY RUN Stop:" + instancesToStop); System.out.println("DRY RUN No need to stop:" + instancesAlreadyNotStarted); System.out.println("DRY RUN Keep unchanged:" + instancesToKeepUnchanged); } else { System.out.println("Terminate:" + instancesToTerminate); if (!instancesToTerminate.isEmpty()) { ec2.terminateInstances(new TerminateInstancesRequest( Lists.transform(instancesToTerminate, TO_INSTANCE_ID_FUNCTION))); } System.out.println("Stop:" + instancesToStop); if (!instancesToStop.isEmpty()) { ec2.stopInstances( new StopInstancesRequest(Lists.transform(instancesToStop, TO_INSTANCE_ID_FUNCTION))); } System.out.println("No need to stop:" + instancesAlreadyNotStarted); System.out.println("Keep unchanged:" + instancesToKeepUnchanged); } // AMIs System.out.println("AMIs"); List<Image> imagesToDeRegister = Lists.newArrayList(); List<Image> imagesToKeep = Lists.newArrayList(); for (Image image : ec2.describeImages(new DescribeImagesRequest().withOwners(ownerId)).getImages()) { Map<String, String> imageTags = tagsByResourceId.get(image.getImageId()); logger.debug("Tags for {}: {}", image, imageTags); if (imageTags.containsKey(TAG_DO_NOT_DEREGISTER)) { imagesToKeep.add(image); } else { imagesToDeRegister.add(image); } } if (dryRun) { System.out.println("DRY RUN Deregister:" + imagesToDeRegister); System.out.println("DRY RUN Keep:" + imagesToKeep); } else { System.out.println("Deregister:" + imagesToDeRegister); for (Image image : imagesToDeRegister) { ec2.deregisterImage(new DeregisterImageRequest(image.getImageId())); } System.out.println("Keep:" + imagesToKeep); } List<String> imageIdsToKeep = Lists.transform(imagesToKeep, TO_IMAGE_ID_FUNCTION); // SNAPSHOTS System.out.println("SNAPSHOTs"); for (Snapshot snapshot : ec2.describeSnapshots(new DescribeSnapshotsRequest().withOwnerIds(ownerId)) .getSnapshots()) { if (snapshot.getDescription().contains("Created by CreateImage")) { boolean associatedWithAnImageToKeep = false; for (String imageIdToKeep : imageIdsToKeep) { if (snapshot.getDescription().contains(imageIdToKeep)) { associatedWithAnImageToKeep = true; break; } } if (associatedWithAnImageToKeep) { System.out.println("Keep: " + snapshot); } else { if (dryRun) { System.out.println("DRY RUN delete: " + snapshot); } else { System.out.println("Delete: " + snapshot); ec2.deleteSnapshot(new DeleteSnapshotRequest(snapshot.getSnapshotId())); } } } } // ELASTIC LOAD BALANCERs // no tags on elb }
From source file:getting_started.InlineGettingStartedCodeSampleApp.java
License:Open Source License
/** * @param args//from ww w .ja va 2s. c o m */ public static void main(String[] args) { //============================================================================================// //=============================== Submitting a Request =======================================// //============================================================================================// // Retrieves the credentials from an AWSCredentials.properties file. AWSCredentials credentials = null; try { credentials = new PropertiesCredentials( InlineGettingStartedCodeSampleApp.class.getResourceAsStream("AwsCredentials.properties")); } catch (IOException e1) { System.out.println("Credentials were not properly entered into AwsCredentials.properties."); System.out.println(e1.getMessage()); System.exit(-1); } // Create the AmazonEC2Client object so we can call various APIs. AmazonEC2 ec2 = new AmazonEC2Client(credentials); // Initializes a Spot Instance Request RequestSpotInstancesRequest requestRequest = new RequestSpotInstancesRequest(); // Request 1 x t1.micro instance with a bid price of $0.03. requestRequest.setSpotPrice("0.03"); requestRequest.setInstanceCount(Integer.valueOf(1)); // Setup the specifications of the launch. This includes the instance type (e.g. t1.micro) // and the latest Amazon Linux AMI id available. Note, you should always use the latest // Amazon Linux AMI id or another of your choosing. LaunchSpecification launchSpecification = new LaunchSpecification(); launchSpecification.setImageId("ami-8c1fece5"); launchSpecification.setInstanceType("t1.micro"); // Add the security group to the request. ArrayList<String> securityGroups = new ArrayList<String>(); securityGroups.add("GettingStartedGroup"); launchSpecification.setSecurityGroups(securityGroups); // Add the launch specifications to the request. requestRequest.setLaunchSpecification(launchSpecification); //============================================================================================// //=========================== Getting the Request ID from the Request ========================// //============================================================================================// // Call the RequestSpotInstance API. RequestSpotInstancesResult requestResult = ec2.requestSpotInstances(requestRequest); List<SpotInstanceRequest> requestResponses = requestResult.getSpotInstanceRequests(); // Setup an arraylist to collect all of the request ids we want to watch hit the running // state. ArrayList<String> spotInstanceRequestIds = new ArrayList<String>(); // Add all of the request ids to the hashset, so we can determine when they hit the // active state. for (SpotInstanceRequest requestResponse : requestResponses) { System.out.println("Created Spot Request: " + requestResponse.getSpotInstanceRequestId()); spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId()); } //============================================================================================// //=========================== Determining the State of the Spot Request ======================// //============================================================================================// // Create a variable that will track whether there are any requests still in the open state. boolean anyOpen; // Initialize variables. ArrayList<String> instanceIds = new ArrayList<String>(); do { // Create the describeRequest with tall of the request id to monitor (e.g. that we started). DescribeSpotInstanceRequestsRequest describeRequest = new DescribeSpotInstanceRequestsRequest(); describeRequest.setSpotInstanceRequestIds(spotInstanceRequestIds); // Initialize the anyOpen variable to false which assumes there are no requests open unless // we find one that is still open. anyOpen = false; try { // Retrieve all of the requests we want to monitor. DescribeSpotInstanceRequestsResult describeResult = ec2 .describeSpotInstanceRequests(describeRequest); List<SpotInstanceRequest> describeResponses = describeResult.getSpotInstanceRequests(); // Look through each request and determine if they are all in the active state. for (SpotInstanceRequest describeResponse : describeResponses) { // If the state is open, it hasn't changed since we attempted to request it. // There is the potential for it to transition almost immediately to closed or // cancelled so we compare against open instead of active. if (describeResponse.getState().equals("open")) { anyOpen = true; break; } // Add the instance id to the list we will eventually terminate. instanceIds.add(describeResponse.getInstanceId()); } } catch (AmazonServiceException e) { // If we have an exception, ensure we don't break out of the loop. // This prevents the scenario where there was blip on the wire. anyOpen = true; } try { // Sleep for 60 seconds. Thread.sleep(60 * 1000); } catch (Exception e) { // Do nothing because it woke up early. } } while (anyOpen); //============================================================================================// //====================================== Canceling the Request ==============================// //============================================================================================// try { // Cancel requests. CancelSpotInstanceRequestsRequest cancelRequest = new CancelSpotInstanceRequestsRequest( spotInstanceRequestIds); ec2.cancelSpotInstanceRequests(cancelRequest); } catch (AmazonServiceException e) { // Write out any exceptions that may have occurred. System.out.println("Error cancelling instances"); System.out.println("Caught Exception: " + e.getMessage()); System.out.println("Reponse Status Code: " + e.getStatusCode()); System.out.println("Error Code: " + e.getErrorCode()); System.out.println("Request ID: " + e.getRequestId()); } //============================================================================================// //=================================== Terminating any Instances ==============================// //============================================================================================// try { // Terminate instances. TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest(instanceIds); ec2.terminateInstances(terminateRequest); } catch (AmazonServiceException e) { // Write out any exceptions that may have occurred. System.out.println("Error terminating instances"); System.out.println("Caught Exception: " + e.getMessage()); System.out.println("Reponse Status Code: " + e.getStatusCode()); System.out.println("Error Code: " + e.getErrorCode()); System.out.println("Request ID: " + e.getRequestId()); } }
From source file:hudson.plugins.ec2.EC2AbstractSlave.java
License:Open Source License
boolean terminateInstance() { try {// ww w . j av a 2s.c o m AmazonEC2 ec2 = getCloud().connect(); TerminateInstancesRequest request = new TerminateInstancesRequest( Collections.singletonList(getInstanceId())); LOGGER.fine("Sending terminate request for " + getInstanceId()); ec2.terminateInstances(request); LOGGER.info("EC2 instance terminate request sent for " + getInstanceId()); return true; } catch (AmazonClientException e) { LOGGER.log(Level.WARNING, "Failed to terminate EC2 instance: " + getInstanceId(), e); return false; } }
From source file:integratedtoolkit.connectors.amazon.EC2.java
License:Apache License
public void poweroff(ResourceDescription rd) throws ConnectorException { String workerIP = rd.getName(); synchronized (stats) { String instanceId = null; synchronized (IPToName) { instanceId = IPToName.remove(workerIP); }/*from w w w. j av a 2 s . c om*/ ArrayList<String> instanceIds = new ArrayList<String>(); instanceIds.add(instanceId); TerminateInstancesRequest tir = new TerminateInstancesRequest(instanceIds); client.terminateInstances(tir); String instanceType = IPToType.remove(workerIP); Long creationTime = IPToStart.remove(workerIP); if (instanceType.compareTo(smallCode) == 0) { smallCount--; accumulatedCost += (((System.currentTimeMillis() - creationTime) / 3600000) + 1) * smallPrice[placement]; } else if (instanceType.compareTo(largeCode) == 0) { largeCount--; accumulatedCost += (((System.currentTimeMillis() - creationTime) / 3600000) + 1) * largePrice[placement]; } else if (instanceType.compareTo(xlargeCode) == 0) { xlargeCount--; accumulatedCost += (((System.currentTimeMillis() - creationTime) / 3600000) + 1) * xlargePrice[placement]; } activeRequests--; } }
From source file:integratedtoolkit.connectors.amazon.EC2.java
License:Apache License
public void destroy(Object worker) throws ConnectorException { RunInstancesResult workerInstance = (RunInstancesResult) worker; synchronized (stats) { String instanceId = null; instanceId = workerInstance.getReservation().getInstances().get(0).getInstanceId(); ArrayList<String> instanceIds = new ArrayList<String>(); instanceIds.add(instanceId);/*w w w . j a v a2 s . co m*/ TerminateInstancesRequest tir = new TerminateInstancesRequest(instanceIds); client.terminateInstances(tir); activeRequests--; } }
From source file:integratedtoolkit.connectors.amazon.EC2_2.java
License:Apache License
private void connectionOnPowerOff(String vmId) throws ConnectorException { try {//from w w w .j a va 2 s . c o m ArrayList<String> instanceIds = new ArrayList<String>(); instanceIds.add(vmId); TerminateInstancesRequest tir = new TerminateInstancesRequest(instanceIds); long now = System.currentTimeMillis(); client.terminateInstances(tir); VM vmInfo; synchronized (this) { vmInfo = vmIdToInfo.get(vmId); } float pricePerHour = VM.getPrice(vmInfo.getType().getCode(), vmInfo.getPlacement()); int numSlots = getNumSlots(now, vmInfo.getStartTime()); currentCostPerHour -= pricePerHour; deletedMachinesCost += numSlots * pricePerHour; //logger.debug("Virtual machine terminated: " + vmInfo); } catch (Exception e) { throw new ConnectorException(e); } }
From source file:io.druid.indexing.coordinator.scaling.EC2AutoScalingStrategy.java
License:Open Source License
@Override public AutoScalingData terminate(List<String> ips) { if (ips.isEmpty()) { return new AutoScalingData(Lists.<String>newArrayList(), Lists.<Instance>newArrayList()); }// w w w . j a v a 2 s . c o m DescribeInstancesResult result = amazonEC2Client.describeInstances( new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", ips))); List<Instance> instances = Lists.newArrayList(); for (Reservation reservation : result.getReservations()) { instances.addAll(reservation.getInstances()); } try { log.info("Terminating instance[%s]", instances); amazonEC2Client.terminateInstances( new TerminateInstancesRequest(Lists.transform(instances, new Function<Instance, String>() { @Override public String apply(Instance input) { return input.getInstanceId(); } }))); return new AutoScalingData(Lists.transform(ips, new Function<String, String>() { @Override public String apply(@Nullable String input) { return String.format("%s:%s", input, config.getWorkerPort()); } }), instances); } catch (Exception e) { log.error(e, "Unable to terminate any instances."); } return null; }