🔨 设置执行参数逻辑.
This commit is contained in:
@@ -167,21 +167,12 @@ public abstract class BaseExecCommandHandler implements IExecCommandHandler {
|
|||||||
Valid.eq(this.status, ExecHostStatusEnum.WAITING, ErrorMessage.TASK_ABSENT, ErrorMessage.ILLEGAL_STATUS);
|
Valid.eq(this.status, ExecHostStatusEnum.WAITING, ErrorMessage.TASK_ABSENT, ErrorMessage.ILLEGAL_STATUS);
|
||||||
// 获取主机会话
|
// 获取主机会话
|
||||||
this.connect = terminalService.getTerminalConnectInfo(execHostLog.getHostId(), execLog.getUserId());
|
this.connect = terminalService.getTerminalConnectInfo(execHostLog.getHostId(), execLog.getUserId());
|
||||||
// 获取内置参数
|
// 设置日志路径
|
||||||
Map<String, Object> commandParams = this.getCommandParams();
|
this.setLogPath();
|
||||||
// 获取实际命令
|
// 设置脚本路径
|
||||||
String command = ExecUtils.format(execLog.getCommand(), commandParams);
|
this.setScriptPath();
|
||||||
// 获取日志路径
|
// 设置执行的命令以及参数
|
||||||
String logPath = fileClient.getReturnPath(EndpointDefine.EXEC_LOG.format(execHostLog.getLogId(), execHostLog.getHostId()));
|
this.setExecCommand();
|
||||||
// 获取脚本路径
|
|
||||||
String scriptPath = null;
|
|
||||||
if (BooleanBit.toBoolean(execLog.getScriptExec())) {
|
|
||||||
scriptPath = this.buildScriptPath();
|
|
||||||
}
|
|
||||||
execHostLog.setCommand(command);
|
|
||||||
execHostLog.setParameter(JSON.toJSONString(commandParams));
|
|
||||||
execHostLog.setLogPath(logPath);
|
|
||||||
execHostLog.setScriptPath(scriptPath);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("BaseExecCommandHandler.initData error id: {}", id, e);
|
log.error("BaseExecCommandHandler.initData error id: {}", id, e);
|
||||||
ex = e;
|
ex = e;
|
||||||
@@ -191,6 +182,9 @@ public abstract class BaseExecCommandHandler implements IExecCommandHandler {
|
|||||||
this.updateStatus(passed ? ExecHostStatusEnum.RUNNING : ExecHostStatusEnum.FAILED, ex, (s) -> {
|
this.updateStatus(passed ? ExecHostStatusEnum.RUNNING : ExecHostStatusEnum.FAILED, ex, (s) -> {
|
||||||
// 修改其他参数
|
// 修改其他参数
|
||||||
s.setCommand(execHostLog.getCommand());
|
s.setCommand(execHostLog.getCommand());
|
||||||
|
s.setParameter(execHostLog.getParameter());
|
||||||
|
s.setLogPath(execHostLog.getLogPath());
|
||||||
|
s.setScriptPath(execHostLog.getScriptPath());
|
||||||
});
|
});
|
||||||
return passed;
|
return passed;
|
||||||
}
|
}
|
||||||
@@ -371,7 +365,59 @@ public abstract class BaseExecCommandHandler implements IExecCommandHandler {
|
|||||||
Streams.close(logOutputStream);
|
Streams.close(logOutputStream);
|
||||||
Streams.close(executor);
|
Streams.close(executor);
|
||||||
Streams.close(sessionStore);
|
Streams.close(sessionStore);
|
||||||
execLogManager.asyncCloseTailFile(execHostLog.getLogPath());
|
execLogManager.asyncCloseTailFile(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置日志路径
|
||||||
|
*/
|
||||||
|
private void setLogPath() {
|
||||||
|
// 构建日志路径
|
||||||
|
String logPath = fileClient.getReturnPath(EndpointDefine.EXEC_LOG.format(execHostLog.getLogId(), id));
|
||||||
|
execHostLog.setLogPath(logPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置脚本路径
|
||||||
|
*/
|
||||||
|
private void setScriptPath() {
|
||||||
|
String scriptPath = null;
|
||||||
|
// 获取脚本路径
|
||||||
|
if (BooleanBit.toBoolean(execLog.getScriptExec())) {
|
||||||
|
HostOsTypeEnum os = HostOsTypeEnum.of(connect.getOsType());
|
||||||
|
String name = FileConst.EXEC
|
||||||
|
+ "/" + execHostLog.getLogId()
|
||||||
|
+ "/" + id
|
||||||
|
+ os.getScriptSuffix();
|
||||||
|
scriptPath = PathUtils.buildAppPath(HostOsTypeEnum.WINDOWS.equals(os), connect.getUsername(), FileConst.SCRIPT, name);
|
||||||
|
}
|
||||||
|
execHostLog.setScriptPath(scriptPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置实际执行的命令以及参数
|
||||||
|
*/
|
||||||
|
private void setExecCommand() {
|
||||||
|
String uuid = UUIds.random();
|
||||||
|
// 参数列表
|
||||||
|
Map<String, Object> params = Maps.newMap(builtParams);
|
||||||
|
params.put("execHostId", id);
|
||||||
|
params.put("hostId", connect.getHostId());
|
||||||
|
params.put("hostName", connect.getHostName());
|
||||||
|
params.put("hostCode", connect.getHostCode());
|
||||||
|
params.put("hostAddress", connect.getHostAddress());
|
||||||
|
params.put("hostPort", connect.getHostPort());
|
||||||
|
params.put("hostUsername", connect.getUsername());
|
||||||
|
params.put("hostUuid", uuid);
|
||||||
|
params.put("hostUuidShort", uuid.replace("-", Strings.EMPTY));
|
||||||
|
params.put("osType", connect.getOsType());
|
||||||
|
params.put("charset", connect.getCharset());
|
||||||
|
params.put("scriptPath", execHostLog.getScriptPath());
|
||||||
|
// 获取实际命令
|
||||||
|
String paramsJson = JSON.toJSONString(params);
|
||||||
|
String command = ExecUtils.format(execLog.getCommand(), paramsJson);
|
||||||
|
execHostLog.setCommand(command);
|
||||||
|
execHostLog.setParameter(paramsJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -404,40 +450,4 @@ public abstract class BaseExecCommandHandler implements IExecCommandHandler {
|
|||||||
return Strings.retain(message, 250);
|
return Strings.retain(message, 250);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取命令实际参数
|
|
||||||
*
|
|
||||||
* @return params
|
|
||||||
*/
|
|
||||||
private Map<String, Object> getCommandParams() {
|
|
||||||
String uuid = UUIds.random();
|
|
||||||
Map<String, Object> params = Maps.newMap(builtParams);
|
|
||||||
params.put("hostId", connect.getHostId());
|
|
||||||
params.put("hostName", connect.getHostName());
|
|
||||||
params.put("hostCode", connect.getHostCode());
|
|
||||||
params.put("hostAddress", connect.getHostAddress());
|
|
||||||
params.put("hostPort", connect.getHostPort());
|
|
||||||
params.put("hostUsername", connect.getUsername());
|
|
||||||
params.put("hostUuid", uuid);
|
|
||||||
params.put("hostUuidShort", uuid.replace("-", Strings.EMPTY));
|
|
||||||
params.put("osType", connect.getOsType());
|
|
||||||
params.put("charset", connect.getCharset());
|
|
||||||
params.put("scriptPath", execHostLog.getScriptPath());
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建脚本路径
|
|
||||||
*
|
|
||||||
* @return scriptPath
|
|
||||||
*/
|
|
||||||
private String buildScriptPath() {
|
|
||||||
HostOsTypeEnum os = HostOsTypeEnum.of(connect.getOsType());
|
|
||||||
String name = FileConst.EXEC
|
|
||||||
+ "/" + execHostLog.getLogId()
|
|
||||||
+ "/" + id
|
|
||||||
+ os.getScriptSuffix();
|
|
||||||
return PathUtils.buildAppPath(HostOsTypeEnum.WINDOWS.equals(os), connect.getUsername(), FileConst.SCRIPT, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -193,12 +193,12 @@ public class ExecTaskHandler implements IExecTaskHandler {
|
|||||||
* @return handler
|
* @return handler
|
||||||
*/
|
*/
|
||||||
private IExecCommandHandler createCommandHandler(Long execHostId) {
|
private IExecCommandHandler createCommandHandler(Long execHostId) {
|
||||||
if (Booleans.isTrue(appLogConfig.getExecAppendAnsi())) {
|
if (Booleans.isTrue(appLogConfig.getExecDetailLog())) {
|
||||||
// 详细日志
|
// 详细日志
|
||||||
return new ExecCommandDetailHandler(id, execLog, builtParams, timeoutChecker);
|
return new ExecCommandDetailHandler(execHostId, execLog, builtParams, timeoutChecker);
|
||||||
} else {
|
} else {
|
||||||
// 原始日志
|
// 原始日志
|
||||||
return new ExecCommandOriginHandler(id, execLog, builtParams, timeoutChecker);
|
return new ExecCommandOriginHandler(execHostId, execLog, builtParams, timeoutChecker);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ export const builtinParams: Array<TemplateParam> = [
|
|||||||
}, {
|
}, {
|
||||||
name: 'execId',
|
name: 'execId',
|
||||||
desc: '执行记录id'
|
desc: '执行记录id'
|
||||||
|
}, {
|
||||||
|
name: 'execHostId',
|
||||||
|
desc: '执行记录主机id'
|
||||||
}, {
|
}, {
|
||||||
name: 'hostId',
|
name: 'hostId',
|
||||||
desc: '执行主机id'
|
desc: '执行主机id'
|
||||||
|
|||||||
Reference in New Issue
Block a user