Ejemplo n.º 1
0
  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));
    }
  }
  @VisibleForTesting
  public Set<String> getSecurityGroupsForTagAndOptions(
      String region, @Nullable String group, TemplateOptions options) {
    Builder<String> groups = ImmutableSet.builder();

    if (group != null) {
      String markerGroup = namingConvention.create().sharedNameForGroup(group);

      groups.add(markerGroup);

      RegionNameAndIngressRules regionNameAndIngressRulesForMarkerGroup;

      if (userSpecifiedTheirOwnGroups(options)) {
        regionNameAndIngressRulesForMarkerGroup =
            new RegionNameAndIngressRules(region, markerGroup, new int[] {}, false);
        groups.addAll(EC2TemplateOptions.class.cast(options).getGroups());
      } else {
        regionNameAndIngressRulesForMarkerGroup =
            new RegionNameAndIngressRules(region, markerGroup, options.getInboundPorts(), true);
      }
      // this will create if not yet exists.
      securityGroupMap.getUnchecked(regionNameAndIngressRulesForMarkerGroup);
    }
    return groups.build();
  }
Ejemplo n.º 3
0
 @Override
 public void copyTo(TemplateOptions to) {
   super.copyTo(to);
   if (to instanceof DockerTemplateOptions) {
     DockerTemplateOptions eTo = DockerTemplateOptions.class.cast(to);
     if (!volumes.isEmpty()) {
       eTo.volumes(volumes);
     }
     eTo.hostname(hostname);
     if (!dns.isEmpty()) {
       eTo.dns(dns);
     }
     eTo.memory(memory);
     eTo.cpuShares(cpuShares);
     if (!commands.isEmpty()) {
       eTo.commands(commands);
     }
     if (!env.isEmpty()) {
       eTo.env(env);
     }
     if (!portBindings.isEmpty()) {
       eTo.portBindings(portBindings);
     }
     eTo.networkMode(networkMode);
     if (!extraHosts.isEmpty()) {
       eTo.extraHosts(extraHosts);
     }
   }
 }
 @Override
 public void copyTo(TemplateOptions to) {
   super.copyTo(to);
   if (to instanceof CloudStackTemplateOptions) {
     CloudStackTemplateOptions eTo = CloudStackTemplateOptions.class.cast(to);
     eTo.securityGroupIds(this.securityGroupIds);
     eTo.keyPair(this.keyPair);
   }
 }
Ejemplo n.º 5
0
 @Override
 public void copyTo(TemplateOptions to) {
   super.copyTo(to);
   if (to instanceof VCloudTemplateOptions) {
     VCloudTemplateOptions eTo = VCloudTemplateOptions.class.cast(to);
     if (getCustomizationScript() != null) eTo.customizationScript(getCustomizationScript());
     if (getDescription() != null) eTo.description(getDescription());
     if (getIpAddressAllocationMode() != null)
       eTo.ipAddressAllocationMode(getIpAddressAllocationMode());
   }
 }
  @VisibleForTesting
  public String createNewKeyPairUnlessUserSpecifiedOtherwise(
      String region, String group, TemplateOptions options) {
    String keyPairName = null;
    boolean shouldAutomaticallyCreateKeyPair = true;

    if (options instanceof EC2TemplateOptions) {
      keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair();
      if (keyPairName == null)
        shouldAutomaticallyCreateKeyPair =
            EC2TemplateOptions.class.cast(options).shouldAutomaticallyCreateKeyPair();
    }

    if (keyPairName == null && shouldAutomaticallyCreateKeyPair) {
      keyPairName = createOrImportKeyPair(region, group, options);
    } else if (keyPairName != null) {
      if (options.getLoginPrivateKey() != null) {
        String pem = options.getLoginPrivateKey();
        KeyPair keyPair =
            KeyPair.builder()
                .region(region)
                .keyName(keyPairName)
                .fingerprint(fingerprintPrivateKey(pem))
                .sha1OfPrivateKey(sha1PrivateKey(pem))
                .keyMaterial(pem)
                .build();
        RegionAndName key = new RegionAndName(region, keyPairName);
        credentialsMap.put(key, keyPair);
      }
    }

    if (options.getRunScript() != null) {
      RegionAndName regionAndName = new RegionAndName(region, keyPairName);
      checkArgument(
          credentialsMap.containsKey(regionAndName),
          "no private key configured for: %s; please use options.overrideLoginCredentialWith(rsa_private_text)",
          regionAndName);
    }
    return keyPairName;
  }
 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();
 }
Ejemplo n.º 8
0
 @Override
 public void copyTo(TemplateOptions to) {
   super.copyTo(to);
   if (to instanceof NovaTemplateOptions) {
     NovaTemplateOptions eTo = NovaTemplateOptions.class.cast(to);
     eTo.autoAssignFloatingIp(shouldAutoAssignFloatingIp());
     eTo.securityGroupNames(getSecurityGroupNames());
     eTo.generateKeyPair(shouldGenerateKeyPair());
     eTo.keyPairName(getKeyPairName());
     if (getUserData() != null) {
       eTo.userData(getUserData());
     }
   }
 }
  public void testCreatePreemptibleNodeWithSsd() throws Exception {
    String group = this.group + "ssd";
    try {
      TemplateOptions options = client.templateOptions();

      options.as(GoogleComputeEngineTemplateOptions.class).bootDiskType("pd-ssd").preemptible(true);

      // create a node
      Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, options);
      assertEquals(nodes.size(), 1, "One node should have been created");

      // Verify the disk on the instance is an ssd.
      NodeMetadata node = Iterables.get(nodes, 0);
      GoogleComputeEngineApi api = client.getContext().unwrapApi(GoogleComputeEngineApi.class);
      Instance instance = api.instancesInZone(node.getLocation().getId()).get(node.getName());
      Disk disk =
          api.disksInZone(node.getLocation().getId()).get(toName(instance.disks().get(0).source()));
      assertTrue(disk.type().toString().endsWith("pd-ssd"));
      assertTrue(instance.scheduling().preemptible());

    } finally {
      client.destroyNodesMatching(NodePredicates.inGroup(group));
    }
  }
Ejemplo n.º 10
0
 @Test
 public void testAs() {
   TemplateOptions options = new GoGridTemplateOptions();
   assertEquals(options.as(GoGridTemplateOptions.class), options);
 }
  protected NodeAndInitialCredentials<ServerInDataCenter> createNodeWithGroupEncodedIntoName(
      String group, String name, TemplateWithDataCenter template) {
    checkArgument(
        template.getLocation().getScope() == LocationScope.ZONE,
        "Template must use a ZONE-scoped location");
    final String dataCenterId = template.getDataCenter().id();
    Hardware hardware = template.getHardware();
    TemplateOptions options = template.getOptions();
    final String loginUser =
        isNullOrEmpty(options.getLoginUser()) ? "root" : options.getLoginUser();
    final String password =
        options.hasLoginPassword() ? options.getLoginPassword() : Passwords.generate();
    final org.jclouds.compute.domain.Image image = template.getImage();

    // provision all volumes based on hardware
    List<? extends Volume> volumes = hardware.getVolumes();
    List<String> volumeIds = Lists.newArrayListWithExpectedSize(volumes.size());

    int i = 1;
    for (final Volume volume : volumes) {
      try {
        logger.trace("<< provisioning volume '%s'", volume);
        final org.apache.jclouds.profitbricks.rest.domain.Volume.Request.CreatePayload.Builder
            request = org.apache.jclouds.profitbricks.rest.domain.Volume.Request.creatingBuilder();
        if (i == 1) {
          request.image(image.getId());
          // we don't need to pass password to the API if we're using a snapshot
          Provisionable.Type provisionableType =
              Provisionable.Type.fromValue(
                  image.getUserMetadata().get(ProvisionableToImage.KEY_PROVISIONABLE_TYPE));
          if (provisionableType == Provisionable.Type.IMAGE) {
            request.imagePassword(password);
          }
        }
        request
            .dataCenterId(dataCenterId)
            .name(format("%s-disk-%d", name, i++))
            .size(volume.getSize().intValue())
            .type(VolumeType.HDD);

        org.apache.jclouds.profitbricks.rest.domain.Volume vol =
            (org.apache.jclouds.profitbricks.rest.domain.Volume)
                provisioningManager.provision(
                    jobFactory.create(
                        dataCenterId,
                        new Supplier<Object>() {
                          @Override
                          public Object get() {
                            return api.volumeApi().createVolume(request.build());
                          }
                        }));

        volumeIds.add(vol.id());
        logger.trace(">> provisioning complete for volume. returned id='%s'", vol.id());
      } catch (Exception ex) {
        if (i - 1 == 1) // if first volume (one with image) provisioning fails; stop method
        {
          throw Throwables.propagate(ex);
        }
        logger.warn(ex, ">> failed to provision volume. skipping..");
      }
    }

    String volumeBootDeviceId = Iterables.get(volumeIds, 0); // must have atleast 1
    waitVolumeUntilAvailable.apply(VolumeRef.create(dataCenterId, volumeBootDeviceId));

    // provision server
    final Server server;
    Double cores = ComputeServiceUtils.getCores(hardware);

    Server.BootVolume bootVolume = Server.BootVolume.create(volumeBootDeviceId);

    try {
      final Server.Request.CreatePayload serverRequest =
          Server.Request.creatingBuilder()
              .dataCenterId(dataCenterId)
              .name(name)
              .bootVolume(bootVolume)
              .cores(cores.intValue())
              .ram(hardware.getRam())
              .build();

      logger.trace("<< provisioning server '%s'", serverRequest);

      server =
          (Server)
              provisioningManager.provision(
                  jobFactory.create(
                      dataCenterId,
                      new Supplier<Object>() {

                        @Override
                        public Object get() {
                          return api.serverApi().createServer(serverRequest);
                        }
                      }));
      logger.trace(">> provisioning complete for server. returned id='%s'", server.id());

    } catch (Exception ex) {
      logger.error(ex, ">> failed to provision server. rollbacking..");
      destroyVolumes(volumeIds, dataCenterId);
      throw Throwables.propagate(ex);
    }

    waitServerUntilAvailable.apply(ServerRef.create(dataCenterId, server.id()));
    waitDcUntilAvailable.apply(dataCenterId);

    // attach bootVolume to Server
    org.apache.jclouds.profitbricks.rest.domain.Volume volume =
        api.serverApi()
            .attachVolume(
                Server.Request.attachVolumeBuilder()
                    .dataCenterId(dataCenterId)
                    .serverId(server.id())
                    .volumeId(bootVolume.id())
                    .build());

    trackables.waitUntilRequestCompleted(volume);
    waitServerUntilAvailable.apply(ServerRef.create(dataCenterId, server.id()));
    waitDcUntilAvailable.apply(dataCenterId);

    // fetch an existing lan and creat if non was found
    Lan lan = null;

    List<Lan> lans = api.lanApi().list(dataCenterId);
    if (lans != null && !lans.isEmpty()) {
      lan =
          FluentIterable.from(lans)
              .firstMatch(
                  new Predicate<Lan>() {
                    @Override
                    public boolean apply(Lan input) {
                      input =
                          api.lanApi().get(dataCenterId, input.id(), new DepthOptions().depth(3));
                      return input.properties().isPublic();
                    }
                  })
              .orNull();
    }
    if (lan == null) {
      logger.warn("Could not find an existing lan Creating one....");
      lan =
          api.lanApi()
              .create(
                  Lan.Request.creatingBuilder()
                      .dataCenterId(dataCenterId)
                      .isPublic(Boolean.TRUE)
                      .name("lan " + name)
                      .build());
      trackables.waitUntilRequestCompleted(lan);
    }

    // add a NIC to the server
    int lanId = DEFAULT_LAN_ID;
    if (options.getNetworks() != null) {
      try {
        String networkId = Iterables.get(options.getNetworks(), 0);
        lanId = Integer.valueOf(networkId);
      } catch (Exception ex) {
        logger.warn(
            "no valid network id found from options. using default id='%d'", DEFAULT_LAN_ID);
      }
    }

    Nic nic =
        api.nicApi()
            .create(
                Nic.Request.creatingBuilder()
                    .dataCenterId(dataCenterId)
                    .name("jclouds" + name)
                    .dhcp(Boolean.TRUE)
                    .lan(lanId)
                    .firewallActive(Boolean.FALSE)
                    .serverId(server.id())
                    .build());

    trackables.waitUntilRequestCompleted(nic);
    waitNICUntilAvailable.apply(NicRef.create(dataCenterId, server.id(), nic.id()));
    waitDcUntilAvailable.apply(dataCenterId);
    waitServerUntilAvailable.apply(ServerRef.create(dataCenterId, server.id()));

    // connect the rest of volumes to server;delete if fails
    final int volumeCount = volumeIds.size();
    for (int j = 1; j < volumeCount; j++) { // skip first; already connected
      final String volumeId = volumeIds.get(j);
      try {
        logger.trace("<< connecting volume '%s' to server '%s'", volumeId, server.id());
        provisioningManager.provision(
            jobFactory.create(
                group,
                new Supplier<Object>() {

                  @Override
                  public Object get() {
                    return api.serverApi()
                        .attachVolume(
                            Server.Request.attachVolumeBuilder()
                                .dataCenterId(dataCenterId)
                                .serverId(server.id())
                                .volumeId(volumeId)
                                .build());
                  }
                }));

        logger.trace(">> volume connected.");
      } catch (Exception ex) {
        try {
          // delete unconnected volume
          logger.warn(ex, ">> failed to connect volume '%s'. deleting..", volumeId);
          destroyVolume(volumeId, dataCenterId);
          logger.warn(ex, ">> rolling back server..", server.id());
          destroyServer(server.id(), dataCenterId);
          throw ex;
        } catch (Exception ex1) {
          logger.error(ex, ">> failed to rollback");
        }
      }
    }
    waitDcUntilAvailable.apply(dataCenterId);
    waitServerUntilAvailable.apply(ServerRef.create(dataCenterId, server.id()));

    LoginCredentials serverCredentials =
        LoginCredentials.builder().user(loginUser).password(password).build();

    String serverInDataCenterId =
        DataCenterAndId.fromDataCenterAndId(dataCenterId, server.id()).slashEncode();
    ServerInDataCenter serverInDatacenter = getNode(serverInDataCenterId);

    return new NodeAndInitialCredentials<ServerInDataCenter>(
        serverInDatacenter, serverInDataCenterId, serverCredentials);
  }
  @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;
  }