Search in sources :

Example 11 with RunNodesException

use of org.jclouds.compute.RunNodesException in project legacy-jclouds-examples by jclouds.

the class MainApp method main.

public static void main(String[] args) {
    if (args.length < PARAMETERS)
        throw new IllegalArgumentException(INVALID_SYNTAX);
    // Args
    String accesskeyid = args[0];
    String secretkey = args[1];
    String group = args[2];
    String command = args[3];
    // Init
    ComputeService compute = new ComputeServiceContextFactory().createContext("aws-ec2", accesskeyid, secretkey).getComputeService();
    // wait up to 60 seconds for ssh to be accessible
    RetryablePredicate<IPSocket> socketTester = new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 60, 1, 1, TimeUnit.SECONDS);
    try {
        if (command.equals("create")) {
            Template template = compute.templateBuilder().build();
            template.getOptions().as(AWSEC2TemplateOptions.class).spotPrice(0.03f).authorizePublicKey(Files.toString(new File(System.getProperty("user.home") + "/.ssh/id_rsa.pub"), Charsets.UTF_8));
            System.out.printf(">> running one spot node type(%s) with ami(%s) in group(%s)%n", template.getHardware().getProviderId(), template.getImage().getId(), group);
            // run only a single node
            NodeMetadata node = Iterables.getOnlyElement(compute.createNodesInGroup(group, 1, template));
            System.out.printf("<< running node(%s)%n", node.getId());
            IPSocket socket = new IPSocket(Iterables.get(node.getPublicAddresses(), 0), node.getLoginPort());
            if (socketTester.apply(socket)) {
                System.out.printf("<< socket ready [%s] node(%s)%n", socket, node.getId());
                System.out.printf("ssh to node with the following command:%n ssh %s@%s%n", node.getCredentials().identity, socket.getAddress());
                System.exit(0);
            } else {
                System.out.printf("<< socket not ready [%s] node(%s)%n", socket, node.getId());
            }
        } else if (command.equals("destroy")) {
            System.out.printf(">> destroying nodes in group(%s)%n", group);
            Set<? extends NodeMetadata> destroyed = compute.destroyNodesMatching(NodePredicates.inGroup(group));
            System.out.printf("<< destroyed(%d)%n", destroyed.size());
            System.exit(0);
        } else {
            System.err.println(INVALID_SYNTAX);
            System.exit(1);
        }
    } catch (RunNodesException e) {
        System.err.println(e.getMessage());
        for (NodeMetadata node : e.getNodeErrors().keySet()) compute.destroyNode(node.getId());
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } finally {
        compute.getContext().close();
    }
}
Also used : RetryablePredicate(org.jclouds.predicates.RetryablePredicate) Set(java.util.Set) InetSocketAddressConnect(org.jclouds.predicates.InetSocketAddressConnect) IOException(java.io.IOException) ComputeService(org.jclouds.compute.ComputeService) ComputeServiceContextFactory(org.jclouds.compute.ComputeServiceContextFactory) IPSocket(org.jclouds.net.IPSocket) Template(org.jclouds.compute.domain.Template) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) RunNodesException(org.jclouds.compute.RunNodesException) File(java.io.File)

Example 12 with RunNodesException

use of org.jclouds.compute.RunNodesException in project camel by apache.

the class JcloudsComputeProducer method createNode.

/**
     * Create a node with the specified group.
     */
protected void createNode(Exchange exchange) throws CamelException {
    String group = getGroup(exchange);
    String imageId = getImageId(exchange);
    String locationId = getLocationId(exchange);
    String hardwareId = getHardwareId(exchange);
    if (ObjectHelper.isEmpty(group)) {
        throw new CamelExchangeException("Group must be specific in the URI or as exchange property for the destroy node operation.", exchange);
    }
    TemplateBuilder builder = computeService.templateBuilder();
    builder.any();
    if (ObjectHelper.isNotEmpty(locationId)) {
        builder.locationId(locationId);
    }
    if (ObjectHelper.isNotEmpty(imageId)) {
        builder.imageId(imageId);
    }
    if (ObjectHelper.isNotEmpty(hardwareId)) {
        builder.hardwareId(hardwareId);
    }
    try {
        Set<? extends NodeMetadata> nodeMetadatas = computeService.createNodesInGroup(group, 1, builder.build());
        exchange.getOut().setBody(nodeMetadatas);
        exchange.getOut().setHeaders(exchange.getIn().getHeaders());
    } catch (RunNodesException e) {
        throw new CamelExchangeException("Error creating jclouds node.", exchange, e);
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) RunNodesException(org.jclouds.compute.RunNodesException) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder)

Aggregations

RunNodesException (org.jclouds.compute.RunNodesException)12 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)11 Template (org.jclouds.compute.domain.Template)7 TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)7 ComputeService (org.jclouds.compute.ComputeService)6 IOException (java.io.IOException)5 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)4 RunScriptOnNodesException (org.jclouds.compute.RunScriptOnNodesException)4 Statement (org.jclouds.scriptbuilder.domain.Statement)4 File (java.io.File)3 InstanceTemplate (org.apache.whirr.service.ClusterSpec.InstanceTemplate)3 LoginCredentials (org.jclouds.domain.LoginCredentials)3 Properties (java.util.Properties)2 RunListBuilder (org.jclouds.chef.util.RunListBuilder)2 Builder.overrideLoginCredentials (org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginCredentials)2 RetryablePredicate (org.jclouds.predicates.RetryablePredicate)2 Predicate (com.google.common.base.Predicate)1 ImmutableList (com.google.common.collect.ImmutableList)1 Module (com.google.inject.Module)1 BufferedReader (java.io.BufferedReader)1