/*
 * Copyright 2012 DTO Labs, Inc. (http://dtolabs.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/*
* LocalExecNodeStepPlugin.java
* 
* User: Greg Schueler <a href="mailto:greg@dtosolutions.com">greg@dtosolutions.com</a>
* Created: 12/13/12 4:54 PM
* 
*/
package com.dtolabs.rundeck.plugin.localexec;

import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.dispatcher.DataContextUtils;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepFailureReason;
import com.dtolabs.rundeck.core.execution.workflow.steps.node.NodeStepException;
import com.dtolabs.rundeck.core.execution.workflow.steps.node.NodeStepFailureReason;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.core.utils.ScriptExecUtil;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription;
import com.dtolabs.rundeck.plugins.descriptions.PluginProperty;
import com.dtolabs.rundeck.plugins.step.NodeStepPlugin;
import com.dtolabs.rundeck.plugins.step.PluginStepContext;

import java.io.IOException;
import java.util.Map;


/**
 * LocalExecNodeStepPlugin is ...
 *
 * @author Greg Schueler <a href="mailto:greg@dtosolutions.com">greg@dtosolutions.com</a>
 */
@Plugin(service = ServiceNameConstants.WorkflowNodeStep, name = LocalExecNodeStepPlugin.PROVIDER_NAME)
@PluginDescription(title = "Local Command", description = "Run a command locally on the server")
public class LocalExecNodeStepPlugin implements NodeStepPlugin {
    public static final String PROVIDER_NAME = "localexec";

    @PluginProperty(title = "Command", description = "The command (runs locally)", required = true)
    private String command;

    @Override
    public void executeNodeStep(PluginStepContext context, Map<String, Object> map, INodeEntry entry)
        throws NodeStepException {
        if(null==command || "".equals(command.trim())) {
            throw new NodeStepException("Command is not set",
                                        StepFailureReason.ConfigurationFailure,
                                        entry.getNodename());
        }
        String[] split = command.split(" ");
        Map<String, Map<String, String>> nodeData = DataContextUtils.addContext("node",
                                                                                DataContextUtils.nodeData(entry),
                                                                                context.getDataContext());
        String[] finalCommand = DataContextUtils.replaceDataReferences(split, nodeData);
        Map<String, String> env = DataContextUtils.generateEnvVarsFromContext(nodeData);
        final int result;
        try {
            result = ScriptExecUtil.runLocalCommand(finalCommand, env, null, System.out, System.err);
            if(result!=0) {
                throw new NodeStepException("Result code was " + result,
                                            NodeStepFailureReason.NonZeroResultCode,
                                            entry.getNodename());
            }
        } catch (IOException e) {
            throw new NodeStepException(e, StepFailureReason.IOFailure, entry.getNodename());
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new NodeStepException(e, StepFailureReason.Interrupted, entry.getNodename());
        }
    }
}

@@ -822,16 +822,62 @@
 lexec;%0A%0A
+import com.dtolabs.rundeck.core.cli.CLIUtils;%0A
 import c
@@ -1262,32 +1262,129 @@
 plugins.Plugin;%0A
+import com.dtolabs.rundeck.core.utils.Converter;%0Aimport com.dtolabs.rundeck.core.utils.OptsUtil;%0A
 import com.dtola
@@ -2818,25 +2818,30 @@
 t = 
-command.split(%22 %22
+OptsUtil.burst(command
 );%0A 
@@ -3148,24 +3148,25 @@
 ext());%0A
+%0A
         
 String%5B%5D
@@ -3149,32 +3149,38 @@
 xt());%0A%0A        
+final 
 String%5B%5D finalCo
@@ -3236,32 +3236,341 @@
 lit, nodeData);%0A
+        StringBuilder preview=new StringBuilder();%0A%0A        for (int i = 0; i %3C finalCommand.length; i++) %7B%0A            preview.append(%22'%22).append(finalCommand%5Bi%5D).append(%22'%22);%0A        %7D%0A        context.getLogger().log(5, %22LocalExecNodeStepPlugin, running command (%22+split.length+%22): %22 + preview.toString());%0A
         Map%3CStri

Use optUtil burst for localexec command string