java實現Linux(centos) 中docker容器下命令交互的代碼(配置向導)
開發需求: 因系統程序部署時,經常是拆分部署(多臺機器) ,手工部署費時費力,且每次都要手工配置系統參數(系統提供配置向導)。
如下圖所示:
1)進行main容器 -> 2)執行系統配置向導 -> 3)選擇服務器模式 -> 4) 選擇web控制臺….然後進行具體的服務器IP設置。
為瞭解放雙手,用java實現瞭Linux(centos) 下docker 應用程序的命令交互。
具體代碼如下:
import java.io.*; /** * @author by dujiajun * @date 2021/4/29. */ public class TestMain extends Thread{ //進入docker main private static String [] cmds = {"docker","exec","-i","main","bash"}; private Process pro; //初始化各機器IP信息 private String role = ""; private String webIp = ""; private String redisIp = ""; private String beanstalkdIp = ""; private String pgIp = ""; //main應用重啟 public static String [] cmdRestart = {"/bin/sh","-c","docker restart main"}; public void cmdRun(String[] machines) throws Exception { //執行xx系統配置向導命令並生成進程 //pro = Runtime.getRuntime().exec(cmds); ProcessBuilder pb = new ProcessBuilder(cmds); /* pb.inheritIO(); pb.redirectErrorStream(true);*/ pro = pb.start(); System.out.println("執行前pro1:" + pro.isAlive()); //解析machines信息 if (machines.length > 0) { for (int i = 0; i < machines.length; i++) { int strEndIndex = machines[i].indexOf(":"); String str = machines[i].substring(0, strEndIndex); String content = machines[i].substring(strEndIndex + 1); switch (str.trim()) { case "role": role = content; break; //web服務器IP case "webIp": webIp = content; break; //redis服務器IP case "redisIp": redisIp = content; break; //redis服務器IP case "beanstalkdIp": beanstalkdIp = content; break; //beanstalkd服務器IP case "pgIp": pgIp = content; break; //beanstalkd服務器IP default: break; } } } new Thread() { public void run() { try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(pro.getOutputStream()))) { if (role != null && role.equals("web-server")) {//系統web控制臺 System.out.println("*********進入web控制臺配置向導*********"); //系統配置向導命令集-web String[] strCmdWeb = { "python /opt/tools/skylar_cli/bin/skylar_cli.pyc",//系統配置向導 "server-type", //服務器模式 "web-server", //管理服務器:提供管理功能及應用功能 "allow-deploy-api-server 0", //設置是否允許部署應用服務器 1允許 0不允許 "cache-info " + redisIp + ":6379", //緩存服務器 "db-info " + pgIp + ":5432", //設置數據庫信息 "queue-info " + beanstalkdIp + ":11300", //設置隊列信息 "report-server-db-info " + pgIp + ":5432", //設置報表數據庫 "sfmg-db-info " + pgIp + ":5432", //設置軟件管傢數據庫 "web-server-port " + webIp + ":8080",//設置管理服務器端口 "commit",//提交 "exit" }; //查看進程是否還存活 System.out.println("執行前pro2:" + pro.isAlive()); for (String strWeb : strCmdWeb) { synchronized (bw) { bw.write(strWeb.trim()); bw.newLine(); System.out.println("step: " + strWeb); bw.wait(1000); } } //查看進程是否還存活 System.out.println("pro3:" + pro.isAlive()); bw.flush();//緩存提交 System.out.println("緩存提交!"); bw.close(); System.out.println(" bw.close();"); System.out.println("web配置向導設置成功!"); } else if (role != null && role.equals("api-server")) {//系統app應用 //系統配置向導命令集-APP System.out.println("*********進入APP服務器控制臺配置向導*********"); String[] strCmdApp = { "python /opt/tools/skylar_cli/bin/skylar_cli.pyc",//系統配置向導 "server-type", //服務器模式 "api-server", //APP服務器 "cache-info " + redisIp + ":6379", //緩存服務器 "db-info " + pgIp + ":5432", //設置數據庫信息 "queue-info " + beanstalkdIp + ":11300", //設置隊列信息 "web-server-info " + webIp + ":8080", //設置管理服務器端口 "commit",//提交配置 "exit" }; for (String str : strCmdApp) { synchronized (bw) { bw.write(str.trim()); bw.newLine(); System.out.println("step: " + str); bw.wait(1000); } } //查看進程是否還存活 System.out.println("pro3:" + pro.isAlive()); bw.flush();//緩存提交 System.out.println("緩存提交!"); System.out.println("app配置向導設置成功!"); bw.close(); } else if (role != null && role.equals("log-analyze-server")) {//系統日志分析服務器 //系統配置向導命令集-log-analyze-server System.out.println("*********進入日志分析服務器控制臺配置向導*********"); String[] strCmdLog = { "python /opt/tools/skylar_cli/bin/skylar_cli.pyc",//天擎配置向導 "server-type", //服務器模式 "log-analyze-server", //管理服務器:提供管理功能及應用功能 "cache-info " + redisIp + ":6379", //緩存服務器 "db-info " + pgIp + ":5432", //設置數據庫信息 "queue-info " + beanstalkdIp + ":11300", //設置隊列信息 "web-server-info " + webIp + ":8080", //設置管理服務器端口 "sfmg-db-info " + pgIp + ":5432", //設置軟件管傢數據庫 "commit",//提交配置 "exit" }; //順序執行配置向導命令 for (String str : strCmdLog) { synchronized (bw) { bw.write(str.trim()); bw.newLine(); System.out.println("step: " + str); bw.wait(1000); } } //測試進程是否還存活 System.out.println("pro3:" + pro.isAlive()); bw.flush();//緩存提交 System.out.println("緩存提交!"); System.out.println("日志分析服務器配置向導設置成功!"); bw.close(); } } catch (IOException | InterruptedException e) { //pro.destroyForcibly(); e.printStackTrace(); //pro.destroy(); System.out.println("e.getMessage(): " + e.getMessage()); } finally { try { Process process = Runtime.getRuntime().exec(cmdRestart);//重啟main System.out.println("process.isAlive:" + process.isAlive()); System.out.println("重啟main成功!"); } catch (IOException e) { e.printStackTrace(); } } } }.start(); new Thread(){ public void run(){ BufferedReader br = new BufferedReader(new InputStreamReader(pro.getErrorStream())); String cmdout = ""; try { cmdout = br.readLine(); while(cmdout!= null&&(!cmdout.equals("exit"))){ System.out.println(cmdout); } } catch (IOException e) { //System.out.println("br:pro.destroy();"); //pro.destroy(); e.printStackTrace(); System.out.println("e.printStackTrace();"); } try { System.out.println(" br.close();"); br.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); pro.waitFor();//進程等待 } public static void main(String[] args) throws Exception { TestMain testMain = new TestMain(); /* //測試用 String[] machines ={ "role:web-server", "webIp:xx.xx.xx.110", "redisIp:xx.xx.xx.211", "beanstalkdIp:xx.xx.xx.211", "pgIp:xx.xx.xx.212" };*/ testMain.cmdRun(args); /* System.exit(0); System.out.println("System.exit(0);");*/ } }
特別註意:
private static String [] cmds = {"docker","exec","-i","main","bash"};
一定要使用 docker exec -i main bash ,不要使用 -it ,否則會報錯 cannot enable tty mode on non tty input。
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container -i, --interactive=false Keep STDIN open even if not attached -t, --tty=false Allocate a pseudo-TTY
打成jar包,執行jar包:
終於看到久違的部署成功界面~
到此這篇關於java實現Linux(centos) 中docker容器下命令交互(配置向導)的文章就介紹到這瞭,更多相關docker容器命令交互內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!