Search in sources :

Example 1 with InstallGit

use of org.jclouds.scriptbuilder.statements.git.InstallGit 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)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ChefService (org.jclouds.chef.ChefService)1 RunListBuilder (org.jclouds.chef.util.RunListBuilder)1 ComputeService (org.jclouds.compute.ComputeService)1 RunNodesException (org.jclouds.compute.RunNodesException)1 RunScriptOnNodesException (org.jclouds.compute.RunScriptOnNodesException)1 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)1 TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)1 Builder.overrideLoginCredentials (org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginCredentials)1 LoginCredentials (org.jclouds.domain.LoginCredentials)1 Statement (org.jclouds.scriptbuilder.domain.Statement)1 StatementList (org.jclouds.scriptbuilder.domain.StatementList)1 InstallGit (org.jclouds.scriptbuilder.statements.git.InstallGit)1