Search in sources :

Example 1 with CreateJCloudsContainerOptions

use of io.fabric8.service.jclouds.CreateJCloudsContainerOptions in project fabric8 by jboss-fuse.

the class JcloudsContainerProvider method destroy.

@Override
public void destroy(Container container) {
    assertValid();
    CreateContainerMetadata metadata = container.getMetadata();
    if (!(metadata instanceof CreateJCloudsContainerMetadata)) {
        throw new IllegalStateException("Container doesn't have valid create container metadata type");
    } else {
        container.setProvisionResult(Container.PROVISION_DELETING);
        CreateJCloudsContainerMetadata jCloudsContainerMetadata = (CreateJCloudsContainerMetadata) metadata;
        CreateJCloudsContainerOptions options = jCloudsContainerMetadata.getCreateOptions();
        String nodeId = jCloudsContainerMetadata.getNodeId();
        ComputeService computeService = getOrCreateComputeService(options);
        computeService.destroyNode(nodeId);
    }
}
Also used : CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) ComputeService(org.jclouds.compute.ComputeService)

Example 2 with CreateJCloudsContainerOptions

use of io.fabric8.service.jclouds.CreateJCloudsContainerOptions 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 3 with CreateJCloudsContainerOptions

use of io.fabric8.service.jclouds.CreateJCloudsContainerOptions in project fabric8 by jboss-fuse.

the class JcloudsContainerProvider method create.

@Override
public CreateJCloudsContainerMetadata create(CreateJCloudsContainerOptions input, CreationStateListener listener) throws MalformedURLException, RunNodesException, URISyntaxException, InterruptedException {
    assertValid();
    CreateJCloudsContainerOptions options = input.updateComputeService(getOrCreateComputeService(input));
    listener.onStateChange("Looking up for compute service.");
    ComputeService computeService = getOrCreateComputeService(options);
    if (computeService == null) {
        throw new IllegalStateException("Compute service could not be found or created.");
    }
    Template template = ToTemplate.apply(options);
    listener.onStateChange(String.format(OVERVIEW_FORMAT, 1, options.getContextName()));
    try {
        Set<? extends NodeMetadata> metadata = computeService.createNodesInGroup(options.getGroup(), 1, template);
        if (metadata == null || metadata.size() != 1) {
            throw new IllegalStateException("JClouds created " + metadata.size() + " containers instead of 1");
        }
        NodeMetadata nodeMetadata = metadata.iterator().next();
        switch(nodeMetadata.getStatus()) {
            case RUNNING:
                listener.onStateChange(String.format(NODE_CREATED_FORMAT, nodeMetadata.getName()));
                break;
            default:
                listener.onStateChange(String.format(NODE_ERROR_FORMAT, nodeMetadata.getStatus()));
        }
        CloudContainerInstallationTask installationTask = new CloudContainerInstallationTask(options.getName(), nodeMetadata, options, computeService, firewallManagerFactory.get(), template.getOptions(), listener);
        return installationTask.install();
    } catch (Throwable ex) {
        CreateJCloudsContainerMetadata failureMetadata = new CreateJCloudsContainerMetadata();
        failureMetadata.setCreateOptions(options);
        failureMetadata.setFailure(ex);
        return failureMetadata;
    }
}
Also used : NodeMetadata(org.jclouds.compute.domain.NodeMetadata) ComputeService(org.jclouds.compute.ComputeService) ToTemplate(io.fabric8.service.jclouds.functions.ToTemplate) Template(org.jclouds.compute.domain.Template)

Example 4 with CreateJCloudsContainerOptions

use of io.fabric8.service.jclouds.CreateJCloudsContainerOptions 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 5 with CreateJCloudsContainerOptions

use of io.fabric8.service.jclouds.CreateJCloudsContainerOptions in project fabric8 by jboss-fuse.

the class CloudFirewallEdit method findTargetComputeService.

/**
 * Finds the {@link ComputeService} to use based on the provider option or the target {@link Container} metadata.
 * @return
 */
private ComputeService findTargetComputeService() {
    if (!Strings.isNullOrEmpty(targetContainerName) && getCurator().getZookeeperClient().isConnected()) {
        CreateJCloudsContainerMetadata metadata = getContainerCloudMetadata(targetContainerName);
        if (metadata != null) {
            CreateJCloudsContainerOptions options = metadata.getCreateOptions();
            contextName = options.getContextName();
        }
    }
    if (!Strings.isNullOrEmpty(contextName)) {
        for (ComputeService computeService : computeServices) {
            if (computeService.getContext().unwrap().getName().equals(contextName)) {
                return computeService;
            }
        }
    }
    if (computeServices == null || computeServices.size() == 0) {
        System.out.println("No compute services are available.");
        return null;
    } else if (computeServices != null && computeServices.size() == 0) {
        return computeServices.get(0);
    } else {
        System.out.println("Multiple cloud provider service available. Please select one using the --provider option.");
        return null;
    }
}
Also used : CreateJCloudsContainerMetadata(io.fabric8.service.jclouds.CreateJCloudsContainerMetadata) CreateJCloudsContainerOptions(io.fabric8.service.jclouds.CreateJCloudsContainerOptions) ComputeService(org.jclouds.compute.ComputeService)

Aggregations

ComputeService (org.jclouds.compute.ComputeService)5 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)3 CreateJCloudsContainerOptions (io.fabric8.service.jclouds.CreateJCloudsContainerOptions)2 ToRunScriptOptions (io.fabric8.service.jclouds.functions.ToRunScriptOptions)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 RunNodesException (org.jclouds.compute.RunNodesException)2 ExecResponse (org.jclouds.compute.domain.ExecResponse)2 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)2 RunScriptOptions (org.jclouds.compute.options.RunScriptOptions)2 CreateJCloudsContainerMetadata (io.fabric8.service.jclouds.CreateJCloudsContainerMetadata)1 ToTemplate (io.fabric8.service.jclouds.functions.ToTemplate)1 Template (org.jclouds.compute.domain.Template)1 LoginCredentials (org.jclouds.domain.LoginCredentials)1