فهرست منبع

add RemoteHostTest

reghao 2 ماه پیش
والد
کامیت
682d9e8652
1فایلهای تغییر یافته به همراه81 افزوده شده و 0 حذف شده
  1. 81 0
      web/src/test/java/devops/RemoteHostTest.java

+ 81 - 0
web/src/test/java/devops/RemoteHostTest.java

@@ -0,0 +1,81 @@
+package devops;
+
+import cn.reghao.bnt.web.WebApplication;
+import cn.reghao.bnt.web.devops.deployer.db.RemoteAgentConfigRepository;
+import cn.reghao.bnt.web.devops.deployer.db.RemoteHostRepository;
+import cn.reghao.bnt.web.devops.deployer.model.constant.NodeType;
+import cn.reghao.bnt.web.devops.deployer.model.constant.SshAuthType;
+import cn.reghao.bnt.web.devops.deployer.model.po.RemoteAgentConfig;
+import cn.reghao.bnt.web.devops.deployer.model.po.RemoteHost;
+import cn.reghao.jutil.jdk.io.TextFile;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2025-12-23 17:05:21
+ */
+@Slf4j
+@ActiveProfiles("dev")
+@SpringBootTest(classes = WebApplication.class)
+public class RemoteHostTest {
+    TextFile textFile = new TextFile();
+    List<RemoteHost> getRemoteHost(String filePath) {
+        List<String> list = textFile.read(filePath);
+        List<RemoteHost> remoteHosts = new ArrayList<>();
+        for (int i = 1; i < list.size(); i++) {
+            // 从 csv 文件的第二行开始读, 第一行为注释
+            String line = list.get(i);
+            String[] arr = line.split(",");
+            RemoteHost remoteHost;
+            if (arr.length == 4) {
+                String host = arr[0];
+                int port = Integer.parseInt(arr[1]);
+                String username = arr[2];
+                String password = arr[3];
+                remoteHost = new RemoteHost(host, port, SshAuthType.password.name(), username, password);
+            } else if (arr.length == 5) {
+                String host = arr[0];
+                int port = Integer.parseInt(arr[1]);
+                String username = arr[2];
+                String prikeyPath = arr[4];
+                String privateKey = textFile.readFile(prikeyPath);
+                remoteHost = new RemoteHost(host, port, username, privateKey);
+            } else {
+                continue;
+            }
+            remoteHosts.add(remoteHost);
+        }
+        return remoteHosts;
+    }
+
+    @Autowired
+    RemoteAgentConfigRepository remoteAgentConfigRepository;
+    @Autowired
+    RemoteHostRepository remoteHostRepository;
+    @Test
+    public void remoteHostTest() {
+        int id = 2;
+        RemoteAgentConfig remoteAgentConfig = remoteAgentConfigRepository.findById(id).orElse(null);
+        if (remoteAgentConfig == null) {
+            return;
+        }
+
+        String serverFile = "/home/reghao/Downloads/servers_prod.csv";
+        List<RemoteHost> remoteHostList = getRemoteHost(serverFile);
+        remoteHostList.forEach(remoteHost -> {
+            remoteHost.setRemoteAgentConfig(remoteAgentConfig);
+            remoteHost.setNodeType(NodeType.agent.name());
+            remoteHost.setAppDir("/opt/app/devops-agent");
+        });
+
+        remoteHostRepository.saveAll(remoteHostList);
+        System.out.println();
+    }
+}