Просмотр исходного кода

Sftp#deployMgrNode 方法中添加在本地部署 mgr 节点的代码

reghao 2 месяцев назад
Родитель
Сommit
19f23d1f6d
1 измененных файлов с 32 добавлено и 12 удалено
  1. 32 12
      web/src/main/java/cn/reghao/bnt/web/devops/deployer/util/Sftp.java

+ 32 - 12
web/src/main/java/cn/reghao/bnt/web/devops/deployer/util/Sftp.java

@@ -3,10 +3,12 @@ package cn.reghao.bnt.web.devops.deployer.util;
 import cn.reghao.bnt.web.devops.deployer.model.po.RemoteHost;
 import cn.reghao.bnt.web.devops.deployer.model.UserInfoImpl;
 import cn.reghao.jutil.jdk.io.TextFile;
+import cn.reghao.jutil.jdk.shell.ShellExecutor;
 import cn.reghao.jutil.jdk.shell.ShellResult;
 import cn.reghao.jutil.jdk.web.result.Result;
 import com.jcraft.jsch.*;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
 
 import java.io.*;
 import java.util.ArrayList;
@@ -21,6 +23,7 @@ import java.util.UUID;
 @Slf4j
 public class Sftp {
     private final TextFile textFile = new TextFile();
+    private ShellExecutor shellExecutor = new ShellExecutor();
     private final String bash = "/usr/bin/bash";
 
     public Session getSession(RemoteHost remoteHost) throws Exception {
@@ -276,8 +279,36 @@ public class Sftp {
         return result;
     }
 
+    public ShellResult exec(RemoteHost remoteHost, String command) throws Exception {
+        Session session = getSession(remoteHost);
+        ShellResult shellResult = exec(session, command);
+        log.info("\nexitCode: {}\nresult:\n{}", shellResult.getExitCode(), shellResult.getResult());
+        if (session.isConnected()) {
+            session.disconnect();
+        }
+
+        return shellResult;
+    }
+
     public Result deployMgrNode(String localDir, String remoteDir, RemoteHost remoteHost) {
         Result result = Result.success();
+        try {
+            FileUtils.copyFileToDirectory(new File(localDir), new File(remoteDir));
+            String command = String.format("cd %s && %s shutdown.sh && %s start.sh", remoteDir, bash, bash);
+            ShellResult shellResult = shellExecutor.exec(command.split("\\s+"));
+            int exitCode = shellResult.getExitCode();
+            if (!shellResult.isSuccess()) {
+                String errMsg = String.format("start application failed\nexitCode: %s\nresult:\n%s", shellResult.getExitCode(), shellResult.getResult());
+                log.error(errMsg);
+                result = Result.fail(errMsg);
+            } else {
+                log.info("start application successfully");
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        /*
         try {
             Session session = getSession(remoteHost);
             upload(session, localDir, remoteDir);
@@ -299,18 +330,7 @@ public class Sftp {
         } catch (Exception e) {
             result = Result.fail(e.getMessage());
         }
-
+        */
         return result;
     }
-
-    public ShellResult exec(RemoteHost remoteHost, String command) throws Exception {
-        Session session = getSession(remoteHost);
-        ShellResult shellResult = exec(session, command);
-        log.info("\nexitCode: {}\nresult:\n{}", shellResult.getExitCode(), shellResult.getResult());
-        if (session.isConnected()) {
-            session.disconnect();
-        }
-
-        return shellResult;
-    }
 }