private void setInstanceInitiatedShutdownBehaviorForInstanceInRegion() {
    try {

      client
          .getInstanceServices()
          .setInstanceInitiatedShutdownBehaviorForInstanceInRegion(
              null, ebsInstance.getId(), InstanceInitiatedShutdownBehavior.STOP);

      assertEquals(
          InstanceInitiatedShutdownBehavior.STOP,
          client
              .getInstanceServices()
              .getInstanceInitiatedShutdownBehaviorForInstanceInRegion(null, ebsInstance.getId()));
      client
          .getInstanceServices()
          .setInstanceInitiatedShutdownBehaviorForInstanceInRegion(
              null, ebsInstance.getId(), InstanceInitiatedShutdownBehavior.TERMINATE);

      assertEquals(
          InstanceInitiatedShutdownBehavior.TERMINATE,
          client
              .getInstanceServices()
              .getInstanceInitiatedShutdownBehaviorForInstanceInRegion(null, ebsInstance.getId()));
      System.out.println("OK: setInstanceInitiatedShutdownBehaviorForInstanceInRegion");
    } catch (Exception e) {
      System.err.println("setInstanceInitiatedShutdownBehaviorForInstanceInRegion");
      e.printStackTrace();
    }
  }
  private void setBlockDeviceMappingForInstanceInRegion() {
    String volumeId = ebsInstance.getEbsBlockDevices().get("/dev/sda1").getVolumeId();

    Map<String, BlockDevice> mapping = Maps.newLinkedHashMap();
    mapping.put("/dev/sda1", new BlockDevice(volumeId, false));
    try {
      client
          .getInstanceServices()
          .setBlockDeviceMappingForInstanceInRegion(null, ebsInstance.getId(), mapping);

      Map<String, BlockDevice> devices =
          client
              .getInstanceServices()
              .getBlockDeviceMappingForInstanceInRegion(null, ebsInstance.getId());
      assertEquals(devices.size(), 1);
      String deviceName = Iterables.getOnlyElement(devices.keySet());
      BlockDevice device = Iterables.getOnlyElement(devices.values());

      assertEquals(device.getVolumeId(), volumeId);
      assertEquals(deviceName, "/dev/sda1");
      assertEquals(device.isDeleteOnTermination(), false);

      System.out.println("OK: setBlockDeviceMappingForInstanceInRegion");
    } catch (Exception e) {
      System.err.println("setBlockDeviceMappingForInstanceInRegion");

      e.printStackTrace();
    }
  }
  private RunningInstance blockUntilWeCanSshIntoInstance(RunningInstance instance)
      throws UnknownHostException {
    System.out.printf(
        "%d: %s awaiting instance to run %n", System.currentTimeMillis(), instance.getId());
    assert runningTester.apply(instance);

    // search my identity for the instance I just created
    Set<? extends Reservation<? extends RunningInstance>> reservations =
        client
            .getInstanceServices()
            .describeInstancesInRegion(instance.getRegion(), instance.getId()); // last
    // parameter
    // (ids)
    // narrows
    // the
    // search

    instance = Iterables.getOnlyElement(Iterables.getOnlyElement(reservations));

    System.out.printf(
        "%d: %s awaiting ssh service to start%n",
        System.currentTimeMillis(), instance.getIpAddress());
    assert socketTester.apply(new IPSocket(instance.getIpAddress(), 22));
    System.out.printf(
        "%d: %s ssh service started%n", System.currentTimeMillis(), instance.getDnsName());
    sshPing(instance);
    System.out.printf("%d: %s ssh connection made%n", System.currentTimeMillis(), instance.getId());
    return instance;
  }
  @Test(
      enabled = false,
      dependsOnMethods = {"testAMIFromBundle"})
  public void testInstanceFromEBS() throws Exception {
    System.out.printf(
        "%d: %s creating instance from ebs-backed ami%n",
        System.currentTimeMillis(), ebsImage.getId());

    ebsInstance = createInstance(ebsImage.getId());

    client
        .getInstanceServices()
        .stopInstancesInRegion(ebsInstance.getRegion(), true, ebsInstance.getId());

    System.out.printf(
        "%d: %s awaiting instance to stop %n", System.currentTimeMillis(), ebsInstance.getId());
    stoppedTester.apply(ebsInstance);
    tryToChangeStuff();
    System.out.printf(
        "%d: %s awaiting instance to start %n", System.currentTimeMillis(), ebsInstance.getId());
    client
        .getInstanceServices()
        .startInstancesInRegion(ebsInstance.getRegion(), ebsInstance.getId());
    ebsInstance = blockUntilWeCanSshIntoInstance(ebsInstance);
  }
 private void setUserDataForInstanceInRegion() {
   client
       .getInstanceServices()
       .setUserDataForInstanceInRegion(null, ebsInstance.getId(), "test".getBytes());
   assertEquals(
       "test",
       client.getInstanceServices().getUserDataForInstanceInRegion(null, ebsInstance.getId()));
 }
 private void setKernelForInstanceInRegion() {
   String oldKernel =
       client.getInstanceServices().getKernelForInstanceInRegion(null, ebsInstance.getId());
   client.getInstanceServices().setKernelForInstanceInRegion(null, ebsInstance.getId(), oldKernel);
   assertEquals(
       oldKernel,
       client.getInstanceServices().getKernelForInstanceInRegion(null, ebsInstance.getId()));
 }
 private void setRamdiskForInstanceInRegion() {
   String ramdisk =
       client.getInstanceServices().getRamdiskForInstanceInRegion(null, ebsInstance.getId());
   client.getInstanceServices().setRamdiskForInstanceInRegion(null, ebsInstance.getId(), ramdisk);
   assertEquals(
       ramdisk,
       client.getInstanceServices().getRamdiskForInstanceInRegion(null, ebsInstance.getId()));
 }
 private String parseGroupFrom(final RunningInstance instance, final Set<String> data) {
   String group = null;
   try {
     Predicate<String> containsAnyGroup = namingConvention.create().containsAnyGroup();
     String encodedGroup = Iterables.getOnlyElement(Iterables.filter(data, containsAnyGroup));
     group = namingConvention.create().extractGroup(encodedGroup);
   } catch (NoSuchElementException e) {
     logger.debug("no group parsed from %s's data: %s", instance.getId(), data);
   } catch (IllegalArgumentException e) {
     logger.debug(
         "too many groups match naming convention; %s's data: %s", instance.getId(), data);
   }
   return group;
 }
 private void setInstanceTypeForInstanceInRegion() {
   client
       .getInstanceServices()
       .setInstanceTypeForInstanceInRegion(null, ebsInstance.getId(), InstanceType.C1_MEDIUM);
   assertEquals(
       InstanceType.C1_MEDIUM,
       client.getInstanceServices().getInstanceTypeForInstanceInRegion(null, ebsInstance.getId()));
   client
       .getInstanceServices()
       .setInstanceTypeForInstanceInRegion(null, ebsInstance.getId(), InstanceType.M1_SMALL);
   assertEquals(
       InstanceType.M1_SMALL,
       client.getInstanceServices().getInstanceTypeForInstanceInRegion(null, ebsInstance.getId()));
 }
 @Override
 protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) {
   LoginCredentials creds =
       LoginCredentials.fromCredentials(
           credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId()));
   String spotRequestId = AWSRunningInstance.class.cast(instance).getSpotInstanceRequestId();
   if (creds == null && spotRequestId != null) {
     creds =
         LoginCredentials.fromCredentials(
             credentialStore.get("node#" + instance.getRegion() + "/" + spotRequestId));
     if (creds != null)
       credentialStore.put("node#" + instance.getRegion() + "/" + instance.getId(), creds);
   }
   if (creds != null) builder.credentials(creds);
 }
Ejemplo n.º 11
0
  // Fires up an instance, finds its root volume ID, takes a snapshot, then
  // terminates the instance.
  private Snapshot createSnapshot() throws RunNodesException {

    String instanceId = null;
    try {
      RunningInstance instance =
          getOnlyElement(
              concat(
                  ec2Client
                      .getInstanceServices()
                      .runInstancesInRegion(regionId, null, imageId, 1, 1)));
      instanceId = instance.getId();

      assertTrue(runningTester.apply(instance), instanceId + "didn't achieve the state running!");

      instance =
          getOnlyElement(
              concat(
                  ec2Client.getInstanceServices().describeInstancesInRegion(regionId, instanceId)));
      BlockDevice device = instance.getEbsBlockDevices().get("/dev/sda1");
      assertNotNull(device, "device: /dev/sda1 not present on: " + instance);
      Snapshot snapshot =
          ec2Client
              .getElasticBlockStoreServices()
              .createSnapshotInRegion(regionId, device.getVolumeId());
      snapshotsToDelete.add(snapshot.getId());
      return snapshot;
    } finally {
      if (instanceId != null)
        ec2Client.getInstanceServices().terminateInstancesInRegion(regionId, instanceId);
    }
  }
 @Override
 public NodeMetadata apply(RunningInstance instance) {
   if (instance == null || instance.getId() == null) return null;
   NodeMetadataBuilder builder = new NodeMetadataBuilder();
   builder = buildInstance(instance, builder);
   return builder.build();
 }
  protected NodeMetadataBuilder buildInstance(
      final RunningInstance instance, NodeMetadataBuilder builder) {
    builder.providerId(instance.getId());
    builder.id(instance.getRegion() + "/" + instance.getId());
    String group = getGroupForInstance(instance);
    builder.group(group);
    // standard convention from aws-ec2, which might not be re-used outside.
    if (instance.getPrivateDnsName() != null)
      builder.hostname(instance.getPrivateDnsName().replaceAll("\\..*", ""));
    addCredentialsForInstance(builder, instance);
    builder.status(instanceToNodeStatus.get(instance.getInstanceState()));
    builder.backendStatus(instance.getRawState());

    // collect all ip addresses into one bundle in case the api mistakenly put a private address
    // into the public address field
    Builder<String> addressesBuilder = ImmutableSet.builder();
    if (Strings.emptyToNull(instance.getIpAddress()) != null)
      addressesBuilder.add(instance.getIpAddress());
    // Add dnsName (if available) to addresses, when the IPAddress is null
    // happens on Eucalyptus sometimes.
    else if (Strings.emptyToNull(instance.getDnsName()) != null)
      addressesBuilder.add(instance.getDnsName());
    if (Strings.emptyToNull(instance.getPrivateIpAddress()) != null)
      addressesBuilder.add(instance.getPrivateIpAddress());

    Set<String> addresses = addressesBuilder.build();

    builder.publicAddresses(filter(addresses, not(IsPrivateIPAddress.INSTANCE)));
    builder.privateAddresses(filter(addresses, IsPrivateIPAddress.INSTANCE));
    builder.hardware(parseHardware(instance));
    Location location = getLocationForAvailabilityZoneOrRegion(instance);
    builder.location(location);
    builder.imageId(instance.getRegion() + "/" + instance.getImageId());

    // extract the operating system from the image
    RegionAndName regionAndName = new RegionAndName(instance.getRegion(), instance.getImageId());
    try {
      Image image = imageMap.get().getUnchecked(regionAndName);
      if (image != null) builder.operatingSystem(image.getOperatingSystem());
    } catch (CacheLoader.InvalidCacheLoadException e) {
      logger.debug("image not found for %s: %s", regionAndName, e);
    } catch (UncheckedExecutionException e) {
      logger.debug("error getting image for %s: %s", regionAndName, e);
    }
    return builder;
  }
Ejemplo n.º 14
0
  @Test(enabled = false, dependsOnMethods = "testCreateAndAttachVolume")
  void testBundleInstance() {
    SshClient ssh =
        sshFactory.create(
            new IPSocket(instance.getIpAddress(), 22),
            new Credentials("ubuntu", keyPair.getKeyMaterial()));
    try {
      ssh.connect();
    } catch (SshException e) { // try twice in case there is a network timeout
      try {
        Thread.sleep(10 * 1000);
      } catch (InterruptedException e1) {
      }
      ssh.connect();
    }
    try {
      System.out.printf(
          "%d: %s writing ebs script%n", System.currentTimeMillis(), instance.getId());
      String script = "/tmp/mkebsboot-init.sh";
      ssh.put(script, Payloads.newStringPayload(mkEbsBoot));

      System.out.printf(
          "%d: %s launching ebs script%n", System.currentTimeMillis(), instance.getId());
      ssh.exec("chmod 755 " + script);
      ssh.exec(script + " init");
      ExecResponse output = ssh.exec("sudo " + script + " start");
      System.out.println(output);
      output = ssh.exec(script + " status");

      assert !output.getOutput().trim().equals("") : output;

      RetryablePredicate<String> scriptTester =
          new RetryablePredicate<String>(
              new ScriptTester(ssh, SCRIPT_END), 600, 10, TimeUnit.SECONDS);
      scriptTester.apply(script);
    } finally {
      if (ssh != null) ssh.disconnect();
    }
  }
Ejemplo n.º 15
0
 private RunningInstance createInstance(String imageId) throws UnknownHostException {
   RunningInstance instance = null;
   while (instance == null) {
     try {
       System.out.printf("%d: running instance%n", System.currentTimeMillis());
       Reservation<? extends RunningInstance> reservation =
           client
               .getInstanceServices()
               .runInstancesInRegion(
                   null,
                   null, // allow
                   // ec2
                   // to
                   // chose
                   // an
                   // availability
                   // zone
                   imageId,
                   1, // minimum instances
                   1, // maximum instances
                   withKeyName(keyPair.getKeyName()) // key I created above
                       .asType(InstanceType.M1_SMALL) // smallest instance
                       // size
                       .withSecurityGroup(securityGroupName)); // group I
       // created
       // above
       instance = Iterables.getOnlyElement(reservation);
     } catch (HttpResponseException htpe) {
       if (htpe.getResponse().getStatusCode() == 400) continue;
       throw htpe;
     }
   }
   assertNotNull(instance.getId());
   assertEquals(instance.getInstanceState(), InstanceState.PENDING);
   instance = blockUntilWeCanSshIntoInstance(instance);
   return instance;
 }
Ejemplo n.º 16
0
  @Test(enabled = false, dependsOnMethods = "testCreateRunningInstance")
  void testCreateAndAttachVolume() {
    volume =
        client
            .getElasticBlockStoreServices()
            .createVolumeInAvailabilityZone(instance.getAvailabilityZone(), VOLUME_SIZE);
    System.out.printf(
        "%d: %s awaiting volume to become available%n", System.currentTimeMillis(), volume.getId());

    assert volumeTester.apply(volume);

    Attachment attachment =
        client
            .getElasticBlockStoreServices()
            .attachVolumeInRegion(
                instance.getRegion(), volume.getId(), instance.getId(), "/dev/sdh");

    System.out.printf(
        "%d: %s awaiting attachment to complete%n", System.currentTimeMillis(), attachment.getId());

    assert attachTester.apply(attachment);
    System.out.printf(
        "%d: %s attachment complete%n", System.currentTimeMillis(), attachment.getId());
  }
Ejemplo n.º 17
0
  @AfterTest
  void cleanup() {
    if (ebsInstance != null) {
      try {
        client
            .getInstanceServices()
            .terminateInstancesInRegion(ebsInstance.getRegion(), ebsInstance.getId());
        terminatedTester.apply(ebsInstance);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (ebsImage != null) {
      try {
        client.getAMIServices().deregisterImageInRegion(ebsImage.getRegion(), ebsImage.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if (snapshot != null) {
      try {
        client
            .getElasticBlockStoreServices()
            .deleteSnapshotInRegion(snapshot.getRegion(), snapshot.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (attachment != null) {
      try {
        client
            .getElasticBlockStoreServices()
            .detachVolumeInRegion(volume.getRegion(), volume.getId(), true);
        assert volumeTester.apply(volume);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (instance != null) {
      try {
        client
            .getInstanceServices()
            .terminateInstancesInRegion(instance.getRegion(), instance.getId());
        terminatedTester.apply(instance);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (volume != null) {
      try {
        client
            .getElasticBlockStoreServices()
            .deleteVolumeInRegion(volume.getRegion(), volume.getId());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (keyPair != null) {
      try {
        client
            .getKeyPairServices()
            .deleteKeyPairInRegion(keyPair.getRegion(), keyPair.getKeyName());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (securityGroupName != null) {
      try {
        client.getSecurityGroupServices().deleteSecurityGroupInRegion(null, securityGroupName);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
 protected void addCredentialsForInstance(NodeMetadataBuilder builder, RunningInstance instance) {
   builder.credentials(
       LoginCredentials.fromCredentials(
           credentialStore.get("node#" + instance.getRegion() + "/" + instance.getId())));
 }
 @Override
 public RegionAndName apply(RunningInstance from) {
   return new RegionAndName(from.getRegion(), from.getId());
 }
  @SuppressWarnings("unchecked")
  private void assertRegionAndZoneForLocation(Location location, String region, String zone) {
    String imageId = "ami1";
    String instanceCreatedId = "instance1";
    // setup mocks
    TemplateBuilder templateBuilder = createMock(TemplateBuilder.class);
    EC2CreateNodesInGroupThenAddToSet strategy = setupStrategy(templateBuilder);
    InputParams input = new InputParams(location);
    InstanceClient instanceClient = createMock(InstanceClient.class);
    RunInstancesOptions ec2Options = createMock(RunInstancesOptions.class);
    RunningInstance instance = createMock(RunningInstance.class);
    Reservation<? extends RunningInstance> reservation =
        new Reservation<RunningInstance>(
            region,
            ImmutableSet.<String>of(),
            ImmutableSet.<RunningInstance>of(instance),
            "ownerId",
            "requesterId",
            "reservationId");
    NodeMetadata nodeMetadata = createMock(NodeMetadata.class);

    // setup expectations
    expect(templateBuilder.fromTemplate(input.template)).andReturn(templateBuilder);
    expect(templateBuilder.build()).andReturn(input.template);
    expect(strategy.client.getInstanceServices()).andReturn(instanceClient).atLeastOnce();
    expect(
            strategy.createKeyPairAndSecurityGroupsAsNeededAndReturncustomize.execute(
                region, input.tag, input.template))
        .andReturn(ec2Options);
    expect(input.template.getLocation()).andReturn(input.location).atLeastOnce();
    expect(input.template.getImage()).andReturn(input.image).atLeastOnce();
    expect(input.image.getProviderId()).andReturn(imageId).atLeastOnce();
    expect(instanceClient.runInstancesInRegion(region, zone, imageId, 1, input.count, ec2Options))
        .andReturn(Reservation.class.cast(reservation));
    expect(instance.getId()).andReturn(instanceCreatedId).atLeastOnce();
    // simulate a lazy credentials fetch
    Credentials creds = new Credentials("foo", "bar");
    expect(strategy.instanceToCredentials.apply(instance)).andReturn(creds);
    expect(instance.getRegion()).andReturn(region).atLeastOnce();
    expect(strategy.credentialStore.put("node#" + region + "/" + instanceCreatedId, creds))
        .andReturn(null);

    expect(strategy.instancePresent.apply(new RegionAndName(region, instanceCreatedId)))
        .andReturn(true);
    expect(input.template.getOptions()).andReturn(input.options).atLeastOnce();

    expect(strategy.runningInstanceToNodeMetadata.apply(instance)).andReturn(nodeMetadata);
    expect(
            strategy.utils.customizeNodesAndAddToGoodMapOrPutExceptionIntoBadMap(
                eq(input.options),
                containsNodeMetadata(nodeMetadata),
                eq(input.nodes),
                eq(input.badNodes),
                eq(input.customization)))
        .andReturn(null);

    // replay mocks
    replay(templateBuilder);
    replay(instanceClient);
    replay(ec2Options);
    replay(instance);
    replay(nodeMetadata);
    input.replayMe();
    replayStrategy(strategy);

    // run
    strategy.execute(
        input.tag, input.count, input.template, input.nodes, input.badNodes, input.customization);

    // verify mocks
    verify(templateBuilder);
    verify(instanceClient);
    verify(ec2Options);
    verify(instance);
    verify(nodeMetadata);
    input.verifyMe();
    verifyStrategy(strategy);
  }