1.项目必须是在docker中运行的
2.传输命令 如ps -ef可以获取pid
public static String deployPid(String cmd) {
log.info("cmd---:{}", cmd);
Process p = null;
try {
p = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", cmd});
int pCode = p.waitFor();
// 0 ,无错误,等于1说明有错误。
if (pCode == 1) {
throw new Exception(getErrorMessage(p));
}
StringBuilder sb = new StringBuilder();
String line = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
log.info("result---{}", sb.toString());
return sb.toString();
} catch (Exception e) {
log.error("docker--error:{}", e.getMessage());
e.printStackTrace();
throw new BusinessException(
DeployExcpConstants.ERROR_CODE_CLUSTER_NO_SELECT,
DeployExcpConstants.ERROR_MSG_MANAGE_CLUSTER_NO_SELECT);
} finally {
try {
if (p != null) {
p.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.通过命令执行stargo命令
/**
* 得到控制台输出的错误信息
*
* @param process
* @return
*/
private static String getErrorMessage(Process process) {
String errMeaage = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
errMeaage = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return errMeaage;
}
public static String deployment(String cmd) {
String result = "";
log.info("cmd---:{}", cmd);
Process p = null;
try {
p = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
result = result + reader.readLine();
log.info("docker--result---{}", result);
return result;
} catch (Exception e) {
log.error("docker--error:{}", e.getMessage());
e.printStackTrace();
throw new BusinessException(
DeployExcpConstants.ERROR_CODE_CLUSTER_NO_SELECT,
DeployExcpConstants.ERROR_MSG_MANAGE_CLUSTER_NO_SELECT);
} finally {
try {
if (p != null) {
p.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}