List of usage examples for com.amazonaws.services.ec2 AmazonEC2Client describeSpotInstanceRequests
@Override
public DescribeSpotInstanceRequestsResult describeSpotInstanceRequests(
DescribeSpotInstanceRequestsRequest request)
Describes the specified Spot Instance requests.
From source file:n3phele.factory.ec2.VirtualServerResource.java
License:Open Source License
/** * Updates virtual server object and data store state * // w w w . java 2 s .c o m * @param item * object to update * @param reference * reference UUID used for notifications * @param sequence * notification sequence number */ private void updateVirtualServer(VirtualServer item, UUID reference, int sequence) throws IllegalArgumentException { AmazonEC2Client client = null; client = getEC2Client(item.getAccessKey(), item.getEncryptedKey(), item.getLocation()); String instanceId = item.getInstanceId(); boolean madeIntoZombie = item.isZombie(); if (!madeIntoZombie && (instanceId == null || instanceId.length() == 0)) { String spotId = item.getSpotId(); if (spotId == null || spotId.length() == 0) { if ("Initializing".equals(item.getStatus())) { // Update has been called on an item under construction -- // ignore log.warning("Ignoring partially formed item " + item.getUri()); return; } else { log.severe("Improper item " + item); } } DescribeSpotInstanceRequestsResult update = null; try { update = client.describeSpotInstanceRequests( new DescribeSpotInstanceRequestsRequest().withSpotInstanceRequestIds(spotId)); } catch (AmazonServiceException e) { log.log(Level.WARNING, "EC2 error " + e.getErrorCode(), e); throw new WebApplicationException(e, Status.BAD_REQUEST); } instanceId = update.getSpotInstanceRequests().get(0).getInstanceId(); item.setInstanceId(instanceId); // // After cleaning up an object, we leave it around with status == // TERMINATED // for one refresh so that others can see the state chance via // polling. // If we come across an object with state terminated, and the // underlying object // is canceled or closed, then this is the second time the item has // been // refreshed, so now we remove the object. // if ((instanceId == null || instanceId.length() == 0) // pure spot // request && item.getStatus().equals(InstanceStateName.Terminated.toString()) && (update.getSpotInstanceRequests().get(0).getState().equals("cancelled") || update.getSpotInstanceRequests().get(0).getState().equals("closed"))) { delete(item); return; } updateStatus(item, update.getSpotInstanceRequests().get(0).getState(), reference, sequence); update(item); if (item.getStatus().equals("cancelled")) { log.warning("Spot Instance " + item.getUri() + " cancelled .. purging"); try { deleteInstance(item, reference, sequence); // will set // object to // terminated } catch (Exception e) { log.log(Level.SEVERE, "Failed to clean up cancelled spot instance", e); updateStatus(item, InstanceStateName.Terminated.toString(), reference, sequence); delete(item); return; } } if (item.getStatus().equals("closed") && ((item.getInstanceId() == null) || (item.getInstanceId().length() == 0))) { log.warning("Spot Instance " + item.getUri() + " not fulfilled .. purging"); updateStatus(item, InstanceStateName.Terminated.toString(), reference, sequence); delete(item); return; } } if (madeIntoZombie) { if (updateStatus(item, "terminated", reference, sequence)) update(item); if (item.getStatus().equals("terminated")) { log.warning("Instance " + item.getName() + " terminated .. purging"); delete(item); return; } } else if (instanceId != null && instanceId.length() > 0) { DescribeInstancesResult update = client .describeInstances(new DescribeInstancesRequest().withInstanceIds(item.getInstanceId())); if (update.getReservations() != null && update.getReservations().size() > 0) { Instance ec2Instance = update.getReservations().get(0).getInstances().get(0); String newStatus = ec2Instance.getState().getName(); if (!item.getStatus().equals(InstanceStateName.Running.toString()) && InstanceStateName.Running.toString().equals(newStatus)) { item.setOutputParameters(Extractor.extract(ec2Instance)); try { client.createTags(new CreateTagsRequest().withResources(ec2Instance.getInstanceId()) .withTags(new Tag("Name", item.getName()), new Tag("n3phele-factory", Resource.get("factoryName", "ec2Factory")), new Tag("n3phele-uri", item.getUri().toString()))); } catch (Exception ex) { log.log(Level.WARNING, "Cant set tag for " + ec2Instance.getInstanceId() + " associated with " + item.getName(), ex); } } if (updateStatus(item, newStatus, reference, sequence)) update(item); if (item.getStatus().equals("terminated")) { log.warning("Instance " + ec2Instance.getInstanceId() + " terminated .. purging"); delete(item); return; } } else { log.warning("Instance " + item.getInstanceId() + " not found, assumed terminated .. purging"); updateStatus(item, InstanceStateName.Terminated.toString(), reference, sequence); delete(item); return; } } }
From source file:n3phele.factory.ec2.VirtualServerResource.java
License:Open Source License
private void cancelSpotVMRequest(VirtualServer item) throws Exception { String spotId = item.getSpotId(); try {//from w ww . j av a 2 s .c o m if (spotId != null && spotId.length() > 0) { AmazonEC2Client client = null; client = getEC2Client(item.getAccessKey(), item.getEncryptedKey(), item.getLocation()); try { if (item.getInstanceId() == null || item.getInstanceId().length() == 0) { DescribeSpotInstanceRequestsResult update = client.describeSpotInstanceRequests( new DescribeSpotInstanceRequestsRequest().withSpotInstanceRequestIds(spotId)); String instanceId = update.getSpotInstanceRequests().get(0).getInstanceId(); item.setInstanceId(instanceId); update(item); } } catch (Exception e) { log.log(Level.WARNING, "Spot request " + spotId + " update failed ", e); } CancelSpotInstanceRequestsRequest request = new CancelSpotInstanceRequestsRequest() .withSpotInstanceRequestIds(spotId); CancelSpotInstanceRequestsResult result = client.cancelSpotInstanceRequests(request); log.info("Cancel spot request " + spotId + " status " + result.getCancelledSpotInstanceRequests().get(0).getState().toString()); } } catch (Exception e) { log.log(Level.SEVERE, "Cleanup delete of spot instance " + spotId, e); throw e; } }