Example #1
0
  @Override
  public synchronized void releaseAddress(String ip) {

    IaasProvider iaasInfo = getIaasProvider();

    ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    ElasticIPAddressApi elasticIPAddressApi =
        context.unwrapApi(AWSEC2Api.class).getElasticIPAddressApi().get();
    String region = ComputeServiceBuilderUtil.extractRegion(iaasInfo);

    elasticIPAddressApi.disassociateAddressInRegion(region, ip);
    elasticIPAddressApi.releaseAddressInRegion(region, ip);
  }
Example #2
0
 /**
  * @param addressApi
  * @param region
  * @param ip
  * @param id
  */
 private boolean associatePublicIp(
     ElasticIPAddressApi addressApi, String region, String ip, String id) {
   try {
     addressApi.associateAddressInRegion(region, ip, id);
     log.info("Successfully associated public IP ");
     return true;
   } catch (Exception e) {
     log.debug("Exception in associating public IP " + e.getMessage());
     return false;
   }
 }
Example #3
0
  @Override
  public synchronized List<String> associateAddresses(NodeMetadata node) {
    IaasProvider iaasInfo = getIaasProvider();
    ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    ElasticIPAddressApi elasticIPAddressApi =
        context.unwrapApi(AWSEC2Api.class).getElasticIPAddressApi().get();
    String region = ComputeServiceBuilderUtil.extractRegion(iaasInfo);
    String ip = null;

    // first try to find an unassigned IP.
    ArrayList<PublicIpInstanceIdPair> unassignedIps =
        Lists.newArrayList(
            Iterables.filter(
                elasticIPAddressApi.describeAddressesInRegion(region),
                new Predicate<PublicIpInstanceIdPair>() {

                  @Override
                  public boolean apply(PublicIpInstanceIdPair arg0) {
                    return arg0.getInstanceId() == null;
                  }
                }));

    if (!unassignedIps.isEmpty()) {
      // try to prevent multiple parallel launches from choosing the same
      // ip.
      Collections.shuffle(unassignedIps);
      ip = Iterables.getLast(unassignedIps).getPublicIp();
    }

    // if no unassigned IP is available, we'll try to allocate an IP.
    if (ip == null || ip.isEmpty()) {
      try {
        ip = elasticIPAddressApi.allocateAddressInRegion(region);
        log.info("Allocated ip [" + ip + "]");

      } catch (Exception e) {
        String msg = "Failed to allocate an IP address. All IP addresses are in use.";
        log.error(msg, e);
        throw new CloudControllerException(msg, e);
      }
    }

    String id = node.getProviderId();

    // wait till the fixed IP address gets assigned - this is needed before
    // we associate a
    // public IP

    while (node.getPrivateAddresses() == null) {
      CloudControllerUtil.sleep(1000);
    }

    int retries = 0;
    while (retries < 12 && !associatePublicIp(elasticIPAddressApi, region, ip, id)) {

      // wait for 5s
      CloudControllerUtil.sleep(5000);
      retries++;
    }

    log.debug("Successfully associated an IP address " + ip + " for node with id: " + node.getId());

    List<String> associatedIPs = new ArrayList<String>();
    associatedIPs.add(ip);
    return associatedIPs;
  }