Example usage for com.amazonaws.services.ec2 AmazonEC2Client terminateInstances

List of usage examples for com.amazonaws.services.ec2 AmazonEC2Client terminateInstances

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2Client terminateInstances.

Prototype

@Override
public TerminateInstancesResult terminateInstances(TerminateInstancesRequest request) 

Source Link

Document

Shuts down the specified instances.

Usage

From source file:au.edu.unsw.cse.soc.federatedcloud.deployers.aws.ec2.AWSEC2VMDeploymentWrapper.java

License:Open Source License

@Override
public void undeployResource(String resourceID) throws Exception {
    //Reading the credentials
    Properties properties = new Properties();
    properties.load(this.getClass().getResourceAsStream("/AwsCredentials.properties"));
    String accessKey = properties.getProperty("accessKey");
    String secretKey = properties.getProperty("secretKey-NULL");
    AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonEC2Client cleint = new AmazonEC2Client(credentials);

    TerminateInstancesRequest request = new TerminateInstancesRequest();
    List<String> idList = new ArrayList<String>();
    idList.add(resourceID);/*from www  .  j  ava  2s  .c om*/
    request.setInstanceIds(idList);
    TerminateInstancesResult result = cleint.terminateInstances(request);

}

From source file:com.rmn.qa.aws.AwsVmManager.java

License:Open Source License

/**
 * Terminates the specified instance.//from   ww  w  . j  ava2s  .  co  m
 *
 * @param  instanceId  Id of the instance to terminate
 */
public boolean terminateInstance(final String instanceId) {
    TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest();
    terminateRequest.withInstanceIds(instanceId);

    AmazonEC2Client localClient = getClient();
    if (localClient == null) {
        throw new RuntimeException("The client is not initialized");
    }
    TerminateInstancesResult result;
    try {
        result = localClient.terminateInstances(terminateRequest);
    } catch (AmazonServiceException ase) {
        // If the node was terminated outside of this plugin, handle the error appropriately
        if (ase.getErrorCode().equals("InvalidInstanceID.NotFound")) {
            log.error("Node not found when attempting to remove: " + instanceId);
            return false;
        } else {
            throw ase;
        }
    }
    List<InstanceStateChange> stateChanges = result.getTerminatingInstances();
    boolean terminatedInstance = false;
    for (InstanceStateChange stateChange : stateChanges) {
        if (instanceId.equals(stateChange.getInstanceId())) {
            terminatedInstance = true;

            InstanceState currentState = stateChange.getCurrentState();
            if (currentState.getCode() != 32 && currentState.getCode() != 48) {
                log.error(String.format(
                        "Machine state for id %s should be terminated (48) or shutting down (32) but was %s instead",
                        instanceId, currentState.getCode()));
                return false;
            }
        }
    }

    if (!terminatedInstance) {
        log.error("Matching terminated instance was not found for instance " + instanceId);
        return false;
    }

    log.info(String.format("Node [%s] successfully terminated", instanceId));
    return true;
}

From source file:de.fischer.thotti.ec2.clients.EC2ExecutorITHelper.java

License:Apache License

private void terminateInstances(Region region, List<String> instances) {
    if (instances.isEmpty())
        return;//www. j  a v a2 s .c  om

    AmazonEC2Client client = getAmazonClient();

    client.setEndpoint(region.getEndpoint());

    TerminateInstancesRequest terminateReq = new TerminateInstancesRequest();

    for (String id : instances) {
        if (logger.isDebugEnabled())
            logger.debug("Going to terminate instance {} in region {}.",
                    new Object[] { id, region.getRegionName() });

        terminateReq.withInstanceIds(id);
    }

    client.terminateInstances(terminateReq);
}

From source file:eu.stratosphere.nephele.instance.ec2.FloatingInstance.java

License:Apache License

/**
 * This method checks if this floating instance has reached the end of its life cycle and, if so, terminates
 * itself.//w ww  .  j  av  a  2s.  c  o  m
 */
public boolean hasLifeCycleEnded() {

    final long currentTime = System.currentTimeMillis();
    final long msremaining = this.leasePeriod - ((currentTime - this.launchTime) % this.leasePeriod);

    if (msremaining < TIME_THRESHOLD) {
        // Destroy this instance
        final AmazonEC2Client client = EC2ClientFactory.getEC2Client(this.awsAccessKey, this.awsSecretKey);
        final TerminateInstancesRequest tr = new TerminateInstancesRequest();
        final LinkedList<String> instanceIDlist = new LinkedList<String>();
        instanceIDlist.add(this.instanceID);
        tr.setInstanceIds(instanceIDlist);
        client.terminateInstances(tr);
        return true;
    }

    return false;
}

From source file:pl.edu.agh.samm.tadapter.eucalyptus.EucalyptusTransportAdapter.java

License:Open Source License

private void stopOneInstanceAction(Resource clusterResource, String instanceId) {
    AmazonEC2Client client = ec2Clients.get(clusterResource);

    logger.info("Terminating instance: ");
    TerminateInstancesRequest stopInstancesRequest = new TerminateInstancesRequest();
    List<String> instancesToStop = new LinkedList<String>();
    instancesToStop.add(instanceId);//from   w  w  w .  j  ava 2s.  com
    stopInstancesRequest.setInstanceIds(instancesToStop);

    TerminateInstancesResult result = client.terminateInstances(stopInstancesRequest);
    logger.info("Instances terminated: " + instancesToStop);
}