feature(cli): 增加日志集中和强制停止脚本
This commit is contained in:
@@ -10,6 +10,14 @@ import cn.hutool.extra.template.TemplateEngine;
|
||||
import cn.hutool.extra.template.TemplateUtil;
|
||||
import com.lanyuanxiaoyao.service.cli.core.HostInfo;
|
||||
import com.lanyuanxiaoyao.service.cli.core.ServiceInfo;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -17,14 +25,8 @@ import java.nio.file.Paths;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Resource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* 启动类
|
||||
@@ -118,8 +120,48 @@ public class RunnerApplication implements ApplicationRunner {
|
||||
);
|
||||
Files.deleteIfExists(stopScriptFile);
|
||||
Files.write(stopScriptFile, stopScript.getBytes());
|
||||
|
||||
Template forceStopTemplate = engine.getTemplate("force-stop.ftl");
|
||||
String forceStopScript = forceStopTemplate.render(MapUtil.builder()
|
||||
.put("hosts", deployInformationProperties.getHosts()
|
||||
.stream()
|
||||
.map(HostInfo::getIp)
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.collect(Collectors.toList()))
|
||||
.put("runtime", deployInformationProperties.getRuntime())
|
||||
.put("info", serviceInfo)
|
||||
.put("arguments", serviceInfo.getArguments())
|
||||
.put("environments", serviceInfo.getEnvironments())
|
||||
.build());
|
||||
Path forceStopScriptFile = Paths.get(
|
||||
root.toString(),
|
||||
StrUtil.format("force-stop-{}.sh", serviceInfo.getName())
|
||||
);
|
||||
Files.deleteIfExists(forceStopScriptFile);
|
||||
Files.write(forceStopScriptFile, forceStopScript.getBytes());
|
||||
|
||||
Template logTemplate = engine.getTemplate("log.ftl");
|
||||
String logScript = logTemplate.render(MapUtil.builder()
|
||||
.put("hosts", deployInformationProperties.getHosts()
|
||||
.stream()
|
||||
.map(HostInfo::getIp)
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.collect(Collectors.toList()))
|
||||
.put("selectedHosts", selectedHosts)
|
||||
.put("runtime", deployInformationProperties.getRuntime())
|
||||
.put("info", serviceInfo)
|
||||
.put("arguments", serviceInfo.getArguments())
|
||||
.put("environments", serviceInfo.getEnvironments())
|
||||
.build());
|
||||
Path logScriptFile = Paths.get(
|
||||
root.toString(),
|
||||
StrUtil.format("log-{}.sh", serviceInfo.getName())
|
||||
);
|
||||
Files.deleteIfExists(logScriptFile);
|
||||
Files.write(logScriptFile, logScript.getBytes());
|
||||
}
|
||||
String stopScript = "original_command=\"$0\"\n" +
|
||||
|
||||
Function<Boolean, String> stopScript = isForce -> "original_command=\"$0\"\n" +
|
||||
"application_name=\"$1\"\n" +
|
||||
"function get_pid() {\n" +
|
||||
" PID=$1\n" +
|
||||
@@ -148,7 +190,7 @@ public class RunnerApplication implements ApplicationRunner {
|
||||
" echo \"Application is already stopped.\"\n" +
|
||||
"else\n" +
|
||||
" echo \"Stopping $pid...\"\n" +
|
||||
" kill ${pid}\n" +
|
||||
" kill" + (isForce ? " -9 " : " ") + "${pid}\n" +
|
||||
"fi\n" +
|
||||
"\n" +
|
||||
"LOOPS=0\n" +
|
||||
@@ -161,6 +203,7 @@ public class RunnerApplication implements ApplicationRunner {
|
||||
" let LOOPS=LOOPS+1\n" +
|
||||
" sleep 1\n" +
|
||||
"done\n";
|
||||
Files.write(Paths.get(root.toString(), "stop.sh"), stopScript.getBytes());
|
||||
Files.write(Paths.get(root.toString(), "stop.sh"), stopScript.apply(false).getBytes());
|
||||
Files.write(Paths.get(root.toString(), "force-stop.sh"), stopScript.apply(true).getBytes());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
hosts=(
|
||||
<#list hosts as host>
|
||||
${host}
|
||||
</#list>
|
||||
)
|
||||
|
||||
for host in <#noparse>${hosts[@]}</#noparse>
|
||||
do
|
||||
hostname=`ssh $host 'echo $HOSTNAME'`
|
||||
echo "$host $hostname"
|
||||
ssh $host 'bash -s' < force-stop.sh ${runtime.jarPath}/${info.name}.jar
|
||||
done
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
|
||||
hosts=(
|
||||
<#list selectedHosts as host>
|
||||
${host}
|
||||
</#list>
|
||||
)
|
||||
|
||||
mkdir -p logs/${info.name}
|
||||
rm -rf logs/${info.name}/*
|
||||
|
||||
for host in <#noparse>${hosts[@]}</#noparse>
|
||||
do
|
||||
hostname=`ssh $host 'echo $HOSTNAME'`
|
||||
echo "$host $hostname"
|
||||
ssh $host "cat ${runtime.logPath}/${info.name}.log" > logs/${info.name}/$hostname.log
|
||||
done
|
||||
Reference in New Issue
Block a user