Search in sources :

Example 61 with ClusterSpec

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;
    }
}
Also used : ClusterSpec(org.apache.whirr.ClusterSpec) OptionSet(joptsimple.OptionSet)

Example 62 with ClusterSpec

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;
    }
}
Also used : ClusterSpec(org.apache.whirr.ClusterSpec) OptionSet(joptsimple.OptionSet) File(java.io.File)

Example 63 with ClusterSpec

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);
}
Also used : RunScriptOptions(org.jclouds.compute.options.RunScriptOptions) Instance(org.apache.whirr.Cluster.Instance) ExecResponse(org.jclouds.compute.domain.ExecResponse) Cluster(org.apache.whirr.Cluster) ClusterSpec(org.apache.whirr.ClusterSpec) ClusterActionEvent(org.apache.whirr.service.ClusterActionEvent) IOException(java.io.IOException) StatementBuilder(org.apache.whirr.service.jclouds.StatementBuilder) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Future(java.util.concurrent.Future) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) InstanceTemplate(org.apache.whirr.InstanceTemplate)

Example 64 with ClusterSpec

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;
}
Also used : OptionSpec(joptsimple.OptionSpec) ArgumentAcceptingOptionSpec(joptsimple.ArgumentAcceptingOptionSpec) Configuration(org.apache.commons.configuration.Configuration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) ClusterSpec(org.apache.whirr.ClusterSpec) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) Map(java.util.Map) Property(org.apache.whirr.ClusterSpec.Property)

Example 65 with 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;
    }
}
Also used : ClusterSpec(org.apache.whirr.ClusterSpec) OptionSet(joptsimple.OptionSet)

Aggregations

ClusterSpec (org.apache.whirr.ClusterSpec)98 Configuration (org.apache.commons.configuration.Configuration)39 Cluster (org.apache.whirr.Cluster)35 Test (org.junit.Test)34 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)16 Instance (org.apache.whirr.Cluster.Instance)14 ClusterController (org.apache.whirr.ClusterController)10 InetAddress (java.net.InetAddress)9 DryRun (org.apache.whirr.service.DryRunModule.DryRun)9 OptionSet (joptsimple.OptionSet)8 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)8 ZooKeeperCluster (org.apache.whirr.service.zookeeper.ZooKeeperCluster)8 IOException (java.io.IOException)7 ComputeService (org.jclouds.compute.ComputeService)7 File (java.io.File)6 ClusterControllerFactory (org.apache.whirr.ClusterControllerFactory)6 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)6 Set (java.util.Set)5 Stack (java.util.Stack)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5