@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 = "testConcurrentUseOfComputeServiceToCreateNodes")
  public void testCreateTwoNodesWithRunScript() throws Exception {
    try {
      client.destroyNodesMatching(inGroup(group));
    } catch (NoSuchElementException e) {

    }
    refreshTemplate();
    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);
    checkNodes(nodes, group, "bootstrap");
    NodeMetadata node1 = nodes.first();
    NodeMetadata node2 = nodes.last();
    // credentials aren't always the same
    // assertEquals(node1.getCredentials(), node2.getCredentials());

    assertLocationSameOrChild(
        checkNotNull(node1.getLocation(), "location of %s", node1), template.getLocation());
    assertLocationSameOrChild(
        checkNotNull(node2.getLocation(), "location of %s", node2), template.getLocation());
    checkImageIdMatchesTemplate(node1);
    checkImageIdMatchesTemplate(node2);
    checkOsMatchesTemplate(node1);
    checkOsMatchesTemplate(node2);
  }
  /** Create a server with the key pair. */
  private NodeMetadata createServer(KeyPair keyPair) throws RunNodesException, TimeoutException {
    System.out.format("  Create Server%n");

    NovaTemplateOptions options = NovaTemplateOptions.Builder.keyPairName(keyPair.getName());

    ZoneAndId zoneAndId = ZoneAndId.fromZoneAndId(ZONE, "performance1-1");
    Template template =
        computeService
            .templateBuilder()
            .locationId(ZONE)
            .osDescriptionMatches(".*Ubuntu 12.04.*")
            .hardwareId(zoneAndId.slashEncode())
            .options(options)
            .build();

    // This method will continue to poll for the server status and won't return until this server is
    // ACTIVE
    // If you want to know what's happening during the polling, enable logging.
    // See /jclouds-example/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java
    Set<? extends NodeMetadata> nodes = computeService.createNodesInGroup(NAME, 1, template);
    NodeMetadata node = Iterables.getOnlyElement(nodes);

    System.out.format("    %s%n", node);

    return node;
  }
  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);
  }
 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;
 }
  @Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithOneSpecifiedName")
  public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception {
    initializeContext();

    Location existingLocation = Iterables.get(this.nodes, 0).getLocation();
    boolean existingLocationIsAssignable =
        Iterables.any(client.listAssignableLocations(), Predicates.equalTo(existingLocation));

    if (existingLocationIsAssignable) {
      getAnonymousLogger()
          .info("creating another node based on existing nodes' location: " + existingLocation);
      template = buildTemplate(client.templateBuilder());
      template =
          addRunScriptToTemplate(
              client
                  .templateBuilder()
                  .fromTemplate(template)
                  .locationId(existingLocation.getId())
                  .build());
    } else {
      refreshTemplate();
      getAnonymousLogger()
          .info(
              format(
                  "%s is not assignable; using template's location %s as  ",
                  existingLocation, template.getLocation()));
    }

    Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, template);
    assertEquals(nodes.size(), 1);
    checkNodes(nodes, group, "bootstrap");
    NodeMetadata node = Iterables.getOnlyElement(nodes);
    if (existingLocationIsAssignable) assertEquals(node.getLocation(), existingLocation);
    else
      this.assertLocationSameOrChild(
          checkNotNull(node.getLocation(), "location of %s", node), template.getLocation());
    checkOsMatchesTemplate(node);
    this.nodes.add(node);
  }
  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));
    }
  }
  // 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
  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;
  }