Search in sources :

Example 1 with RunningInstance

use of org.jclouds.ec2.domain.RunningInstance in project legacy-jclouds-examples by jclouds.

the class MainApp method createSecurityGroupKeyPairAndInstance.

private static RunningInstance createSecurityGroupKeyPairAndInstance(EC2Client client, String name) throws TimeoutException {
    // create a new security group
    createSecurityGroupAndAuthorizePorts(client, name);
    // create a new instance
    RunningInstance instance = runInstance(client, name, name);
    // await for the instance to start
    return blockUntilInstanceRunning(client, instance);
}
Also used : RunningInstance(org.jclouds.ec2.domain.RunningInstance)

Example 2 with RunningInstance

use of org.jclouds.ec2.domain.RunningInstance in project legacy-jclouds-examples by jclouds.

the class MainApp method main.

public static void main(String[] args) throws TimeoutException {
    if (args.length < PARAMETERS)
        throw new IllegalArgumentException(INVALID_SYNTAX);
    // Args
    String accesskeyid = args[0];
    String secretkey = args[1];
    String command = args[2];
    String name = args[3];
    // Init
    RestContext<EC2Client, EC2AsyncClient> context = ContextBuilder.newBuilder("aws-ec2").credentials(accesskeyid, secretkey).build();
    // Get a synchronous client
    EC2Client client = context.getApi();
    try {
        if (command.equals("create")) {
            KeyPair pair = createKeyPair(client, name);
            RunningInstance instance = createSecurityGroupKeyPairAndInstance(client, name);
            System.out.printf("instance %s ready%n", instance.getId());
            System.out.printf("ip address: %s%n", instance.getIpAddress());
            System.out.printf("dns name: %s%n", instance.getDnsName());
            System.out.printf("login identity:%n%s%n", pair.getKeyMaterial());
        } else if (command.equals("destroy")) {
            destroySecurityGroupKeyPairAndInstance(client, name);
        } else {
            throw new IllegalArgumentException(INVALID_SYNTAX);
        }
    } finally {
        // Close connecton
        context.close();
        System.exit(0);
    }
}
Also used : KeyPair(org.jclouds.ec2.domain.KeyPair) RunningInstance(org.jclouds.ec2.domain.RunningInstance) EC2AsyncClient(org.jclouds.ec2.EC2AsyncClient) EC2Client(org.jclouds.ec2.EC2Client)

Example 3 with RunningInstance

use of org.jclouds.ec2.domain.RunningInstance in project legacy-jclouds-examples by jclouds.

the class MainApp method blockUntilInstanceRunning.

static RunningInstance blockUntilInstanceRunning(EC2Client client, RunningInstance instance) throws TimeoutException {
    // create utilities that wait for the instance to finish
    RetryablePredicate<RunningInstance> runningTester = new RetryablePredicate<RunningInstance>(new InstanceStateRunning(client), 180, 5, TimeUnit.SECONDS);
    System.out.printf("%d: %s awaiting instance to run %n", System.currentTimeMillis(), instance.getId());
    if (!runningTester.apply(instance))
        throw new TimeoutException("timeout waiting for instance to run: " + instance.getId());
    instance = findInstanceById(client, instance.getId());
    RetryablePredicate<HostAndPort> socketTester = new RetryablePredicate<HostAndPort>(new InetSocketAddressConnect(), 300, 1, TimeUnit.SECONDS);
    System.out.printf("%d: %s awaiting ssh service to start%n", System.currentTimeMillis(), instance.getIpAddress());
    if (!socketTester.apply(HostAndPort.fromParts(instance.getIpAddress(), 22)))
        throw new TimeoutException("timeout waiting for ssh to start: " + instance.getIpAddress());
    System.out.printf("%d: %s ssh service started%n", System.currentTimeMillis(), instance.getIpAddress());
    System.out.printf("%d: %s awaiting http service to start%n", System.currentTimeMillis(), instance.getIpAddress());
    if (!socketTester.apply(HostAndPort.fromParts(instance.getIpAddress(), 80)))
        throw new TimeoutException("timeout waiting for http to start: " + instance.getIpAddress());
    System.out.printf("%d: %s http service started%n", System.currentTimeMillis(), instance.getIpAddress());
    return instance;
}
Also used : RetryablePredicate(org.jclouds.predicates.RetryablePredicate) HostAndPort(com.google.common.net.HostAndPort) RunningInstance(org.jclouds.ec2.domain.RunningInstance) InetSocketAddressConnect(org.jclouds.predicates.InetSocketAddressConnect) InstanceStateRunning(org.jclouds.ec2.predicates.InstanceStateRunning) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

RunningInstance (org.jclouds.ec2.domain.RunningInstance)3 HostAndPort (com.google.common.net.HostAndPort)1 TimeoutException (java.util.concurrent.TimeoutException)1 EC2AsyncClient (org.jclouds.ec2.EC2AsyncClient)1 EC2Client (org.jclouds.ec2.EC2Client)1 KeyPair (org.jclouds.ec2.domain.KeyPair)1 InstanceStateRunning (org.jclouds.ec2.predicates.InstanceStateRunning)1 InetSocketAddressConnect (org.jclouds.predicates.InetSocketAddressConnect)1 RetryablePredicate (org.jclouds.predicates.RetryablePredicate)1