@Override
 public NodeMetadata createNodeWithGroupEncodedIntoNameThenStoreCredentials(
     String group, String name, Template template, Map<String, Credentials> credentialStore) {
   NodeMetadataBuilder builder = new NodeMetadataBuilder();
   String id = idProvider.get() + "";
   builder.ids(id);
   builder.name(name);
   // using a predictable name so tests will pass
   builder.hostname(group);
   builder.tags(template.getOptions().getTags());
   builder.group(group);
   builder.location(location.get());
   builder.imageId(template.getImage().getId());
   builder.operatingSystem(template.getImage().getOperatingSystem());
   builder.state(NodeState.PENDING);
   builder.publicAddresses(ImmutableSet.<String>of(publicIpPrefix + id));
   builder.privateAddresses(ImmutableSet.<String>of(privateIpPrefix + id));
   Credentials creds = template.getOptions().getOverridingCredentials();
   if (creds == null) creds = new Credentials(null, null);
   if (creds.identity == null) creds = creds.toBuilder().identity("root").build();
   if (creds.credential == null) creds = creds.toBuilder().credential(passwordPrefix + id).build();
   builder.credentials(creds);
   NodeMetadata node = builder.build();
   credentialStore.put("node#" + node.getId(), node.getCredentials());
   nodes.put(node.getId(), node);
   StubComputeServiceDependenciesModule.setState(node, NodeState.RUNNING, 100);
   return node;
 }
  public RunInstancesOptions execute(String region, String group, Template template) {

    RunInstancesOptions instanceOptions =
        getOptionsProvider().get().asType(template.getHardware().getId());

    String keyPairName =
        createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, template.getOptions());

    addSecurityGroups(region, group, template, instanceOptions);
    if (template.getOptions() instanceof EC2TemplateOptions) {

      if (keyPairName != null) instanceOptions.withKeyName(keyPairName);

      byte[] userData = EC2TemplateOptions.class.cast(template.getOptions()).getUserData();

      if (userData != null) instanceOptions.withUserData(userData);

      Set<BlockDeviceMapping> blockDeviceMappings =
          EC2TemplateOptions.class.cast(template.getOptions()).getBlockDeviceMappings();
      if (!blockDeviceMappings.isEmpty()) {
        checkState(
            "ebs".equals(template.getImage().getUserMetadata().get("rootDeviceType")),
            "BlockDeviceMapping only available on ebs boot");
        instanceOptions.withBlockDeviceMappings(blockDeviceMappings);
      }

      String clientToken = EC2TemplateOptions.class.cast(template.getOptions()).getClientToken();

      if (clientToken != null) {
        instanceOptions.withClientToken(clientToken);
      }
    }
    return instanceOptions;
  }
  @Override
  public Map<?, ListenableFuture<Void>> execute(
      String group,
      int count,
      Template template,
      Set<NodeMetadata> goodNodes,
      Map<NodeMetadata, Exception> badNodes,
      Multimap<NodeMetadata, CustomizationResponse> customizationResponses) {

    Template mutableTemplate = template.clone();

    Set<RunningInstance> started = runInstancesAndWarnOnInvisible(group, count, mutableTemplate);
    if (started.size() == 0) {
      logger.warn("<< unable to start instances(%s)", mutableTemplate);
      return ImmutableMap.of();
    }
    populateCredentials(started, template.getOptions());

    if (autoAllocateElasticIps) // before customization as the elastic ips may be needed
    blockUntilRunningAndAssignElasticIpsToInstancesOrPutIntoBadMap(started, badNodes);

    return utils.customizeNodesAndAddToGoodMapOrPutExceptionIntoBadMap(
        mutableTemplate.getOptions(),
        transform(started, runningInstanceToNodeMetadata),
        goodNodes,
        badNodes,
        customizationResponses);
  }
  @Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript")
  public void testCreateTwoNodesWithOneSpecifiedName() throws Exception {
    template = buildTemplate(client.templateBuilder());
    template.getOptions().nodeNames(ImmutableSet.of("first-node"));
    Set<? extends NodeMetadata> nodes;
    try {
      nodes = newTreeSet(client.createNodesInGroup(group, 2, template));
    } catch (RunNodesException e) {
      nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()));
      throw e;
    }

    assertEquals(nodes.size(), 2, "expected two nodes but was " + nodes);
    NodeMetadata node1 = Iterables.getFirst(nodes, null);
    NodeMetadata node2 = Iterables.getLast(nodes, null);
    // credentials aren't always the same
    // assertEquals(node1.getCredentials(), node2.getCredentials());

    assertTrue(
        node1.getName().equals("first-node") || node2.getName().equals("first-node"),
        "one node should be named 'first-node'");
    assertFalse(
        node1.getName().equals("first-node") && node2.getName().equals("first-node"),
        "one node should be named something other than 'first-node");

    this.nodes.addAll(nodes);
  }
  @Test(enabled = true, dependsOnMethods = "testTemplateMatch")
  public void testCreateTwoNodesWithRunScript() throws Exception {
    try {
      client.destroyNodesMatching(NodePredicates.withTag(tag));
    } catch (HttpResponseException e) {
      // TODO hosting.com throws 400 when we try to delete a vApp
    } catch (NoSuchElementException e) {

    }
    template = buildTemplate(client.templateBuilder());

    template
        .getOptions()
        .installPrivateKey(keyPair.get("private"))
        .authorizePublicKey(keyPair.get("public"))
        .runScript(buildScript(template.getImage().getOsFamily()).getBytes());
    try {
      nodes = Sets.newTreeSet(client.runNodesWithTag(tag, 2, template));
    } catch (RunNodesException e) {
      nodes = Sets.newTreeSet(Iterables.concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()));
      throw e;
    }
    assertEquals(nodes.size(), 2);
    checkNodes(nodes, tag);
    NodeMetadata node1 = nodes.first();
    NodeMetadata node2 = nodes.last();
    // credentials aren't always the same
    // assertEquals(node1.getCredentials(), node2.getCredentials());

    assertLocationSameOrChild(node1.getLocation(), template.getLocation());
    assertLocationSameOrChild(node2.getLocation(), template.getLocation());

    assertEquals(node1.getImage(), template.getImage());
    assertEquals(node2.getImage(), template.getImage());
  }
  @Test(enabled = true)
  public void testCreateAndRunAService() throws Exception {

    String tag = this.tag + "service";
    try {
      client.destroyNodesMatching(withTag(tag));
    } catch (Exception e) {

    }

    template =
        client
            .templateBuilder()
            .options(blockOnComplete(false).blockOnPort(8080, 600).inboundPorts(22, 8080))
            .build();
    // note this is a dependency on the template resolution
    template
        .getOptions()
        .runScript(
            RunScriptData.createScriptInstallAndStartJBoss(
                keyPair.get("public"), template.getImage().getOperatingSystem()));
    try {
      NodeMetadata node = getOnlyElement(client.runNodesWithTag(tag, 1, template));

      checkHttpGet(node);
    } finally {
      client.destroyNodesMatching(withTag(tag));
    }
  }
 @Test(enabled = true /* , dependsOnMethods = "testTemplateMatch" */)
 public void testTemplateOptions() throws Exception {
   TemplateOptions options = new TemplateOptions().withMetadata();
   Template t = client.templateBuilder().smallest().options(options).build();
   assert t.getOptions().isIncludeMetadata()
       : "The metadata option should be 'true' " + "for the created template";
 }
  private void refreshTemplate() {
    template = buildTemplate(client.templateBuilder());

    template
        .getOptions()
        .installPrivateKey(keyPair.get("private"))
        .authorizePublicKey(keyPair.get("public"))
        .runScript(buildScript(template.getImage().getOperatingSystem()));
  }
  @Override
  public NodeAndInitialCredentials<ServerInfo> createNodeWithGroupEncodedIntoName(
      String tag, String name, Template template) {
    long bootSize =
        (long) (template.getHardware().getVolumes().get(0).getSize() * 1024 * 1024 * 1024l);
    AffinityType affinityType = AffinityType.HDD;
    if (template.getOptions() instanceof CloudSigmaTemplateOptions) {
      CloudSigmaTemplateOptions options =
          CloudSigmaTemplateOptions.class.cast(template.getOptions());
      affinityType = options.getDiskDriveAffinity();
    }
    logger.debug(
        ">> imaging boot drive source(%s) bytes(%d) affinityType(%s)",
        template.getImage().getId(), bootSize, affinityType);
    DriveInfo drive =
        client.cloneDrive(
            template.getImage().getId(),
            template.getImage().getId(),
            new CloneDriveOptions().size(bootSize).affinity(affinityType));
    boolean success = driveNotClaimed.apply(drive);
    logger.debug("<< image(%s) complete(%s)", drive.getUuid(), success);
    if (!success) {
      client.destroyDrive(drive.getUuid());
      throw new IllegalStateException("could not image drive in time!");
    }

    Server toCreate =
        Servers.small(name, drive.getUuid(), defaultVncPassword)
            .mem(template.getHardware().getRam())
            .cpu((int) (template.getHardware().getProcessors().get(0).getSpeed()))
            .build();

    logger.debug(">> creating server");
    ServerInfo from = client.createServer(toCreate);
    logger.debug("<< created server(%s)", from.getUuid());
    logger.debug(">> starting server(%s)", from.getUuid());
    client.startServer(from.getUuid());
    return new NodeAndInitialCredentials<ServerInfo>(
        from,
        from.getUuid(),
        LoginCredentials.builder().password(defaultVncPassword).authenticateSudo(true).build());
  }
  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());
  }
 @Test
 public void testDefaultTemplateBuilder() throws IOException {
   Template defaultTemplate = view.getComputeService().templateBuilder().build();
   assertEquals(defaultTemplate.getImage().getOperatingSystem().getVersion(), "10.04");
   assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true);
   assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
   assertEquals(getCores(defaultTemplate.getHardware()), 1.0d);
   assertEquals(defaultTemplate.getHardware().getRam(), 1);
   assertEquals(getSpace(defaultTemplate.getHardware()), 25.0d);
   assertEquals(defaultTemplate.getHardware().getVolumes().get(0).getType(), Volume.Type.LOCAL);
   // test that we bound the correct templateoptions in guice
   assertEquals(defaultTemplate.getOptions().getClass(), SoftLayerTemplateOptions.class);
 }
Exemplo n.º 12
0
  protected void createAndRunAServiceInGroup(String group) throws RunNodesException {
    // note that some cloud providers do not support mixed case tag names
    ImmutableMap<String, String> userMetadata = ImmutableMap.<String, String>of("test", group);

    ImmutableSet<String> tags = ImmutableSet.of(group);
    Stopwatch watch = Stopwatch.createStarted();

    template = buildTemplate(client.templateBuilder());
    template
        .getOptions()
        .inboundPorts(22, 8080)
        .blockOnPort(22, 300)
        .userMetadata(userMetadata)
        .tags(tags);

    NodeMetadata node = getOnlyElement(client.createNodesInGroup(group, 1, template));
    long createSeconds = watch.elapsed(TimeUnit.SECONDS);

    final String nodeId = node.getId();

    checkUserMetadataContains(node, userMetadata);
    checkTagsInNodeEquals(node, tags);

    getAnonymousLogger()
        .info(
            format(
                "<< available node(%s) os(%s) in %ss",
                node.getId(), node.getOperatingSystem(), createSeconds));

    watch.reset().start();

    client.runScriptOnNode(nodeId, JettyStatements.install(), nameTask("configure-jetty"));

    long configureSeconds = watch.elapsed(TimeUnit.SECONDS);

    getAnonymousLogger()
        .info(
            format(
                "<< configured node(%s) with %s and jetty %s in %ss",
                nodeId,
                exec(nodeId, "java -fullversion"),
                exec(nodeId, JettyStatements.version()),
                configureSeconds));

    trackAvailabilityOfProcessOnNode(JettyStatements.start(), "start jetty", node);

    client.runScriptOnNode(
        nodeId, JettyStatements.stop(), runAsRoot(false).wrapInInitScript(false));

    trackAvailabilityOfProcessOnNode(JettyStatements.start(), "start jetty", node);
  }
  private void setSecurityGroupOnTemplate(
      final JcloudsLocation location,
      final Template template,
      final SecurityGroupExtension securityApi) {
    SecurityGroup shared;
    Tasks.setBlockingDetails(
        "Loading security group shared by instances in "
            + template.getLocation()
            + " in app "
            + applicationId);
    try {
      shared =
          sharedGroupCache.get(
              template.getLocation(),
              new Callable<SecurityGroup>() {
                @Override
                public SecurityGroup call() throws Exception {
                  return getOrCreateSharedSecurityGroup(template.getLocation(), securityApi);
                }
              });
    } catch (ExecutionException e) {
      throw Throwables.propagate(new Exception(e.getCause()));
    } finally {
      Tasks.resetBlockingDetails();
    }

    Set<String> originalGroups = template.getOptions().getGroups();
    template.getOptions().securityGroups(shared.getName());
    if (!originalGroups.isEmpty()) {
      LOG.info(
          "Replaced configured security groups: configured={}, replaced with={}",
          originalGroups,
          template.getOptions().getGroups());
    } else {
      LOG.debug(
          "Configured security groups at {} to: {}", location, template.getOptions().getGroups());
    }
  }
Exemplo n.º 14
0
 public Set<NodeMetadata> createNodes(int count) throws RunNodesException {
   Set<NodeMetadata> result = Sets.newHashSet();
   Template template =
       mComputeService.templateBuilder().hardwareId(mInstanceType).imageId(mImageId).build();
   template
       .getOptions()
       .as(AWSEC2TemplateOptions.class)
       .keyPair(mkeyPair)
       .securityGroupIds(mSecurityGroup)
       .blockOnPort(22, 60)
       .spotPrice(mMaxBid)
       .tags(Collections.singletonList(mGroupTag));
   result.addAll(mComputeService.createNodesInGroup(mGroupName, count, template));
   return result;
 }
 private LoginCredentials resolveNodeCredentials(Template template) {
   TemplateOptions options = template.getOptions();
   LoginCredentials.Builder credentials =
       LoginCredentials.builder(template.getImage().getDefaultCredentials());
   if (!Strings.isNullOrEmpty(options.getLoginUser())) {
     credentials.user(options.getLoginUser());
   }
   if (!Strings.isNullOrEmpty(options.getLoginPrivateKey())) {
     credentials.privateKey(options.getLoginPrivateKey());
   }
   if (!Strings.isNullOrEmpty(options.getLoginPassword())) {
     credentials.password(options.getLoginPassword());
   }
   if (options.shouldAuthenticateSudo() != null) {
     credentials.authenticateSudo(options.shouldAuthenticateSudo());
   }
   return credentials.build();
 }
  public void testStartCCInstance() throws Exception {

    Template template =
        view.getComputeService()
            .templateBuilder()
            .fromHardware(EC2HardwareBuilder.cc2_8xlarge().build())
            .osFamily(OsFamily.AMZN_LINUX)
            .build();
    assert template != null : "The returned template was null, but it should have a value.";
    assertEquals(template.getHardware().getProviderId(), InstanceType.CC2_8XLARGE);
    assertEquals(template.getImage().getUserMetadata().get("rootDeviceType"), "ebs");
    assertEquals(template.getImage().getUserMetadata().get("virtualizationType"), "hvm");
    assertEquals(template.getImage().getUserMetadata().get("hypervisor"), "xen");

    template
        .getOptions()
        .runScript(Statements.newStatementList(AdminAccess.standard(), InstallJDK.fromOpenJDK()));

    String group = PREFIX + "cccluster";
    view.getComputeService().destroyNodesMatching(NodePredicates.inGroup(group));
    // TODO make this not lookup an explicit region
    client
        .getPlacementGroupApi()
        .get()
        .deletePlacementGroupInRegion(null, "jclouds#" + group + "#us-east-1");

    try {
      Set<? extends NodeMetadata> nodes =
          view.getComputeService().createNodesInGroup(group, 1, template);
      NodeMetadata node = getOnlyElement(nodes);

      getOnlyElement(
          getOnlyElement(
              client.getInstanceApi().get().describeInstancesInRegion(null, node.getProviderId())));

    } catch (RunNodesException e) {
      System.err.println(e.getNodeErrors().keySet());
      Throwables.propagate(e);
    } finally {
      view.getComputeService().destroyNodesMatching(NodePredicates.inGroup(group));
    }
  }
Exemplo n.º 17
0
  @Test(enabled = true, dependsOnMethods = "testCompareSizes")
  public void testConcurrentUseOfComputeServiceToCreateNodes() throws Exception {
    final long timeoutMs = 20 * 60 * 1000;
    List<String> groups = Lists.newArrayList();
    List<ListenableFuture<NodeMetadata>> futures = Lists.newArrayList();
    ListeningExecutorService userExecutor =
        context
            .utils()
            .injector()
            .getInstance(
                Key.get(ListeningExecutorService.class, Names.named(PROPERTY_USER_THREADS)));

    try {
      for (int i = 0; i < 2; i++) {
        final int groupNum = i;
        final String group = "twin" + groupNum;
        groups.add(group);
        template = buildTemplate(client.templateBuilder());
        template.getOptions().inboundPorts(22, 8080).blockOnPort(22, 300 + groupNum);
        ListenableFuture<NodeMetadata> future =
            userExecutor.submit(
                new Callable<NodeMetadata>() {
                  public NodeMetadata call() throws Exception {
                    NodeMetadata node =
                        getOnlyElement(client.createNodesInGroup(group, 1, template));
                    getAnonymousLogger().info("Started node " + node.getId());
                    return node;
                  }
                });
        futures.add(future);
      }

      ListenableFuture<List<NodeMetadata>> compoundFuture = Futures.allAsList(futures);
      compoundFuture.get(timeoutMs, TimeUnit.MILLISECONDS);

    } finally {
      for (String group : groups) {
        client.destroyNodesMatching(inGroup(group));
      }
    }
  }
 @Override
 public NodeMetadata createNodeWithGroupEncodedIntoName(
     String group, String name, Template template) {
   Server addedServer = null;
   boolean notStarted = true;
   int numOfRetries = 20;
   GetIpListOptions unassignedIps =
       new GetIpListOptions()
           .onlyUnassigned()
           .inDatacenter(template.getLocation().getId())
           .onlyWithType(IpType.PUBLIC);
   // lock-free consumption of a shared resource: IP address pool
   while (notStarted) { // TODO: replace with Predicate-based thread
     // collision avoidance for simplicity
     Set<Ip> availableIps = client.getIpServices().getIpList(unassignedIps);
     if (availableIps.isEmpty()) throw new RuntimeException("No IPs available on this identity.");
     int ipIndex = new SecureRandom().nextInt(availableIps.size());
     Ip availableIp = Iterables.get(availableIps, ipIndex);
     try {
       addedServer = addServer(name, template, availableIp);
       notStarted = false;
     } catch (Exception e) {
       if (--numOfRetries == 0) Throwables.propagate(e);
       notStarted = true;
     }
   }
   if (template.getOptions().shouldBlockUntilRunning()) {
     serverLatestJobCompleted.apply(addedServer);
     client.getServerServices().power(addedServer.getName(), PowerCommand.START);
     serverLatestJobCompletedShort.apply(addedServer);
     addedServer =
         Iterables.getOnlyElement(
             client.getServerServices().getServersByName(addedServer.getName()));
   }
   Credentials credentials =
       client.getServerServices().getServerCredentialsList().get(addedServer.getName());
   if (credentials != null) credentialStore.put("node#" + addedServer.getId(), credentials);
   else logger.warn("couldn't get credentials for server %s", addedServer.getName());
   return serverToNodeMetadata.apply(addedServer);
 }
Exemplo n.º 19
0
  public void testOptionToNotBlock() throws Exception {
    String group = this.group + "block";
    try {
      client.destroyNodesMatching(inGroup(group));
    } catch (Exception e) {

    }
    // no inbound ports
    template = buildTemplate(client.templateBuilder());
    template.getOptions().blockUntilRunning(false).inboundPorts();
    try {
      long time = currentTimeMillis();
      Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, template);
      NodeMetadata node = getOnlyElement(nodes);
      assert node.getStatus() != Status.RUNNING : node;
      long duration = (currentTimeMillis() - time) / 1000;
      assert duration < nonBlockDurationSeconds
          : format(
              "duration(%d) longer than expected(%d) seconds! ", duration, nonBlockDurationSeconds);
    } finally {
      client.destroyNodesMatching(inGroup(group));
    }
  }
Exemplo n.º 20
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);
  }
  public void testInstantiateVAppFromTemplateWhenUsingOverriddenNetworkAndFenceMode()
      throws Exception {

    String name = "group-abcd";
    FenceMode fenceMode = FenceMode.NAT_ROUTED;
    URI parentNetwork = URI.create(ENDPOINT + "/v1.0/network/" + "foooooooo");

    String instantiateXML =
        XMLBuilder.create("InstantiateVAppTemplateParams")
            .a("xmlns", ns)
            .a("xmlns:ovf", "http://schemas.dmtf.org/ovf/envelope/1")
            .a("deploy", "false")
            .a("name", name)
            .a("powerOn", "false")
            .e("Description")
            .up()
            .e("InstantiationParams")
            .e("NetworkConfigSection")
            .e("ovf:Info")
            .t("Configuration parameters for logical networks")
            .up()
            .e("NetworkConfig")
            .a("networkName", "jclouds") // NOTE not "None"
            .e("Configuration")
            .e("ParentNetwork")
            .a("href", parentNetwork.toASCIIString())
            .up()
            .e("FenceMode")
            .t(fenceMode.toString())
            .up()
            .up()
            .up()
            .up()
            .up()
            .e("Source")
            .a("href", ENDPOINT + "/v1.0/vAppTemplate/" + templateId)
            .up()
            .e("AllEULAsAccepted")
            .t("true")
            .up()
            .asString(outputProperties);

    HttpRequest version1_0InstantiateWithCustomizedNetwork =
        HttpRequest.builder()
            .method("POST")
            .endpoint(ENDPOINT + "/v1.0/vdc/" + vdcId + "/action/instantiateVAppTemplate")
            .addHeader(HttpHeaders.ACCEPT, "application/vnd.vmware.vcloud.vApp+xml")
            .addHeader("x-vcloud-authorization", sessionToken)
            .addHeader(HttpHeaders.COOKIE, "vcloud-token=" + sessionToken)
            .payload(
                payloadFromStringWithContentType(
                    instantiateXML,
                    "application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml"))
            .build();

    ComputeService compute =
        requestsSendResponses(
            ImmutableMap.<HttpRequest, HttpResponse>builder()
                .put(versionsRequest, versionsResponseFromVCD1_5)
                .put(
                    version1_0LoginRequest,
                    successfulVersion1_0LoginResponseFromVCD1_5WithSingleOrg)
                .put(
                    version1_0GetOrgRequest,
                    successfulVersion1_0GetOrgResponseFromVCD1_5WithSingleTasksListVDCAndNetwork)
                .put(
                    version1_0GetCatalogRequest,
                    successfulVersion1_0GetCatalogResponseFromVCD1_5WithSingleTemplate)
                .put(
                    version1_0GetCatalogItemRequest,
                    successfulVersion1_0GetCatalogItemResponseFromVCD1_5ForTemplate)
                .put(
                    version1_0GetVDCRequest,
                    successfulVersion1_0GetVDCResponseFromVCD1_5WithSingleTemplateAndNetwork)
                .put(
                    version1_0GetVAppTemplateRequest,
                    successfulVersion1_0GetVAppTemplateResponseFromVCD1_5WithSingleVMAndVDCParent)
                .put(
                    version1_0GetOVFForVAppTemplateRequest,
                    successfulVersion1_0GetOVFForVAppTemplateResponseFromVCD1_5WithSingleVM)
                .put(
                    version1_0InstantiateWithCustomizedNetwork,
                    successfulVersion1_0InstantiatedVApp)
                .build());

    InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn starter =
        compute
            .getContext()
            .utils()
            .injector()
            .getInstance(
                InstantiateVAppTemplateWithGroupEncodedIntoNameThenCustomizeDeployAndPowerOn.class);

    Template template = compute.templateBuilder().build();
    template
        .getOptions()
        .as(VCloudTemplateOptions.class)
        .parentNetwork(parentNetwork)
        .fenceMode(fenceMode);
    starter.instantiateVAppFromTemplate(name, template);
  }
 protected void addSecurityGroups(
     String region, String group, Template template, RunInstancesOptions instanceOptions) {
   Set<String> groups = getSecurityGroupsForTagAndOptions(region, group, template.getOptions());
   instanceOptions.withSecurityGroups(groups);
 }
Exemplo n.º 23
0
  // since surefire and eclipse don't otherwise guarantee the order, we are
  // starting this one alphabetically before create2nodes..
  @Test(
      enabled = true,
      dependsOnMethods = {"testCompareSizes"})
  public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception {
    String group = this.group + "r";
    try {
      client.destroyNodesMatching(inGroup(group));
    } catch (Exception e) {

    }
    template = buildTemplate(client.templateBuilder());
    template.getOptions().blockOnPort(22, 120);
    try {
      Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, template);
      NodeMetadata node = get(nodes, 0);
      LoginCredentials good = node.getCredentials();
      assert good.identity != null : nodes;

      for (Entry<? extends NodeMetadata, ExecResponse> response :
          client
              .runScriptOnNodesMatching(
                  runningInGroup(group),
                  "hostname",
                  wrapInInitScript(false).runAsRoot(false).overrideLoginCredentials(good))
              .entrySet()) {
        checkResponseEqualsHostname(response.getValue(), response.getKey());
      }

      // test single-node execution
      ExecResponse response =
          client.runScriptOnNode(
              node.getId(), "hostname", wrapInInitScript(false).runAsRoot(false));
      checkResponseEqualsHostname(response, node);
      OperatingSystem os = node.getOperatingSystem();

      // test bad password
      tryBadPassword(group, good);

      runScriptWithCreds(group, os, good);

      checkNodes(nodes, group, "runScriptWithCreds");

      // test adding AdminAccess later changes the default boot user, in this
      // case to foo, with home dir /over/ridden/foo
      ListenableFuture<ExecResponse> future =
          client.submitScriptOnNode(
              node.getId(),
              AdminAccess.builder().adminUsername("foo").adminHome("/over/ridden/foo").build(),
              nameTask("adminUpdate"));

      response = future.get(3, TimeUnit.MINUTES);

      assert response.getExitStatus() == 0 : node.getId() + ": " + response;

      node = client.getNodeMetadata(node.getId());
      // test that the node updated to the correct admin user!
      assertEquals(node.getCredentials().identity, "foo");
      assert node.getCredentials().credential != null : nodes;

      weCanCancelTasks(node);

      assert response.getExitStatus() == 0 : node.getId() + ": " + response;

      response =
          client.runScriptOnNode(
              node.getId(), "echo $USER", wrapInInitScript(false).runAsRoot(false));

      assert response.getOutput().trim().equals("foo") : node.getId() + ": " + response;

    } finally {
      client.destroyNodesMatching(inGroup(group));
    }
  }
  @Override
  public NodeAndInitialCredentials<Instance> createNodeWithGroupEncodedIntoName(
      String group, String name, Template template) {
    GoogleComputeEngineTemplateOptions options =
        GoogleComputeEngineTemplateOptions.class.cast(template.getOptions());

    checkNotNull(options.getNetworks(), "template options must specify a network");
    checkNotNull(template.getHardware().getUri(), "hardware must have a URI");
    checkNotNull(template.getImage().getUri(), "image URI is null");

    List<AttachDisk> disks = Lists.newArrayList();
    disks.add(AttachDisk.newBootDisk(template.getImage().getUri()));

    Iterator<String> networks = options.getNetworks().iterator();

    URI network = URI.create(networks.next());
    assert !networks.hasNext() : "Error: Options should specify only one network";

    // Add tags from template
    ArrayList<String> tags = new ArrayList<String>(options.getTags());

    // Add tags for firewalls
    FirewallTagNamingConvention naming = firewallTagNamingConvention.get(group);
    List<String> ports = simplifyPorts(options.getInboundPorts());
    if (ports != null) {
      tags.add(naming.name(ports));
    }

    NewInstance newInstance =
        new NewInstance.Builder(
                name,
                template.getHardware().getUri(), // machineType
                network,
                disks)
            .description(group)
            .tags(Tags.create(null, ImmutableList.copyOf(tags)))
            .serviceAccounts(options.serviceAccounts())
            .build();

    // Add metadata from template and for ssh key and image id
    newInstance.metadata().putAll(options.getUserMetadata());

    LoginCredentials credentials = resolveNodeCredentials(template);
    if (options.getPublicKey() != null) {
      newInstance
          .metadata()
          .put(
              "sshKeys",
              format(
                  "%s:%s %s@localhost",
                  credentials.getUser(), options.getPublicKey(), credentials.getUser()));
    }

    String zone = template.getLocation().getId();
    InstanceApi instanceApi = api.instancesInZone(zone);
    Operation create = instanceApi.create(newInstance);

    // We need to see the created instance so that we can access the newly created disk.
    AtomicReference<Instance> instance =
        Atomics.newReference(
            Instance.create( //
                "0000000000000000000", // id can't be null, but isn't available until provisioning
                                       // is done.
                null, // creationTimestamp
                create.targetLink(), // selfLink
                newInstance.name(), // name
                newInstance.description(), // description
                newInstance.tags(), // tags
                newInstance.machineType(), // machineType
                Instance.Status.PROVISIONING, // status
                null, // statusMessage
                create.zone(), // zone
                null, // canIpForward
                null, // networkInterfaces
                null, // disks
                newInstance.metadata(), // metadata
                newInstance.serviceAccounts(), // serviceAccounts
                Scheduling.create(OnHostMaintenance.MIGRATE, true) // scheduling
                ));
    checkState(instanceVisible.apply(instance), "instance %s is not api visible!", instance.get());

    // Add lookup for InstanceToNodeMetadata
    diskToSourceImage.put(instance.get().disks().get(0).source(), template.getImage().getUri());

    return new NodeAndInitialCredentials<Instance>(
        instance.get(), instance.get().selfLink().toString(), credentials);
  }
Exemplo n.º 25
0
 protected static Template addRunScriptToTemplate(Template template) {
   template
       .getOptions()
       .runScript(Statements.newStatementList(AdminAccess.standard(), InstallJDK.fromOpenJDK()));
   return template;
 }
Exemplo n.º 26
0
  @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);
  }