Search in sources :

Example 1 with TemplateBuilder

use of org.jclouds.compute.domain.TemplateBuilder 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 TemplateBuilder

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

the class AcquireActionScript method execute.

@Override
public void execute() throws RunNodesException {
    final ResourceContainer container = this.action.getSourceResourceContainer();
    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(AcquireActionScript.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));
    AcquireActionScript.LOGGER.info(String.format("Acquired 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 3 with TemplateBuilder

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

the class BootstrapTemplateTest method assertSpotPriceIs.

@SuppressWarnings("unchecked")
private void assertSpotPriceIs(ClusterSpec clusterSpec, final String templateGroup, float spotPrice) throws MalformedURLException {
    InstanceTemplate instanceTemplate = getOnlyElement(filter(clusterSpec.getInstanceTemplates(), new Predicate<InstanceTemplate>() {

        private Joiner plusJoiner = Joiner.on("+");

        @Override
        public boolean apply(InstanceTemplate group) {
            return plusJoiner.join(group.getRoles()).equals(templateGroup);
        }
    }));
    ComputeService computeService = mock(AWSEC2ComputeService.class);
    ComputeServiceContext context = mock(ComputeServiceContext.class);
    when(computeService.getContext()).thenReturn(context);
    when(context.getComputeService()).thenReturn(computeService);
    TemplateBuilder templateBuilder = mock(TemplateBuilder.class);
    when(computeService.templateBuilder()).thenReturn(templateBuilder);
    when(templateBuilder.from((TemplateBuilderSpec) any())).thenReturn(templateBuilder);
    when(templateBuilder.options((TemplateOptions) any())).thenReturn(templateBuilder);
    Template template = mock(Template.class);
    TemplateOptions options = mock(TemplateOptions.class);
    Image image = mock(Image.class);
    when(templateBuilder.build()).thenReturn(template);
    when(template.getOptions()).thenReturn(options);
    when(template.getImage()).thenReturn(image);
    AWSEC2TemplateOptions awsEec2TemplateOptions = mock(AWSEC2TemplateOptions.class);
    when(options.as((Class<TemplateOptions>) any())).thenReturn(awsEec2TemplateOptions);
    BootstrapTemplate.build(clusterSpec, computeService, statementBuilder, instanceTemplate);
    verify(awsEec2TemplateOptions).spotPrice(spotPrice);
}
Also used : Joiner(com.google.common.base.Joiner) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) TemplateOptions(org.jclouds.compute.options.TemplateOptions) AWSEC2TemplateOptions(org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions) Image(org.jclouds.compute.domain.Image) AWSEC2TemplateOptions(org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions) AWSEC2ComputeService(org.jclouds.aws.ec2.compute.AWSEC2ComputeService) ComputeService(org.jclouds.compute.ComputeService) InstanceTemplate(org.apache.whirr.InstanceTemplate) Predicate(com.google.common.base.Predicate) InstanceTemplate(org.apache.whirr.InstanceTemplate) Template(org.jclouds.compute.domain.Template)

Example 4 with TemplateBuilder

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

the class BootstrapClusterActionTest method testDoActionRetriesSucceed.

@SuppressWarnings("unchecked")
@Test
public void testDoActionRetriesSucceed() 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 hadoop-namenode+hadoop-jobtracker,4 hadoop-datanode+hadoop-tasktracker");
    conf.addProperty("whirr.instance-templates-max-percent-failures", "60 hadoop-datanode+hadoop-tasktracker");
    conf.addProperty("whirr.provider", "ec2");
    config.addConfiguration(conf);
    ClusterSpec clusterSpec = ClusterSpec.withTemporaryKeys(conf);
    Set<String> jtnn = new HashSet<String>();
    jtnn.add("hadoop-jobtracker");
    jtnn.add("hadoop-namenode");
    Set<String> dntt = new HashSet<String>();
    dntt.add("hadoop-datanode");
    dntt.add("hadoop-tasktracker");
    TestNodeStarterFactory nodeStarterFactory = null;
    ClusterActionHandler handler = mock(ClusterActionHandler.class);
    LoadingCache<String, ClusterActionHandler> handlerMap = convertMapToLoadingCache(ImmutableMap.<String, ClusterActionHandler>builder().put("hadoop-jobtracker", handler).put("hadoop-namenode", handler).put("hadoop-datanode", handler).put("hadoop-tasktracker", handler).build());
    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);
    // here is a scenario when jt+nn fails once, then the retry is successful
    // and from the dn+tt one node fails, then the retry is successful
    Map<Set<String>, Stack<Integer>> reaction = Maps.newHashMap();
    Stack<Integer> jtnnStack = new Stack<Integer>();
    // then ok
    jtnnStack.push(1);
    // initially fail
    jtnnStack.push(0);
    reaction.put(jtnn, jtnnStack);
    Stack<Integer> ddttStack = new Stack<Integer>();
    // 3 from 4, just enough
    ddttStack.push(3);
    reaction.put(dntt, ddttStack);
    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) TemplateOptions(org.jclouds.compute.options.TemplateOptions) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ComputeService(org.jclouds.compute.ComputeService) Template(org.jclouds.compute.domain.Template) Stack(java.util.Stack) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterActionHandler(org.apache.whirr.service.ClusterActionHandler) TypeToken(com.google.common.reflect.TypeToken) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with TemplateBuilder

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

the class BootstrapClusterActionTest method testDoActionRetriesExceeds.

@SuppressWarnings("unchecked")
@Test(expected = IOException.class)
public void testDoActionRetriesExceeds() 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 hadoop-namenode+hadoop-jobtracker,4 hadoop-datanode+hadoop-tasktracker");
    conf.addProperty("whirr.instance-templates-max-percent-failures", "60 hadoop-datanode+hadoop-tasktracker");
    conf.addProperty("whirr.provider", "ec2");
    config.addConfiguration(conf);
    ClusterSpec clusterSpec = ClusterSpec.withTemporaryKeys(conf);
    Set<String> jtnn = new HashSet<String>();
    jtnn.add("hadoop-jobtracker");
    jtnn.add("hadoop-namenode");
    Set<String> dntt = new HashSet<String>();
    dntt.add("hadoop-datanode");
    dntt.add("hadoop-tasktracker");
    TestNodeStarterFactory nodeStarterFactory = null;
    ClusterActionHandler handler = mock(ClusterActionHandler.class);
    LoadingCache<String, ClusterActionHandler> handlerMap = convertMapToLoadingCache(ImmutableMap.<String, ClusterActionHandler>builder().put("hadoop-jobtracker", handler).put("hadoop-namenode", handler).put("hadoop-datanode", handler).put("hadoop-tasktracker", handler).build());
    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);
    // here is a scenario when jt+nn does not fail
    // but the dn+tt one node fails 3, then in the retry fails 2
    // at the end result, the cluster will fail, throwing IOException
    Map<Set<String>, Stack<Integer>> reaction = Maps.newHashMap();
    Stack<Integer> jtnnStack = new Stack<Integer>();
    jtnnStack.push(new Integer(1));
    reaction.put(jtnn, jtnnStack);
    Stack<Integer> ddttStack = new Stack<Integer>();
    // 1 from 4, retryRequired
    ddttStack.push(new Integer(1));
    // 1 from 4, still retryRequired
    ddttStack.push(new Integer(1));
    reaction.put(dntt, ddttStack);
    nodeStarterFactory = new TestNodeStarterFactory(reaction);
    BootstrapClusterAction bootstrapper = new BootstrapClusterAction(getCompute, handlerMap, nodeStarterFactory);
    // this should file with too many retries
    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) TemplateOptions(org.jclouds.compute.options.TemplateOptions) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ComputeService(org.jclouds.compute.ComputeService) Template(org.jclouds.compute.domain.Template) Stack(java.util.Stack) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterActionHandler(org.apache.whirr.service.ClusterActionHandler) TypeToken(com.google.common.reflect.TypeToken) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)15 ComputeService (org.jclouds.compute.ComputeService)12 Template (org.jclouds.compute.domain.Template)10 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)9 RunNodesException (org.jclouds.compute.RunNodesException)7 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)7 TemplateOptions (org.jclouds.compute.options.TemplateOptions)6 Statement (org.jclouds.scriptbuilder.domain.Statement)5 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