Example usage for com.amazonaws.services.ec2.model TerminateInstancesRequest TerminateInstancesRequest

List of usage examples for com.amazonaws.services.ec2.model TerminateInstancesRequest TerminateInstancesRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model TerminateInstancesRequest TerminateInstancesRequest.

Prototype

public TerminateInstancesRequest() 

Source Link

Document

Default constructor for TerminateInstancesRequest object.

Usage

From source file:com.netflix.spinnaker.clouddriver.aws.event.AfterResizeEventHandler.java

License:Apache License

default void terminateInstancesInAutoScalingGroup(Task task, AmazonEC2 amazonEC2,
        AutoScalingGroup autoScalingGroup) {
    String serverGroupName = autoScalingGroup.getAutoScalingGroupName();

    List<String> instanceIds = autoScalingGroup.getInstances().stream().map(Instance::getInstanceId)
            .collect(Collectors.toList());

    int terminatedCount = 0;
    for (List<String> partition : Lists.partition(instanceIds, MAX_SIMULTANEOUS_TERMINATIONS)) {
        try {/*from ww w .  j  a  va2 s. co m*/
            terminatedCount += partition.size();
            task.updateStatus(PHASE, String.format("Terminating %d of %d instances in %s", terminatedCount,
                    instanceIds.size(), serverGroupName));
            amazonEC2.terminateInstances(new TerminateInstancesRequest().withInstanceIds(partition));
        } catch (Exception e) {
            task.updateStatus(PHASE,
                    String.format("Unable to terminate instances, reason: '%s'", e.getMessage()));
        }
    }
}

From source file:com.pinterest.arcee.aws.EC2HostInfoDAOImpl.java

License:Apache License

@Override
public void terminateHost(String hostId) throws Exception {
    TerminateInstancesRequest request = new TerminateInstancesRequest();
    request.setInstanceIds(Arrays.asList(hostId));
    try {/*from  www.j a  v  a 2s  .  co  m*/
        ec2Client.terminateInstances(request);
    } catch (AmazonClientException ex) {
        LOG.error(String.format("Failed to call aws terminateInstances whem terminating host %s", hostId), ex);
        throw new DeployInternalException(
                String.format("Failed to call aws terminateInstances whem terminating host %s", hostId), ex);
    }
}

From source file:com.pinterest.clusterservice.cm.AwsVmManager.java

License:Apache License

@Override
public void terminateHosts(String clusterName, Collection<String> hostIds, boolean replaceHost)
        throws Exception {
    if (replaceHost) {
        TerminateInstancesRequest termianteRequest = new TerminateInstancesRequest();
        termianteRequest.setInstanceIds(hostIds);
        try {/* w ww.ja  v  a2 s .c om*/
            ec2Client.terminateInstances(termianteRequest);
        } catch (AmazonClientException e) {
            LOG.error(String.format("Failed to termiante hosts %s: %s", hostIds.toString(), e.getMessage()));
            throw new Exception(
                    String.format("Failed to termiante hosts %s: %s", hostIds.toString(), e.getMessage()));
        }
    } else {
        // Do not replace host and decrease the cluster capacity
        AutoScalingGroup group = getAutoScalingGroup(clusterName);
        if (group == null) {
            LOG.error(String.format("Failed to terminate hosts: auto scaling group %s does not exist",
                    clusterName));
            throw new Exception(String.format("Failed to terminate hosts: auto scaling group %s does not exist",
                    clusterName));
        }

        UpdateAutoScalingGroupRequest updateRequest = new UpdateAutoScalingGroupRequest();
        updateRequest.setAutoScalingGroupName(clusterName);
        updateRequest.setMinSize(Math.max(group.getMinSize() - hostIds.size(), 0));

        for (String hostId : hostIds) {
            TerminateInstanceInAutoScalingGroupRequest terminateRequest = new TerminateInstanceInAutoScalingGroupRequest();
            terminateRequest.setShouldDecrementDesiredCapacity(true);
            terminateRequest.setInstanceId(hostId);
            aasClient.terminateInstanceInAutoScalingGroup(terminateRequest);
        }
    }
}

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

License:Open Source License

/**
 * Terminates the specified instance.//from w w w. ja v  a 2  s.c  o 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:com.urbancode.terraform.tasks.aws.helpers.AWSHelper.java

License:Apache License

/**
 * Will send a request to amazon to terminate all instances in the given list.
 *
 * @param instanceIds/*from w w w . j av a  2s . c  o m*/
 * @param ec2Client
 */
public void terminateInstances(List<String> instanceIds, AmazonEC2 ec2Client) {
    TerminateInstancesRequest request = new TerminateInstancesRequest().withInstanceIds(instanceIds);
    ec2Client.terminateInstances(request);
}

From source file:com.yosanai.java.aws.console.DefaultAWSConnectionProvider.java

License:Open Source License

@Override
public void terminateInstances(String... instanceIds) throws Exception {
    List<String> instanceIdsFiltered = getInstances(STATE_TERMINATED, false, instanceIds);
    if (!instanceIdsFiltered.isEmpty()) {
        TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest();
        terminateInstancesRequest.setInstanceIds(instanceIdsFiltered);
        getConnection().terminateInstances(terminateInstancesRequest);
    }/*w w w .j  a va2s . c o  m*/
}

From source file:com.zotoh.cloudapi.aws.EC2Instance.java

License:Open Source License

@Override
public void terminate(String server) throws InternalException, CloudException {
    tstEStrArg("instance-id", server);
    _svc.getCloud().getEC2().terminateInstances(new TerminateInstancesRequest().withInstanceIds(server));
}

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;/*from  w  ww .  j a  v  a2 s.co m*/

    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:de.fischer.thotti.ec2.clients.EC2Terminator.java

License:Apache License

private void prepareRequestsPerRegion(EC2RequestData<TerminateInstancesRequest> requestData,
        RegionType region) {//from   ww  w . ja  va  2s  .c  om
    TerminateInstancesRequest request = new TerminateInstancesRequest();

    for (InstanceType server : region.getInstances()) {
        request.withInstanceIds(server.getInstanceID());
    }

    requestData.getRegion(region.getName()).addRequest(request);
}

From source file:edu.brandeis.wisedb.aws.VMCreator.java

License:Open Source License

public void terminateVM(VM vm) throws VirtualMachineException {
    TerminateInstancesResult tir = getEC2()
            .terminateInstances(new TerminateInstancesRequest().withInstanceIds(vm.id));
    if (!tir.getTerminatingInstances().get(0).getInstanceId().equals(vm.id)) {
        throw new VirtualMachineException("termination failed. the VM id was not included in the response");
    }//from  w w  w .  ja  v a2s . c  o  m

    int code = tir.getTerminatingInstances().get(0).getCurrentState().getCode();

    // these constants aren't given anywhere... the first is
    // shutting-down, the second is terminated.
    if (code != 32 && code != 48) {
        throw new VirtualMachineException("termination failed.");
    }

}