Search in sources :

Example 1 with JsonBall

use of org.jclouds.domain.JsonBall in project gora by apache.

the class ChefSoftwareProvisioning method performChefComputeServiceBootstrapping.

private static void performChefComputeServiceBootstrapping(Properties properties) throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    // Get the credentials that will be used to authenticate to the Chef server
    String rsContinent = DataStoreFactory.findProperty(properties, MemStore.class.getDeclaredConstructor().newInstance(), RS_CONTINENT, "rackspace-cloudservers-us");
    String rsUser = DataStoreFactory.findProperty(properties, MemStore.class.getDeclaredConstructor().newInstance(), RS_USERNAME, "asf-gora");
    String rsApiKey = DataStoreFactory.findProperty(properties, MemStore.class.getDeclaredConstructor().newInstance(), RS_APIKEY, null);
    String rsRegion = DataStoreFactory.findProperty(properties, MemStore.class.getDeclaredConstructor().newInstance(), RS_REGION, "DFW");
    String client = DataStoreFactory.findProperty(properties, MemStore.class.getDeclaredConstructor().newInstance(), CHEF_CLIENT, System.getProperty("user.name"));
    String organization = DataStoreFactory.findProperty(properties, MemStore.class.getDeclaredConstructor().newInstance(), CHEF_ORGANIZATION, null);
    String pemFile = System.getProperty("user.home") + "/.chef/" + client + ".pem";
    String credential = Files.toString(new File(pemFile), Charsets.UTF_8);
    // Provide the validator information to let the nodes to auto-register themselves
    // in the Chef server during bootstrap
    String validator = organization + "-validator";
    String validatorPemFile = System.getProperty("user.home") + "/.chef/" + validator + ".pem";
    String validatorCredential = Files.toString(new File(validatorPemFile), Charsets.UTF_8);
    Properties chefConfig = new Properties();
    chefConfig.put(ChefProperties.CHEF_VALIDATOR_NAME, validator);
    chefConfig.put(ChefProperties.CHEF_VALIDATOR_CREDENTIAL, validatorCredential);
    // Create the connection to the Chef server
    ChefApi chefApi = ContextBuilder.newBuilder(new ChefApiMetadata()).endpoint("https://api.opscode.com/organizations/" + organization).credentials(client, credential).overrides(chefConfig).buildApi(ChefApi.class);
    // Create the connection to the compute provider. Note that ssh will be used to bootstrap chef
    ComputeServiceContext computeContext = ContextBuilder.newBuilder(rsContinent).endpoint(rsRegion).credentials(rsUser, rsApiKey).modules(ImmutableSet.<Module>of(new SshjSshClientModule())).buildView(ComputeServiceContext.class);
    // Group all nodes in both Chef and the compute provider by this group
    String group = "jclouds-chef-goraci";
    // Set the recipe to install and the configuration values to override
    String recipe = "apache2";
    JsonBall attributes = new JsonBall("{\"apache\": {\"listen_ports\": \"8080\"}}");
    // Check to see if the recipe you want exists
    List<String> runlist = null;
    Iterable<? extends CookbookVersion> cookbookVersions = chefApi.chefService().listCookbookVersions();
    if (any(cookbookVersions, CookbookVersionPredicates.containsRecipe(recipe))) {
        runlist = new RunListBuilder().addRecipe(recipe).build();
    }
    for (Iterator<String> iterator = runlist.iterator(); iterator.hasNext(); ) {
        String string = (String) iterator.next();
        LOG.info(string);
    }
    // Update the chef service with the run list you wish to apply to all nodes in the group
    // and also provide the json configuration used to customize the desired values
    BootstrapConfig config = BootstrapConfig.builder().runList(runlist).attributes(attributes).build();
    chefApi.chefService().updateBootstrapConfigForGroup(group, config);
    // Build the script that will bootstrap the node
    Statement bootstrap = chefApi.chefService().createBootstrapScriptForGroup(group);
    TemplateBuilder templateBuilder = computeContext.getComputeService().templateBuilder();
    templateBuilder.options(runScript(bootstrap));
    // Run a node on the compute provider that bootstraps chef
    try {
        Set<? extends NodeMetadata> nodes = computeContext.getComputeService().createNodesInGroup(group, 1, templateBuilder.build());
        for (NodeMetadata nodeMetadata : nodes) {
            LOG.info("<< node %s: %s%n", nodeMetadata.getId(), concat(nodeMetadata.getPrivateAddresses(), nodeMetadata.getPublicAddresses()));
        }
    } catch (RunNodesException e) {
        throw new RuntimeException(e.getMessage());
    }
    // Release resources
    chefApi.close();
    computeContext.close();
}
Also used : Statement(org.jclouds.scriptbuilder.domain.Statement) BootstrapConfig(org.jclouds.chef.domain.BootstrapConfig) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) Properties(java.util.Properties) ChefProperties(org.jclouds.chef.config.ChefProperties) ChefApi(org.jclouds.chef.ChefApi) ChefApiMetadata(org.jclouds.chef.ChefApiMetadata) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) RunNodesException(org.jclouds.compute.RunNodesException) SshjSshClientModule(org.jclouds.sshj.config.SshjSshClientModule) RunListBuilder(org.jclouds.chef.util.RunListBuilder) Module(com.google.inject.Module) SshjSshClientModule(org.jclouds.sshj.config.SshjSshClientModule) JsonBall(org.jclouds.domain.JsonBall) File(java.io.File)

Aggregations

Module (com.google.inject.Module)1 File (java.io.File)1 Properties (java.util.Properties)1 ChefApi (org.jclouds.chef.ChefApi)1 ChefApiMetadata (org.jclouds.chef.ChefApiMetadata)1 ChefProperties (org.jclouds.chef.config.ChefProperties)1 BootstrapConfig (org.jclouds.chef.domain.BootstrapConfig)1 RunListBuilder (org.jclouds.chef.util.RunListBuilder)1 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)1 RunNodesException (org.jclouds.compute.RunNodesException)1 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)1 TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)1 JsonBall (org.jclouds.domain.JsonBall)1 Statement (org.jclouds.scriptbuilder.domain.Statement)1 SshjSshClientModule (org.jclouds.sshj.config.SshjSshClientModule)1