Пример #1
0
  /**
   * Rules of updating AmazonInfo: - instanceId must exist - localIp/privateIp most exist -
   * publicHostname does not necessarily need to exist (e.g. in vpc)
   */
  /* visible for testing */ static boolean shouldUpdate(AmazonInfo newInfo, AmazonInfo oldInfo) {
    if (newInfo.getMetadata().isEmpty()) {
      logger.warn("Newly resolved AmazonInfo is empty, skipping an update cycle");
    } else if (!newInfo.equals(oldInfo)) {
      if (isBlank(newInfo.get(MetaDataKey.instanceId))) {
        logger.warn("instanceId is blank, skipping an update cycle");
        return false;
      } else if (isBlank(newInfo.get(MetaDataKey.localIpv4))) {
        logger.warn("localIpv4 is blank, skipping an update cycle");
        return false;
      } else {
        Set<String> newKeys = new HashSet<>(newInfo.getMetadata().keySet());
        Set<String> oldKeys = new HashSet<>(oldInfo.getMetadata().keySet());

        Set<String> union = new HashSet<>(newKeys);
        union.retainAll(oldKeys);
        newKeys.removeAll(union);
        oldKeys.removeAll(union);

        for (String key : newKeys) {
          logger.info("Adding new metadata {}={}", key, newInfo.getMetadata().get(key));
        }

        for (String key : oldKeys) {
          logger.info("Removing old metadata {}={}", key, oldInfo.getMetadata().get(key));
        }
      }

      return true;
    }
    return false;
  }
Пример #2
0
 @Override
 public String getHostName(boolean refresh) {
   if (refresh) {
     refreshAmazonInfo();
   }
   return info.get(MetaDataKey.publicHostname);
 }
Пример #3
0
  public String resolveDefaultAddress() {
    // In this method invocation data center info will be refreshed.
    String result = getHostName(true);

    for (String name : getDefaultAddressResolutionOrder()) {
      try {
        AmazonInfo.MetaDataKey key = AmazonInfo.MetaDataKey.valueOf(name);
        String address = info.get(key);
        if (address != null && !address.isEmpty()) {
          result = address;
          break;
        }
      } catch (Exception e) {
        logger.error("failed to resolve default address for key {}, skipping", name, e);
      }
    }

    return result;
  }
Пример #4
0
 private AmazonInfo initDataCenterInfo() {
   AmazonInfo info;
   try {
     info = AmazonInfo.Builder.newBuilder().autoBuild(namespace);
     logger.info("Datacenter is: " + Name.Amazon);
   } catch (Throwable e) {
     logger.error("Cannot initialize amazon info :", e);
     throw new RuntimeException(e);
   }
   // Instance id being null means we could not get the amazon metadata
   if (info.get(MetaDataKey.instanceId) == null) {
     if (propValidateInstanceId.get()) {
       throw new RuntimeException(
           "Your datacenter is defined as cloud but we are not able to get the amazon metadata to "
               + "register. \nSet the property "
               + namespace
               + "validateInstanceId to false to "
               + "ignore the metadata call");
     } else {
       // The property to not validate instance ids may be set for
       // development and in that scenario, populate instance id
       // and public hostname with the hostname of the machine
       Map<String, String> metadataMap = new HashMap<String, String>();
       metadataMap.put(MetaDataKey.instanceId.getName(), super.getIpAddress());
       metadataMap.put(MetaDataKey.publicHostname.getName(), super.getHostName(false));
       info.setMetadata(metadataMap);
     }
   } else if ((info.get(MetaDataKey.publicHostname) == null)
       && (info.get(MetaDataKey.localIpv4) != null)) {
     // :( legacy code and logic
     // This might be a case of VPC where the instance id is not null, but
     // public hostname might be null
     info.getMetadata()
         .put(MetaDataKey.publicHostname.getName(), (info.get(MetaDataKey.localIpv4)));
   }
   return info;
 }
Пример #5
0
 @Override
 public String getIpAddress() {
   String ipAddr = info.get(MetaDataKey.localIpv4);
   return ipAddr == null ? super.getIpAddress() : ipAddr;
 }