Ejemplo n.º 1
0
 protected boolean isEc2ProvisionedAmiSlave(Instance i, String ami, String templateDesc) {
   // Check if the ami matches
   if (ami == null || StringUtils.equals(ami, i.getImageId())) {
     // Check if there is a ec2slave tag...
     for (Tag tag : i.getTags()) {
       if (StringUtils.equals(tag.getKey(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE)) {
         if (ami == null || templateDesc == null) {
           return true;
         } else if (StringUtils.equals(tag.getValue(), EC2Cloud.EC2_SLAVE_TYPE_DEMAND)
             || StringUtils.equals(tag.getValue(), EC2Cloud.EC2_SLAVE_TYPE_SPOT)) {
           // To handle cases where description is null and also upgrade cases for existing slave
           // nodes.
           return true;
         } else if (StringUtils.equals(
                 tag.getValue(),
                 getSlaveTypeTagValue(EC2Cloud.EC2_SLAVE_TYPE_DEMAND, templateDesc))
             || StringUtils.equals(
                 tag.getValue(),
                 getSlaveTypeTagValue(EC2Cloud.EC2_SLAVE_TYPE_SPOT, templateDesc))) {
           return true;
         } else {
           return false;
         }
       }
     }
     return false;
   }
   return false;
 }
Ejemplo n.º 2
0
  public static void main(String[] args) throws UnknownHostException {
    AmazonEC2 ec2 = new AmazonEC2Client(new ClasspathPropertiesFileCredentialsProvider());
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    ec2.setRegion(usWest2);
    List<String> instanceIds = new ArrayList<>();
    String instanceid = null;
    DescribeInstancesRequest request = new DescribeInstancesRequest();

    List<String> valuesT1 = new ArrayList<String>();
    valuesT1.add("hz-nodes");
    Filter filter = new Filter("tag-value", valuesT1);

    DescribeInstancesResult result = ec2.describeInstances(request.withFilters(filter));

    List<Reservation> reservations = result.getReservations();

    for (Reservation reservation : reservations) {
      List<Instance> instances = reservation.getInstances();
      for (Instance instance : instances) {
        instanceid = instance.getInstanceId();
        PrintManager.PrintMessage(instanceid);
        instanceIds.add(instanceid);
        List<Tag> abc = instance.getTags();
        for (Tag aaa : abc) {
          PrintManager.PrintMessage(aaa.getKey() + " : " + aaa.getValue());
        }
      }
    }
    instanceIds.remove("i-cb45adfc");
    StopInstancesRequest stopReq = new StopInstancesRequest(instanceIds);
    ec2.stopInstances(stopReq);
    RebootInstancesRequest requestRe = new RebootInstancesRequest(instanceIds);
    ec2.rebootInstances(requestRe);
  }
Ejemplo n.º 3
0
  /*
   * Much of the EC2 data is beyond our direct control, therefore we need to refresh it from time to time to ensure we
   * reflect the reality of the instances.
   */
  protected void fetchLiveInstanceData(boolean force) throws AmazonClientException {
    /*
     * If we've grabbed the data recently, don't bother getting it again unless we are forced
     */
    long now = System.currentTimeMillis();
    if ((lastFetchTime > 0) && (now - lastFetchTime < MIN_FETCH_TIME) && !force) {
      return;
    }

    if (getInstanceId() == null || getInstanceId() == "") {
      /*
       * The getInstanceId() implementation on EC2SpotSlave can return null if the spot request doesn't yet know
       * the instance id that it is starting. What happens is that null is passed to getInstanceId() which
       * searches AWS but without an instanceID the search returns some random box. We then fetch its metadata,
       * including tags, and then later, when the spot request eventually gets the instanceID correctly we push
       * the saved tags from that random box up to the new spot resulting in confusion and delay.
       */
      return;
    }

    Instance i = getInstance(getInstanceId(), getCloud());

    lastFetchTime = now;
    lastFetchInstance = i;
    if (i == null) return;

    publicDNS = i.getPublicDnsName();
    privateDNS = i.getPrivateIpAddress();
    createdTime = i.getLaunchTime().getTime();
    tags = new LinkedList<EC2Tag>();

    for (Tag t : i.getTags()) {
      tags.add(new EC2Tag(t.getKey(), t.getValue()));
    }
  }
  /** Query amazon to get ASG name. Currently not available as part of instance info api. */
  private String populateASGName(String region, String instanceId) {
    AmazonEC2 client = new AmazonEC2Client(provider.getCredentials());
    client.setEndpoint("ec2." + region + ".amazonaws.com");
    DescribeInstancesRequest desc = new DescribeInstancesRequest().withInstanceIds(instanceId);
    DescribeInstancesResult res = client.describeInstances(desc);

    for (Reservation resr : res.getReservations()) {
      for (Instance ins : resr.getInstances()) {
        for (com.amazonaws.services.ec2.model.Tag tag : ins.getTags()) {
          if (tag.getKey().equals("aws:autoscaling:groupName")) return tag.getValue();
        }
      }
    }
    return null;
  }
Ejemplo n.º 5
0
    @Override
    public String retriableCall() throws IllegalStateException {
      DescribeInstancesRequest desc = new DescribeInstancesRequest().withInstanceIds(instanceId);
      DescribeInstancesResult res = client.describeInstances(desc);

      for (Reservation resr : res.getReservations()) {
        for (Instance ins : resr.getInstances()) {
          for (com.amazonaws.services.ec2.model.Tag tag : ins.getTags()) {
            if (tag.getKey().equals("aws:autoscaling:groupName")) return tag.getValue();
          }
        }
      }

      logger.warn("Couldn't determine ASG name");
      throw new IllegalStateException("Couldn't determine ASG name");
    }
Ejemplo n.º 6
0
  private List<Instance> runningInstances(String resourceId) {
    Tag tag = new EC2TagHelper(env).resourceId(resourceId);
    DescribeTagsRequest request =
        new DescribeTagsRequest()
            .withFilters(
                new Filter("key").withValues(tag.getKey()),
                new Filter("value").withValues(tag.getValue()),
                new Filter("resource-type").withValues("instance"));
    List<TagDescription> remoteTags = AWS.ec2.describeTags(request);
    List<String> instanceIds =
        remoteTags.stream().map(TagDescription::getResourceId).collect(Collectors.toList());

    if (instanceIds.isEmpty()) {
      com.amazonaws.services.autoscaling.model.AutoScalingGroup asGroup =
          AWS.as.describeASGroup(env.name + "-" + this.resourceId);
      if (asGroup == null)
        throw new Error("can not find any running instance or asGroup, id=" + this.resourceId);

      instanceIds =
          asGroup
              .getInstances()
              .stream()
              .map(com.amazonaws.services.autoscaling.model.Instance::getInstanceId)
              .collect(Collectors.toList());
    }

    logger.info("find instanceId, {} => {}", resourceId, instanceIds);

    List<Instance> instances =
        AWS.ec2
            .describeInstances(instanceIds)
            .stream()
            .filter(instance -> "running".equals(instance.getState().getName()))
            .collect(Collectors.toList());
    if (instances.isEmpty()) throw new Error("can not find any running instance, id=" + resourceId);

    return instances;
  }
Ejemplo n.º 7
0
 /* Constructor from Amazon Tag */
 public EC2Tag(Tag t) {
   name = t.getKey();
   value = t.getValue();
 }