Search in sources :

Example 6 with Template

use of org.jclouds.compute.domain.Template in project whirr by apache.

the class ZooKeeperService method launchCluster.

@Override
public ZooKeeperCluster launchCluster(ClusterSpec clusterSpec) throws IOException {
    ComputeServiceContext computeServiceContext = ComputeServiceContextBuilder.build(clusterSpec);
    ComputeService computeService = computeServiceContext.getComputeService();
    byte[] bootScript = RunUrlBuilder.runUrls("sun/java/install", "apache/zookeeper/install");
    TemplateBuilder templateBuilder = computeService.templateBuilder().osFamily(UBUNTU).options(runScript(bootScript).installPrivateKey(clusterSpec.readPrivateKey()).authorizePublicKey(clusterSpec.readPublicKey()));
    // TODO extract this logic elsewhere
    if (clusterSpec.getProvider().equals("ec2"))
        templateBuilder.imageNameMatches(".*10\\.?04.*").osDescriptionMatches("^ubuntu-images.*").architecture(Architecture.X86_32);
    Template template = templateBuilder.build();
    InstanceTemplate instanceTemplate = clusterSpec.getInstanceTemplate(ZOOKEEPER_ROLE);
    checkNotNull(instanceTemplate);
    int ensembleSize = instanceTemplate.getNumberOfInstances();
    Set<? extends NodeMetadata> nodeMap;
    try {
        nodeMap = computeService.runNodesWithTag(clusterSpec.getClusterName(), ensembleSize, template);
    } catch (RunNodesException e) {
        // TODO: can we do better here - proceed if ensemble is big enough?
        throw new IOException(e);
    }
    FirewallSettings.authorizeIngress(computeServiceContext, nodeMap, clusterSpec, CLIENT_PORT);
    List<NodeMetadata> nodes = Lists.newArrayList(nodeMap);
    // Pass list of all servers in ensemble to configure script.
    // Position is significant: i-th server has id i.
    String servers = Joiner.on(' ').join(getPrivateIps(nodes));
    byte[] configureScript = RunUrlBuilder.runUrls("apache/zookeeper/post-configure " + servers);
    try {
        computeService.runScriptOnNodesMatching(runningWithTag(clusterSpec.getClusterName()), configureScript);
    } catch (RunScriptOnNodesException e) {
        // TODO: retry
        throw new IOException(e);
    }
    String hosts = Joiner.on(',').join(getHosts(nodes));
    return new ZooKeeperCluster(getInstances(nodes), hosts);
}
Also used : TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) IOException(java.io.IOException) ComputeService(org.jclouds.compute.ComputeService) Template(org.jclouds.compute.domain.Template) InstanceTemplate(org.apache.whirr.service.ClusterSpec.InstanceTemplate) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) RunNodesException(org.jclouds.compute.RunNodesException) RunScriptOnNodesException(org.jclouds.compute.RunScriptOnNodesException) InstanceTemplate(org.apache.whirr.service.ClusterSpec.InstanceTemplate)

Example 7 with Template

use of org.jclouds.compute.domain.Template in project hive by apache.

the class TestCloudExecutionContextProvider method setup.

@Before
public void setup() throws Exception {
    dataDir = baseDir.newFolder().getAbsolutePath();
    workingDir = baseDir.newFolder().getAbsolutePath();
    cloudComputeService = mock(CloudComputeService.class);
    sshCommandExecutor = new MockSSHCommandExecutor(LOG);
    node1 = mock(NodeMetadata.class);
    node2 = mock(NodeMetadata.class);
    node3 = mock(NodeMetadata.class);
    template = mock(Template.class);
    when(template.getLocation()).thenReturn(mock(Location.class));
    when(template.getImage()).thenReturn(mock(Image.class));
    when(template.getHardware()).thenReturn(mock(Hardware.class));
    when(node1.getHostname()).thenReturn("node1");
    when(node1.getPublicAddresses()).thenReturn(Collections.singleton("1.1.1.1"));
    when(node2.getHostname()).thenReturn("node2");
    when(node2.getPublicAddresses()).thenReturn(Collections.singleton("1.1.1.2"));
    when(node3.getHostname()).thenReturn("node3");
    when(node3.getPublicAddresses()).thenReturn(Collections.singleton("1.1.1.3"));
    runNodesException = new RunNodesException("", 2, template, Collections.singleton(node1), Collections.<String, Exception>emptyMap(), Collections.singletonMap(node2, new Exception("For testing")));
}
Also used : NodeMetadata(org.jclouds.compute.domain.NodeMetadata) MockSSHCommandExecutor(org.apache.hive.ptest.execution.MockSSHCommandExecutor) RunNodesException(org.jclouds.compute.RunNodesException) Hardware(org.jclouds.compute.domain.Hardware) Image(org.jclouds.compute.domain.Image) RunNodesException(org.jclouds.compute.RunNodesException) Template(org.jclouds.compute.domain.Template) Location(org.jclouds.domain.Location) Before(org.junit.Before)

Example 8 with Template

use of org.jclouds.compute.domain.Template 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 9 with Template

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

the class CreateVolumeAndAttach method createServer.

private NodeMetadata createServer() throws RunNodesException, TimeoutException {
    Template template = compute.templateBuilder().locationId(Constants.ZONE).osDescriptionMatches(".*CentOS 6.2.*").minRam(512).build();
    System.out.println("Create Server");
    Set<? extends NodeMetadata> nodes = compute.createNodesInGroup(Constants.NAME, 1, template);
    NodeMetadata nodeMetadata = nodes.iterator().next();
    String publicAddress = nodeMetadata.getPublicAddresses().iterator().next();
    // We set the password to something we know so we can login in the DetachVolume example
    nova.getApi().getServerApiForZone(Constants.ZONE).changeAdminPass(nodeMetadata.getProviderId(), Constants.PASSWORD);
    System.out.println("  " + nodeMetadata);
    System.out.println("  Login: ssh " + nodeMetadata.getCredentials().getUser() + "@" + publicAddress);
    System.out.println("  Password: " + Constants.PASSWORD);
    return nodeMetadata;
}
Also used : NodeMetadata(org.jclouds.compute.domain.NodeMetadata) Template(org.jclouds.compute.domain.Template)

Example 10 with Template

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

the class CreateServer method createServer.

/**
    * Create a server based on a Template. This method uses Template.fromHardware() and Template.fromImage() to
    * also demonstrate iterating through Hardware and Images. Alternatively you do the same without iterating
    * using the following Template.
    * 
    * Template template = compute.templateBuilder()
    *     .locationId(getLocationId())
    *     .osFamily(OsFamily.UBUNTU)
    *     .osVersionMatches("12.04")
    *     .minRam(512)
    *     .build();
    */
private void createServer() throws RunNodesException, TimeoutException {
    Template template = compute.templateBuilder().locationId(getLocationId()).fromHardware(getHardware()).fromImage(getImage()).build();
    System.out.println("Create Server");
    // This method will continue to poll for the server status and won't return until this server is ACTIVE
    // If you want to know what's happening during the polling, enable logging. See
    // /jclouds-example/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java
    Set<? extends NodeMetadata> nodes = compute.createNodesInGroup(Constants.NAME, 1, template);
    NodeMetadata nodeMetadata = nodes.iterator().next();
    String publicAddress = nodeMetadata.getPublicAddresses().iterator().next();
    System.out.println("  " + nodeMetadata);
    System.out.println("  Login: ssh " + nodeMetadata.getCredentials().getUser() + "@" + publicAddress);
    System.out.println("  Password: " + nodeMetadata.getCredentials().getPassword());
}
Also used : NodeMetadata(org.jclouds.compute.domain.NodeMetadata) Template(org.jclouds.compute.domain.Template)

Aggregations

NodeMetadata (org.jclouds.compute.domain.NodeMetadata)10 Template (org.jclouds.compute.domain.Template)10 RunNodesException (org.jclouds.compute.RunNodesException)7 IOException (java.io.IOException)5 ComputeService (org.jclouds.compute.ComputeService)4 InstanceTemplate (org.apache.whirr.service.ClusterSpec.InstanceTemplate)3 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)3 TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)3 RunScriptOnNodesException (org.jclouds.compute.RunScriptOnNodesException)2 RetryablePredicate (org.jclouds.predicates.RetryablePredicate)2 Predicate (com.google.common.base.Predicate)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 InetAddress (java.net.InetAddress)1 Properties (java.util.Properties)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 Nullable (javax.annotation.Nullable)1 MockSSHCommandExecutor (org.apache.hive.ptest.execution.MockSSHCommandExecutor)1