Search in sources :

Example 41 with ComputeService

use of org.jclouds.compute.ComputeService in project legacy-jclouds-examples by jclouds.

the class MainApp method main.

public static void main(String[] args) {
    if (args.length < PARAMETERS)
        throw new IllegalArgumentException(INVALID_SYNTAX);
    // Args
    String accesskeyid = args[0];
    String secretkey = args[1];
    String group = args[2];
    String command = args[3];
    // Init
    ComputeService compute = new ComputeServiceContextFactory().createContext("aws-ec2", accesskeyid, secretkey).getComputeService();
    // wait up to 60 seconds for ssh to be accessible
    RetryablePredicate<IPSocket> socketTester = new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 60, 1, 1, TimeUnit.SECONDS);
    try {
        if (command.equals("create")) {
            Template template = compute.templateBuilder().build();
            template.getOptions().as(AWSEC2TemplateOptions.class).spotPrice(0.03f).authorizePublicKey(Files.toString(new File(System.getProperty("user.home") + "/.ssh/id_rsa.pub"), Charsets.UTF_8));
            System.out.printf(">> running one spot node type(%s) with ami(%s) in group(%s)%n", template.getHardware().getProviderId(), template.getImage().getId(), group);
            // run only a single node
            NodeMetadata node = Iterables.getOnlyElement(compute.createNodesInGroup(group, 1, template));
            System.out.printf("<< running node(%s)%n", node.getId());
            IPSocket socket = new IPSocket(Iterables.get(node.getPublicAddresses(), 0), node.getLoginPort());
            if (socketTester.apply(socket)) {
                System.out.printf("<< socket ready [%s] node(%s)%n", socket, node.getId());
                System.out.printf("ssh to node with the following command:%n ssh %s@%s%n", node.getCredentials().identity, socket.getAddress());
                System.exit(0);
            } else {
                System.out.printf("<< socket not ready [%s] node(%s)%n", socket, node.getId());
            }
        } else if (command.equals("destroy")) {
            System.out.printf(">> destroying nodes in group(%s)%n", group);
            Set<? extends NodeMetadata> destroyed = compute.destroyNodesMatching(NodePredicates.inGroup(group));
            System.out.printf("<< destroyed(%d)%n", destroyed.size());
            System.exit(0);
        } else {
            System.err.println(INVALID_SYNTAX);
            System.exit(1);
        }
    } catch (RunNodesException e) {
        System.err.println(e.getMessage());
        for (NodeMetadata node : e.getNodeErrors().keySet()) compute.destroyNode(node.getId());
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } finally {
        compute.getContext().close();
    }
}
Also used : RetryablePredicate(org.jclouds.predicates.RetryablePredicate) Set(java.util.Set) InetSocketAddressConnect(org.jclouds.predicates.InetSocketAddressConnect) IOException(java.io.IOException) ComputeService(org.jclouds.compute.ComputeService) ComputeServiceContextFactory(org.jclouds.compute.ComputeServiceContextFactory) IPSocket(org.jclouds.net.IPSocket) Template(org.jclouds.compute.domain.Template) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) RunNodesException(org.jclouds.compute.RunNodesException) File(java.io.File)

Example 42 with ComputeService

use of org.jclouds.compute.ComputeService in project iobserve-analysis by research-iobserve.

the class AllocateActionScript method execute.

@Override
public void execute() throws RunNodesException {
    final ResourceContainer container = this.action.getTargetResourceContainer();
    final ResourceContainerCloud cloudContainer = this.getResourceContainerCloud(container);
    final ComputeService client = this.getComputeServiceForContainer(cloudContainer);
    final TemplateBuilder templateBuilder = client.templateBuilder();
    final VMType instanceType = cloudContainer.getInstanceType();
    templateBuilder.hardwareId(instanceType.getName());
    templateBuilder.locationId(instanceType.getLocation());
    // TODO maybe make this configurable
    templateBuilder.osFamily(OsFamily.UBUNTU);
    final Statement setupAdminInstructions = AdminAccess.standard();
    // Necessary to set hostname to allow mapping for later events
    final TemplateOptions options = Builder.runScript(setupAdminInstructions).runScript(AllocateActionScript.getChangeHostnameScript(cloudContainer)).runScript(this.getStartupScript());
    templateBuilder.options(options);
    final Template template = templateBuilder.build();
    final String groupName = ModelHelper.getGroupName(cloudContainer);
    final NodeMetadata node = Iterables.getOnlyElement(client.createNodesInGroup(groupName, 1, template));
    AllocateActionScript.LOGGER.info(String.format("Allocated node for resource container '%s'. NodeID: %s, Hostname: %s, Adresses: %s", cloudContainer.getEntityName(), node.getId(), node.getHostname(), Iterables.concat(node.getPrivateAddresses(), node.getPublicAddresses())));
// TODO write resource container to original model to enable mapping
}
Also used : NodeMetadata(org.jclouds.compute.domain.NodeMetadata) ResourceContainerCloud(org.palladiosimulator.pcm.cloud.pcmcloud.resourceenvironmentcloud.ResourceContainerCloud) VMType(org.palladiosimulator.pcm.cloud.pcmcloud.cloudprofile.VMType) Statement(org.jclouds.scriptbuilder.domain.Statement) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) TemplateOptions(org.jclouds.compute.options.TemplateOptions) ComputeService(org.jclouds.compute.ComputeService) ResourceContainer(org.palladiosimulator.pcm.resourceenvironment.ResourceContainer) Template(org.jclouds.compute.domain.Template)

Example 43 with ComputeService

use of org.jclouds.compute.ComputeService in project iobserve-analysis by research-iobserve.

the class DeallocateActionScript method execute.

@Override
public void execute() throws RunScriptOnNodesException {
    final ResourceContainer container = this.action.getTargetResourceContainer();
    final ResourceContainerCloud cloudContainer = this.getResourceContainerCloud(container);
    final ComputeService client = this.getComputeServiceForContainer(cloudContainer);
    // If the container group has already been terminated, do nothing
    if (!this.data.getTerminatedGroups().contains(ModelHelper.getGroupName(cloudContainer))) {
        client.runScriptOnNodesMatching(node -> node.getGroup().equals(cloudContainer.getGroupName()), this.getScript(AdaptationData.NODE_PRE_TERMINATE_SCRIPT_NAME));
        client.destroyNodesMatching(node -> node.getGroup().equals(cloudContainer.getGroupName()));
        this.data.getTerminatedGroups().add(cloudContainer.getGroupName());
    }
}
Also used : ResourceContainerCloud(org.palladiosimulator.pcm.cloud.pcmcloud.resourceenvironmentcloud.ResourceContainerCloud) ComputeService(org.jclouds.compute.ComputeService) ResourceContainer(org.palladiosimulator.pcm.resourceenvironment.ResourceContainer)

Example 44 with ComputeService

use of org.jclouds.compute.ComputeService in project iobserve-analysis by research-iobserve.

the class ReplicateActionScript method execute.

@Override
public void execute() throws RunScriptOnNodesException, IOException {
    final ResourceContainer container = this.action.getTargetAllocationContext().getResourceContainer_AllocationContext();
    final ResourceContainerCloud cloudContainer = this.getResourceContainerCloud(container);
    final ComputeService client = this.getComputeServiceForContainer(cloudContainer);
    final String assemblyContextName = this.action.getTargetAllocationContext().getAssemblyContext_AllocationContext().getEntityName();
    // nothing
    if (!this.data.getAllocatedContexts().contains(assemblyContextName)) {
        client.runScriptOnNodesMatching(node -> node.getGroup().equals(cloudContainer.getGroupName()), this.getAllocateScript(this.action.getTargetAllocationContext().getAssemblyContext_AllocationContext()));
        this.data.getAllocatedContexts().add(assemblyContextName);
    // TODO add possibility to open up ports defined in a config file
    }
}
Also used : ResourceContainerCloud(org.palladiosimulator.pcm.cloud.pcmcloud.resourceenvironmentcloud.ResourceContainerCloud) ComputeService(org.jclouds.compute.ComputeService) ResourceContainer(org.palladiosimulator.pcm.resourceenvironment.ResourceContainer)

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