Selaa lähdekoodia

添加 Sftp#deployMgrNode 方法, mgr 节点的部署和 agent 节点不同

reghao 2 kuukautta sitten
vanhempi
commit
3d5f075241

+ 9 - 5
web/src/main/java/cn/reghao/bnt/web/devops/deployer/service/DeployApp.java

@@ -190,11 +190,15 @@ public class DeployApp {
                         textFile.write(new File(mgrConfigPath), mgrConfig);
                     }
 
-                    Result result = sftp.deploy(localDir, remoteDir, remoteHost);
-                    if (result.getCode() == ResultStatus.SUCCESS.getCode()) {
-                        remoteHost.setAppVersion(commitId);
-                        remoteHost.setUpdateTime(LocalDateTime.now());
-                        remoteHostRepository.save(remoteHost);
+                    if (nodeType.equals(NodeType.mgr.name())) {
+                        sftp.deployMgrNode(localDir, remoteDir, remoteHost);
+                    } else {
+                        Result result = sftp.deploy(localDir, remoteDir, remoteHost);
+                        if (result.getCode() == ResultStatus.SUCCESS.getCode()) {
+                            remoteHost.setAppVersion(commitId);
+                            remoteHost.setUpdateTime(LocalDateTime.now());
+                            remoteHostRepository.save(remoteHost);
+                        }
                     }
                 } catch (Exception e) {
                     e.printStackTrace();

+ 27 - 0
web/src/main/java/cn/reghao/bnt/web/devops/deployer/util/Sftp.java

@@ -276,6 +276,33 @@ public class Sftp {
         return result;
     }
 
+    public Result deployMgrNode(String localDir, String remoteDir, RemoteHost remoteHost) {
+        Result result = Result.success();
+        try {
+            Session session = getSession(remoteHost);
+            upload(session, localDir, remoteDir);
+            log.info("files uploaded");
+
+            String command = String.format("cd %s && %s shutdown.sh && %s start.sh", remoteDir, bash, bash);
+            ShellResult shellResult = exec(session, command);
+            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");
+            }
+
+            if (session.isConnected()) {
+                session.disconnect();
+            }
+        } 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);