java实现Linux(centos) 中docker容器下命令交互的代码(配置向导)
程序员文章站
2022-03-16 13:09:03
开发需求: 因系统程序部署时,经常是拆分部署(多台机器) ,手工部署费时费力,且每次都要手工配置系统参数(系统提供配置向导)。如下图所示:1)进行main容器 -> 2)执行系统配置向导 -&g...
开发需求: 因系统程序部署时,经常是拆分部署(多台机器) ,手工部署费时费力,且每次都要手工配置系统参数(系统提供配置向导)。
如下图所示:
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容器命令交互内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!