iis服务器助手广告广告
返回顶部
首页 > 资讯 > 操作系统 >java实现Linux(centos) 中docker容器下命令交互的代码(配置向导)
  • 912
分享到

java实现Linux(centos) 中docker容器下命令交互的代码(配置向导)

2024-04-02 19:04:59 912人浏览 八月长安
摘要

开发需求: 因系统程序部署时,经常是拆分部署(多台机器) ,手工部署费时费力,且每次都要手工配置系统参数(系统提供配置向导)。 如下图所示: 1)进行main容器 -> 2)执

开发需求: 因系统程序部署时,经常是拆分部署(多台机器) ,手工部署费时费力,且每次都要手工配置系统参数(系统提供配置向导)。

如下图所示:

1)进行main容器 -> 2)执行系统配置向导 -> 3)选择服务器模式 -> 4) 选择WEB控制台....然后进行具体的服务器IP设置。

为了解放双手,用java实现了linux(Centos) 下Docker 应用程序的命令交互。

具体代码如下:


import java.io.*;
 

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);
       
        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();
 
        
 
        testMain.cmdRun(args);
       
    }
}

特别注意:


 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容器命令交互内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: java实现Linux(centos) 中docker容器下命令交互的代码(配置向导)

本文链接: https://www.lsjlt.com/news/125856.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作