private StageWrapper getExecuteStageWrapper(UpgradeContext ctx, ExecuteStage execution) {
    String service   = execution.service;
    String component = execution.component;
    ExecuteTask et = (ExecuteTask) execution.task;

    if (StringUtils.isNotBlank(service) && StringUtils.isNotBlank(component)) {

      // !!! if the context is not scoped for the execute-stage, bail
      if (!ctx.isScoped(execution.scope)) {
        return null;
      }

      // !!! if the context is targeted and does not include the service, bail
      if (!ctx.isServiceSupported(service)) {
        return null;
      }

      // !!! FUTURE: check for component

      HostsType hosts = ctx.getResolver().getMasterAndHosts(service, component);

      if (hosts != null) {

        Set<String> realHosts = new LinkedHashSet<>(hosts.getHosts());
        if (ExecuteHostType.MASTER == et.hosts && hosts.hasMasters()) {
          realHosts = hosts.getMasters();
        }

        // Pick a random host.
        if (ExecuteHostType.ANY == et.hosts && !hosts.getHosts().isEmpty()) {
          realHosts = Collections.singleton(hosts.getHosts().iterator().next());
        }

        // Pick the first host sorted alphabetically (case insensitive)
        if (ExecuteHostType.FIRST == et.hosts && !hosts.getHighAvailabilityHosts().isEmpty()) {
          List<String> sortedHosts = new ArrayList<>(hosts.getHosts());
          Collections.sort(sortedHosts, String.CASE_INSENSITIVE_ORDER);
          realHosts = Collections.singleton(sortedHosts.get(0));
        }

        // !!! cannot execute against empty hosts (safety net)
        if (realHosts.isEmpty()) {
          return null;
        }

        return new StageWrapper(
            StageWrapper.Type.UPGRADE_TASKS,
            execution.title,
            new TaskWrapper(service, component, realHosts, et));
      }
    } else if (null == service && null == component) {
      // no service and no component will distributed the task to all healthy
      // hosts not in maintenance mode
      Cluster cluster = ctx.getCluster();
      Set<String> hostNames = new HashSet<>();
      for (Host host : ctx.getCluster().getHosts()) {
        MaintenanceState maintenanceState = host.getMaintenanceState(cluster.getClusterId());
        if (maintenanceState == MaintenanceState.OFF) {
          hostNames.add(host.getHostName());
        }
      }

      return new StageWrapper(
          StageWrapper.Type.UPGRADE_TASKS,
          execution.title,
          new TaskWrapper(service, component, hostNames, et));
    }
    return null;
  }
Return a Stage Wrapper for a task meant to execute code, typically on
Ambari Server.

@param ctx
         Upgrade Context
@param execution
         Execution Stage
@return Returns a Stage Wrapper, or null if a valid one could not be
        created.
ambari-server/src/main/java/org/apache/ambari/server/stack/upgrade/ClusterGrouping.java