@Override
  @Nullable
  public LoginCredentials get() {
    if (credentialStore.containsKey("image")) {
      return LoginCredentials.builder(credentialStore.get("image")).build();
    }
    Builder builder = LoginCredentials.builder();

    String loginUser = config.apply(provider + ".image.login-user");
    if (loginUser == null) loginUser = config.apply("jclouds.image.login-user");
    if (loginUser != null) {
      int pos = loginUser.indexOf(':');
      if (pos != -1) {
        builder.user(loginUser.substring(0, pos)).password(loginUser.substring(pos + 1));
      } else builder.user(loginUser);
    }

    String authenticateSudo = config.apply(provider + ".image.authenticate-sudo");
    if (authenticateSudo == null)
      authenticateSudo = config.apply("jclouds.image.authenticate-sudo");
    if (authenticateSudo != null) {
      builder.authenticateSudo(Boolean.valueOf(authenticateSudo));
    }

    LoginCredentials creds = builder.build();
    if (creds != null) credentialStore.put("image", creds);
    return creds;
  }
コード例 #2
0
 public TemplateBuilder createTemplateBuilder() {
   final Supplier<Set<? extends Image>> images =
       Suppliers.<Set<? extends Image>>ofInstance(
           ImmutableSet.of(
               new ImageBuilder()
                   .providerId("ebs-image-provider")
                   .name("image")
                   .id("us-east-1/bogus-image")
                   .location(jcloudsDomainLocation)
                   .userMetadata(ImmutableMap.of("rootDeviceType", RootDeviceType.EBS.value()))
                   .operatingSystem(
                       new OperatingSystem(
                           OsFamily.UBUNTU,
                           null,
                           "1.0",
                           VirtualizationType.PARAVIRTUAL.value(),
                           "ubuntu",
                           true))
                   .description("description")
                   .version("1.0")
                   .defaultCredentials(LoginCredentials.builder().user("root").build())
                   .status(Image.Status.AVAILABLE)
                   .build()));
   ImmutableMap<RegionAndName, Image> imageMap =
       (ImmutableMap<RegionAndName, Image>) ImagesToRegionAndIdMap.imagesToMap(images.get());
   Supplier<LoadingCache<RegionAndName, ? extends Image>> imageCache =
       Suppliers.<LoadingCache<RegionAndName, ? extends Image>>ofInstance(
           CacheBuilder.newBuilder()
               .<RegionAndName, Image>build(CacheLoader.from(Functions.forMap(imageMap))));
   JcloudsStubTemplateBuilder jcloudsStubTemplateBuilder = new JcloudsStubTemplateBuilder();
   return jcloudsStubTemplateBuilder.newTemplateBuilder(images, imageCache);
 }
コード例 #3
0
  public void testParseCCImage() {

    Set<org.jclouds.compute.domain.Image> result = convertImages("/describe_images_cc.xml");

    assertEquals(
        Iterables.get(result, 0),
        new ImageBuilder()
            .name("EC2 CentOS 5.4 HVM AMI")
            .operatingSystem(
                new OperatingSystem.Builder()
                    .family(OsFamily.CENTOS)
                    .arch("hvm")
                    .version("5.4")
                    .description("amazon/EC2 CentOS 5.4 HVM AMI")
                    .is64Bit(true)
                    .build())
            .description("EC2 CentOS 5.4 HVM AMI")
            .defaultCredentials(LoginCredentials.builder().user("root").build())
            .id("us-east-1/ami-7ea24a17")
            .providerId("ami-7ea24a17")
            .location(defaultLocation)
            .userMetadata(
                ImmutableMap.of(
                    "owner", "206029621532",
                    "rootDeviceType", "ebs",
                    "virtualizationType", "hvm",
                    "hypervisor", "xen"))
            .status(org.jclouds.compute.domain.Image.Status.AVAILABLE)
            .build());
    assertEquals(
        Iterables.get(result, 0).getStatus(), org.jclouds.compute.domain.Image.Status.AVAILABLE);
  }
コード例 #4
0
  public void testParseVostokImage() {

    Set<org.jclouds.compute.domain.Image> result = convertImages("/vostok.xml");

    assertEquals(
        Iterables.get(result, 0),
        new ImageBuilder()
            .operatingSystem(
                new OperatingSystem.Builder()
                    .family(OsFamily.UNRECOGNIZED)
                    .arch("paravirtual")
                    .version("")
                    .description("vostok-builds/vostok-0.95-5622/vostok-0.95-5622.manifest.xml")
                    .is64Bit(false)
                    .build())
            .description("vostok-builds/vostok-0.95-5622/vostok-0.95-5622.manifest.xml")
            .defaultCredentials(LoginCredentials.builder().user("root").build())
            .id("us-east-1/ami-870de2ee")
            .providerId("ami-870de2ee")
            .location(defaultLocation)
            .version("5622")
            .userMetadata(
                ImmutableMap.of("owner", "133804938231", "rootDeviceType", "instance-store"))
            .status(org.jclouds.compute.domain.Image.Status.AVAILABLE)
            .build());
  }
コード例 #5
0
  protected void tryOverrideUsingPropertyKey(String propertyKey) {
    ComputeServiceContext context = null;
    try {
      Properties overrides = setupProperties();
      String login = loginUser != null ? loginUser : "******";
      overrides.setProperty(propertyKey + ".image.login-user", login);
      boolean auth = authenticateSudo != null ? Boolean.valueOf(authenticateSudo) : true;
      overrides.setProperty(propertyKey + ".image.authenticate-sudo", auth + "");

      context =
          new ComputeServiceContextFactory()
              .createContext(provider, ImmutableSet.<Module>of(getLoggingModule()), overrides);

      Iterable<String> userPass = Splitter.on(':').split(login);
      String user = Iterables.get(userPass, 0);
      String pass = Iterables.size(userPass) > 1 ? Iterables.get(userPass, 1) : null;
      assertEquals(
          context.getComputeService().templateBuilder().build().getImage().getDefaultCredentials(),
          LoginCredentials.builder().user(user).password(pass).authenticateSudo(auth).build());
    } finally {
      if (context != null) {
        // Need to clear persisted credentials; otherwise next time a ComputeServiceContext is
        // created
        // then it will have these "foo" credentials!
        context.credentialStore().clear();
        context.close();
      }
    }
  }
コード例 #6
0
 @Override
 protected SshClient getConnectionFor(IPSocket socket) {
   return sshFactory.create(
       socket,
       LoginCredentials.builder()
           .user("vcloud")
           .password("TmrkCl0ud1s#1!")
           .privateKey(key.getPrivateKey())
           .authenticateSudo(true)
           .build());
 }
コード例 #7
0
 @Override
 public NodeMetadata apply(VirtualGuest from) {
   NodeMetadataBuilder builder = new NodeMetadataBuilder();
   builder.ids(from.getId() + "");
   builder.name(from.getHostname());
   builder.hostname(from.getFullyQualifiedDomainName());
   if (from.getDatacenter() != null) {
     builder.location(
         from(locations.get())
             .firstMatch(LocationPredicates.idEquals(from.getDatacenter().getName()))
             .orNull());
   }
   builder.group(nodeNamingConvention.groupInUniqueNameOrNull(from.getHostname()));
   builder.hardware(virtualGuestToHardware.apply(from));
   Image image = virtualGuestToImage.apply(from);
   if (image != null) {
     builder.imageId(image.getId());
     builder.operatingSystem(image.getOperatingSystem());
   }
   if (from.getPowerState() != null) {
     builder.status(serverStateToNodeStatus.get(from.getPowerState().getKeyName()));
   }
   if (from.getPrimaryIpAddress() != null)
     builder.publicAddresses(ImmutableSet.of(from.getPrimaryIpAddress()));
   if (from.getPrimaryBackendIpAddress() != null)
     builder.privateAddresses(ImmutableSet.of(from.getPrimaryBackendIpAddress()));
   // TODO simplify once we move domain classes to AutoValue
   if (from.getOperatingSystem() != null
       && from.getOperatingSystem().getPasswords() != null
       && !from.getOperatingSystem().getPasswords().isEmpty()) {
     Password password = getBestPassword(from.getOperatingSystem().getPasswords(), from);
     builder.credentials(
         LoginCredentials.builder()
             .identity(password.getUsername())
             .credential(password.getPassword())
             .build());
   }
   if (from.getTagReferences() != null && !from.getTagReferences().isEmpty()) {
     List<String> tags = Lists.newArrayList();
     for (TagReference tagReference : from.getTagReferences()) {
       if (tagReference != null) {
         tags.add(tagReference.getTag().getName());
       }
     }
     builder.tags(tags);
   }
   return builder.build();
 }
 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();
 }
コード例 #9
0
  @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());
  }
コード例 #10
0
  @Override
  public Image apply(DiskImage from) {
    checkNotNull(from, "disk image");

    ImageBuilder builder = new ImageBuilder();

    builder.ids(from.getId());
    builder.name(from.getName());
    builder.description(from.getDescription());
    builder.location(Iterables.getOnlyElement(regionSupplier.get()));
    // in fgcp, if the image is listed it is available
    builder.status(Status.AVAILABLE);

    OperatingSystem os = diskImageToOperatingSystem.apply(from);
    builder.operatingSystem(os);
    String user = os.getFamily() == OsFamily.WINDOWS ? "Administrator" : "root";
    builder.defaultCredentials(LoginCredentials.builder().identity(user).noPassword().build());

    return builder.build();
  }
コード例 #11
0
  protected SshjSshClient createClient(final Properties props) {
    Injector i =
        Guice.createInjector(
            module(),
            new AbstractModule() {

              @Override
              protected void configure() {
                bindProperties(binder(), props);
              }
            },
            new SLF4JLoggingModule());
    SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
    SshjSshClient ssh =
        SshjSshClient.class.cast(
            factory.create(
                HostAndPort.fromParts("localhost", 22),
                LoginCredentials.builder().user("username").password("password").build()));
    return ssh;
  }
コード例 #12
0
 @Test(dependsOnMethods = "testCreateMachine")
 protected void testSsh() {
   String publicAddress =
       Iterables.find(machine.getIps(), not(InetAddresses2.IsPrivateIPAddress.INSTANCE));
   HostAndPort socket = HostAndPort.fromParts(publicAddress, 22);
   assertTrue(socketTester.apply(socket), socket.toString());
   SshClient api =
       context
           .utils()
           .injector()
           .getInstance(SshClient.Factory.class)
           .create(
               socket,
               LoginCredentials.builder().user("root").privateKey(key.get("private")).build());
   try {
     api.connect();
     ExecResponse exec = api.exec("echo hello");
     System.out.println(exec);
     assertEquals(exec.getOutput().trim(), "hello");
   } finally {
     if (api != null) api.disconnect();
   }
 }
コード例 #13
0
 @Test(enabled = false)
 protected void tryBadPassword(String group, Credentials good) throws AssertionError {
   try {
     Map<? extends NodeMetadata, ExecResponse> responses =
         client.runScriptOnNodesMatching(
             runningInGroup(group),
             "echo I put a bad password",
             wrapInInitScript(false)
                 .runAsRoot(false)
                 .overrideLoginCredentials(
                     LoginCredentials.builder()
                         .user(good.identity)
                         .noPrivateKey()
                         .password("romeo")
                         .build()));
     assert responses.size() == 0 : "shouldn't pass with a bad password\n" + responses;
   } catch (AssertionError e) {
     throw e;
   } catch (RunScriptOnNodesException e) {
     assert Iterables.any(
             e.getNodeErrors().values(), Predicates.instanceOf(AuthorizationException.class))
         : e + " not authexception!";
   }
 }
コード例 #14
0
  public void testParseRightScaleImage() {

    Set<org.jclouds.compute.domain.Image> result = convertImages("/rightscale_images.xml");

    assertEquals(
        Iterables.get(result, 0),
        new ImageBuilder()
            .operatingSystem(
                new OperatingSystem.Builder()
                    .family(OsFamily.CENTOS)
                    .arch("paravirtual")
                    .version("5.4")
                    .description("rightscale-us-east/CentOS_5.4_x64_v4.4.10.manifest.xml")
                    .is64Bit(true)
                    .build())
            .description("rightscale-us-east/CentOS_5.4_x64_v4.4.10.manifest.xml")
            .defaultCredentials(LoginCredentials.builder().user("root").build())
            .id("us-east-1/ami-ccb35ea5")
            .providerId("ami-ccb35ea5")
            .location(defaultLocation)
            .version("4.4.10")
            .userMetadata(ImmutableMap.of("owner", "admin", "rootDeviceType", "instance-store"))
            .status(org.jclouds.compute.domain.Image.Status.AVAILABLE)
            .backendStatus("available")
            .build());
    assertEquals(
        Iterables.get(result, 0).getStatus(), org.jclouds.compute.domain.Image.Status.AVAILABLE);

    assertEquals(
        new Gson().toJson(Iterables.get(result, 1)),
        "{\"operatingSystem\":{\"family\":\"UBUNTU\",\"arch\":\"paravirtual\",\"version\":\"9.10\",\"description\":\"411009282317/RightImage_Ubuntu_9.10_x64_v4.5.3_EBS_Alpha\",\"is64Bit\":true},\"status\":\"AVAILABLE\",\"backendStatus\":\"available\",\"version\":\"4.5.3_EBS_Alpha\",\"description\":\"RightImage_Ubuntu_9.10_x64_v4.5.3_EBS_Alpha\",\"defaultCredentials\":{\"authenticateSudo\":false,\"identity\":\"root\"},\"id\":\"us-east-1/ami-c19db6b5\",\"type\":\"IMAGE\",\"tags\":[],\"providerId\":\"ami-c19db6b5\",\"name\":\"RightImage_Ubuntu_9.10_x64_v4.5.3_EBS_Alpha\",\"location\":{\"scope\":\"REGION\",\"id\":\"us-east-1\",\"description\":\"us-east-1\",\"iso3166Codes\":[],\"metadata\":{}},\"userMetadata\":{\"owner\":\"411009282317\",\"rootDeviceType\":\"ebs\",\"virtualizationType\":\"paravirtual\",\"hypervisor\":\"xen\"}}");

    assertEquals(
        new Gson().toJson(Iterables.get(result, 2)),
        "{\"operatingSystem\":{\"family\":\"WINDOWS\",\"arch\":\"hvm\",\"version\":\"2003\",\"description\":\"411009282317/RightImage Windows_2003_i386_v5.4.3\",\"is64Bit\":false},\"status\":\"AVAILABLE\",\"backendStatus\":\"available\",\"version\":\"5.4.3\",\"description\":\"Built by RightScale\",\"defaultCredentials\":{\"authenticateSudo\":false,\"identity\":\"root\"},\"id\":\"us-east-1/ami-710c2605\",\"type\":\"IMAGE\",\"tags\":[],\"providerId\":\"ami-710c2605\",\"name\":\"RightImage Windows_2003_i386_v5.4.3\",\"location\":{\"scope\":\"REGION\",\"id\":\"us-east-1\",\"description\":\"us-east-1\",\"iso3166Codes\":[],\"metadata\":{}},\"userMetadata\":{\"owner\":\"411009282317\",\"rootDeviceType\":\"ebs\",\"virtualizationType\":\"hvm\",\"hypervisor\":\"xen\"}}");
  }
コード例 #15
0
  public void testParseAmznImage() {

    Set<org.jclouds.compute.domain.Image> result = convertImages("/amzn_images.xml");

    assertEquals(
        Iterables.get(result, 0),
        new ImageBuilder()
            .name("amzn-ami-0.9.7-beta.i386-ebs")
            .operatingSystem(
                new OperatingSystem.Builder()
                    .family(OsFamily.AMZN_LINUX)
                    .arch("paravirtual")
                    .version("0.9.7-beta")
                    .description("137112412989/amzn-ami-0.9.7-beta.i386-ebs")
                    .is64Bit(false)
                    .build())
            .description("Amazon")
            .defaultCredentials(LoginCredentials.builder().user("ec2-user").build())
            .id("us-east-1/ami-82e4b5c7")
            .providerId("ami-82e4b5c7")
            .location(defaultLocation)
            .version("0.9.7-beta")
            .userMetadata(
                ImmutableMap.of(
                    "owner", "137112412989",
                    "rootDeviceType", "ebs",
                    "virtualizationType", "paravirtual",
                    "hypervisor", "xen"))
            .status(org.jclouds.compute.domain.Image.Status.AVAILABLE)
            .build());
    assertEquals(
        Iterables.get(result, 0).getStatus(), org.jclouds.compute.domain.Image.Status.AVAILABLE);

    assertEquals(
        Iterables.get(result, 3),
        new ImageBuilder()
            .name("amzn-ami-0.9.7-beta.x86_64-S3")
            .operatingSystem(
                new OperatingSystem.Builder()
                    .family(OsFamily.AMZN_LINUX)
                    .arch("paravirtual")
                    .version("0.9.7-beta")
                    .description("amzn-ami-us-west-1/amzn-ami-0.9.7-beta.x86_64.manifest.xml")
                    .is64Bit(true)
                    .build())
            .description("Amazon Linux AMI x86_64 S3")
            .defaultCredentials(LoginCredentials.builder().user("ec2-user").build())
            .id("us-east-1/ami-f2e4b5b7")
            .providerId("ami-f2e4b5b7")
            .location(defaultLocation)
            .version("0.9.7-beta")
            .userMetadata(
                ImmutableMap.of(
                    "owner", "137112412989",
                    "rootDeviceType", "ebs",
                    "virtualizationType", "paravirtual",
                    "hypervisor", "xen"))
            .status(org.jclouds.compute.domain.Image.Status.AVAILABLE)
            .build());
    assertEquals(
        Iterables.get(result, 3).getStatus(), org.jclouds.compute.domain.Image.Status.AVAILABLE);
  }
  // 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());
  }
コード例 #17
0
  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);
  }
コード例 #18
0
  public void testParseAlesticCanonicalImage() {

    Set<org.jclouds.compute.domain.Image> result = convertImages("/alestic_canonical.xml");

    assertEquals(
        Iterables.get(result, 0),
        new ImageBuilder()
            .operatingSystem(
                new OperatingSystem.Builder()
                    .family(OsFamily.UBUNTU)
                    .arch("paravirtual")
                    .version("8.04")
                    .description(
                        "ubuntu-images-us/ubuntu-hardy-8.04-i386-server-20091130.manifest.xml")
                    .is64Bit(false)
                    .build())
            .description("ubuntu-images-us/ubuntu-hardy-8.04-i386-server-20091130.manifest.xml")
            .defaultCredentials(LoginCredentials.builder().user("ubuntu").build())
            .id("us-east-1/ami-7e28ca17")
            .providerId("ami-7e28ca17")
            .location(defaultLocation)
            .version("20091130")
            .userMetadata(
                ImmutableMap.of(
                    "owner", "099720109477",
                    "rootDeviceType", "instance-store",
                    "virtualizationType", "paravirtual",
                    "hypervisor", "xen"))
            .status(org.jclouds.compute.domain.Image.Status.AVAILABLE)
            .backendStatus("available")
            .build());
    assertEquals(
        Iterables.get(result, 0).getStatus(), org.jclouds.compute.domain.Image.Status.AVAILABLE);

    assertEquals(
        Iterables.get(result, 4),
        new ImageBuilder()
            .operatingSystem(
                new OperatingSystem.Builder()
                    .family(OsFamily.UBUNTU)
                    .arch("paravirtual")
                    .version("8.04")
                    .description("alestic/ubuntu-8.04-hardy-base-20080905.manifest.xml")
                    .is64Bit(false)
                    .build())
            .description("alestic/ubuntu-8.04-hardy-base-20080905.manifest.xml")
            .defaultCredentials(LoginCredentials.builder().user("ubuntu").build())
            .id("us-east-1/ami-c0fa1ea9")
            .providerId("ami-c0fa1ea9")
            .location(defaultLocation)
            .version("20080905")
            .userMetadata(
                ImmutableMap.of("owner", "063491364108", "rootDeviceType", "instance-store"))
            .status(org.jclouds.compute.domain.Image.Status.AVAILABLE)
            .backendStatus("available")
            .build());
    assertEquals(
        Iterables.get(result, 4).getStatus(), org.jclouds.compute.domain.Image.Status.AVAILABLE);

    assertEquals(
        Iterables.get(result, 6),
        new ImageBuilder()
            .name("ebs/ubuntu-images/ubuntu-lucid-10.04-i386-server-20100827")
            .operatingSystem(
                new OperatingSystem.Builder()
                    .family(OsFamily.UBUNTU)
                    .arch("paravirtual")
                    .version("10.04")
                    .description(
                        "099720109477/ebs/ubuntu-images/ubuntu-lucid-10.04-i386-server-20100827")
                    .is64Bit(false)
                    .build())
            .description("099720109477/ebs/ubuntu-images/ubuntu-lucid-10.04-i386-server-20100827")
            .defaultCredentials(LoginCredentials.builder().user("ubuntu").build())
            .id("us-east-1/ami-10f3a255")
            .providerId("ami-10f3a255")
            .location(defaultLocation)
            .version("20100827")
            .userMetadata(
                ImmutableMap.of(
                    "owner", "099720109477",
                    "rootDeviceType", "ebs",
                    "virtualizationType", "paravirtual",
                    "hypervisor", "xen"))
            .status(org.jclouds.compute.domain.Image.Status.AVAILABLE)
            .backendStatus("available")
            .build());
    assertEquals(
        Iterables.get(result, 6).getStatus(), org.jclouds.compute.domain.Image.Status.AVAILABLE);
  }
コード例 #19
0
 protected LoginCredentials getSshCredentials(Server server) {
   return LoginCredentials.builder().user("toor").password(server.getVnc().getPassword()).build();
 }
コード例 #20
0
 @Provides
 @Singleton
 protected Map<OsFamily, LoginCredentials> osFamilyToCredentials(Injector injector) {
   return ImmutableMap.of(
       OsFamily.WINDOWS, LoginCredentials.builder().user("Administrator").build());
 }
コード例 #21
0
  @BeforeGroups(groups = {"live"})
  public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException {
    int port = Integer.parseInt(sshPort);
    if (sshUser == null
        || ((sshPass == null || sshPass.trim().equals(""))
            && (sshKeyFile == null || sshKeyFile.trim().equals("")))
        || sshUser.trim().equals("")) {
      System.err.println("ssh credentials not present.  Tests will be lame");
      return new SshClient() {

        public void connect() {}

        public void disconnect() {}

        public Payload get(String path) {
          if (path.equals("/etc/passwd")) {
            return Payloads.newStringPayload("root");
          } else if (path.equals(temp.getAbsolutePath())) {
            return Payloads.newStringPayload("rabbit");
          }
          throw new RuntimeException("path " + path + " not stubbed");
        }

        public ExecResponse exec(String command) {
          if (command.equals("hostname")) {
            return new ExecResponse(sshHost, "", 0);
          }
          throw new RuntimeException("command " + command + " not stubbed");
        }

        @Override
        public void put(String path, Payload contents) {}

        @Override
        public String getHostAddress() {
          return null;
        }

        @Override
        public String getUsername() {
          return null;
        }

        @Override
        public void put(String path, String contents) {}
      };
    } else {
      Injector i = Guice.createInjector(new JschSshClientModule(), new SLF4JLoggingModule());
      SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
      SshClient connection;
      if (Strings.emptyToNull(sshKeyFile) != null) {
        connection =
            factory.create(
                new IPSocket(sshHost, port),
                LoginCredentials.builder()
                    .user(sshUser)
                    .privateKey(Strings2.toStringAndClose(new FileInputStream(sshKeyFile)))
                    .build());
      } else {
        connection =
            factory.create(
                new IPSocket(sshHost, port),
                LoginCredentials.builder().user(sshUser).password(sshPass).build());
      }
      connection.connect();
      return connection;
    }
  }