Search in sources :

Example 1 with RunScriptOptions

use of org.jclouds.compute.options.RunScriptOptions in project fabric8 by jboss-fuse.

the class JcloudsContainerProvider method stop.

@Override
public void stop(Container container) {
    assertValid();
    CreateContainerMetadata metadata = container.getMetadata();
    if (!(metadata instanceof CreateJCloudsContainerMetadata)) {
        throw new IllegalStateException("Container doesn't have valid create container metadata type");
    } else {
        CreateJCloudsContainerMetadata jCloudsContainerMetadata = (CreateJCloudsContainerMetadata) metadata;
        CreateJCloudsContainerOptions options = jCloudsContainerMetadata.getCreateOptions();
        try {
            ComputeService computeService = getOrCreateComputeService(options);
            String nodeId = jCloudsContainerMetadata.getNodeId();
            Optional<RunScriptOptions> runScriptOptions = ToRunScriptOptions.withComputeService(computeService).apply(jCloudsContainerMetadata);
            String script = buildStopScript(container.getId(), options);
            ExecResponse response;
            container.setProvisionResult(Container.PROVISION_STOPPING);
            if (runScriptOptions.isPresent()) {
                response = computeService.runScriptOnNode(nodeId, script, runScriptOptions.get());
            } else {
                response = computeService.runScriptOnNode(nodeId, script);
            }
            if (response == null) {
                jCloudsContainerMetadata.setFailure(new Exception("No response received for fabric install script."));
            } else if (response.getOutput() != null && response.getOutput().contains(ContainerProviderUtils.FAILURE_PREFIX)) {
                jCloudsContainerMetadata.setFailure(new Exception(ContainerProviderUtils.parseScriptFailure(response.getOutput())));
            }
        } catch (Throwable t) {
            container.setProvisionResult(Container.PROVISION_STOPPED);
            jCloudsContainerMetadata.setFailure(t);
        }
    }
}
Also used : RunScriptOptions(org.jclouds.compute.options.RunScriptOptions) ToRunScriptOptions(io.fabric8.service.jclouds.functions.ToRunScriptOptions) ExecResponse(org.jclouds.compute.domain.ExecResponse) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) ComputeService(org.jclouds.compute.ComputeService) URISyntaxException(java.net.URISyntaxException) RunNodesException(org.jclouds.compute.RunNodesException) MalformedURLException(java.net.MalformedURLException)

Example 2 with RunScriptOptions

use of org.jclouds.compute.options.RunScriptOptions in project whirr by apache.

the class ByonClusterAction method doAction.

@Override
protected void doAction(Map<InstanceTemplate, ClusterActionEvent> eventMap) throws IOException, InterruptedException {
    final Collection<Future<ExecResponse>> futures = Sets.newHashSet();
    List<NodeMetadata> nodes = Lists.newArrayList();
    List<NodeMetadata> usedNodes = Lists.newArrayList();
    int numberAllocated = 0;
    Set<Instance> allInstances = Sets.newLinkedHashSet();
    for (Entry<InstanceTemplate, ClusterActionEvent> entry : eventMap.entrySet()) {
        final ClusterSpec clusterSpec = entry.getValue().getClusterSpec();
        final StatementBuilder statementBuilder = entry.getValue().getStatementBuilder();
        if (statementBuilder.isEmpty()) {
            // skip
            continue;
        }
        final ComputeServiceContext computeServiceContext = getCompute().apply(clusterSpec);
        final ComputeService computeService = computeServiceContext.getComputeService();
        LoginCredentials credentials = LoginCredentials.builder().user(clusterSpec.getClusterUser()).privateKey(clusterSpec.getPrivateKey()).build();
        final RunScriptOptions options = overrideLoginCredentials(credentials);
        if (numberAllocated == 0) {
            for (ComputeMetadata compute : computeService.listNodes()) {
                if (!(compute instanceof NodeMetadata)) {
                    throw new IllegalArgumentException("Not an instance of NodeMetadata: " + compute);
                }
                nodes.add((NodeMetadata) compute);
            }
        }
        int num = entry.getKey().getNumberOfInstances();
        Predicate<NodeMetadata> unused = not(in(usedNodes));
        // TODO: This seems very fragile and a bug.  It is not required that someone passes a hardware id,
        // so this is likely to break badly. Even if there was, why do we assume it is splittable?!
        // this logic should be refactored or removed ASAP
        Predicate<NodeMetadata> instancePredicate = Predicates.alwaysTrue();
        if (entry.getKey().getTemplate() != null) {
            String hardwareId = entry.getKey().getTemplate().getHardwareId();
            if (hardwareId != null)
                instancePredicate = new TagsPredicate(StringUtils.split(hardwareId));
        }
        List<NodeMetadata> templateNodes = Lists.newArrayList(filter(nodes, and(unused, instancePredicate)));
        if (templateNodes.size() < num) {
            LOG.warn("Not enough nodes available for template " + StringUtils.join(entry.getKey().getRoles(), "+"));
        }
        templateNodes = templateNodes.subList(0, num);
        usedNodes.addAll(templateNodes);
        numberAllocated = usedNodes.size();
        Set<Instance> templateInstances = getInstances(credentials, entry.getKey().getRoles(), templateNodes);
        allInstances.addAll(templateInstances);
        for (final Instance instance : templateInstances) {
            futures.add(runStatementOnInstanceInCluster(statementBuilder, instance, clusterSpec, options));
        }
    }
    for (Future<ExecResponse> future : futures) {
        try {
            future.get();
        } catch (ExecutionException e) {
            throw new IOException(e.getCause());
        }
    }
    if (action.equals(ClusterActionHandler.BOOTSTRAP_ACTION)) {
        Cluster cluster = new Cluster(allInstances);
        for (ClusterActionEvent event : eventMap.values()) {
            event.setCluster(cluster);
        }
    }
}
Also used : RunScriptOptions(org.jclouds.compute.options.RunScriptOptions) Instance(org.apache.whirr.Cluster.Instance) ExecResponse(org.jclouds.compute.domain.ExecResponse) ClusterActionEvent(org.apache.whirr.service.ClusterActionEvent) ComputeMetadata(org.jclouds.compute.domain.ComputeMetadata) LoginCredentials(org.jclouds.domain.LoginCredentials) Builder.overrideLoginCredentials(org.jclouds.compute.options.RunScriptOptions.Builder.overrideLoginCredentials) ExecutionException(java.util.concurrent.ExecutionException) Cluster(org.apache.whirr.Cluster) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) ClusterSpec(org.apache.whirr.ClusterSpec) IOException(java.io.IOException) ComputeService(org.jclouds.compute.ComputeService) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) StatementBuilder(org.apache.whirr.service.jclouds.StatementBuilder) Future(java.util.concurrent.Future) InstanceTemplate(org.apache.whirr.InstanceTemplate)

Example 3 with RunScriptOptions

use of org.jclouds.compute.options.RunScriptOptions in project fabric8 by jboss-fuse.

the class JcloudsContainerProvider method start.

@Override
public void start(Container container) {
    assertValid();
    CreateContainerMetadata metadata = container.getMetadata();
    if (!(metadata instanceof CreateJCloudsContainerMetadata)) {
        throw new IllegalStateException("Container doesn't have valid create container metadata type");
    } else {
        CreateJCloudsContainerMetadata jCloudsContainerMetadata = (CreateJCloudsContainerMetadata) metadata;
        CreateJCloudsContainerOptions options = jCloudsContainerMetadata.getCreateOptions();
        ComputeService computeService = getOrCreateComputeService(options);
        try {
            String nodeId = jCloudsContainerMetadata.getNodeId();
            Optional<RunScriptOptions> runScriptOptions = ToRunScriptOptions.withComputeService(computeService).apply(jCloudsContainerMetadata);
            String script = buildStartScript(container.getId(), options);
            ExecResponse response;
            if (runScriptOptions.isPresent()) {
                response = computeService.runScriptOnNode(nodeId, script, runScriptOptions.get());
            } else {
                response = computeService.runScriptOnNode(nodeId, script);
            }
            if (response == null) {
                jCloudsContainerMetadata.setFailure(new Exception("No response received for fabric install script."));
            } else if (response.getOutput() != null && response.getOutput().contains(ContainerProviderUtils.FAILURE_PREFIX)) {
                jCloudsContainerMetadata.setFailure(new Exception(ContainerProviderUtils.parseScriptFailure(response.getOutput())));
            }
        } catch (Throwable t) {
            jCloudsContainerMetadata.setFailure(t);
        }
    }
}
Also used : RunScriptOptions(org.jclouds.compute.options.RunScriptOptions) ToRunScriptOptions(io.fabric8.service.jclouds.functions.ToRunScriptOptions) ExecResponse(org.jclouds.compute.domain.ExecResponse) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) ComputeService(org.jclouds.compute.ComputeService) URISyntaxException(java.net.URISyntaxException) RunNodesException(org.jclouds.compute.RunNodesException) MalformedURLException(java.net.MalformedURLException)

Example 4 with RunScriptOptions

use of org.jclouds.compute.options.RunScriptOptions in project whirr by apache.

the class ByonClusterController method runScriptOnNodesMatching.

public Map<? extends NodeMetadata, ExecResponse> runScriptOnNodesMatching(final ClusterSpec spec, Predicate<NodeMetadata> condition, final Statement statement) throws IOException, RunScriptOnNodesException {
    ComputeServiceContext computeServiceContext = getCompute().apply(spec);
    ComputeService computeService = computeServiceContext.getComputeService();
    Cluster cluster = getClusterStateStore(spec).load();
    RunScriptOptions options = RunScriptOptions.Builder.runAsRoot(false).wrapInInitScript(false);
    return computeService.runScriptOnNodesMatching(Predicates.<NodeMetadata>and(condition, runningIn(cluster)), statement, options);
}
Also used : RunScriptOptions(org.jclouds.compute.options.RunScriptOptions) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) ComputeService(org.jclouds.compute.ComputeService)

Example 5 with RunScriptOptions

use of org.jclouds.compute.options.RunScriptOptions 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)

Aggregations

RunScriptOptions (org.jclouds.compute.options.RunScriptOptions)8 ExecResponse (org.jclouds.compute.domain.ExecResponse)6 ComputeService (org.jclouds.compute.ComputeService)4 ScriptBuilder (org.jclouds.scriptbuilder.ScriptBuilder)3 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)2 ToRunScriptOptions (io.fabric8.service.jclouds.functions.ToRunScriptOptions)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 Cluster (org.apache.whirr.Cluster)2 Instance (org.apache.whirr.Cluster.Instance)2 ClusterSpec (org.apache.whirr.ClusterSpec)2 InstanceTemplate (org.apache.whirr.InstanceTemplate)2 ClusterActionEvent (org.apache.whirr.service.ClusterActionEvent)2 StatementBuilder (org.apache.whirr.service.jclouds.StatementBuilder)2 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)2 RunNodesException (org.jclouds.compute.RunNodesException)2 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)2