Search in sources :

Example 1 with ComputeService

use of org.jclouds.compute.ComputeService in project whirr by apache.

the class CassandraService method launchCluster.

@Override
public Cluster launchCluster(ClusterSpec clusterSpec) throws IOException {
    ComputeServiceContext computeServiceContext = ComputeServiceContextBuilder.build(clusterSpec);
    ComputeService computeService = computeServiceContext.getComputeService();
    byte[] bootScript = RunUrlBuilder.runUrls("sun/java/install", "apache/cassandra/install");
    TemplateBuilder templateBuilder = computeService.templateBuilder().osFamily(UBUNTU).options(runScript(bootScript).installPrivateKey(clusterSpec.readPrivateKey()).authorizePublicKey(clusterSpec.readPublicKey()));
    // TODO extract this logic elsewhere
    if (clusterSpec.getProvider().equals("ec2"))
        templateBuilder.imageNameMatches(".*10\\.?04.*").osDescriptionMatches("^ubuntu-images.*").architecture(Architecture.X86_32);
    Template template = templateBuilder.build();
    InstanceTemplate instanceTemplate = clusterSpec.getInstanceTemplate(CASSANDRA_ROLE);
    checkNotNull(instanceTemplate);
    int clusterSize = instanceTemplate.getNumberOfInstances();
    Set<? extends NodeMetadata> nodeMap;
    try {
        nodeMap = computeService.runNodesWithTag(clusterSpec.getClusterName(), clusterSize, template);
    } catch (RunNodesException e) {
        // TODO: can we do better here
        throw new IOException(e);
    }
    FirewallSettings.authorizeIngress(computeServiceContext, nodeMap, clusterSpec, CLIENT_PORT);
    List<NodeMetadata> nodes = Lists.newArrayList(nodeMap);
    List<NodeMetadata> seeds = getSeeds(nodes);
    // Pass list of all servers in cluster to configure script.
    String servers = Joiner.on(' ').join(getPrivateIps(seeds));
    byte[] configureScript = RunUrlBuilder.runUrls("apache/cassandra/post-configure " + servers);
    try {
        Map<? extends NodeMetadata, ExecResponse> responses = computeService.runScriptOnNodesMatching(runningWithTag(clusterSpec.getClusterName()), configureScript);
        assert responses.size() > 0 : "no nodes matched " + clusterSpec.getClusterName();
    } catch (RunScriptOnNodesException e) {
        // TODO: retry
        throw new IOException(e);
    }
    return new Cluster(getInstances(nodes));
}
Also used : ExecResponse(org.jclouds.ssh.ExecResponse) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) Cluster(org.apache.whirr.service.Cluster) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) IOException(java.io.IOException) ComputeService(org.jclouds.compute.ComputeService) Template(org.jclouds.compute.domain.Template) InstanceTemplate(org.apache.whirr.service.ClusterSpec.InstanceTemplate) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) RunNodesException(org.jclouds.compute.RunNodesException) RunScriptOnNodesException(org.jclouds.compute.RunScriptOnNodesException) InstanceTemplate(org.apache.whirr.service.ClusterSpec.InstanceTemplate)

Example 2 with ComputeService

use of org.jclouds.compute.ComputeService 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 3 with ComputeService

use of org.jclouds.compute.ComputeService 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 4 with ComputeService

use of org.jclouds.compute.ComputeService 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 5 with ComputeService

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

the class CloudFirewallEdit method doExecute.

@Override
protected Object doExecute() throws Exception {
    if (validateArguments()) {
        ComputeService computeService = findTargetComputeService();
        if (computeService == null) {
            return null;
        }
        Set<String> sourceCidrs = collectCirds();
        FirewallManager firewallManager = firewallManagerFactory.getFirewallManager(computeService);
        NodeMetadata node = null;
        if (!Strings.isNullOrEmpty(targetContainerName) && getCurator().getZookeeperClient().isConnected() && fabricService != null) {
            CreateJCloudsContainerMetadata metadata = getContainerCloudMetadata(targetContainerName);
            if (metadata != null && !Strings.isNullOrEmpty(metadata.getNodeId())) {
                targetNodeId = metadata.getNodeId();
            }
        }
        if (!Strings.isNullOrEmpty(targetNodeId)) {
            node = computeService.getNodeMetadata(targetNodeId);
        }
        if (node == null) {
            System.err.println("Could not find target node. Make sure you specified either --target-node-id or --target-container using a valid cloud container.");
            return null;
        }
        if (flush) {
            firewallManager.addRule(Rule.create().destination(node).flush());
            return null;
        }
        for (String cidr : sourceCidrs) {
            Rule rule = Rule.create().destination(node).source(cidr);
            if (port != null && port.length > 0) {
                rule = rule.ports(port);
            }
            if (revoke) {
                firewallManager.addRule(rule.revoke());
            } else {
                firewallManager.addRule(rule);
            }
        }
    }
    return null;
}
Also used : NodeMetadata(org.jclouds.compute.domain.NodeMetadata) FirewallManager(io.fabric8.service.jclouds.firewall.FirewallManager) CreateJCloudsContainerMetadata(io.fabric8.service.jclouds.CreateJCloudsContainerMetadata) Rule(io.fabric8.service.jclouds.firewall.Rule) ComputeService(org.jclouds.compute.ComputeService)

Aggregations

ComputeService (org.jclouds.compute.ComputeService)44 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)13 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)13 Template (org.jclouds.compute.domain.Template)13 TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)13 ResourceContainerCloud (org.palladiosimulator.pcm.cloud.pcmcloud.resourceenvironmentcloud.ResourceContainerCloud)10 ResourceContainer (org.palladiosimulator.pcm.resourceenvironment.ResourceContainer)10 RunNodesException (org.jclouds.compute.RunNodesException)8 IOException (java.io.IOException)7 ClusterSpec (org.apache.whirr.ClusterSpec)7 TemplateOptions (org.jclouds.compute.options.TemplateOptions)7 Set (java.util.Set)6 Test (org.junit.Test)6 ClusterActionHandler (org.apache.whirr.service.ClusterActionHandler)5 Statement (org.jclouds.scriptbuilder.domain.Statement)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 HashSet (java.util.HashSet)4 Stack (java.util.Stack)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)4