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.dowdandassociates.gentoo.bootstrap.DefaultTestSnapshotProvider.java

License:Apache License

public Optional<Snapshot> get() {
    BootstrapInstanceInformation instanceInfo = bootstrapResultInformation.getInstanceInfo();
    Optional<Instance> instance = instanceInfo.getInstance();
    Optional<Volume> volume = instanceInfo.getVolume();
    Optional<Integer> exitStatus = bootstrapResultInformation.getExitStatus();

    if (!instance.isPresent()) {
        log.info("Instance is absent");
        return Optional.absent();
    }/*from w  ww .ja  va 2 s .  co m*/

    String instanceId = instance.get().getInstanceId();

    TerminateInstancesResult terminateInstancesResult = ec2Client
            .terminateInstances(new TerminateInstancesRequest().withInstanceIds(instanceId));

    if (!volume.isPresent()) {
        log.info("Volume is absent");
        return Optional.absent();
    }

    String volumeId = volume.get().getVolumeId();

    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
            .withInstanceIds(instanceId);

    DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest().withVolumeIds(volumeId);
    try {
        while (true) {
            log.info("Sleeping for " + instanceSleep.get() + " ms");
            Thread.sleep(instanceSleep.get());
            DescribeInstancesResult describeInstancesResult = ec2Client
                    .describeInstances(describeInstancesRequest);
            String state = describeInstancesResult.getReservations().get(0).getInstances().get(0).getState()
                    .getName();
            log.info("Instance State = " + state);
            if ("terminated".equals(state)) {
                break;
            }
        }

        CreateSnapshotResult createSnapshotResult = ec2Client
                .createSnapshot(new CreateSnapshotRequest().withVolumeId(volumeId));

        log.info("SnapshotId = " + createSnapshotResult.getSnapshot().getSnapshotId());

        DescribeSnapshotsRequest describeSnapshotsRequest = new DescribeSnapshotsRequest()
                .withSnapshotIds(createSnapshotResult.getSnapshot().getSnapshotId());

        Snapshot snapshot;

        while (true) {
            log.info("Sleeping for " + snapshotSleep.get() + " ms");
            Thread.sleep(snapshotSleep.get());

            DescribeSnapshotsResult describeSnapshotsResult = ec2Client
                    .describeSnapshots(describeSnapshotsRequest);
            String state = describeSnapshotsResult.getSnapshots().get(0).getState();
            log.info("Snapshot State = " + state);
            if ("error".equals(state)) {
                return Optional.absent();
            }
            if ("completed".equals(state)) {
                snapshot = describeSnapshotsResult.getSnapshots().get(0);
                break;
            }
        }

        ec2Client.deleteVolume(new DeleteVolumeRequest().withVolumeId(volumeId));

        if (!exitStatus.isPresent()) {
            log.info("Exit status is not present");
            return Optional.absent();
        }

        log.info("exit status = " + exitStatus.get());

        if (0 != exitStatus.get()) {
            return Optional.absent();
        }

        return Optional.fromNullable(snapshot);
    } catch (InterruptedException e) {
        return Optional.absent();
    }
}

From source file:com.eucalyptus.tests.awssdk.CloudCleaner.java

License:Open Source License

@Test
public void clean() throws Exception {
    testInfo(this.getClass().getSimpleName());
    getCloudInfo();/*from  ww w .  j a  v  a  2  s .  c o m*/

    //Terminate All instances
    List<String> instancesToTerminate = new ArrayList<String>();
    DescribeInstancesResult result = ec2.describeInstances();
    List<Reservation> reservations = result.getReservations();
    if (reservations.size() > 0) {
        print("Found instances to terminate");
        for (Reservation reservation : reservations) {
            List<Instance> instances = reservation.getInstances();
            for (Instance instance : instances) {
                print("Terminating: " + instance.getInstanceId());
                instancesToTerminate.add(instance.getInstanceId());
            }
        }
        TerminateInstancesRequest term = new TerminateInstancesRequest();
        term.setInstanceIds(instancesToTerminate);
        ec2.terminateInstances(term);
    } else {
        print("No instances found");
    }

    // delete all keypairs
    if (getKeyPairCount() > 0) {
        print("Found Keypairs to delete");
        DescribeKeyPairsResult describeKeyPairsResult = ec2.describeKeyPairs();
        for (KeyPairInfo keypair : describeKeyPairsResult.getKeyPairs()) {
            deleteKeyPair(keypair.getKeyName());
        }
    } else {
        print("No keypairs found");
    }

    // delete all groups except default group
    List<SecurityGroup> groups = describeSecurityGroups();
    if (groups.size() > 1) {
        print("Found security groups to delete");
        for (SecurityGroup group : groups) {
            if (!group.getGroupName().equals("default")) {
                deleteSecurityGroup(group.getGroupName());
            }
        }
    } else {
        print("No Security Groups found (other than default)");
    }

    // delete all policies
    List<ScalingPolicy> policies = describePolicies();
    if (policies.size() > 0) {
        print("Found Policies to delete");
        for (ScalingPolicy policy : policies) {
            deletePolicy(policy.getPolicyName());
        }
    } else {
        print("No auto scaling policies found");
    }

    // delete launch configs
    List<LaunchConfiguration> lcs = describeLaunchConfigs();
    if (lcs.size() > 0) {
        print("Found Launch Configs to delete");
        for (LaunchConfiguration lc : lcs) {
            deleteLaunchConfig(lc.getLaunchConfigurationName());
        }
    } else {
        print("No launch configs found");
    }

    // delete autoscaling groups
    List<AutoScalingGroup> asGroups = describeAutoScalingGroups();
    if (asGroups.size() > 0) {
        print("Found Auto Scaling Groups to delete");
        for (AutoScalingGroup asg : asGroups) {
            deleteAutoScalingGroup(asg.getAutoScalingGroupName(), true);
        }
    } else {
        print("No auto scaling groups found");
    }

    // delete volumes
    List<Volume> volumes = ec2.describeVolumes().getVolumes();
    if (volumes.size() > 0) {
        print("Found volumes to delete");
        for (Volume vol : volumes) {
            deleteVolume(vol.getVolumeId());
        }
    } else {
        print("No volumes found");
    }

    //delete snapshots
    List<Snapshot> snapshots = ec2.describeSnapshots().getSnapshots();
    if (snapshots.size() > 0) {
        print("Found snapshots to delete");
        for (Snapshot snap : snapshots) {
            deleteSnapshot(snap.getSnapshotId());
        }
    } else {
        print("No volumes found");
    }
}

From source file:com.github.trask.sandbox.ec2.Ec2Service.java

License:Apache License

public void terminateInstanceForName(String name) {
    Instance instance = getInstanceForName(name);
    if (instance == null) {
        return;/*  w  w  w .  j av a2s  . c  o  m*/
    }
    TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest()
            .withInstanceIds(instance.getInstanceId());
    logger.debug("terminateInstanceForName(): calling ec2.terminateInstances({})", instance.getInstanceId());
    ec2.terminateInstances(terminateInstancesRequest);
}

From source file:com.github.trask.sandbox.ec2.Ec2Service.java

License:Apache License

public void terminateInstanceForId(String instanceId) {
    TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest()
            .withInstanceIds(instanceId);
    logger.debug("terminateInstanceForId(): calling ec2.terminateInstances({})", instanceId);
    ec2.terminateInstances(terminateInstancesRequest);
}

From source file:com.hazelcast.simulator.provisioner.AwsProvisioner.java

License:Open Source License

private void terminateInstances(List<Instance> instances) {
    List<String> ids = new ArrayList<String>();
    for (Instance instance : instances) {
        ids.add(instance.getInstanceId());
    }/*from www .  j  a  v a2  s  .  c  o m*/

    TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest();
    terminateInstancesRequest.withInstanceIds(ids);
    ec2.terminateInstances(terminateInstancesRequest);
}

From source file:com.hpcloud.daas.ec2.AwsConsoleApp.java

License:Open Source License

public static void TerminateInstance(String instanceId) {
    try {//from w w  w.j  av  a 2  s. c  o m
        TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest();

        List<String> instanceIds = new ArrayList<String>();
        instanceIds.add(instanceId);
        terminateRequest.setInstanceIds(instanceIds);

        TerminateInstancesResult result = ec2.terminateInstances(terminateRequest);

        System.out.println(result);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Reponse Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());
    }
}

From source file:com.liferay.amazontools.AMIBuilder.java

License:Open Source License

protected void terminateInstance(String instanceId) {
    TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest();

    List<String> instanceIds = terminateInstancesRequest.getInstanceIds();

    instanceIds.add(instanceId);//from   www  .  j  a  va2 s.com

    TerminateInstancesResult terminateInstancesResult = amazonEC2Client
            .terminateInstances(terminateInstancesRequest);

    List<InstanceStateChange> instanceStateChanges = terminateInstancesResult.getTerminatingInstances();

    if (!instanceStateChanges.isEmpty()) {
        System.out.println("Terminated instance " + instanceId);
    } else {
        System.out.println("Unable to terminate instance " + instanceId);
    }
}

From source file:com.lunabeat.dooper.HadoopCluster.java

License:Apache License

public TerminateInstancesResult terminateCluster() {
    update();//from w w  w . jav  a  2  s .c  om
    ArrayList<String> iids = new ArrayList<String>();
    if (_master != null) {
        iids.add(_master.getInstance().getInstanceId());
    }
    for (ClusterInstance ci : _slaves) {
        iids.add(ci.getInstance().getInstanceId());
    }
    if (iids.size() < 1) {
        return null;
    }
    TerminateInstancesRequest tir = new TerminateInstancesRequest().withInstanceIds(iids);
    return _ec2.terminateInstances(tir);
}

From source file:com.lunabeat.dooper.HadoopCluster.java

License:Apache License

public TerminateInstancesResult terminateMaster() {
    update();/*from  w w w. ja  v a  2s.co  m*/

    if (_master == null) {
        return null;
    }
    TerminateInstancesRequest tir;
    tir = new TerminateInstancesRequest().withInstanceIds(_master.getInstance().getInstanceId());
    return _ec2.terminateInstances(tir);

}

From source file:com.lunabeat.dooper.HadoopCluster.java

License:Apache License

/**
 *
 * @param howMany/*from w  ww  .j av a2 s  .  c om*/
 * @return result of aws call to terminate
 */
public TerminateInstancesResult terminateSlaves(int howMany) {
    update();
    int terminated = 0;
    ArrayList<String> iids = new ArrayList<String>();
    for (ClusterInstance slave : _slaves) {
        InstanceStateName state = InstanceStateName.fromValue(slave.getInstance().getState().getName());
        if (terminated < howMany
                && (state == InstanceStateName.Running || state == InstanceStateName.Pending)) {
            iids.add(slave.getInstance().getInstanceId());
            terminated++;
        }

    }
    if (iids.size() < 1) {
        return null;
    }
    TerminateInstancesRequest tir = new TerminateInstancesRequest().withInstanceIds(iids);
    return _ec2.terminateInstances(tir);
}