/**
   * Determines whether the AMI of the given instance matches the AMI of template and has the
   * required label (if requiredLabel is non-null)
   */
  private boolean checkInstance(
      PrintStream logger,
      Instance existingInstance,
      Label requiredLabel,
      EC2AbstractSlave[] returnNode) {
    logProvision(logger, "checkInstance: " + existingInstance);
    if (StringUtils.isNotBlank(getIamInstanceProfile())) {
      if (existingInstance.getIamInstanceProfile() != null) {
        if (!existingInstance.getIamInstanceProfile().getArn().equals(getIamInstanceProfile())) {
          logProvision(logger, " false - IAM Instance profile does not match");
          return false;
        }
        // Match, fall through
      } else {
        logProvision(logger, " false - Null IAM Instance profile");
        return false;
      }
    }

    if (existingInstance
            .getState()
            .getName()
            .equalsIgnoreCase(InstanceStateName.Terminated.toString())
        || existingInstance
            .getState()
            .getName()
            .equalsIgnoreCase(InstanceStateName.ShuttingDown.toString())) {
      logProvision(logger, " false - Instance is terminated or shutting down");
      return false;
    }
    // See if we know about this and it has capacity
    for (EC2AbstractSlave node : NodeIterator.nodes(EC2AbstractSlave.class)) {
      if (node.getInstanceId().equals(existingInstance.getInstanceId())) {
        logProvision(logger, "Found existing corresponding Jenkins slave: " + node.getInstanceId());
        if (!node.toComputer().isPartiallyIdle()) {
          logProvision(logger, " false - Node is not partially idle");
          return false;
        }
        // REMOVEME - this was added to force provision to work, but might not allow
        // stopped instances to be found - need to investigate further
        else if (false && node.toComputer().isOffline()) {
          logProvision(logger, " false - Node is offline");
          return false;
        } else if (requiredLabel != null && !requiredLabel.matches(node.getAssignedLabels())) {
          logProvision(logger, " false - we need a Node having label " + requiredLabel);
          return false;
        } else {
          logProvision(logger, " true - Node has capacity - can use it");
          returnNode[0] = node;
          return true;
        }
      }
    }
    logProvision(logger, " true - Instance has no node, but can be used");
    return true;
  }
  /** Initializes data structure that we don't persist. */
  protected Object readResolve() {
    labelSet = Label.parse(labels);
    securityGroupSet = parseSecurityGroups();

    /**
     * In releases of this plugin prior to 1.18, template-specific instance caps could be configured
     * but were not enforced. As a result, it was possible to have the instance cap for a template
     * be configured to 0 (zero) with no ill effects. Starting with version 1.18, template-specific
     * instance caps are enforced, so if a configuration has a cap of zero for a template, no
     * instances will be launched from that template. Since there is no practical value of
     * intentionally setting the cap to zero, this block will override such a setting to a value
     * that means 'no cap'.
     */
    if (instanceCap == 0) {
      instanceCap = Integer.MAX_VALUE;
    }

    if (amiType == null) {
      amiType = new UnixData(rootCommandPrefix, sshPort);
    }
    return this;
  }