Commit c876eab6 authored by 赵啸非's avatar 赵啸非

添加批量激活设备

parent efadbe43
package com.mortals.xhx.common.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
@Slf4j
public class SynchronousLocalShellCommand {
/** 命令信息 */
private final List<String> command;
public SynchronousLocalShellCommand(List<String> command) {
this.command = command;
}
/**
* 执行命令并返回结果
*
* @return 命令执行结果
*/
public String doCommand() {
ProcessBuilder processBuilder = new ProcessBuilder(command);
try {
// 将错误输出流转移到标准输出流中,但使用Runtime不可以
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
String dataMsg = reader(process.getInputStream());
int rsp = process.waitFor();
log.info("run command {}, response {}", command, rsp);
return dataMsg;
} catch (IOException | InterruptedException e) {
log.error("command : {} ,exception", command, e);
}
return null;
}
/**
* 数据读取操作
*
* @param input 输入流
*/
private String reader(InputStream input) {
StringBuilder outDat = new StringBuilder();
try (InputStreamReader inputReader = new InputStreamReader(input, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputReader)) {
String line;
while ((line = bufferedReader.readLine()) != null) {
outDat.append(line);
// outDat.append(Symbol.LINE);
}
} catch (IOException e) {
log.error("command : {} ,exception", command, e);
}
return outDat.toString();
}
}
\ No newline at end of file
......@@ -8,6 +8,7 @@ import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.code.AppTypeEnum;
import com.mortals.xhx.common.utils.SynchronousLocalShellCommand;
import com.mortals.xhx.module.app.dao.AppPublishDao;
import com.mortals.xhx.module.app.model.AppPublishEntity;
import com.mortals.xhx.module.app.service.AppPublishService;
......@@ -18,6 +19,8 @@ import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
......@@ -126,10 +129,12 @@ public class AppPublishServiceImpl extends AbstractCRUDServiceImpl<AppPublishDao
String scriptPath = "/home/temp/" + appPublishEntity.getAppCode() + "/deploy.sh";
this.runSyncCommand(scriptPath);
// ProcessBuilder sh = new ProcessBuilder("sh", scriptPath);
ProcessBuilder sh = new ProcessBuilder("sh","deploy.sh");
/* ProcessBuilder sh = new ProcessBuilder("sh","deploy.sh");
File file = new File("/home/temp/" + appPublishEntity.getAppCode() + "/deploy.sh");
asynExeLocalComand(file, sh);
asynExeLocalComand(file, sh);*/
//执行sh发布脚本
// RuntimeUtil.exec("/bin/sh /home/temp/"+appPublishEntity.getAppCode()+"/deploy.sh");
}
......@@ -156,4 +161,19 @@ public class AppPublishServiceImpl extends AbstractCRUDServiceImpl<AppPublishDao
// 执行命令进程
pb.start();
}
/**
* 执行同步的命令操作
*
* @param commandStr
*/
private void runSyncCommand(String commandStr) {
List<String> commandList = Arrays.asList("bash", "-c", commandStr);
SynchronousLocalShellCommand command = new SynchronousLocalShellCommand(commandList);
String commandRsp = command.doCommand();
System.out.println("同步执行结果:" + commandRsp);
System.out.println("结束---------------");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment