Ejemplo n.º 1
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);
    }
  }
  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();
    }
  }