Search in sources :

Example 11 with TemplateBuilder

use of org.jclouds.compute.domain.TemplateBuilder in project camel by apache.

the class JcloudsComputeProducer method createNode.

/**
     * Create a node with the specified group.
     */
protected void createNode(Exchange exchange) throws CamelException {
    String group = getGroup(exchange);
    String imageId = getImageId(exchange);
    String locationId = getLocationId(exchange);
    String hardwareId = getHardwareId(exchange);
    if (ObjectHelper.isEmpty(group)) {
        throw new CamelExchangeException("Group must be specific in the URI or as exchange property for the destroy node operation.", exchange);
    }
    TemplateBuilder builder = computeService.templateBuilder();
    builder.any();
    if (ObjectHelper.isNotEmpty(locationId)) {
        builder.locationId(locationId);
    }
    if (ObjectHelper.isNotEmpty(imageId)) {
        builder.imageId(imageId);
    }
    if (ObjectHelper.isNotEmpty(hardwareId)) {
        builder.hardwareId(hardwareId);
    }
    try {
        Set<? extends NodeMetadata> nodeMetadatas = computeService.createNodesInGroup(group, 1, builder.build());
        exchange.getOut().setBody(nodeMetadatas);
        exchange.getOut().setHeaders(exchange.getIn().getHeaders());
    } catch (RunNodesException e) {
        throw new CamelExchangeException("Error creating jclouds node.", exchange, e);
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) RunNodesException(org.jclouds.compute.RunNodesException) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder)

Example 12 with TemplateBuilder

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

the class ToTemplate method apply.

public static Template apply(CreateJCloudsContainerOptions options) {
    ComputeService service = options.getComputeService();
    TemplateOptions templateOptions = service.templateOptions();
    TemplateBuilder builder = service.templateBuilder().any();
    applyInstanceType(builder, options);
    applyImageType(builder, options);
    applyLocation(builder, options);
    applyProviderSpecificOptions(templateOptions, options);
    Optional<AdminAccess> adminAccess = ToAdminAccess.apply(options);
    if (adminAccess.isPresent()) {
        templateOptions.runScript(adminAccess.get());
    }
    builder = builder.options(templateOptions);
    return builder.build();
}
Also used : TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) AdminAccess(org.jclouds.scriptbuilder.statements.login.AdminAccess) TemplateOptions(org.jclouds.compute.options.TemplateOptions) ComputeService(org.jclouds.compute.ComputeService)

Example 13 with TemplateBuilder

use of org.jclouds.compute.domain.TemplateBuilder in project whirr by apache.

the class BootstrapTemplate method build.

public static Template build(final ClusterSpec clusterSpec, ComputeService computeService, StatementBuilder statementBuilder, InstanceTemplate instanceTemplate) {
    String name = "bootstrap-" + Joiner.on('_').join(instanceTemplate.getRoles());
    LOG.info("Configuring template for {}", name);
    statementBuilder.name(name);
    ensureUserExistsAndAuthorizeSudo(statementBuilder, clusterSpec.getClusterUser(), clusterSpec.getPublicKey(), clusterSpec.getPrivateKey());
    Statement bootstrap = statementBuilder.build(clusterSpec);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Running script {}:\n{}", name, bootstrap.render(OsFamily.UNIX));
    }
    TemplateBuilder templateBuilder = computeService.templateBuilder().from(instanceTemplate.getTemplate() != null ? instanceTemplate.getTemplate() : clusterSpec.getTemplate());
    Template template = templateBuilder.build();
    template.getOptions().runScript(bootstrap);
    return setSpotInstancePriceIfSpecified(computeService.getContext(), clusterSpec, template, instanceTemplate);
}
Also used : Statement(org.jclouds.scriptbuilder.domain.Statement) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) InstanceTemplate(org.apache.whirr.InstanceTemplate) Template(org.jclouds.compute.domain.Template)

Example 14 with TemplateBuilder

use of org.jclouds.compute.domain.TemplateBuilder in project whirr by apache.

the class BootstrapClusterActionTest method testSubroleInvoked.

@Test
@SuppressWarnings("unchecked")
public void testSubroleInvoked() throws Exception {
    CompositeConfiguration config = new CompositeConfiguration();
    if (System.getProperty("config") != null) {
        config.addConfiguration(new PropertiesConfiguration(System.getProperty("config")));
    }
    Configuration conf = new PropertiesConfiguration();
    conf.addProperty("whirr.service-name", "test-service");
    conf.addProperty("whirr.cluster-name", "test-cluster");
    conf.addProperty("whirr.instance-templates", "1 puppet:module::manifest+something-else");
    conf.addProperty("whirr.provider", "ec2");
    config.addConfiguration(conf);
    ClusterSpec clusterSpec = ClusterSpec.withTemporaryKeys(conf);
    Set<String> nn = new HashSet<String>();
    nn.add("puppet:module::manifest");
    nn.add("something-else");
    TestNodeStarterFactory nodeStarterFactory = null;
    ClusterActionHandlerFactory puppetHandlerFactory = mock(ClusterActionHandlerFactory.class);
    ClusterActionHandler handler = mock(ClusterActionHandler.class);
    when(puppetHandlerFactory.getRolePrefix()).thenReturn("puppet:");
    when(puppetHandlerFactory.create("module::manifest")).thenReturn(handler);
    when(handler.getRole()).thenReturn("something-else");
    LoadingCache<String, ClusterActionHandler> handlerMap = new HandlerMapFactory().create(ImmutableSet.of(puppetHandlerFactory), ImmutableSet.of(handler));
    Function<ClusterSpec, ComputeServiceContext> getCompute = mock(Function.class);
    ComputeServiceContext serviceContext = mock(ComputeServiceContext.class);
    ComputeService computeService = mock(ComputeService.class);
    TemplateBuilder templateBuilder = mock(TemplateBuilder.class);
    Template template = mock(Template.class);
    TemplateOptions templateOptions = mock(TemplateOptions.class);
    when(getCompute.apply(clusterSpec)).thenReturn(serviceContext);
    when(serviceContext.getComputeService()).thenReturn(computeService);
    when(computeService.getContext()).thenReturn(serviceContext);
    when(serviceContext.getBackendType()).thenReturn(TypeToken.class.cast(TypeToken.of(Context.class)));
    when(computeService.templateBuilder()).thenReturn(templateBuilder);
    when(templateBuilder.from((TemplateBuilderSpec) any())).thenReturn(templateBuilder);
    when(templateBuilder.options((TemplateOptions) any())).thenReturn(templateBuilder);
    when(templateBuilder.build()).thenReturn(template);
    when(template.getOptions()).thenReturn(templateOptions);
    Map<Set<String>, Stack<Integer>> reaction = Maps.newHashMap();
    Stack<Integer> nnStack = new Stack<Integer>();
    nnStack.push(new Integer(1));
    reaction.put(nn, nnStack);
    nodeStarterFactory = new TestNodeStarterFactory(reaction);
    BootstrapClusterAction bootstrapper = new BootstrapClusterAction(getCompute, handlerMap, nodeStarterFactory);
    bootstrapper.execute(clusterSpec, null);
    if (nodeStarterFactory != null) {
        nodeStarterFactory.validateCompletion();
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) Configuration(org.apache.commons.configuration.Configuration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) TemplateOptions(org.jclouds.compute.options.TemplateOptions) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) HandlerMapFactory(org.apache.whirr.HandlerMapFactory) Template(org.jclouds.compute.domain.Template) ClusterActionHandlerFactory(org.apache.whirr.service.ClusterActionHandlerFactory) HashSet(java.util.HashSet) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) ClusterSpec(org.apache.whirr.ClusterSpec) ComputeService(org.jclouds.compute.ComputeService) Stack(java.util.Stack) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterActionHandler(org.apache.whirr.service.ClusterActionHandler) TypeToken(com.google.common.reflect.TypeToken) Test(org.junit.Test)

Example 15 with TemplateBuilder

use of org.jclouds.compute.domain.TemplateBuilder in project whirr by apache.

the class BootstrapClusterActionTest method testSubroleNotSupported.

@SuppressWarnings("unchecked")
@Test(expected = IllegalArgumentException.class)
public /**
 * test is the same as previous (SubroleInvoked) except it knows puppet, not puppet:, as the role;
 * the colon in the role def'n is the indication it accepts subroles,
 * so this should throw IllegalArgument when we refer to puppet:module...
 */
void testSubroleNotSupported() throws Exception {
    CompositeConfiguration config = new CompositeConfiguration();
    if (System.getProperty("config") != null) {
        config.addConfiguration(new PropertiesConfiguration(System.getProperty("config")));
    }
    Configuration conf = new PropertiesConfiguration();
    conf.addProperty("whirr.service-name", "test-service");
    conf.addProperty("whirr.cluster-name", "test-cluster");
    conf.addProperty("whirr.instance-templates", "1 puppet:module::manifest+something-else");
    conf.addProperty("whirr.provider", "ec2");
    config.addConfiguration(conf);
    ClusterSpec clusterSpec = ClusterSpec.withTemporaryKeys(conf);
    Set<String> nn = new HashSet<String>();
    nn.add("puppet:module::manifest");
    nn.add("something-else");
    TestNodeStarterFactory nodeStarterFactory = null;
    ClusterActionHandlerFactory puppetHandlerFactory = mock(ClusterActionHandlerFactory.class);
    ClusterActionHandler handler = mock(ClusterActionHandler.class);
    when(puppetHandlerFactory.getRolePrefix()).thenReturn("puppet");
    when(handler.getRole()).thenReturn("something-else");
    LoadingCache<String, ClusterActionHandler> handlerMap = new HandlerMapFactory().create(ImmutableSet.of(puppetHandlerFactory), ImmutableSet.of(handler));
    Function<ClusterSpec, ComputeServiceContext> getCompute = mock(Function.class);
    ComputeServiceContext serviceContext = mock(ComputeServiceContext.class);
    ComputeService computeService = mock(ComputeService.class);
    TemplateBuilder templateBuilder = mock(TemplateBuilder.class);
    Template template = mock(Template.class);
    when(getCompute.apply(clusterSpec)).thenReturn(serviceContext);
    when(serviceContext.getComputeService()).thenReturn(computeService);
    when(computeService.templateBuilder()).thenReturn(templateBuilder);
    when(templateBuilder.from((TemplateBuilderSpec) any())).thenReturn(templateBuilder);
    when(templateBuilder.options((TemplateOptions) any())).thenReturn(templateBuilder);
    when(templateBuilder.build()).thenReturn(template);
    Map<Set<String>, Stack<Integer>> reaction = Maps.newHashMap();
    Stack<Integer> nnStack = new Stack<Integer>();
    nnStack.push(1);
    reaction.put(nn, nnStack);
    nodeStarterFactory = new TestNodeStarterFactory(reaction);
    BootstrapClusterAction bootstrapper = new BootstrapClusterAction(getCompute, handlerMap, nodeStarterFactory);
    bootstrapper.execute(clusterSpec, null);
    if (nodeStarterFactory != null) {
        nodeStarterFactory.validateCompletion();
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) Configuration(org.apache.commons.configuration.Configuration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) ClusterSpec(org.apache.whirr.ClusterSpec) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ComputeService(org.jclouds.compute.ComputeService) HandlerMapFactory(org.apache.whirr.HandlerMapFactory) Template(org.jclouds.compute.domain.Template) Stack(java.util.Stack) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterActionHandler(org.apache.whirr.service.ClusterActionHandler) ClusterActionHandlerFactory(org.apache.whirr.service.ClusterActionHandlerFactory) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)16 ComputeService (org.jclouds.compute.ComputeService)13 Template (org.jclouds.compute.domain.Template)11 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)9 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)8 RunNodesException (org.jclouds.compute.RunNodesException)7 TemplateOptions (org.jclouds.compute.options.TemplateOptions)7 Statement (org.jclouds.scriptbuilder.domain.Statement)6 ImmutableSet (com.google.common.collect.ImmutableSet)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 Stack (java.util.Stack)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)4 Configuration (org.apache.commons.configuration.Configuration)4 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)4 ClusterSpec (org.apache.whirr.ClusterSpec)4 ClusterActionHandler (org.apache.whirr.service.ClusterActionHandler)4 RunScriptOnNodesException (org.jclouds.compute.RunScriptOnNodesException)4 Test (org.junit.Test)4