예제 #1
0
  public static void trace(@Nonnull CloudProvider provider, @Nonnull String apiCall) {
    if (logger.isInfoEnabled()) {
      ProviderContext ctx = provider.getContext();
      String accountNumber = getAccountNumber(ctx);
      String callName =
          provider.getProviderName().replaceAll(DELIMITER_REGEX, "_")
              + DELIMITER
              + provider.getCloudName().replaceAll(DELIMITER_REGEX, "_")
              + DELIMITER
              + accountNumber.replaceAll(DELIMITER_REGEX, "_")
              + DELIMITER
              + apiCall;

      try {
        CloudOperation current = null;

        if (logger.isDebugEnabled()) {
          long thread = Thread.currentThread().getId();

          current = operations.get(thread);
          if (current != null) {
            while (current.currentChild != null) {
              current = current.currentChild;
            }
            current.calls++;
          }
        }
        synchronized (apiCount) {
          if (apiCount.containsKey(callName)) {
            apiCount.put(callName, apiCount.get(callName) + 1);
          } else {
            apiCount.put(callName, 1L);
          }
        }
        if (logger.isTraceEnabled()) {
          if (current != null) {
            if (current.apiCalls == null) {
              current.apiCalls = new ArrayList<String>();
            }
            current.apiCalls.add(apiCall);
          }
        }
      } catch (Throwable t) {
        logger.warn("Error with API trace trace: " + t.getMessage());
      }
    }
  }
예제 #2
0
  public static void begin(@Nonnull CloudProvider provider, @Nonnull String operationName) {
    if (logger.isDebugEnabled()) {
      try {
        ProviderContext ctx = provider.getContext();
        String accountNumber = getAccountNumber(ctx);

        operationName =
            provider.getProviderName().replaceAll(DELIMITER_REGEX, "_")
                + DELIMITER
                + provider.getCloudName().replaceAll(DELIMITER_REGEX, "_")
                + DELIMITER
                + accountNumber.replaceAll(DELIMITER_REGEX, "_")
                + DELIMITER
                + operationName;
        long thread = Thread.currentThread().getId();
        CloudOperation operation = new CloudOperation(operationName);
        CloudOperation current = operations.get(thread);

        if (current == null) {
          operations.put(thread, operation);
        } else {
          while (current.currentChild != null) {
            current = current.currentChild;
          }
          current.currentChild = operation;
        }
        synchronized (operationCount) {
          if (operationCount.containsKey(operationName)) {
            operationCount.put(operationName, operationCount.get(operationName) + 1);
          } else {
            operationCount.put(operationName, 1L);
          }
        }
      } catch (Throwable t) {
        logger.warn("Error with API trace begin: " + t.getMessage());
      }
    }
  }
  /**
   * Matches a converged infrastructure against the criteria in this set of filter options.
   *
   * @param ci the converged infrastructure to test
   * @return true if the converged infrastructure matches all criteria
   */
  public boolean matches(@Nonnull ConvergedInfrastructure ci) {
    if (accountNumber != null) {
      if (!accountNumber.equals(ci.getProviderOwnerId())) {
        if (!matchesAny) {
          return false;
        }
      } else if (matchesAny) {
        return true;
      }
    }
    if (regex != null) {
      boolean matches = (ci.getName().matches(regex) || ci.getDescription().matches(regex));

      if (!matches) {
        for (Map.Entry<String, String> tag : ci.getTags().entrySet()) {
          String value = tag.getValue();

          if (value != null && value.matches(regex)) {
            matches = true;
            break;
          }
        }
      }
      if (!matches && !matchesAny) {
        return false;
      } else if (matches && matchesAny) {
        return true;
      }
    }
    if (tags != null && !tags.isEmpty()) {
      if (!CloudProvider.matchesTags(ci.getTags(), ci.getName(), ci.getDescription(), tags)) {
        if (!matchesAny) {
          return false;
        }
      } else if (matchesAny) {
        return true;
      }
    }
    return !matchesAny;
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * com.msi.tough.workflow.core.AbstractWorker#doWork0(com.google.protobuf
   * .Message, com.msi.tough.query.ServiceRequestContext)
   */
  @Override
  protected AssociateAddressResponse doWork0(
      AssociateAddressRequest req, ServiceRequestContext context) throws Exception {

    final AssociateAddressResponse.Builder result = AssociateAddressResponse.newBuilder();
    final AccountBean account = context.getAccountBean();

    final CloudProvider cloudProvider =
        DaseinHelper.getProvider(
            account.getDefZone(), account.getTenant(),
            account.getAccessKey(), account.getSecretKey());
    final ComputeServices compute = cloudProvider.getComputeServices();
    final VirtualMachineSupport vmSupport = compute.getVirtualMachineSupport();

    final String publicIp = req.getPublicIp();
    final String instanceId = req.getInstanceId();

    VirtualMachine vm = vmSupport.getVirtualMachine(instanceId);
    // Check if instance id refers to existing instance
    if (vm == null) {
      throw ComputeFaults.instanceDoesNotExist(instanceId);
    }

    final NetworkServices network = cloudProvider.getNetworkServices();
    final IpAddressSupport ipsupport = network.getIpAddressSupport();

    // check if specified address exists in the pool
    IpAddress address = null;
    for (final IpAddress i : ipsupport.listIpPool(IPVersion.IPV4, false)) {
      if (i.getRawAddress().getIpAddress().equals(publicIp)) {
        address = i;
        break;
      }
    }
    if (address == null || "".equals(address.getRawAddress().getIpAddress())) {
      throw ComputeFaults.IpAddressDoesNotExist(publicIp);
    }

    logger.debug("Address info - BEGIN: \n" + address.toString() + "\n - END");
    logger.debug("Address ID: " + address.getProviderIpAddressId());

    // Currently Dasein gets for the actual string "null" rather than the
    // null object for address.getServerId() if there is no assigned
    // instance
    // According to AWS docs, if address is associated with another
    // instance, disassociate it and reassociate to the instance specified
    // in the request.
    if (address.getServerId() != null && !address.getServerId().equals("null")) {
      logger.info("The address " + publicIp + " is currently associated with an instance.");
      logger.info("Diassociating address...");
      ipsupport.releaseFromServer(address.getProviderIpAddressId());
    }

    logger.info(
        "Associating address "
            + address.getRawAddress().getIpAddress()
            + " to instance "
            + instanceId);

    if ("OpenStack".equals(cloudProvider.getProviderName())) {
      String privateIp = null;
      int retryCount = 0;
      while (privateIp == null && retryCount++ < RETRY_MAX) {
        // Must avoid associating too early; instance should have a fixed IP.
        if (vm.getPrivateAddresses() != null && vm.getPrivateAddresses().length > 0) {
          privateIp = vm.getPrivateAddresses()[0].getIpAddress();
        }
        if (privateIp == null || privateIp.length() == 0 || privateIp.equals("0.0.0.0")) {
          logger.debug("Instance does not have private IP, waiting for network ready.");
          privateIp = null;
          Thread.sleep(RETRY_SECS * 1000);
          vm = vmSupport.getVirtualMachine(instanceId);
        }
      }
      if (retryCount >= RETRY_MAX) {
        logger.error("Error assigning IP Address: instance doesn't " + "have a private IP.");
        throw QueryFaults.invalidState();
      }
    }

    /*
     * TODO: Add VPC Support.
     *   THIS IMPLEMENTATION SUPPORTS EC2-CLASSIC ONLY!
     */
    try {
      ipsupport.assign(address.getProviderIpAddressId(), instanceId);
    } catch (final CloudException e) {
      final ExceptionItems eitms = NovaException.parseException(e.getHttpCode(), e.getMessage());
      throw new Exception("Error assigning IP Address: error type = " + eitms.type.toString());
    }

    /* If execution arrives here, no exceptions occurred */
    result.setReturn(true);
    return result.buildPartial();
  }