@Test(dataProvider = "osNotSupported", expectedExceptions = NoSuchElementException.class)
 public void testTemplateBuilderCannotFind(OsFamilyVersion64Bit matrix)
     throws InterruptedException {
   TemplateBuilder builder =
       context
           .getComputeService()
           .templateBuilder()
           .osFamily(matrix.family)
           .os64Bit(matrix.is64Bit);
   if (!matrix.version.equals("")) builder.osVersionMatches("^" + matrix.version + "$");
   builder.build();
 }
 @Test(dataProvider = "osSupported")
 public void testTemplateBuilderCanFind(OsFamilyVersion64Bit matrix) throws InterruptedException {
   TemplateBuilder builder =
       context
           .getComputeService()
           .templateBuilder()
           .osFamily(matrix.family)
           .os64Bit(matrix.is64Bit);
   if (!matrix.version.equals("")) builder.osVersionMatches("^" + matrix.version + "$");
   Template template = builder.build();
   if (!matrix.version.equals(""))
     assertEquals(template.getImage().getOperatingSystem().getVersion(), matrix.version);
   assertEquals(template.getImage().getOperatingSystem().is64Bit(), matrix.is64Bit);
   assertEquals(template.getImage().getOperatingSystem().getFamily(), matrix.family);
 }
 @Override
 protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) {
   return template
       .osFamily(UBUNTU)
       .osVersionMatches("1[10].[10][04]")
       .imageNameMatches(".*w/ None.*");
 }
 // 128MB is perhaps too little ram
 @Override
 protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) {
   return template
       .minRam(512)
       .osFamily(OsFamily.UBUNTU)
       .osVersionMatches("1[10].[10][04]")
       .os64Bit(true);
 }
  @SuppressWarnings("unchecked")
  @Test
  public void testOptionsUsesDefaultTemplateBuilder() {
    TemplateOptions options = provideTemplateOptions();
    TemplateOptions from = provideTemplateOptions();

    Supplier<Set<? extends Location>> locations =
        Suppliers.<Set<? extends Location>>ofInstance(ImmutableSet.<Location>of());
    Supplier<Set<? extends Image>> images =
        Suppliers.<Set<? extends Image>>ofInstance(ImmutableSet.<Image>of());
    Supplier<Set<? extends Hardware>> hardwares =
        Suppliers.<Set<? extends Hardware>>ofInstance(ImmutableSet.<Hardware>of());
    Location defaultLocation = createMock(Location.class);
    Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
    Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
    TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);

    expect(templateBuilderProvider.get()).andReturn(defaultTemplate);
    expect(defaultTemplate.options(from)).andReturn(defaultTemplate);
    expect(defaultTemplate.build()).andReturn(null);
    expect(optionsProvider.get()).andReturn(from).atLeastOnce();

    replay(defaultTemplate);
    replay(defaultLocation);
    replay(optionsProvider);
    replay(templateBuilderProvider);

    TemplateBuilderImpl template =
        createTemplateBuilder(
            null,
            locations,
            images,
            hardwares,
            defaultLocation,
            optionsProvider,
            templateBuilderProvider);

    template.options(options).build();

    verify(defaultTemplate);
    verify(defaultLocation);
    verify(optionsProvider);
    verify(templateBuilderProvider);
  }
 /** The default template if none is provided. */
 @Provides
 @Named("DEFAULT")
 protected TemplateBuilder provideTemplateOptionallyFromProperties(
     Injector injector,
     TemplateBuilder template,
     @Provider String provider,
     ValueOfConfigurationKeyOrNull config) {
   String templateString = config.apply(provider + ".template");
   if (templateString == null) templateString = config.apply(TEMPLATE);
   if (templateString != null) {
     template.from(templateString);
   } else {
     template.osFamily(UBUNTU).osVersionMatches("1[012].[01][04]").os64Bit(true);
   }
   String imageId = config.apply(provider + ".image-id");
   if (imageId == null) imageId = config.apply(IMAGE_ID);
   if (imageId != null) template.imageId(imageId);
   return template;
 }
  public void testCreateNodeWithGroupEncodedIntoNameThenStoreCredentials() {
    String group = "foo";
    String name = "container" + new Random().nextInt();

    Template template = templateBuilder.imageId(defaultImage.id()).build();

    DockerTemplateOptions options = template.getOptions().as(DockerTemplateOptions.class);
    options.env(ImmutableList.of("ROOT_PASSWORD=password"));
    guest = adapter.createNodeWithGroupEncodedIntoName(group, name, template);
    assertEquals(guest.getNodeId(), guest.getNode().id());
  }
 /** The default template if none is provided. */
 @Provides
 @Named("DEFAULT")
 protected TemplateBuilder provideTemplateOptionallyFromProperties(
     Injector injector,
     TemplateBuilder template,
     @Provider String provider,
     ValueOfConfigurationKeyOrNull config) {
   template = provideTemplate(injector, template);
   String imageId = config.apply(provider + ".image-id");
   if (imageId == null) imageId = config.apply(ComputeServiceConstants.PROPERTY_IMAGE_ID);
   if (imageId != null) template.imageId(imageId);
   return template;
 }
 protected Template buildTemplate(TemplateBuilder templateBuilder) {
   return templateBuilder.build();
 }
 protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) {
   return template.osFamily(UBUNTU).osVersionMatches("1[012].[01][04]").os64Bit(true);
 }
  @Override
  protected Object doExecute() throws Exception {
    ComputeService service = null;
    try {
      service = getComputeService();
    } catch (Throwable t) {
      System.err.println(t.getMessage());
      return null;
    }

    TemplateBuilder builder = service.templateBuilder();
    builder.any();
    if (smallest) {
      builder.smallest();
    }
    if (fastest) {
      builder.fastest();
    }
    if (biggest) {
      builder.biggest();
    }
    if (locationId != null) {
      builder.locationId(locationId);
    }
    if (imageId != null) {
      builder.imageId(imageId);
    }
    if (hardwareId != null) {
      builder.hardwareId(hardwareId);
    }

    if (osFamily != null) {
      builder.osFamily(OsFamily.fromValue(osFamily));
    }

    if (osVersion != null) {
      builder.osVersionMatches(osVersion);
    }

    TemplateOptions options = service.templateOptions();
    List<Statement> statements = Lists.newLinkedList();

    if (adminAccess) {
      statements.add(AdminAccess.standard());
    }
    if (recipes != null) {
      for (String recipe : recipes) {
        statements.add(recipeManager.createStatement(recipe, group));
      }
    }
    if (ec2SecurityGroups != null) {
      options.as(EC2TemplateOptions.class).securityGroups(ec2SecurityGroups);
    }
    if (ec2KeyPair != null) {
      options.as(EC2TemplateOptions.class).keyPair(ec2KeyPair);
    }
    if (ec2NoKeyPair != null) {
      options.as(EC2TemplateOptions.class).noKeyPair();
    }

    Set<? extends NodeMetadata> metadatas = null;

    if (!statements.isEmpty()) {
      options.runScript(new StatementList(statements));
    }

    try {
      metadatas = service.createNodesInGroup(group, number, builder.options(options).build());
    } catch (RunNodesException ex) {
      System.out.println("Failed to create nodes:" + ex.getMessage());
    }

    if (metadatas != null && !metadatas.isEmpty()) {
      System.out.println("Created nodes:");
      printNodes(service, metadatas, System.out);

      for (NodeMetadata node : metadatas) {
        for (String cacheKey : ServiceHelper.findCacheKeysForService(service)) {
          cacheProvider
              .getProviderCacheForType(Constants.ACTIVE_NODE_CACHE)
              .put(cacheKey, node.getId());
          cacheProvider
              .getProviderCacheForType(Constants.INACTIVE_NODE_CACHE)
              .put(cacheKey, node.getId());
          cacheProvider
              .getProviderCacheForType(Constants.SUSPENDED_NODE_CACHE)
              .put(cacheKey, node.getId());
        }
      }
    }

    return null;
  }
  @Test
  public void testMatchesHardwareWithIdPredicate() {

    final Supplier<Set<? extends Location>> locations =
        Suppliers.<Set<? extends Location>>ofInstance(ImmutableSet.<Location>of(region));
    final Supplier<Set<? extends Image>> images =
        Suppliers.<Set<? extends Image>>ofInstance(
            ImmutableSet.<Image>of(
                new ImageBuilder()
                    .ids("Ubuntu 11.04 x64")
                    .name("Ubuntu 11.04 x64")
                    .description("Ubuntu 11.04 x64")
                    .location(region)
                    .status(Status.AVAILABLE)
                    .operatingSystem(
                        OperatingSystem.builder()
                            .name("Ubuntu 11.04 x64")
                            .description("Ubuntu 11.04 x64")
                            .is64Bit(true)
                            .version("11.04")
                            .family(OsFamily.UBUNTU)
                            .build())
                    .build(),
                new ImageBuilder()
                    .ids("Ubuntu 11.04 64-bit")
                    .name("Ubuntu 11.04 64-bit")
                    .description("Ubuntu 11.04 64-bit")
                    .location(region)
                    .status(Status.AVAILABLE)
                    .operatingSystem(
                        OperatingSystem.builder()
                            .name("Ubuntu 11.04 64-bit")
                            .description("Ubuntu 11.04 64-bit")
                            .is64Bit(true)
                            .version("11.04")
                            .family(OsFamily.UBUNTU)
                            .build())
                    .build()));

    final Supplier<Set<? extends Hardware>> hardwares =
        Suppliers.<Set<? extends Hardware>>ofInstance(
            ImmutableSet.<Hardware>of(
                new HardwareBuilder()
                    .ids(
                        String.format(
                            "datacenter(%s)platform(%s)cpuCores(%d)memorySizeMB(%d)diskSizeGB(%d)",
                            "Falkenberg", "Xen", 1, 512, 5))
                    .ram(512)
                    .processors(ImmutableList.of(new Processor(1, 1.0)))
                    .volumes(ImmutableList.<Volume>of(new VolumeImpl((float) 5, true, true)))
                    .hypervisor("Xen")
                    .location(region)
                    .supportsImage(ImagePredicates.idIn(ImmutableSet.of("Ubuntu 11.04 x64")))
                    .build(),
                new HardwareBuilder()
                    .ids(
                        String.format(
                            "datacenter(%s)platform(%s)cpuCores(%d)memorySizeMB(%d)diskSizeGB(%d)",
                            "Falkenberg", "OpenVZ", 1, 512, 5))
                    .ram(512)
                    .processors(ImmutableList.of(new Processor(1, 1.0)))
                    .volumes(ImmutableList.<Volume>of(new VolumeImpl((float) 5, true, true)))
                    .hypervisor("OpenVZ")
                    .location(region)
                    .supportsImage(ImagePredicates.idIn(ImmutableSet.of("Ubuntu 11.04 64-bit")))
                    .build()));

    final Provider<TemplateOptions> optionsProvider =
        new Provider<TemplateOptions>() {

          @Override
          public TemplateOptions get() {
            return new TemplateOptions();
          }
        };
    Provider<TemplateBuilder> templateBuilderProvider =
        new Provider<TemplateBuilder>() {

          @Override
          public TemplateBuilder get() {
            return createTemplateBuilder(
                null, locations, images, hardwares, region, optionsProvider, this);
          }
        };

    TemplateBuilder templateBuilder =
        templateBuilderProvider
            .get()
            .minRam(512)
            .osFamily(OsFamily.UBUNTU)
            .hypervisorMatches("OpenVZ")
            .osVersionMatches("1[10].[10][04]")
            .os64Bit(true);

    assertEquals(
        templateBuilder.toString(),
        "{minRam=512, minRam=512, osFamily=ubuntu, osVersion=1[10].[10][04], os64Bit=true, hypervisor=OpenVZ}");

    Template template = templateBuilder.build();
    assertEquals(template.getHardware().getHypervisor(), "OpenVZ");
    assertEquals(template.getImage().getId(), "Ubuntu 11.04 64-bit");
  }
  @Test
  public void testFromSpecWithLoginUser() {

    final Supplier<Set<? extends Location>> locations =
        Suppliers.<Set<? extends Location>>ofInstance(ImmutableSet.<Location>of(region));
    final Supplier<Set<? extends Image>> images =
        Suppliers.<Set<? extends Image>>ofInstance(
            ImmutableSet.<Image>of(
                new ImageBuilder()
                    .id("us-east-2/ami-ffff")
                    .providerId("ami-ffff")
                    .name("Ubuntu 11.04 x64")
                    .description("Ubuntu 11.04 x64")
                    .location(region2)
                    .status(Status.AVAILABLE)
                    .operatingSystem(
                        OperatingSystem.builder()
                            .name("Ubuntu 11.04 x64")
                            .description("Ubuntu 11.04 x64")
                            .is64Bit(true)
                            .version("11.04")
                            .family(OsFamily.UBUNTU)
                            .build())
                    .build()));

    final Supplier<Set<? extends Hardware>> hardwares =
        Suppliers.<Set<? extends Hardware>>ofInstance(
            ImmutableSet.<Hardware>of(
                new HardwareBuilder()
                    .ids("m1.small")
                    .ram(512)
                    .processors(ImmutableList.of(new Processor(1, 1.0)))
                    .volumes(ImmutableList.<Volume>of(new VolumeImpl((float) 5, true, true)))
                    .build()));

    final Provider<TemplateOptions> optionsProvider =
        new Provider<TemplateOptions>() {

          @Override
          public TemplateOptions get() {
            return new TemplateOptions();
          }
        };
    Provider<TemplateBuilder> templateBuilderProvider =
        new Provider<TemplateBuilder>() {

          @Override
          public TemplateBuilder get() {
            return createTemplateBuilder(
                null, locations, images, hardwares, region, optionsProvider, this);
          }
        };

    TemplateBuilder templateBuilder =
        templateBuilderProvider
            .get()
            .from(
                "hardwareId=m1.small,imageId=us-east-2/ami-ffff,loginUser=user:Password01,authenticateSudo=true");

    assertEquals(templateBuilder.toString(), "{imageId=us-east-2/ami-ffff, hardwareId=m1.small}");

    Template template = templateBuilder.build();
    assertEquals(template.getLocation().getId(), "us-east-2");
    assertEquals(template.getOptions().getLoginUser(), "user");
    assertEquals(template.getOptions().getLoginPassword(), "Password01");
    assertEquals(template.getOptions().getLoginPrivateKey(), null);
    assertEquals(template.getOptions().shouldAuthenticateSudo(), Boolean.TRUE);
  }
Exemple #14
0
  public void buildTemplate() {
    IaasProvider iaasInfo = getIaasProvider();
    if (iaasInfo.getComputeService() == null) {
      String msg = "Compute service is null for IaaS provider: " + iaasInfo.getName();
      log.fatal(msg);
      throw new CloudControllerException(msg);
    }

    TemplateBuilder templateBuilder = iaasInfo.getComputeService().templateBuilder();

    // set image id specified
    templateBuilder.imageId(iaasInfo.getImage());

    if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
      Set<? extends Location> locations = iaasInfo.getComputeService().listAssignableLocations();
      for (Location location : locations) {
        if (location.getScope().toString().equalsIgnoreCase(CloudControllerConstants.ZONE_ELEMENT)
            && location
                .getId()
                .equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
          templateBuilder.locationId(location.getId());
          log.info(
              "ZONE has been set as "
                  + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
                  + " with id: "
                  + location.getId());
          break;
        }
      }
    }

    if (iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE) != null) {
      // set instance type eg: m1.large
      templateBuilder.hardwareId(iaasInfo.getProperty(CloudControllerConstants.INSTANCE_TYPE));
    }

    // build the Template
    Template template = templateBuilder.build();

    if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
      if (!template
          .getLocation()
          .getId()
          .equals(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE))) {
        log.warn(
            "couldn't find assignable ZONE of id :"
                + iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE)
                + " in the IaaS. "
                + "Hence using the default location as "
                + template.getLocation().getScope().toString()
                + " with the id "
                + template.getLocation().getId());
      }
    }

    // if you wish to auto assign IPs, instance spawning call should be
    // blocking, but if you
    // wish to assign IPs manually, it can be non-blocking.
    // is auto-assign-ip mode or manual-assign-ip mode?
    boolean blockUntilRunning =
        Boolean.parseBoolean(iaasInfo.getProperty(CloudControllerConstants.AUTO_ASSIGN_IP));
    template.getOptions().as(TemplateOptions.class).blockUntilRunning(blockUntilRunning);

    // this is required in order to avoid creation of additional security
    // groups by jclouds.
    template.getOptions().as(TemplateOptions.class).inboundPorts();

    // set EC2 specific options

    if (iaasInfo.getProperty(CloudControllerConstants.ASSOCIATE_PUBLIC_IP_ADDRESS) != null) {
      boolean associatePublicIp =
          Boolean.parseBoolean(
              iaasInfo.getProperty(CloudControllerConstants.ASSOCIATE_PUBLIC_IP_ADDRESS));
      if (associatePublicIp) {
        template.getOptions().as(AWSEC2TemplateOptions.class).associatePublicIpAddress();
      }
    }

    if (iaasInfo.getProperty(CloudControllerConstants.SUBNET_ID) != null) {
      template
          .getOptions()
          .as(AWSEC2TemplateOptions.class)
          .subnetId(iaasInfo.getProperty(CloudControllerConstants.SUBNET_ID));
    }

    if (iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE) != null) {
      template
          .getOptions()
          .as(AWSEC2TemplateOptions.class)
          .placementGroup(iaasInfo.getProperty(CloudControllerConstants.AVAILABILITY_ZONE));
    }

    // security group names
    if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUPS) != null) {
      template
          .getOptions()
          .as(AWSEC2TemplateOptions.class)
          .securityGroups(
              iaasInfo
                  .getProperty(CloudControllerConstants.SECURITY_GROUPS)
                  .split(CloudControllerConstants.ENTRY_SEPARATOR));
    }

    // ability to define tags
    if (iaasInfo.getProperty(CloudControllerConstants.TAGS) != null) {
      template
          .getOptions()
          .as(AWSEC2TemplateOptions.class)
          .tags(
              Arrays.asList(
                  iaasInfo
                      .getProperty(CloudControllerConstants.TAGS)
                      .split(CloudControllerConstants.ENTRY_SEPARATOR)));
    }

    // ability to define tags with Key-value pairs
    Map<String, String> keyValuePairTagsMap = new HashMap<String, String>();

    for (String propertyKey : iaasInfo.getProperties().keySet()) {
      if (propertyKey.startsWith(CloudControllerConstants.TAGS_AS_KEY_VALUE_PAIRS_PREFIX)) {
        keyValuePairTagsMap.put(
            propertyKey.substring(CloudControllerConstants.TAGS_AS_KEY_VALUE_PAIRS_PREFIX.length()),
            iaasInfo.getProperties().get(propertyKey));
        template.getOptions().as(AWSEC2TemplateOptions.class).userMetadata(keyValuePairTagsMap);
      }
    }

    if (iaasInfo.getProperty(CloudControllerConstants.SECURITY_GROUP_IDS) != null) {
      template
          .getOptions()
          .as(AWSEC2TemplateOptions.class)
          .securityGroupIds(
              iaasInfo
                  .getProperty(CloudControllerConstants.SECURITY_GROUP_IDS)
                  .split(CloudControllerConstants.ENTRY_SEPARATOR));
    }

    if (iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR) != null) {
      template
          .getOptions()
          .as(AWSEC2TemplateOptions.class)
          .keyPair(iaasInfo.getProperty(CloudControllerConstants.KEY_PAIR));
    }

    if (iaasInfo.getNetworkInterfaces() != null) {
      List<String> networks = new ArrayList<String>(iaasInfo.getNetworkInterfaces().length);
      for (NetworkInterface ni : iaasInfo.getNetworkInterfaces()) {
        networks.add(ni.getNetworkUuid());
      }
      template.getOptions().as(AWSEC2TemplateOptions.class).networks(networks);
    }

    // set Template
    iaasInfo.setTemplate(template);
  }
  @SuppressWarnings("unchecked")
  private void assertRegionAndZoneForLocation(Location location, String region, String zone) {
    String imageId = "ami1";
    String instanceCreatedId = "instance1";
    // setup mocks
    TemplateBuilder templateBuilder = createMock(TemplateBuilder.class);
    EC2CreateNodesInGroupThenAddToSet strategy = setupStrategy(templateBuilder);
    InputParams input = new InputParams(location);
    InstanceClient instanceClient = createMock(InstanceClient.class);
    RunInstancesOptions ec2Options = createMock(RunInstancesOptions.class);
    RunningInstance instance = createMock(RunningInstance.class);
    Reservation<? extends RunningInstance> reservation =
        new Reservation<RunningInstance>(
            region,
            ImmutableSet.<String>of(),
            ImmutableSet.<RunningInstance>of(instance),
            "ownerId",
            "requesterId",
            "reservationId");
    NodeMetadata nodeMetadata = createMock(NodeMetadata.class);

    // setup expectations
    expect(templateBuilder.fromTemplate(input.template)).andReturn(templateBuilder);
    expect(templateBuilder.build()).andReturn(input.template);
    expect(strategy.client.getInstanceServices()).andReturn(instanceClient).atLeastOnce();
    expect(
            strategy.createKeyPairAndSecurityGroupsAsNeededAndReturncustomize.execute(
                region, input.tag, input.template))
        .andReturn(ec2Options);
    expect(input.template.getLocation()).andReturn(input.location).atLeastOnce();
    expect(input.template.getImage()).andReturn(input.image).atLeastOnce();
    expect(input.image.getProviderId()).andReturn(imageId).atLeastOnce();
    expect(instanceClient.runInstancesInRegion(region, zone, imageId, 1, input.count, ec2Options))
        .andReturn(Reservation.class.cast(reservation));
    expect(instance.getId()).andReturn(instanceCreatedId).atLeastOnce();
    // simulate a lazy credentials fetch
    Credentials creds = new Credentials("foo", "bar");
    expect(strategy.instanceToCredentials.apply(instance)).andReturn(creds);
    expect(instance.getRegion()).andReturn(region).atLeastOnce();
    expect(strategy.credentialStore.put("node#" + region + "/" + instanceCreatedId, creds))
        .andReturn(null);

    expect(strategy.instancePresent.apply(new RegionAndName(region, instanceCreatedId)))
        .andReturn(true);
    expect(input.template.getOptions()).andReturn(input.options).atLeastOnce();

    expect(strategy.runningInstanceToNodeMetadata.apply(instance)).andReturn(nodeMetadata);
    expect(
            strategy.utils.customizeNodesAndAddToGoodMapOrPutExceptionIntoBadMap(
                eq(input.options),
                containsNodeMetadata(nodeMetadata),
                eq(input.nodes),
                eq(input.badNodes),
                eq(input.customization)))
        .andReturn(null);

    // replay mocks
    replay(templateBuilder);
    replay(instanceClient);
    replay(ec2Options);
    replay(instance);
    replay(nodeMetadata);
    input.replayMe();
    replayStrategy(strategy);

    // run
    strategy.execute(
        input.tag, input.count, input.template, input.nodes, input.badNodes, input.customization);

    // verify mocks
    verify(templateBuilder);
    verify(instanceClient);
    verify(ec2Options);
    verify(instance);
    verify(nodeMetadata);
    input.verifyMe();
    verifyStrategy(strategy);
  }