Example usage for com.amazonaws.services.ec2.model InstanceStateName toString

List of usage examples for com.amazonaws.services.ec2.model InstanceStateName toString

Introduction

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

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:jenkins.plugins.ec2slave.EC2ImageLaunchWrapper.java

License:Open Source License

/**
 * Creates an EC2 instance given an AMI, readying it to serve as a Jenkins slave once launch is called later
 *
 * @param logger where to write messages so the user will see them in the tailed log in Jenkins
 *
 * @throws InterruptedException if the check status wait fails
 */// ww  w.  j av a2s.  co m
public void preLaunch(PrintStream logger) throws InterruptedException {

    logger.println("Starting EC2 instance [" + instanceId + "]...");
    if (testMode)
        return;

    int retries = 0;
    InstanceStateName state = null;

    while (++retries <= maxRetries) {

        logger.println(MessageFormat.format("Checking state of instance [{0}]...", instanceId));

        state = getInstanceState(instanceId);

        logger.println(MessageFormat.format("State of instance [{0}] is [{1}]", instanceId, state.toString()));
        if (state == Running) {
            logger.println(MessageFormat.format(
                    "Instance [{0}] is running, proceeding to launching Jenkins on this instance", instanceId));
            return;
        } else if (state == Pending) {
            logger.println(
                    MessageFormat.format("Instance [{0}] is pending, waiting for [{1}] seconds before retrying",
                            instanceId, retryIntervalSeconds));
            Thread.sleep(retryIntervalSeconds * 1000);
        } else if (state == Stopped) {
            startInstance(logger);
        } else {
            String msg = MessageFormat.format(
                    "Instance [{0}] encountered unexpected state [{1}]. Aborting launch", instanceId,
                    state.toString());
            logger.println(msg);
            throw new IllegalStateException(msg);
        }
    }
    throw new IllegalStateException("Maximum Number of retries " + maxRetries + " exceeded. Aborting launch");
}

From source file:org.apache.usergrid.chop.api.store.amazon.EC2InstanceManager.java

License:Apache License

/**
 * @param name  Causes the method to return instances with given Name tag, give null if you want to get
 *              instances with all names
 * @param state Causes the method to return instances with given state, give null if you want to get instances in
 *              all states/* ww  w  .  j  ava 2  s .c  om*/
 * @return      all instances that satisfy given parameters
 */
protected Collection<com.amazonaws.services.ec2.model.Instance> getEC2Instances(String name,
        InstanceStateName state) {

    Collection<com.amazonaws.services.ec2.model.Instance> instances = new LinkedList<com.amazonaws.services.ec2.model.Instance>();

    DescribeInstancesRequest request = new DescribeInstancesRequest();

    if (name != null) {

        List<String> valuesT1 = new ArrayList<String>();
        valuesT1.add(name);
        Filter filter = new Filter("tag:Name", valuesT1);
        request = request.withFilters(filter);

    }

    if (state != null) {

        List<String> valuesT1 = new ArrayList<String>();
        valuesT1.add(state.toString());
        Filter filter = new Filter("instance-state-name", valuesT1);
        request = request.withFilters(filter);

    }

    DescribeInstancesResult result = null;
    try {
        result = client.describeInstances(request);
    } catch (Exception e) {
        LOG.error("Error while getting instance information from AWS.", e);
        return Collections.EMPTY_LIST;
    }

    for (Reservation reservation : result.getReservations()) {
        for (com.amazonaws.services.ec2.model.Instance in : reservation.getInstances()) {
            instances.add(in);
        }
    }

    return instances;
}