use of org.apache.whirr.ClusterSpec in project whirr by apache.
the class ListClusterCommand method run.
@Override
public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception {
OptionSet optionSet = parser.parse(args.toArray(new String[args.size()]));
if (!optionSet.nonOptionArguments().isEmpty()) {
printUsage(err);
return -1;
}
try {
ClusterSpec clusterSpec = getClusterSpec(optionSet);
printProviderInfo(out, err, clusterSpec, optionSet);
return run(in, out, err, clusterSpec);
} catch (IllegalArgumentException e) {
printErrorAndHelpHint(err, e);
return -1;
}
}
use of org.apache.whirr.ClusterSpec in project whirr by apache.
the class RunScriptCommand method run.
@Override
public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception {
OptionSet optionSet = parser.parse(args.toArray(new String[0]));
if (!optionSet.has(scriptOption)) {
err.println("Please specify a script file to be executed.");
err.println("Get more help: whirr help " + getName());
return -1;
}
if (!(new File(optionSet.valueOf(scriptOption))).exists()) {
err.printf("Script file '%s' not found.", optionSet.valueOf(scriptOption));
err.println("Get more help: whirr help " + getName());
return -2;
}
try {
ClusterSpec clusterSpec = getClusterSpec(optionSet);
String[] ids = null;
String[] roles = null;
if (optionSet.has(instancesOption)) {
ids = optionSet.valueOf(instancesOption).split(",");
}
if (optionSet.has(rolesOption)) {
roles = optionSet.valueOf(rolesOption).split(",");
}
printProviderInfo(out, err, clusterSpec, optionSet);
return run(in, out, err, clusterSpec, ids, roles, optionSet.valueOf(scriptOption));
} catch (IllegalArgumentException e) {
printErrorAndHelpHint(err, e);
return -3;
}
}
use of org.apache.whirr.ClusterSpec in project whirr by apache.
the class ScriptBasedClusterAction method runScripts.
protected void runScripts(Map<InstanceTemplate, ClusterActionEvent> eventMap) throws InterruptedException, IOException {
final String phaseName = getAction();
final Collection<Future<ExecResponse>> futures = Sets.newHashSet();
final ClusterSpec clusterSpec = eventMap.values().iterator().next().getClusterSpec();
final RunScriptOptions options = overrideLoginCredentials(LoginCredentials.builder().user(clusterSpec.getClusterUser()).privateKey(clusterSpec.getPrivateKey()).build());
for (Map.Entry<InstanceTemplate, ClusterActionEvent> entry : eventMap.entrySet()) {
if (shouldIgnoreInstanceTemplate(entry.getKey())) {
// skip if not in the target
continue;
}
Cluster cluster = entry.getValue().getCluster();
StatementBuilder statementBuilder = entry.getValue().getStatementBuilder();
if (statementBuilder.isEmpty()) {
// skip execution if we have an empty list
continue;
}
Set<Instance> instances = cluster.getInstancesMatching(Predicates.<Instance>and(onlyRolesIn(entry.getKey().getRoles()), not(instanceIsNotInTarget())));
LOG.info("Starting to run scripts on cluster for phase {} " + "on instances: {}", phaseName, asString(instances));
for (Instance instance : instances) {
futures.add(runStatementOnInstanceInCluster(statementBuilder, instance, clusterSpec, options));
}
}
for (Future<ExecResponse> future : futures) {
try {
future.get();
} catch (ExecutionException e) {
throw new IOException(e.getCause());
}
}
LOG.info("Finished running {} phase scripts on all cluster instances", phaseName);
}
use of org.apache.whirr.ClusterSpec in project whirr by apache.
the class AbstractClusterCommand method getClusterSpec.
/**
* Load the cluster spec by parsing the command line option set
*/
protected ClusterSpec getClusterSpec(OptionSet optionSet) throws ConfigurationException {
Configuration optionsConfig = new PropertiesConfiguration();
for (Map.Entry<Property, OptionSpec<?>> entry : optionSpecs.entrySet()) {
Property property = entry.getKey();
OptionSpec<?> option = entry.getValue();
Object value;
if (property.hasMultipleArguments()) {
value = optionSet.valuesOf(option);
} else {
value = optionSet.valueOf(option);
}
if (value == null && property.getType().equals(Boolean.class) && optionSet.has(property.getSimpleName())) {
value = Boolean.TRUE.toString();
}
if (value != null) {
optionsConfig.setProperty(property.getConfigName(), value);
}
}
CompositeConfiguration config = new CompositeConfiguration();
config.addConfiguration(optionsConfig);
if (optionSet.has(configOption)) {
Configuration defaults = new PropertiesConfiguration(optionSet.valueOf(configOption));
config.addConfiguration(defaults);
}
ClusterSpec clusterSpec = new ClusterSpec(config);
for (Property required : EnumSet.of(CLUSTER_NAME, PROVIDER, IDENTITY, CREDENTIAL, INSTANCE_TEMPLATES, PRIVATE_KEY_FILE)) {
if (clusterSpec.getConfiguration().getString(required.getConfigName()) == null) {
throw new IllegalArgumentException(String.format("Option '%s' not set.", required.getSimpleName()));
}
}
return clusterSpec;
}
use of org.apache.whirr.ClusterSpec in project whirr by apache.
the class DestroyInstanceCommand method run.
@Override
public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception {
OptionSet optionSet = parser.parse(args.toArray(new String[args.size()]));
if (!optionSet.nonOptionArguments().isEmpty()) {
printUsage(err);
return -1;
}
try {
if (!optionSet.hasArgument(instanceOption)) {
throw new IllegalArgumentException("--instance-id is a mandatory argument");
}
ClusterSpec clusterSpec = getClusterSpec(optionSet);
String instanceId = optionSet.valueOf(instanceOption);
printProviderInfo(out, err, clusterSpec, optionSet);
return run(in, out, err, clusterSpec, instanceId);
} catch (IllegalArgumentException e) {
printErrorAndHelpHint(err, e);
return -1;
}
}
Aggregations