Search in sources :

Example 1 with RunScriptOnNodesException

use of org.jclouds.compute.RunScriptOnNodesException in project whirr by apache.

the class CassandraService method launchCluster.

@Override
public Cluster launchCluster(ClusterSpec clusterSpec) throws IOException {
    ComputeServiceContext computeServiceContext = ComputeServiceContextBuilder.build(clusterSpec);
    ComputeService computeService = computeServiceContext.getComputeService();
    byte[] bootScript = RunUrlBuilder.runUrls("sun/java/install", "apache/cassandra/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(CASSANDRA_ROLE);
    checkNotNull(instanceTemplate);
    int clusterSize = instanceTemplate.getNumberOfInstances();
    Set<? extends NodeMetadata> nodeMap;
    try {
        nodeMap = computeService.runNodesWithTag(clusterSpec.getClusterName(), clusterSize, template);
    } catch (RunNodesException e) {
        // TODO: can we do better here
        throw new IOException(e);
    }
    FirewallSettings.authorizeIngress(computeServiceContext, nodeMap, clusterSpec, CLIENT_PORT);
    List<NodeMetadata> nodes = Lists.newArrayList(nodeMap);
    List<NodeMetadata> seeds = getSeeds(nodes);
    // Pass list of all servers in cluster to configure script.
    String servers = Joiner.on(' ').join(getPrivateIps(seeds));
    byte[] configureScript = RunUrlBuilder.runUrls("apache/cassandra/post-configure " + servers);
    try {
        Map<? extends NodeMetadata, ExecResponse> responses = computeService.runScriptOnNodesMatching(runningWithTag(clusterSpec.getClusterName()), configureScript);
        assert responses.size() > 0 : "no nodes matched " + clusterSpec.getClusterName();
    } catch (RunScriptOnNodesException e) {
        // TODO: retry
        throw new IOException(e);
    }
    return new Cluster(getInstances(nodes));
}
Also used : ExecResponse(org.jclouds.ssh.ExecResponse) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) Cluster(org.apache.whirr.service.Cluster) 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 2 with RunScriptOnNodesException

use of org.jclouds.compute.RunScriptOnNodesException 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);
    }
    String provider = args[0];
    String identity = args[1];
    String credential = args[2];
    String groupName = args[3];
    Action action = Action.valueOf(args[4].toUpperCase());
    if ((action == Action.CHEF || action == Action.SOLO) && args.length < PARAMETERS + 1) {
        throw new IllegalArgumentException("please provide the list of recipes to install, separated by commas");
    }
    String recipes = action == Action.CHEF || action == Action.SOLO ? args[5] : "apache2";
    String minRam = System.getProperty("minRam");
    // note that you can check if a provider is present ahead of time
    checkArgument(contains(allKeys, provider), "provider %s not in supported list: %s", provider, allKeys);
    LoginCredentials login = action != Action.DESTROY ? getLoginForCommandExecution(action) : null;
    ComputeService compute = initComputeService(provider, identity, credential);
    try {
        switch(action) {
            case ADD:
                System.out.printf(">> adding node to group %s%n", groupName);
                // Default template chooses the smallest size on an operating
                // system that tested to work with java, which tends to be Ubuntu
                // or CentOS
                TemplateBuilder templateBuilder = compute.templateBuilder();
                // can just tweak minRam
                if (minRam != null) {
                    templateBuilder.minRam(Integer.parseInt(minRam));
                }
                // note this will create a user with the same name as you on the
                // node. ex. you can connect via ssh publicip
                Statement bootInstructions = AdminAccess.standard();
                // to run commands as root, we use the runScript option in the
                // template.
                templateBuilder.options(runScript(bootInstructions));
                NodeMetadata node = getOnlyElement(compute.createNodesInGroup(groupName, 1, templateBuilder.build()));
                System.out.printf("<< node %s: %s%n", node.getId(), concat(node.getPrivateAddresses(), node.getPublicAddresses()));
            case SOLO:
                System.out.printf(">> installing [%s] on group %s as %s%n", recipes, groupName, login.identity);
                Iterable<String> recipeList = Splitter.on(',').split(recipes);
                ImmutableList.Builder<Statement> bootstrapBuilder = ImmutableList.builder();
                bootstrapBuilder.add(new InstallGit());
                // Clone community cookbooks into the node
                for (String recipe : recipeList) {
                    bootstrapBuilder.add(CloneGitRepo.builder().repository("git://github.com/opscode-cookbooks/" + recipe + ".git").directory(//
                    "/var/chef/cookbooks/" + recipe).build());
                }
                // Configure Chef Solo to bootstrap the selected recipes
                bootstrapBuilder.add(InstallRuby.builder().build());
                bootstrapBuilder.add(InstallRubyGems.builder().build());
                bootstrapBuilder.add(//
                ChefSolo.builder().cookbookPath(//
                "/var/chef/cookbooks").runlist(//
                RunList.builder().recipes(recipeList).build()).build());
                // Build the statement that will perform all the operations above
                StatementList bootstrap = new StatementList(bootstrapBuilder.build());
                // Run the script in the nodes of the group
                runScriptOnGroup(compute, login, groupName, bootstrap);
                break;
            case CHEF:
                // Create the connection to the Chef server
                ChefService chef = initChefService(System.getProperty("chef.client"), System.getProperty("chef.validator"));
                // Build the runlist for the deployed nodes
                System.out.println("Configuring node runlist in the Chef server...");
                List<String> runlist = new RunListBuilder().addRecipes(recipes.split(",")).build();
                chef.updateRunListForGroup(runlist, groupName);
                Statement chefServerBootstrap = chef.createBootstrapScriptForGroup(groupName);
                // Run the script in the nodes of the group
                System.out.printf(">> installing [%s] on group %s as %s%n", recipes, groupName, login.identity);
                runScriptOnGroup(compute, login, groupName, chefServerBootstrap);
                break;
            case DESTROY:
                System.out.printf(">> destroying nodes in group %s%n", groupName);
                // you can use predicates to select which nodes you wish to
                // destroy.
                Set<? extends NodeMetadata> destroyed = //
                compute.destroyNodesMatching(Predicates.<NodeMetadata>and(not(TERMINATED), inGroup(groupName)));
                System.out.printf("<< destroyed nodes %s%n", destroyed);
                break;
        }
    } catch (RunNodesException e) {
        System.err.println("error adding node to group " + groupName + ": " + e.getMessage());
        error = 1;
    } catch (RunScriptOnNodesException e) {
        System.err.println("error installing " + recipes + " on group " + groupName + ": " + e.getMessage());
        error = 1;
    } catch (Exception e) {
        System.err.println("error: " + e.getMessage());
        error = 1;
    } finally {
        compute.getContext().close();
        System.exit(error);
    }
}
Also used : Statement(org.jclouds.scriptbuilder.domain.Statement) ImmutableList(com.google.common.collect.ImmutableList) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) InstallGit(org.jclouds.scriptbuilder.statements.git.InstallGit) ChefService(org.jclouds.chef.ChefService) ComputeService(org.jclouds.compute.ComputeService) RunNodesException(org.jclouds.compute.RunNodesException) RunScriptOnNodesException(org.jclouds.compute.RunScriptOnNodesException) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) Builder.overrideLoginCredentials(org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginCredentials) LoginCredentials(org.jclouds.domain.LoginCredentials) RunNodesException(org.jclouds.compute.RunNodesException) StatementList(org.jclouds.scriptbuilder.domain.StatementList) RunListBuilder(org.jclouds.chef.util.RunListBuilder) RunScriptOnNodesException(org.jclouds.compute.RunScriptOnNodesException)

Example 3 with RunScriptOnNodesException

use of org.jclouds.compute.RunScriptOnNodesException 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);
    String provider = args[0];
    String identity = args[1];
    String credential = args[2];
    String groupName = args[3];
    Action action = Action.valueOf(args[4].toUpperCase());
    if (action == Action.EXEC && args.length < PARAMETERS + 1)
        throw new IllegalArgumentException("please quote the command to exec as the last parameter");
    String command = (action == Action.EXEC) ? args[5] : "echo hello";
    if (action == Action.RUN && args.length < PARAMETERS + 1)
        throw new IllegalArgumentException("please pass the local file to run as the last parameter");
    File file = null;
    if (action == Action.RUN) {
        file = new File(args[5]);
        if (!file.exists())
            throw new IllegalArgumentException("file must exist! " + file);
    }
    String minRam = System.getProperty("minRam");
    String loginUser = System.getProperty("loginUser", "toor");
    // note that you can check if a provider is present ahead of time
    checkArgument(contains(allKeys, provider), "provider %s not in supported list: %s", provider, allKeys);
    LoginCredentials login = (action != Action.DESTROY) ? getLoginForCommandExecution(action) : null;
    ComputeService compute = initComputeService(provider, identity, credential);
    try {
        switch(action) {
            case ADD:
                System.out.printf(">> adding node to group %s%n", groupName);
                // Default template chooses the smallest size on an operating system
                // that tested to work with java, which tends to be Ubuntu or CentOS
                TemplateBuilder templateBuilder = compute.templateBuilder();
                // just tweak minRam
                if (minRam != null)
                    templateBuilder.minRam(Integer.parseInt(minRam));
                // note this will create a user with the same name as you on the
                // node. ex. you can connect via ssh publicip
                Statement bootInstructions = AdminAccess.standard();
                // to run commands as root, we use the runScript option in the template.
                if (provider.equalsIgnoreCase("virtualbox"))
                    templateBuilder.options(overrideLoginUser(loginUser).runScript(bootInstructions));
                else
                    templateBuilder.options(runScript(bootInstructions));
                NodeMetadata node = getOnlyElement(compute.createNodesInGroup(groupName, 1, templateBuilder.build()));
                System.out.printf("<< node %s: %s%n", node.getId(), concat(node.getPrivateAddresses(), node.getPublicAddresses()));
            case EXEC:
                System.out.printf(">> running [%s] on group %s as %s%n", command, groupName, login.identity);
                // when you run commands, you can pass options to decide whether to
                // run it as root, supply or own credentials vs from cache, and wrap
                // in an init script vs directly invoke
                Map<? extends NodeMetadata, ExecResponse> responses = //
                compute.runScriptOnNodesMatching(// predicate used to select nodes
                inGroup(groupName), // what you actually intend to run
                exec(command), // use my local user &
                overrideLoginCredentials(login).runAsRoot(// don't attempt to run as root (sudo)
                false).wrapInInitScript(// run command directly
                false));
                for (Entry<? extends NodeMetadata, ExecResponse> response : responses.entrySet()) {
                    System.out.printf("<< node %s: %s%n", response.getKey().getId(), concat(response.getKey().getPrivateAddresses(), response.getKey().getPublicAddresses()));
                    System.out.printf("<<     %s%n", response.getValue());
                }
                break;
            case RUN:
                System.out.printf(">> running [%s] on group %s as %s%n", file, groupName, login.identity);
                // when running a sequence of commands, you probably want to have jclouds use the default behavior, 
                // which is to fork a background process.
                responses = //
                compute.runScriptOnNodesMatching(inGroup(groupName), // passing in a string with the contents of the file
                Files.toString(file, Charsets.UTF_8), overrideLoginCredentials(login).runAsRoot(false).nameTask(// ensuring task name isn't
                "_" + file.getName().replaceAll("\\..*", "")));
                for (Entry<? extends NodeMetadata, ExecResponse> response : responses.entrySet()) {
                    System.out.printf("<< node %s: %s%n", response.getKey().getId(), concat(response.getKey().getPrivateAddresses(), response.getKey().getPublicAddresses()));
                    System.out.printf("<<     %s%n", response.getValue());
                }
                break;
            case DESTROY:
                System.out.printf(">> destroying nodes in group %s%n", groupName);
                // you can use predicates to select which nodes you wish to destroy.
                Set<? extends NodeMetadata> destroyed = //
                compute.destroyNodesMatching(Predicates.<NodeMetadata>and(not(TERMINATED), inGroup(groupName)));
                System.out.printf("<< destroyed nodes %s%n", destroyed);
                break;
        }
    } catch (RunNodesException e) {
        System.err.println("error adding node to group " + groupName + ": " + e.getMessage());
        error = 1;
    } catch (RunScriptOnNodesException e) {
        System.err.println("error executing " + command + " on group " + groupName + ": " + e.getMessage());
        error = 1;
    } catch (Exception e) {
        System.err.println("error: " + e.getMessage());
        error = 1;
    } finally {
        compute.getContext().close();
        System.exit(error);
    }
}
Also used : ExecResponse(org.jclouds.compute.domain.ExecResponse) Statement(org.jclouds.scriptbuilder.domain.Statement) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) ComputeService(org.jclouds.compute.ComputeService) RunScriptOnNodesException(org.jclouds.compute.RunScriptOnNodesException) RunNodesException(org.jclouds.compute.RunNodesException) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) LoginCredentials(org.jclouds.domain.LoginCredentials) Builder.overrideLoginCredentials(org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginCredentials) RunNodesException(org.jclouds.compute.RunNodesException) File(java.io.File) RunScriptOnNodesException(org.jclouds.compute.RunScriptOnNodesException)

Example 4 with RunScriptOnNodesException

use of org.jclouds.compute.RunScriptOnNodesException 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)

Aggregations

ComputeService (org.jclouds.compute.ComputeService)4 RunNodesException (org.jclouds.compute.RunNodesException)4 RunScriptOnNodesException (org.jclouds.compute.RunScriptOnNodesException)4 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)4 TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)4 IOException (java.io.IOException)2 InstanceTemplate (org.apache.whirr.service.ClusterSpec.InstanceTemplate)2 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)2 Template (org.jclouds.compute.domain.Template)2 Builder.overrideLoginCredentials (org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginCredentials)2 LoginCredentials (org.jclouds.domain.LoginCredentials)2 Statement (org.jclouds.scriptbuilder.domain.Statement)2 ImmutableList (com.google.common.collect.ImmutableList)1 File (java.io.File)1 Cluster (org.apache.whirr.service.Cluster)1 ChefService (org.jclouds.chef.ChefService)1 RunListBuilder (org.jclouds.chef.util.RunListBuilder)1 ExecResponse (org.jclouds.compute.domain.ExecResponse)1 StatementList (org.jclouds.scriptbuilder.domain.StatementList)1 InstallGit (org.jclouds.scriptbuilder.statements.git.InstallGit)1