|
|
@ -17,29 +17,30 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.apache.dolphinscheduler.plugin.task.shell; |
|
|
|
package org.apache.dolphinscheduler.plugin.task.shell; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.apache.dolphinscheduler.spi.task.TaskConstants.EXIT_CODE_FAILURE; |
|
|
|
|
|
|
|
import static org.apache.dolphinscheduler.spi.task.TaskConstants.RWXR_XR_X; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.plugin.task.api.AbstractTaskExecutor; |
|
|
|
import org.apache.dolphinscheduler.plugin.task.api.AbstractTaskExecutor; |
|
|
|
import org.apache.dolphinscheduler.plugin.task.api.ShellCommandExecutor; |
|
|
|
import org.apache.dolphinscheduler.plugin.task.api.ShellCommandExecutor; |
|
|
|
import org.apache.dolphinscheduler.plugin.task.api.TaskException; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.plugin.task.api.TaskResponse; |
|
|
|
import org.apache.dolphinscheduler.plugin.task.api.TaskResponse; |
|
|
|
import org.apache.dolphinscheduler.plugin.task.util.OSUtils; |
|
|
|
import org.apache.dolphinscheduler.plugin.task.util.OSUtils; |
|
|
|
import org.apache.dolphinscheduler.spi.task.AbstractParameters; |
|
|
|
import org.apache.dolphinscheduler.spi.task.AbstractParameters; |
|
|
|
import org.apache.dolphinscheduler.spi.task.Direct; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.spi.task.Property; |
|
|
|
import org.apache.dolphinscheduler.spi.task.Property; |
|
|
|
import org.apache.dolphinscheduler.spi.task.TaskConstants; |
|
|
|
import org.apache.dolphinscheduler.spi.task.paramparser.ParamUtils; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.spi.task.paramparser.ParameterUtils; |
|
|
|
import org.apache.dolphinscheduler.spi.task.request.TaskRequest; |
|
|
|
import org.apache.dolphinscheduler.spi.task.request.TaskRequest; |
|
|
|
import org.apache.dolphinscheduler.spi.utils.JSONUtils; |
|
|
|
import org.apache.dolphinscheduler.spi.utils.JSONUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.collections4.MapUtils; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.StandardOpenOption; |
|
|
|
import java.nio.file.StandardOpenOption; |
|
|
|
import java.nio.file.attribute.FileAttribute; |
|
|
|
import java.nio.file.attribute.FileAttribute; |
|
|
|
import java.nio.file.attribute.PosixFilePermission; |
|
|
|
import java.nio.file.attribute.PosixFilePermission; |
|
|
|
import java.nio.file.attribute.PosixFilePermissions; |
|
|
|
import java.nio.file.attribute.PosixFilePermissions; |
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
|
@ -61,49 +62,47 @@ public class ShellTask extends AbstractTaskExecutor { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* taskExecutionContext |
|
|
|
* taskExecutionContext |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private TaskRequest taskRequest; |
|
|
|
private TaskRequest taskExecutionContext; |
|
|
|
|
|
|
|
|
|
|
|
private String command; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* constructor |
|
|
|
* constructor |
|
|
|
* |
|
|
|
* |
|
|
|
* @param taskRequest taskRequest |
|
|
|
* @param taskExecutionContext taskExecutionContext |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public ShellTask(TaskRequest taskRequest) { |
|
|
|
public ShellTask(TaskRequest taskExecutionContext) { |
|
|
|
super(taskRequest); |
|
|
|
super(taskExecutionContext); |
|
|
|
|
|
|
|
|
|
|
|
this.taskRequest = taskRequest; |
|
|
|
this.taskExecutionContext = taskExecutionContext; |
|
|
|
this.shellCommandExecutor = new ShellCommandExecutor(this::logHandle, |
|
|
|
this.shellCommandExecutor = new ShellCommandExecutor(this::logHandle, |
|
|
|
taskRequest, |
|
|
|
taskExecutionContext, |
|
|
|
logger); |
|
|
|
logger); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void init() { |
|
|
|
public void init() { |
|
|
|
logger.info("shell task params {}", taskRequest.getTaskParams()); |
|
|
|
logger.info("shell task params {}", taskExecutionContext.getTaskParams()); |
|
|
|
|
|
|
|
|
|
|
|
shellParameters = JSONUtils.parseObject(taskRequest.getTaskParams(), ShellParameters.class); |
|
|
|
shellParameters = JSONUtils.parseObject(taskExecutionContext.getTaskParams(), ShellParameters.class); |
|
|
|
|
|
|
|
|
|
|
|
assert shellParameters != null; |
|
|
|
|
|
|
|
if (!shellParameters.checkParameters()) { |
|
|
|
if (!shellParameters.checkParameters()) { |
|
|
|
throw new RuntimeException("shell task params is not valid"); |
|
|
|
throw new RuntimeException("shell task params is not valid"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void handle() { |
|
|
|
public void handle() throws Exception { |
|
|
|
try { |
|
|
|
try { |
|
|
|
// construct process
|
|
|
|
// construct process
|
|
|
|
TaskResponse response = shellCommandExecutor.run(command); |
|
|
|
String command = buildCommand(); |
|
|
|
setExitStatusCode(response.getExitStatusCode()); |
|
|
|
TaskResponse commandExecuteResult = shellCommandExecutor.run(command); |
|
|
|
setAppIds(response.getAppIds()); |
|
|
|
setExitStatusCode(commandExecuteResult.getExitStatusCode()); |
|
|
|
setProcessId(response.getProcessId()); |
|
|
|
setAppIds(commandExecuteResult.getAppIds()); |
|
|
|
setResult(shellCommandExecutor.getTaskResultString()); |
|
|
|
setProcessId(commandExecuteResult.getProcessId()); |
|
|
|
|
|
|
|
shellParameters.dealOutParam(shellCommandExecutor.getVarPool()); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("shell task error", e); |
|
|
|
logger.error("shell task error", e); |
|
|
|
setExitStatusCode(TaskConstants.EXIT_CODE_FAILURE); |
|
|
|
setExitStatusCode(EXIT_CODE_FAILURE); |
|
|
|
throw new TaskException("shell task error", e); |
|
|
|
throw e; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -113,36 +112,32 @@ public class ShellTask extends AbstractTaskExecutor { |
|
|
|
shellCommandExecutor.cancelApplication(); |
|
|
|
shellCommandExecutor.cancelApplication(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public String getPreScript() { |
|
|
|
|
|
|
|
return shellParameters.getRawScript().replaceAll("\\r\\n", "\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* set command |
|
|
|
* create command |
|
|
|
* |
|
|
|
* |
|
|
|
* @throws IOException exception |
|
|
|
* @return file name |
|
|
|
|
|
|
|
* @throws Exception exception |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
private String buildCommand() throws Exception { |
|
|
|
public void setCommand(String command) throws IOException { |
|
|
|
|
|
|
|
// generate scripts
|
|
|
|
// generate scripts
|
|
|
|
String fileName = String.format("%s/%s_node.%s", |
|
|
|
String fileName = String.format("%s/%s_node.%s", |
|
|
|
taskRequest.getExecutePath(), |
|
|
|
taskExecutionContext.getExecutePath(), |
|
|
|
taskRequest.getTaskAppId(), OSUtils.isWindows() ? "bat" : "sh"); |
|
|
|
taskExecutionContext.getTaskAppId(), OSUtils.isWindows() ? "bat" : "sh"); |
|
|
|
|
|
|
|
|
|
|
|
Path path = new File(fileName).toPath(); |
|
|
|
Path path = new File(fileName).toPath(); |
|
|
|
|
|
|
|
|
|
|
|
if (Files.exists(path)) { |
|
|
|
if (Files.exists(path)) { |
|
|
|
this.command = fileName; |
|
|
|
return fileName; |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
this.command = command; |
|
|
|
|
|
|
|
shellParameters.setRawScript(command); |
|
|
|
String script = shellParameters.getRawScript().replaceAll("\\r\\n", "\n"); |
|
|
|
|
|
|
|
script = parseScript(script); |
|
|
|
|
|
|
|
shellParameters.setRawScript(script); |
|
|
|
|
|
|
|
|
|
|
|
logger.info("raw script : {}", shellParameters.getRawScript()); |
|
|
|
logger.info("raw script : {}", shellParameters.getRawScript()); |
|
|
|
logger.info("task execute path : {}", taskRequest.getExecutePath()); |
|
|
|
logger.info("task execute path : {}", taskExecutionContext.getExecutePath()); |
|
|
|
|
|
|
|
|
|
|
|
Set<PosixFilePermission> perms = PosixFilePermissions.fromString(TaskConstants.RWXR_XR_X); |
|
|
|
Set<PosixFilePermission> perms = PosixFilePermissions.fromString(RWXR_XR_X); |
|
|
|
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); |
|
|
|
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); |
|
|
|
|
|
|
|
|
|
|
|
if (OSUtils.isWindows()) { |
|
|
|
if (OSUtils.isWindows()) { |
|
|
@ -152,7 +147,8 @@ public class ShellTask extends AbstractTaskExecutor { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Files.write(path, shellParameters.getRawScript().getBytes(), StandardOpenOption.APPEND); |
|
|
|
Files.write(path, shellParameters.getRawScript().getBytes(), StandardOpenOption.APPEND); |
|
|
|
this.command = fileName; |
|
|
|
|
|
|
|
|
|
|
|
return fileName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -160,16 +156,15 @@ public class ShellTask extends AbstractTaskExecutor { |
|
|
|
return shellParameters; |
|
|
|
return shellParameters; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void setResult(String result) { |
|
|
|
private String parseScript(String script) { |
|
|
|
Map<String, Property> localParams = shellParameters.getLocalParametersMap(); |
|
|
|
// combining local and global parameters
|
|
|
|
List<Map<String, String>> outProperties = new ArrayList<>(); |
|
|
|
Map<String, Property> paramsMap = ParamUtils.convert(taskExecutionContext,getParameters()); |
|
|
|
Map<String, String> p = new HashMap<>(); |
|
|
|
if (MapUtils.isEmpty(paramsMap)) { |
|
|
|
localParams.forEach((k, v) -> { |
|
|
|
paramsMap = new HashMap<>(); |
|
|
|
if (v.getDirect() == Direct.OUT) { |
|
|
|
} |
|
|
|
p.put(k, result); |
|
|
|
if (MapUtils.isNotEmpty(taskExecutionContext.getParamsMap())) { |
|
|
|
} |
|
|
|
paramsMap.putAll(taskExecutionContext.getParamsMap()); |
|
|
|
}); |
|
|
|
} |
|
|
|
outProperties.add(p); |
|
|
|
return ParameterUtils.convertParameterPlaceholders(script, ParamUtils.convert(paramsMap)); |
|
|
|
resultString = JSONUtils.toJsonString(outProperties); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|