Ejemplo n.º 1
0
 @Test(
     enabled = true,
     dependsOnMethods = {"testImagesCache"})
 public void testTemplateMatch() throws Exception {
   template = buildTemplate(client.templateBuilder());
   Template toMatch = client.templateBuilder().imageId(template.getImage().getId()).build();
   assertEquals(toMatch.getImage(), template.getImage());
 }
Ejemplo n.º 2
0
  @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);
  }
  /** 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;
  }
Ejemplo n.º 4
0
  protected void doCompareSizes() throws Exception {
    Hardware defaultSize = client.templateBuilder().build().getHardware();

    Hardware smallest = client.templateBuilder().smallest().build().getHardware();
    Hardware fastest = client.templateBuilder().fastest().build().getHardware();
    Hardware biggest = client.templateBuilder().biggest().build().getHardware();

    getAnonymousLogger().info("smallest " + smallest);
    getAnonymousLogger().info("fastest " + fastest);
    getAnonymousLogger().info("biggest " + biggest);

    assertEquals(defaultSize, smallest);

    assert getCores(smallest) <= getCores(fastest) : format("%s ! <= %s", smallest, fastest);
    assert getCores(biggest) <= getCores(fastest) : format("%s ! <= %s", biggest, fastest);

    assert biggest.getRam() >= fastest.getRam() : format("%s ! >= %s", biggest, fastest);
    assert biggest.getRam() >= smallest.getRam() : format("%s ! >= %s", biggest, smallest);

    assert getCores(fastest) >= getCores(biggest) : format("%s ! >= %s", fastest, biggest);
    assert getCores(fastest) >= getCores(smallest) : format("%s ! >= %s", fastest, smallest);
  }
Ejemplo n.º 5
0
  @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);
  }
Ejemplo n.º 6
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);
  }
Ejemplo n.º 7
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;
 }
Ejemplo n.º 8
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));
      }
    }
  }
Ejemplo n.º 9
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));
    }
  }
Ejemplo n.º 10
0
 private Template refreshTemplate() {
   return template = addRunScriptToTemplate(buildTemplate(client.templateBuilder()));
 }
Ejemplo n.º 11
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));
    }
  }
  // TODO: finish me!
  @Test(enabled = false)
  public void
      testCreateNodeUsingVCloud1_0ApiAgainstVCloudDirector1_5WhenVAppTemplateHasNetworkNamedNone()
          throws Exception {

    String group = "group";
    String name = "group-abcd";

    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", "orgNet-jclouds-External") // NOTE not "None"
            .e("Configuration")
            .e("ParentNetwork")
            .a("href", ENDPOINT + "/v1.0/network/" + networkId)
            .up()
            .e("FenceMode")
            .t("bridged")
            .up()
            .up()
            .up()
            .up()
            .up()
            .e("Source")
            .a("href", ENDPOINT + "/v1.0/vAppTemplate/" + templateId)
            .up()
            .e("AllEULAsAccepted")
            .t("true")
            .up()
            .asString(outputProperties);

    HttpRequest version1_0InstantiateWithNetworkNamedSameAsOrgNetwork =
        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_0InstantiateWithNetworkNamedSameAsOrgNetwork,
                    successfulVersion1_0InstantiatedVApp)
                .build());

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

    NodeAndInitialCredentials<VApp> appAndCreds =
        starter.createNodeWithGroupEncodedIntoName(group, name, compute.templateBuilder().build());

    assertEquals(appAndCreds.getNode().getName(), name);
    assertEquals(
        appAndCreds.getCredentials(),
        LoginCredentials.builder().user("root").password("fromVApp").build());
  }
  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);
  }
  @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;
  }