use of org.jclouds.scriptbuilder.domain.Statement 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);
}
}
use of org.jclouds.scriptbuilder.domain.Statement 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);
}
}
use of org.jclouds.scriptbuilder.domain.Statement in project legacy-jclouds-examples by jclouds.
the class NodeManager method createNodeWithAdminUserAndJDKInGroupOpeningPortAndMinRam.
public NodeMetadata createNodeWithAdminUserAndJDKInGroupOpeningPortAndMinRam(String group, int port, int minRam) {
ImmutableMap<String, String> userMetadata = ImmutableMap.<String, String>of("Name", group);
// we want everything as defaults except ram
Template defaultTemplate = compute.templateBuilder().build();
Template minecraft = compute.templateBuilder().fromTemplate(defaultTemplate).minRam(minRam).build();
// setup the template to customize the node with jdk, etc. also opening ports.
Statement bootstrap = newStatementList(AdminAccess.standard(), InstallJDK.fromOpenJDK());
minecraft.getOptions().inboundPorts(22, port).userMetadata(userMetadata).runScript(bootstrap);
// example of using a cloud-specific hook
if (minecraft.getOptions() instanceof AWSEC2TemplateOptions)
minecraft.getOptions().as(AWSEC2TemplateOptions.class).enableMonitoring();
logger.info(">> creating node type(%s) in group %s, opening ports 22, %s with admin user and jdk", minecraft.getHardware().getId(), group, port);
try {
NodeMetadata node = getOnlyElement(compute.createNodesInGroup(group, 1, minecraft));
logger.info("<< available node(%s) os(%s) publicAddresses%s", node.getId(), node.getOperatingSystem(), node.getPublicAddresses());
return node;
} catch (RunNodesException e) {
throw destroyBadNodesAndPropagate(e);
}
}
use of org.jclouds.scriptbuilder.domain.Statement in project gora by apache.
the class ChefSoftwareProvisioning method performChefComputeServiceBootstrapping.
private static void performChefComputeServiceBootstrapping(Properties properties) throws IOException, InstantiationException, IllegalAccessException {
// Get the credentials that will be used to authenticate to the Chef server
String rsContinent = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_CONTINENT, "rackspace-cloudservers-us");
String rsUser = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_USERNAME, "asf-gora");
String rsApiKey = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_APIKEY, null);
String rsRegion = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), RS_REGION, "DFW");
String client = DataStoreFactory.findProperty(properties, MemStore.class.newInstance(), CHEF_CLIENT, System.getProperty("user.name"));
String organization = DataStoreFactory.findProperty(properties, MemStore.class.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
ChefContext chefContext = ContextBuilder.newBuilder("chef").endpoint("https://api.opscode.com/organizations/" + organization).credentials(client, credential).overrides(chefConfig).buildView(ChefContext.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 = chefContext.getChefService().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();
chefContext.getChefService().updateBootstrapConfigForGroup(group, config);
// Build the script that will bootstrap the node
Statement bootstrap = chefContext.getChefService().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
chefContext.close();
computeContext.close();
}
Aggregations