Exemplo n.º 1
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;
  }
Exemplo n.º 2
0
 private Instance locateInstanceToSSH(List<Instance> instances) {
   for (int i = 0; i < instances.size(); i++) {
     com.amazonaws.services.ec2.model.Instance remoteInstance = instances.get(i);
     logger.info(
         "index={}, instanceId={}, state={}, publicDNS={}, privateDNS={}",
         i,
         remoteInstance.getInstanceId(),
         remoteInstance.getState().getName(),
         remoteInstance.getPublicDnsName(),
         remoteInstance.getPrivateDnsName());
   }
   Asserts.isTrue(
       instances.size() == 1 || instanceIndex != null,
       "more than one remoteInstance, use --{} to specify index",
       Param.INSTANCE_INDEX.key);
   return instances.size() == 1 ? instances.get(0) : instances.get(instanceIndex);
 }
  private String getHostName(EC2Computer computer)
      throws AmazonClientException, InterruptedException {
    Instance instance;
    instance = computer.updateInstanceDescription();
    String vpc_id = instance.getVpcId();
    String host;

    if (computer.getNode().usePrivateDnsName) {
      host = instance.getPrivateDnsName();
    } else {
      /* VPC hosts don't have public DNS names, so we need to use an IP address instead */
      if (vpc_id == null || vpc_id.equals("")) {
        host = instance.getPublicDnsName();
      } else {
        host = instance.getPrivateIpAddress();
      }
    }
    return host;
  }
Exemplo n.º 4
0
 protected EC2OndemandSlave newOndemandSlave(Instance inst) throws FormException, IOException {
   return new EC2OndemandSlave(
       inst.getInstanceId(),
       description,
       remoteFS,
       getNumExecutors(),
       labels,
       mode,
       initScript,
       tmpDir,
       remoteAdmin,
       jvmopts,
       stopOnTerminate,
       idleTerminationMinutes,
       inst.getPublicDnsName(),
       inst.getPrivateDnsName(),
       EC2Tag.fromAmazonTags(inst.getTags()),
       parent.name,
       usePrivateDnsName,
       useDedicatedTenancy,
       getLaunchTimeout(),
       amiType);
 }
Exemplo n.º 5
0
 private String hostName(Instance remoteInstance) {
   String publicDNS = remoteInstance.getPublicDnsName();
   return publicDNS != null ? publicDNS : remoteInstance.getPrivateDnsName();
 }
  @Override
  public List<DiscoveryNode> buildDynamicNodes() {
    List<DiscoveryNode> discoNodes = new ArrayList<>();

    DescribeInstancesResult descInstances;
    try {
      // Query EC2 API based on AZ, instance state, and tag.

      // NOTE: we don't filter by security group during the describe instances request for two
      // reasons:
      // 1. differences in VPCs require different parameters during query (ID vs Name)
      // 2. We want to use two different strategies: (all security groups vs. any security groups)
      descInstances = client.describeInstances(buildDescribeInstancesRequest());
    } catch (AmazonClientException e) {
      logger.info("Exception while retrieving instance list from AWS API: {}", e.getMessage());
      logger.debug("Full exception:", e);
      return discoNodes;
    }

    logger.trace("building dynamic unicast discovery nodes...");
    for (Reservation reservation : descInstances.getReservations()) {
      for (Instance instance : reservation.getInstances()) {
        // lets see if we can filter based on groups
        if (!groups.isEmpty()) {
          List<GroupIdentifier> instanceSecurityGroups = instance.getSecurityGroups();
          ArrayList<String> securityGroupNames = new ArrayList<String>();
          ArrayList<String> securityGroupIds = new ArrayList<String>();
          for (GroupIdentifier sg : instanceSecurityGroups) {
            securityGroupNames.add(sg.getGroupName());
            securityGroupIds.add(sg.getGroupId());
          }
          if (bindAnyGroup) {
            // We check if we can find at least one group name or one group id in groups.
            if (Collections.disjoint(securityGroupNames, groups)
                && Collections.disjoint(securityGroupIds, groups)) {
              logger.trace(
                  "filtering out instance {} based on groups {}, not part of {}",
                  instance.getInstanceId(),
                  instanceSecurityGroups,
                  groups);
              // continue to the next instance
              continue;
            }
          } else {
            // We need tp match all group names or group ids, otherwise we ignore this instance
            if (!(securityGroupNames.containsAll(groups) || securityGroupIds.containsAll(groups))) {
              logger.trace(
                  "filtering out instance {} based on groups {}, does not include all of {}",
                  instance.getInstanceId(),
                  instanceSecurityGroups,
                  groups);
              // continue to the next instance
              continue;
            }
          }
        }

        String address = null;
        switch (hostType) {
          case PRIVATE_DNS:
            address = instance.getPrivateDnsName();
            break;
          case PRIVATE_IP:
            address = instance.getPrivateIpAddress();
            break;
          case PUBLIC_DNS:
            address = instance.getPublicDnsName();
            break;
          case PUBLIC_IP:
            address = instance.getPublicIpAddress();
            break;
        }
        if (address != null) {
          try {
            TransportAddress[] addresses = transportService.addressesFromString(address);
            // we only limit to 1 addresses, makes no sense to ping 100 ports
            for (int i = 0; (i < addresses.length && i < UnicastZenPing.LIMIT_PORTS_COUNT); i++) {
              logger.trace(
                  "adding {}, address {}, transport_address {}",
                  instance.getInstanceId(),
                  address,
                  addresses[i]);
              discoNodes.add(
                  new DiscoveryNode(
                      "#cloud-" + instance.getInstanceId() + "-" + i,
                      addresses[i],
                      version.minimumCompatibilityVersion()));
            }
          } catch (Exception e) {
            logger.warn("failed ot add {}, address {}", e, instance.getInstanceId(), address);
          }
        } else {
          logger.trace(
              "not adding {}, address is null, host_type {}", instance.getInstanceId(), hostType);
        }
      }
    }

    logger.debug("using dynamic discovery nodes {}", discoNodes);

    return discoNodes;
  }