From fa295b15c61d0f28ed5f9fca1c5f434b75e31873 Mon Sep 17 00:00:00 2001 From: v-zhangjc9 Date: Thu, 24 Apr 2025 15:56:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(launcher):=20=E5=A2=9E=E5=8A=A0=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E6=89=A7=E8=A1=8Cssh=E5=91=BD=E4=BB=A4=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/cli/runner/RunnerApplication.java | 26 ++++++++++++++----- .../src/main/resources/template/cloud/log.ftl | 2 +- .../resources/template/cloud/ssh-script.ftl | 16 ++++++++++++ .../main/resources/template/cloud/stop.ftl | 13 +++------- 4 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 service-cli/service-cli-runner/src/main/resources/template/cloud/ssh-script.ftl diff --git a/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/RunnerApplication.java b/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/RunnerApplication.java index 5ff4b38..fe7a637 100644 --- a/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/RunnerApplication.java +++ b/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/RunnerApplication.java @@ -168,8 +168,8 @@ public class RunnerApplication implements ApplicationRunner { MapUtil.builder() .put("currentPath", absolutRootPath) .put("hosts", hostInfoList - .collect(HostInfoWrapper::getHostnameIp) - .toSortedList((o1, o2) -> Comparator.naturalOrder().compare(o1.getIp(), o2.getIp()))) + .collect(HostInfoWrapper::getHostnameIp) + .toSortedList((o1, o2) -> Comparator.naturalOrder().compare(o1.getIp(), o2.getIp()))) .put("selectedHosts", selectedHosts) .put("runtime", runtimeInfo) .put("info", serviceInfo) @@ -187,8 +187,8 @@ public class RunnerApplication implements ApplicationRunner { MapUtil.builder() .put("currentPath", absolutRootPath) .put("hosts", hostInfoList - .collect(HostInfoWrapper::getIp) - .toSortedList(Comparator.naturalOrder())) + .collect(HostInfoWrapper::getHostnameIp) + .toSortedList((o1, o2) -> Comparator.naturalOrder().compare(o1.getIp(), o2.getIp()))) .put("runtime", runtimeInfo) .put("info", serviceInfo) .put("arguments", serviceInfo.getArguments()) @@ -199,13 +199,13 @@ public class RunnerApplication implements ApplicationRunner { StrUtil.format("stop-{}.sh", serviceInfo.getName()) ) ); - generateTemplate( + generateTemplate( "cloud/log.ftl", MapUtil.builder() .put("currentPath", absolutRootPath) .put("hosts", hostInfoList - .collect(HostInfoWrapper::getIp) - .toSortedList(Comparator.naturalOrder())) + .collect(HostInfoWrapper::getHostnameIp) + .toSortedList((o1, o2) -> Comparator.naturalOrder().compare(o1.getIp(), o2.getIp()))) .put("selectedHosts", selectedHosts) .put("runtime", runtimeInfo) .put("info", serviceInfo) @@ -228,6 +228,18 @@ public class RunnerApplication implements ApplicationRunner { Paths.get(root.toString(), "stop.sh") ); + generateTemplate( + "cloud/ssh-script.ftl", + MapUtil.builder() + .put("currentPath", absolutRootPath) + .put("hosts", hostInfoList + .collect(HostInfoWrapper::getHostnameIp) + .toSortedList((o1, o2) -> Comparator.naturalOrder().compare(o1.getIp(), o2.getIp()))) + .put("runtime", runtimeInfo) + .build(), + Paths.get(root.toString(), "ssh.sh") + ); + MutableMap> groups = Maps.mutable.empty(); for (ServiceInfoWrapper service : serviceInfoList) { service.getGroups().add(0, "all"); diff --git a/service-cli/service-cli-runner/src/main/resources/template/cloud/log.ftl b/service-cli/service-cli-runner/src/main/resources/template/cloud/log.ftl index 8a80471..90e622c 100644 --- a/service-cli/service-cli-runner/src/main/resources/template/cloud/log.ftl +++ b/service-cli/service-cli-runner/src/main/resources/template/cloud/log.ftl @@ -13,5 +13,5 @@ for host in <#noparse>${hosts[@]} do hostname=`ssh $host 'echo $HOSTNAME'` echo "$host $hostname" - ssh $host "cat ${runtime.logPath}/${info.name}.log" > ${currentPath}/logs/${info.name}/$hostname.log + scp $host:${runtime.logPath}/${info.name}.log ${currentPath}/logs/${info.name}/$hostname.log done diff --git a/service-cli/service-cli-runner/src/main/resources/template/cloud/ssh-script.ftl b/service-cli/service-cli-runner/src/main/resources/template/cloud/ssh-script.ftl new file mode 100644 index 0000000..f3ecced --- /dev/null +++ b/service-cli/service-cli-runner/src/main/resources/template/cloud/ssh-script.ftl @@ -0,0 +1,16 @@ +#!/bin/bash + +command=$@ + +hosts=( +<#list hosts as host> + ${host.ip} + +) + +for host in <#noparse>${hosts[@]} +do + hostname=`ssh $host 'echo $HOSTNAME'` + echo "$host $hostname" + ssh $host $command +done diff --git a/service-cli/service-cli-runner/src/main/resources/template/cloud/stop.ftl b/service-cli/service-cli-runner/src/main/resources/template/cloud/stop.ftl index 62b6067..56362fb 100644 --- a/service-cli/service-cli-runner/src/main/resources/template/cloud/stop.ftl +++ b/service-cli/service-cli-runner/src/main/resources/template/cloud/stop.ftl @@ -1,14 +1,7 @@ #!/bin/bash -hosts=( <#list hosts as host> - ${host} +host=${host.ip} +echo "${info.name} ${host.ip} ${host.hostname} $datetime" +ssh $host 'bash -s' < ${currentPath}/stop.sh ${runtime.jarPath}/${info.name}.jar -) - -for host in <#noparse>${hosts[@]} -do - hostname=`ssh $host 'echo $HOSTNAME'` - echo "$host $hostname" - ssh $host 'bash -s' < ${currentPath}/stop.sh ${runtime.jarPath}/${info.name}.jar -done