protected Object doExecute()

in commands/src/main/java/org/jclouds/karaf/commands/compute/RunScriptBase.java [64:124]


   protected Object doExecute() throws Exception {
      ComputeService service = null;
      try {
         service = getComputeService();
      } catch (Throwable t) {
         System.err.println(t.getMessage());
         t.printStackTrace(System.err);
         return null;
      }
      Set<? extends NodeMetadata> nodeMetaDataSet = service.listNodesDetailsMatching(getComputeFilter());
      if (nodeMetaDataSet != null && !nodeMetaDataSet.isEmpty()) {
         NodeMetadata nodeMetadata = nodeMetaDataSet.toArray(new NodeMetadata[0])[0];

         // If we have multiple nodes we just want the headers, but not full details.
         printNodeInfo(service, nodeMetaDataSet, nodeMetaDataSet.size() == 1, System.out);

         LoginCredentials credentials = nodeMetadata.getCredentials();

         user = EnvHelper.getUser(user);
         password = EnvHelper.getPassword(password);

         if (user != null) {
            LoginCredentials.Builder loginBuilder;
            if (credentials == null) {
               loginBuilder = LoginCredentials.builder();
            } else {
               loginBuilder = credentials.toBuilder();
            }
            if (password != null) {
               credentials = loginBuilder.user(user).password(password).build();
            } else {
               credentials = loginBuilder.user(user).build();
            }
         }

         Map<? extends NodeMetadata, ExecResponse> responseMap = null;

          if (getScript() != null) {
              responseMap = service.runScriptOnNodesMatching(getNodeFilter(),
                      getScript(), overrideLoginCredentials(credentials).runAsRoot(runAsRoot()));
          } else if (getStatement() != null) {
             responseMap = service.runScriptOnNodesMatching(getNodeFilter(),
                      getStatement(), overrideLoginCredentials(credentials).runAsRoot(runAsRoot()));
          }

          for (Map.Entry<? extends NodeMetadata, ExecResponse> entry : responseMap.entrySet()) {
            ExecResponse response = entry.getValue();
            NodeMetadata node = entry.getKey();
            System.out.println("");
            if (response.getOutput() != null && !response.getOutput().isEmpty()) {
               System.out.println("Node:" + node.getId() + " Output:" + response.getOutput());
            }

            if (response.getError() != null && !response.getError().isEmpty()) {
               System.out.println("Node:" + node.getId() + " Error:" + response.getError());
            }
         }
      }

      return null;
   }