mirror of
https://gitee.com/dromara/Jpom.git
synced 2024-11-29 18:38:32 +08:00
fix SSH 脚本触发器支持传入参数当环境变量
This commit is contained in:
parent
b8ffe8599f
commit
24a7b44217
@ -10,6 +10,7 @@
|
||||
4. 【server】优化 前端 ES lint 配置规范前端代码(感谢[@a20070322](https://gitee.com/a20070322) [Gitee Pr 214](https://gitee.com/dromara/Jpom/pulls/214) / [Gitee Pr 215](https://gitee.com/dromara/Jpom/pulls/215) / [Gitee Pr 217](https://gitee.com/dromara/Jpom/pulls/217) )
|
||||
5. 【server】修复 docker 控制台网络选项卡页面空白(感谢@破冰)
|
||||
6. 【server】修复 节点历史监控统计图表时间查询不生效(感谢@九問)
|
||||
7. 【server】优化 SSH 脚本触发器支持传入参数当环境变量(感谢@小朱)
|
||||
|
||||
------
|
||||
|
||||
|
@ -32,7 +32,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -63,7 +65,7 @@ public class SshCommandTriggerApiController extends BaseJpomController {
|
||||
* @return json
|
||||
*/
|
||||
@RequestMapping(value = ServerOpenApi.SSH_COMMAND_TRIGGER_URL, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public IJsonMessage<JSONObject> trigger2(@PathVariable String id, @PathVariable String token) {
|
||||
public IJsonMessage<JSONObject> trigger2(@PathVariable String id, @PathVariable String token, HttpServletRequest request) {
|
||||
CommandModel item = sshCommandService.getByKey(id);
|
||||
Assert.notNull(item, "没有对应数据");
|
||||
Assert.state(StrUtil.equals(token, item.getTriggerToken()), "触发token错误,或者已经失效");
|
||||
@ -72,11 +74,18 @@ public class SshCommandTriggerApiController extends BaseJpomController {
|
||||
UserModel userModel = triggerTokenLogServer.getUserByToken(token, sshCommandService.typeName());
|
||||
//
|
||||
Assert.notNull(userModel, "触发token错误,或者已经失效:-1");
|
||||
|
||||
// 解析参数
|
||||
Map<String, String> paramMap = ServletUtil.getParamMap(request);
|
||||
Map<String, String> newParamMap = new HashMap<>(10);
|
||||
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
||||
String key = StrUtil.format("trigger_{}", entry.getKey());
|
||||
key = StrUtil.toUnderlineCase(key);
|
||||
newParamMap.put(key, entry.getValue());
|
||||
}
|
||||
String batchId;
|
||||
try {
|
||||
BaseServerController.resetInfo(userModel);
|
||||
batchId = sshCommandService.executeBatch(item, item.getDefParams(), item.getSshIds(), 2);
|
||||
batchId = sshCommandService.executeBatch(item, item.getDefParams(), item.getSshIds(), 2, newParamMap);
|
||||
} catch (Exception e) {
|
||||
log.error("触发自动执行SSH命令模版异常", e);
|
||||
return new JsonMessage<>(500, "执行异常:" + e.getMessage());
|
||||
|
@ -173,6 +173,20 @@ public class SshCommandService extends BaseWorkspaceService<CommandModel> implem
|
||||
* @return 批次ID
|
||||
*/
|
||||
public String executeBatch(CommandModel commandModel, String params, String nodes, int triggerExecType) {
|
||||
return executeBatch(commandModel, params, nodes, triggerExecType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量执行命令
|
||||
*
|
||||
* @param commandModel 命令模版
|
||||
* @param nodes ssh节点
|
||||
* @param params 参数
|
||||
* @param envMap 环境变量
|
||||
* @param triggerExecType 触发方式
|
||||
* @return 批次ID
|
||||
*/
|
||||
public String executeBatch(CommandModel commandModel, String params, String nodes, int triggerExecType, Map<String, String> envMap) {
|
||||
Assert.notNull(commandModel, "没有对应对命令");
|
||||
List<String> sshIds = StrUtil.split(nodes, StrUtil.COMMA, true, true);
|
||||
Assert.notEmpty(sshIds, "请选择 ssh 节点");
|
||||
@ -180,7 +194,7 @@ public class SshCommandService extends BaseWorkspaceService<CommandModel> implem
|
||||
String name = "ssh-command-batch:" + batchId;
|
||||
StrictSyncFinisher syncFinisher = SyncFinisherUtil.create(name, sshIds.size());
|
||||
for (String sshId : sshIds) {
|
||||
this.executeItem(syncFinisher, commandModel, params, sshId, batchId, triggerExecType);
|
||||
this.executeItem(syncFinisher, commandModel, params, sshId, batchId, triggerExecType, envMap);
|
||||
}
|
||||
ThreadUtil.execute(() -> {
|
||||
try {
|
||||
@ -203,7 +217,7 @@ public class SshCommandService extends BaseWorkspaceService<CommandModel> implem
|
||||
* @param sshId ssh id
|
||||
* @param batchId 批次ID
|
||||
*/
|
||||
private void executeItem(StrictSyncFinisher syncFinisher, CommandModel commandModel, String commandParams, String sshId, String batchId, int triggerExecType) {
|
||||
private void executeItem(StrictSyncFinisher syncFinisher, CommandModel commandModel, String commandParams, String sshId, String batchId, int triggerExecType, Map<String, String> envMap) {
|
||||
SshModel sshModel = sshService.getByKey(sshId, false);
|
||||
|
||||
CommandExecLogModel commandExecLogModel = new CommandExecLogModel();
|
||||
@ -225,7 +239,7 @@ public class SshCommandService extends BaseWorkspaceService<CommandModel> implem
|
||||
|
||||
syncFinisher.addWorker(() -> {
|
||||
try {
|
||||
this.execute(commandModel, commandExecLogModel, sshModel, commandParamsLine);
|
||||
this.execute(commandModel, commandExecLogModel, sshModel, commandParamsLine, envMap);
|
||||
} catch (Exception e) {
|
||||
log.error("命令模版执行链接异常", e);
|
||||
this.updateStatus(commandExecLogModel.getId(), CommandExecLogModel.Status.SESSION_ERROR);
|
||||
@ -242,7 +256,7 @@ public class SshCommandService extends BaseWorkspaceService<CommandModel> implem
|
||||
* @param sshModel ssh
|
||||
* @param commandParamsLine 参数
|
||||
*/
|
||||
private void execute(CommandModel commandModel, CommandExecLogModel commandExecLogModel, SshModel sshModel, String commandParamsLine) {
|
||||
private void execute(CommandModel commandModel, CommandExecLogModel commandExecLogModel, SshModel sshModel, String commandParamsLine, Map<String, String> envMap) {
|
||||
File file = commandExecLogModel.logFile();
|
||||
try (LogRecorder logRecorder = LogRecorder.builder().file(file).charset(CharsetUtil.CHARSET_UTF_8).build()) {
|
||||
if (sshModel == null) {
|
||||
@ -253,6 +267,7 @@ public class SshCommandService extends BaseWorkspaceService<CommandModel> implem
|
||||
EnvironmentMapBuilder environmentMapBuilder = workspaceEnvVarService.getEnv(commandModel.getWorkspaceId());
|
||||
environmentMapBuilder.put("JPOM_SSH_ID", sshModel.getId());
|
||||
environmentMapBuilder.put("JPOM_COMMAND_ID", commandModel.getId());
|
||||
environmentMapBuilder.putStr(envMap);
|
||||
environmentMapBuilder.eachStr(logRecorder::system);
|
||||
Map<String, String> environment = environmentMapBuilder.environment();
|
||||
String commands = StringUtil.formatStrByMap(commandModel.getCommand(), environment);
|
||||
|
@ -205,6 +205,10 @@
|
||||
重置为重新生成触发地址,重置成功后之前的触发器地址将失效,触发器绑定到生成触发器到操作人上,如果将对应的账号删除触发器将失效
|
||||
</li>
|
||||
<li>批量触发参数 BODY json: [ { "id":"1", "token":"a" } ]</li>
|
||||
<li>
|
||||
单个触发器请求支持将参数解析为环境变量传入脚本执行,比如传入参数名为 abc=efg
|
||||
在脚本中引入则为:${trigger_abc}
|
||||
</li>
|
||||
</ul>
|
||||
</template>
|
||||
</a-alert>
|
||||
|
@ -326,6 +326,10 @@
|
||||
重置为重新生成触发地址,重置成功后之前的触发器地址将失效,触发器绑定到生成触发器到操作人上,如果将对应的账号删除触发器将失效
|
||||
</li>
|
||||
<li>批量触发参数 BODY json: [ { "id":"1", "token":"a" } ]</li>
|
||||
<li>
|
||||
单个触发器请求支持将参数解析为环境变量传入脚本执行,比如传入参数名为 abc=efg
|
||||
在脚本中引入则为:${trigger_abc}
|
||||
</li>
|
||||
</ul>
|
||||
</template>
|
||||
</a-alert>
|
||||
|
@ -366,6 +366,10 @@
|
||||
重置为重新生成触发地址,重置成功后之前的触发器地址将失效,触发器绑定到生成触发器到操作人上,如果将对应的账号删除触发器将失效
|
||||
</li>
|
||||
<li>批量触发参数 BODY json: [ { "id":"1", "token":"a" } ]</li>
|
||||
<li>
|
||||
单个触发器请求支持将参数解析为环境变量传入脚本执行,比如传入参数名为 abc=efg
|
||||
在脚本中引入则为:${trigger_abc}
|
||||
</li>
|
||||
</ul>
|
||||
</template>
|
||||
</a-alert>
|
||||
|
Loading…
Reference in New Issue
Block a user