@Test
 public void testLaunchCluster() throws RunNodesException {
   int numNodes = 3;
   final String clusterName = "test-launch-cluster";
   Set<? extends NodeMetadata> nodes =
       context
           .getComputeService()
           .createNodesInGroup(
               clusterName,
               numNodes,
               TemplateOptions.Builder.overrideLoginUser("toor")
                   .runScript(AdminAccess.standard()));
   assertEquals(numNodes, nodes.size(), "wrong number of nodes");
   for (NodeMetadata node : nodes) {
     assertEquals("test-launch-cluster", node.getGroup());
     logger.debug("Created Node: %s", node);
     SshClient client = context.utils().sshForNode().apply(node);
     client.connect();
     ExecResponse hello = client.exec("echo hello");
     assertEquals(hello.getOutput().trim(), "hello");
   }
   context
       .getComputeService()
       .destroyNodesMatching(
           new Predicate<NodeMetadata>() {
             @Override
             public boolean apply(NodeMetadata input) {
               return input.getId().contains(clusterName);
             }
           });
 }
  public void testNoSsh() throws Exception {

    Map<String, String> keyPair = ComputeTestUtils.setupKeyPair();

    AWSInstanceClient instanceClient =
        AWSEC2Client.class.cast(context.getApi()).getInstanceServices();

    String group = PREFIX + "unssh";
    computeContext.getComputeService().destroyNodesMatching(inGroup(group));

    TemplateOptions options = computeContext.getComputeService().templateOptions();

    options.authorizePublicKey(keyPair.get("public")).as(AWSEC2TemplateOptions.class);

    ComputeServiceContext noSshContext = null;
    try {
      noSshContext =
          new ComputeServiceContextFactory()
              .createContext(
                  provider, ImmutableSet.of(new Log4JLoggingModule()), setupProperties());

      Set<? extends NodeMetadata> nodes =
          noSshContext.getComputeService().createNodesInGroup(group, 1, options);

      NodeMetadata first = get(nodes, 0);
      assert first.getCredentials() != null : first;
      assert first.getCredentials().identity != null : first;
      assert first.getCredentials().credential == null : first;

      AWSRunningInstance instance = getInstance(instanceClient, first.getProviderId());

      assert instance.getSpotInstanceRequestId() != null : instance;
      assertEquals(instance.getKeyName(), "jclouds#" + group);

      Map<? extends NodeMetadata, ExecResponse> responses =
          computeContext
              .getComputeService()
              .runScriptOnNodesMatching(
                  runningInGroup(group),
                  exec("echo hello"),
                  overrideCredentialsWith(
                          new Credentials(first.getCredentials().identity, keyPair.get("private")))
                      .wrapInInitScript(false)
                      .runAsRoot(false));

      ExecResponse hello = getOnlyElement(responses.values());
      assertEquals(hello.getOutput().trim(), "hello");

    } finally {
      noSshContext.close();
      computeContext.getComputeService().destroyNodesMatching(inGroup(group));
    }
  }
Exemple #3
0
  public Ec2Context(Ec2Credentials credentials) {
    this.credentials = credentials;
    Properties properties = new Properties();
    long scriptTimeout = TimeUnit.MILLISECONDS.convert(50, TimeUnit.MINUTES);
    properties.setProperty(TIMEOUT_SCRIPT_COMPLETE, scriptTimeout + "");
    properties.setProperty(TIMEOUT_PORT_OPEN, scriptTimeout + "");
    properties.setProperty(PROPERTY_CONNECTION_TIMEOUT, scriptTimeout + "");
    properties.setProperty(
        PROPERTY_EC2_AMI_QUERY, "owner-id=137112412989;state=available;image-type=machine");
    properties.setProperty(PROPERTY_EC2_CC_AMI_QUERY, "");
    properties.setProperty(
        Constants.PROPERTY_MAX_RETRIES, Settings.JCLOUDS_PROPERTY_MAX_RETRIES + "");
    properties.setProperty(
        Constants.PROPERTY_RETRY_DELAY_START, Settings.JCLOUDS_PROPERTY_RETRY_DELAY_START + "");

    Iterable<Module> modules =
        ImmutableSet.<Module>of(
            new SshjSshClientModule(),
            new SLF4JLoggingModule(),
            new EnterpriseConfigurationModule());

    ContextBuilder build =
        ContextBuilder.newBuilder("aws-ec2")
            .credentials(credentials.getAccessKey(), credentials.getSecretKey())
            .modules(modules)
            .overrides(properties);
    ComputeServiceContext context = build.buildView(ComputeServiceContext.class);
    this.computeService = (AWSEC2ComputeService) context.getComputeService();
    this.ec2api = computeService.getContext().unwrapApi(EC2Api.class);
    this.securityGroupApi = ec2api.getSecurityGroupApi().get();
    this.keypairApi = (AWSKeyPairApi) ec2api.getKeyPairApi().get();

    vmBatchSize = Settings.AWS_VM_BATCH_SIZE();
  }
 public CloudComputeService(
     String apiKey,
     String accessKey,
     String instanceType,
     String groupName,
     String imageId,
     String keyPair,
     String securityGroup,
     float maxBid) {
   mInstanceType = instanceType;
   mGroupName = groupName;
   mImageId = imageId;
   mkeyPair = keyPair;
   mSecurityGroup = securityGroup;
   mMaxBid = maxBid;
   mGroupTag = "group=" + mGroupName;
   Properties overrides = new Properties();
   overrides.put(ComputeServiceProperties.POLL_INITIAL_PERIOD, String.valueOf(60L * 1000L));
   overrides.put(ComputeServiceProperties.POLL_MAX_PERIOD, String.valueOf(600L * 1000L));
   overrides.put(Constants.PROPERTY_MAX_RETRIES, String.valueOf(60));
   mComputeServiceContext =
       ContextBuilder.newBuilder("aws-ec2")
           .credentials(apiKey, accessKey)
           .modules(ImmutableSet.of(new Log4JLoggingModule()))
           .overrides(overrides)
           .buildView(ComputeServiceContext.class);
   mComputeService = mComputeServiceContext.getComputeService();
 }
 protected boolean isOSX(String id) {
   return context
       .getComputeService()
       .getNodeMetadata(hostId)
       .getOperatingSystem()
       .getDescription()
       .equals("Mac OS X");
 }
  @Test
  public void testTemplateBuilderFindsMegabitUplink() throws IOException {
    ComputeServiceContext context = null;
    try {
      Properties overrides = setupProperties();
      overrides.setProperty(PROPERTY_SOFTLAYER_VIRTUALGUEST_PORT_SPEED, "100");

      context = createView(overrides, setupModules());

      // TODO add something to the template about port speed?
      context.getComputeService().templateBuilder().build();

    } finally {
      if (context != null) context.close();
    }
  }
  /** Rigorous Test :-) */
  public void testCanUseStub() throws IOException {

    Iterable<String> providers = ComputeServiceUtils.getSupportedProviders();

    ComputeServiceContext context =
        new ComputeServiceContextFactory().createContext("stub", "foo", "bar");

    ComputeService client = context.getComputeService();

    // Set<? extends ComputeMetadata> nodes =
    // Sets.newHashSet(connection.getNodes().values());

    for (ComputeMetadata node : client.listNodes()) {
      assertNotNull(node.getId());
      assertNotNull(node.getLocation().getId()); // where in the
      // world is the node
    }
  }
  @Test
  public void testBiggestTemplateBuilderWhenBootIsSAN() throws IOException {
    ComputeServiceContext context = null;
    try {
      Properties overrides = setupProperties();
      overrides.setProperty(PROPERTY_SOFTLAYER_VIRTUALGUEST_DISK0_TYPE, "SAN");

      context = createView(overrides, setupModules());

      Template template = context.getComputeService().templateBuilder().biggest().build();
      assertEquals(getCores(template.getHardware()), 16.0d);
      assertEquals(template.getHardware().getRam(), 16);
      assertEquals(getSpace(template.getHardware()), 100.0d);
      assertEquals(template.getHardware().getVolumes().get(0).getType(), Volume.Type.SAN);
    } finally {
      if (context != null) context.close();
    }
  }
  public CreateServerWithKeyPair(String username, String apiKey) {
    Iterable<Module> modules = ImmutableSet.<Module>of(new SshjSshClientModule());

    // These properties control how often jclouds polls for a status update
    Properties overrides = new Properties();
    overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
    overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS);

    ComputeServiceContext context =
        ContextBuilder.newBuilder(PROVIDER)
            .credentials(username, apiKey)
            .overrides(overrides)
            .modules(modules)
            .buildView(ComputeServiceContext.class);

    computeService = context.getComputeService();
    novaApi = context.unwrapApi(NovaApi.class);
  }
  @Test
  public void testBiggestTemplateBuilderWhenPrivateNetwork() throws IOException {
    ComputeServiceContext context = null;
    try {
      Properties overrides = setupProperties();
      overrides.setProperty(
          PROPERTY_SOFTLAYER_VIRTUALGUEST_CPU_REGEX, "Private [0-9]+ x ([.0-9]+) GHz Core[s]?");

      context = createView(overrides, setupModules());

      Template template = context.getComputeService().templateBuilder().biggest().build();
      assertEquals(getCores(template.getHardware()), 8.0d);
      assertEquals(template.getHardware().getRam(), 16);
      assertEquals(getSpace(template.getHardware()), 100.0d);
      assertEquals(template.getHardware().getVolumes().get(0).getType(), Volume.Type.LOCAL);
    } finally {
      if (context != null) context.close();
    }
  }
 @Test(groups = {"Live", "WIP"})
 public void buildClaimAndDestroy() {
   ComputeService svc = context.getComputeService();
   SamplePool p = new SamplePool(svc);
   log.info("buildClaimAndDestroy: created pool");
   p.refresh();
   log.info("buildClaimAndDestroy: refreshed pool");
   p.ensureExists(2, SamplePool.USUAL_VM);
   log.info("buildClaimAndDestroy: ensure have 2");
   MachineSet l = p.claim(1, SamplePool.USUAL_VM);
   Assert.assertEquals(l.size(), 1);
   log.info("buildClaimAndDestroy: claimed 1");
   MachineSet unclaimedUsual = p.unclaimed(MachinePoolPredicates.matching(SamplePool.USUAL_VM));
   log.info("buildClaimAndDestroy: unclaimed now " + unclaimedUsual);
   Assert.assertTrue(!unclaimedUsual.isEmpty());
   p.destroy(unclaimedUsual);
   unclaimedUsual = p.unclaimed(MachinePoolPredicates.matching(SamplePool.USUAL_VM));
   log.info("buildClaimAndDestroy: destroyed, unclaimed now " + unclaimedUsual);
   log.info("end");
 }
 @Test(enabled = true, expectedExceptions = AuthorizationException.class)
 public void testCorrectAuthException() throws Exception {
   ComputeServiceContext context = null;
   try {
     Properties overrides = setupProperties();
     overrides.setProperty(provider + ".identity", "MOM:MA");
     overrides.setProperty(provider + ".credential", "MIA");
     context =
         newBuilder()
             .modules(ImmutableSet.of(getLoggingModule(), credentialStoreModule))
             .overrides(overrides)
             .build(ComputeServiceContext.class);
     context.getComputeService().listNodes();
   } catch (AuthorizationException e) {
     throw e;
   } catch (RuntimeException e) {
     e.printStackTrace();
     throw e;
   } finally {
     if (context != null) context.close();
   }
 }
 @Test(expectedExceptions = AuthorizationException.class)
 @Override
 public void testCorrectAuthException() throws Exception {
   ComputeServiceContext context = null;
   try {
     String credential = toStringAndClose(getClass().getResourceAsStream("/test"));
     Properties overrides = setupProperties();
     overrides.setProperty(provider + ".identity", "*****@*****.**");
     overrides.setProperty(provider + ".credential", credential);
     context =
         newBuilder()
             .modules(ImmutableSet.of(getLoggingModule(), credentialStoreModule))
             .overrides(overrides)
             .build(ComputeServiceContext.class);
     context.getComputeService().listNodes();
   } catch (AuthorizationException e) {
     throw e;
   } catch (RuntimeException e) {
     e.printStackTrace();
     throw e;
   } finally {
     if (context != null) context.close();
   }
 }
  @Override
  public ComputeService findComputeService(ConfigBag conf, boolean allowReuse) {
    String provider = checkNotNull(conf.get(CLOUD_PROVIDER), "provider must not be null");
    String identity =
        checkNotNull(conf.get(CloudLocationConfig.ACCESS_IDENTITY), "identity must not be null");
    String credential =
        checkNotNull(
            conf.get(CloudLocationConfig.ACCESS_CREDENTIAL), "credential must not be null");

    Properties properties = new Properties();
    properties.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, Boolean.toString(true));
    properties.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, Boolean.toString(true));
    properties.setProperty(
        "jclouds.ssh.max-retries",
        conf.getStringKey("jclouds.ssh.max-retries") != null
            ? conf.getStringKey("jclouds.ssh.max-retries").toString()
            : "50");
    // Enable aws-ec2 lazy image fetching, if given a specific imageId; otherwise customize for
    // specific owners; or all as a last resort
    // See https://issues.apache.org/jira/browse/WHIRR-416
    if ("aws-ec2".equals(provider)) {
      // TODO convert AWS-only flags to config keys
      if (groovyTruth(conf.get(IMAGE_ID))) {
        properties.setProperty(PROPERTY_EC2_AMI_QUERY, "");
        properties.setProperty(PROPERTY_EC2_CC_AMI_QUERY, "");
      } else if (groovyTruth(conf.getStringKey("imageOwner"))) {
        properties.setProperty(
            PROPERTY_EC2_AMI_QUERY,
            "owner-id=" + conf.getStringKey("imageOwner") + ";state=available;image-type=machine");
      } else if (groovyTruth(conf.getStringKey("anyOwner"))) {
        // set `anyOwner: true` to override the default query (which is restricted to certain owners
        // as per below),
        // allowing the AMI query to bind to any machine
        // (note however, we sometimes pick defaults in JcloudsLocationFactory);
        // (and be careful, this can give a LOT of data back, taking several minutes,
        // and requiring extra memory allocated on the command-line)
        properties.setProperty(PROPERTY_EC2_AMI_QUERY, "state=available;image-type=machine");
        /*
         * by default the following filters are applied:
         * Filter.1.Name=owner-id&Filter.1.Value.1=137112412989&
         * Filter.1.Value.2=063491364108&
         * Filter.1.Value.3=099720109477&
         * Filter.1.Value.4=411009282317&
         * Filter.2.Name=state&Filter.2.Value.1=available&
         * Filter.3.Name=image-type&Filter.3.Value.1=machine&
         */
      }

      // occasionally can get com.google.common.util.concurrent.UncheckedExecutionException:
      // java.lang.RuntimeException:
      //     security group eu-central-1/jclouds#brooklyn-bxza-alex-eu-central-shoul-u2jy-nginx-ielm
      // is not available after creating
      // the default timeout was 500ms so let's raise it in case that helps
      properties.setProperty(
          EC2Constants.PROPERTY_EC2_TIMEOUT_SECURITYGROUP_PRESENT,
          "" + Duration.seconds(30).toMilliseconds());
    }

    // FIXME Deprecated mechanism, should have a ConfigKey for overrides
    Map<String, Object> extra =
        Maps.filterKeys(conf.getAllConfig(), Predicates.containsPattern("^jclouds\\."));
    if (extra.size() > 0) {
      LOG.warn("Jclouds using deprecated property overrides: " + Sanitizer.sanitize(extra));
    }
    properties.putAll(extra);

    String endpoint = conf.get(CloudLocationConfig.CLOUD_ENDPOINT);
    if (!groovyTruth(endpoint)) endpoint = getDeprecatedProperty(conf, Constants.PROPERTY_ENDPOINT);
    if (groovyTruth(endpoint)) properties.setProperty(Constants.PROPERTY_ENDPOINT, endpoint);

    Map<?, ?> cacheKey =
        MutableMap.builder()
            .putAll(properties)
            .put("provider", provider)
            .put("identity", identity)
            .put("credential", credential)
            .putIfNotNull("endpoint", endpoint)
            .build()
            .asUnmodifiable();

    if (allowReuse) {
      ComputeService result = cachedComputeServices.get(cacheKey);
      if (result != null) {
        LOG.trace(
            "jclouds ComputeService cache hit for compute service, for "
                + Sanitizer.sanitize(properties));
        return result;
      }
      LOG.debug(
          "jclouds ComputeService cache miss for compute service, creating, for "
              + Sanitizer.sanitize(properties));
    }

    Iterable<Module> modules = getCommonModules();

    // Synchronizing to avoid deadlock from sun.reflect.annotation.AnnotationType.
    // See https://github.com/brooklyncentral/brooklyn/issues/974
    ComputeServiceContext computeServiceContext;
    synchronized (createComputeServicesMutex) {
      computeServiceContext =
          ContextBuilder.newBuilder(provider)
              .modules(modules)
              .credentials(identity, credential)
              .overrides(properties)
              .build(ComputeServiceContext.class);
    }
    final ComputeService computeService = computeServiceContext.getComputeService();
    if (allowReuse) {
      synchronized (cachedComputeServices) {
        ComputeService result = cachedComputeServices.get(cacheKey);
        if (result != null) {
          LOG.debug(
              "jclouds ComputeService cache recovery for compute service, for "
                  + Sanitizer.sanitize(cacheKey));
          // keep the old one, discard the new one
          computeService.getContext().close();
          return result;
        }
        LOG.debug(
            "jclouds ComputeService created "
                + computeService
                + ", adding to cache, for "
                + Sanitizer.sanitize(properties));
        cachedComputeServices.put(cacheKey, computeService);
      }
    }
    return computeService;
  }
 protected ExecResponse runScriptOnNode(String nodeId, String command, RunScriptOptions options) {
   ExecResponse toReturn = context.getComputeService().runScriptOnNode(nodeId, command, options);
   assert toReturn.getExitCode() == 0 : toReturn;
   return toReturn;
 }