コード例 #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);
    }
  }
  protected Set<RunningInstance> createNodesInRegionAndZone(
      String region,
      String zone,
      String group,
      int count,
      Template template,
      RunInstancesOptions instanceOptions) {
    int countStarted = 0;
    int tries = 0;
    Set<RunningInstance> started = ImmutableSet.<RunningInstance>of();

    while (countStarted < count && tries++ < count) {
      if (logger.isDebugEnabled())
        logger.debug(
            ">> running %d instance region(%s) zone(%s) ami(%s) params(%s)",
            count - countStarted,
            region,
            zone,
            template.getImage().getProviderId(),
            instanceOptions.buildFormParameters());

      started =
          ImmutableSet.copyOf(
              concat(
                  started,
                  client
                      .getInstanceServices()
                      .runInstancesInRegion(
                          region,
                          zone,
                          template.getImage().getProviderId(),
                          1,
                          count - countStarted,
                          instanceOptions)));

      countStarted = size(started);
      if (countStarted < count)
        logger.debug(
            ">> not enough instances (%d/%d) started, attempting again", countStarted, count);
    }
    return started;
  }