private AutoScalingGroup mkAsg(String asgName, String instanceId) {
   AutoScalingGroup asg = new AutoScalingGroup();
   asg.setAutoScalingGroupName(asgName);
   Instance inst = new Instance();
   inst.setInstanceId(instanceId);
   asg.setInstances(Arrays.asList(inst));
   return asg;
 }
Example #2
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;
  }