protected String getImageId(String imageName) {
    DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest();

    List<Filter> filters = new ArrayList<Filter>();

    Filter filter = new Filter();

    filter.setName("name");

    List<String> values = new ArrayList<String>();

    values.add(imageName);

    filter.setValues(values);

    filters.add(filter);

    describeImagesRequest.setFilters(filters);

    DescribeImagesResult describeImagesResult =
        amazonEC2Client.describeImages(describeImagesRequest);

    List<Image> images = describeImagesResult.getImages();

    if (images.isEmpty()) {
      throw new RuntimeException("Image " + imageName + " does not exist");
    }

    Image image = images.get(0);

    return image.getImageId();
  }
Example #2
0
  public String toString() {
    String ret = "\n";
    while (true) {
      if (amazon == null)
        try {
          amazon = JCS.getInstance("Instances");
        } catch (CacheException e) {

          e.printStackTrace();
          continue;
        }
      synchronized (AmazonService.class) {
        for (Object key : amazon.getGroupKeys("Instances")) {
          Instance i = (Instance) amazon.getFromGroup(key, "Instances");
          if (i == null) continue;
          ret +=
              (i.getInstanceId()
                      + " "
                      + i.getImageId()
                      + " "
                      + i.getImageId()
                      + " "
                      + i.getArchitecture()
                      + " "
                      + i.getPrivateDnsName()
                      + " "
                      + i.getState().getName())
                  + "\n";
        }
        for (Object key : amazon.getGroupKeys("AMIs")) {
          Image r = (Image) amazon.getFromGroup(key, "AMIs");
          ;
          if (r == null) continue;
          ret += (r.getImageId() + " " + r.getDescription() + " " + r.getState()) + "\n";
        }
      }
      if (ret.length() > 10) break;
      else {
        try {
          Thread.sleep(1000 * 5);
        } catch (InterruptedException e) {

          e.printStackTrace();
        }
        continue;
      }
    }

    return ret;
  }
Example #3
0
  private List<BlockDeviceMapping> getAmiBlockDeviceMappings() {

    /*
     * AmazonEC2#describeImageAttribute does not work due to a bug
     * https://forums.aws.amazon.com/message.jspa?messageID=231972
     */
    for (final Image image : getParent().connect().describeImages().getImages()) {

      if (ami.equals(image.getImageId())) {

        return image.getBlockDeviceMappings();
      }
    }

    throw new AmazonClientException("Unable to get AMI device mapping for " + ami);
  }
Example #4
0
  private static void putCache() throws CacheException {
    if (amazon == null) amazon = JCS.getInstance("Instances");
    synchronized (AmazonService.class) {
      amazon.clear();
      DescribeInstancesResult result = amazonClient.describeInstances();
      for (Reservation r : result.getReservations()) {
        for (Instance i : r.getInstances()) {
          amazon.putInGroup(i.getInstanceId(), "Instances", i);
        }
      }

      DescribeImagesRequest ir = new DescribeImagesRequest().withOwners("self");
      DescribeImagesResult images = amazonClient.describeImages(ir);
      for (Image r : images.getImages()) {
        amazon.putInGroup(r.getImageId(), "AMIs", r);
      }
      log.debug("put cache finished");
    }
  }