feat(cli): 副本数变化时重新生成部署主机列表
This commit is contained in:
@@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.service.cli.runner;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.template.Template;
|
||||
@@ -65,6 +66,23 @@ public class RunnerApplication implements ApplicationRunner {
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
}
|
||||
|
||||
private List<String> selectHosts(ServiceInfo serviceInfo) {
|
||||
return serviceInfo.getReplicas() == 0
|
||||
? deployInformationProperties.getHosts()
|
||||
.stream()
|
||||
.map(HostInfo::getIp)
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.collect(Collectors.toList())
|
||||
: ListUtil.sort(
|
||||
RandomUtil.randomEleList(
|
||||
deployInformationProperties.getHosts()
|
||||
.stream()
|
||||
.map(HostInfo::getIp)
|
||||
.collect(Collectors.toList()
|
||||
), serviceInfo.getReplicas()
|
||||
), Comparator.naturalOrder());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws IOException {
|
||||
Path planPath = Paths.get("deploy.plan");
|
||||
@@ -85,21 +103,12 @@ public class RunnerApplication implements ApplicationRunner {
|
||||
List<String> selectedHosts;
|
||||
if (deployPlans.containsKey(serviceInfo.getName())) {
|
||||
selectedHosts = deployPlans.get(serviceInfo.getName());
|
||||
if (ObjectUtil.notEqual(selectedHosts.size(), serviceInfo.getReplicas())) {
|
||||
selectedHosts = selectHosts(serviceInfo);
|
||||
deployPlans.put(serviceInfo.getName(), selectedHosts);
|
||||
}
|
||||
} else {
|
||||
selectedHosts = serviceInfo.getReplicas() == 0
|
||||
? deployInformationProperties.getHosts()
|
||||
.stream()
|
||||
.map(HostInfo::getIp)
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.collect(Collectors.toList())
|
||||
: ListUtil.sort(
|
||||
RandomUtil.randomEleList(
|
||||
deployInformationProperties.getHosts()
|
||||
.stream()
|
||||
.map(HostInfo::getIp)
|
||||
.collect(Collectors.toList()
|
||||
), serviceInfo.getReplicas()
|
||||
), Comparator.naturalOrder());
|
||||
selectedHosts = selectHosts(serviceInfo);
|
||||
deployPlans.put(serviceInfo.getName(), selectedHosts);
|
||||
}
|
||||
String deployScript = deployTemplate.render(MapUtil.builder()
|
||||
@@ -181,47 +190,47 @@ public class RunnerApplication implements ApplicationRunner {
|
||||
}
|
||||
|
||||
Function<Boolean, String> stopScript = isForce -> "original_command=\"$0\"\n" +
|
||||
"application_name=\"$1\"\n" +
|
||||
"function get_pid() {\n" +
|
||||
" PID=$1\n" +
|
||||
" if [[ ! -z \"$PID\" ]]; then\n" +
|
||||
" ID=$(ps -ef | grep \"$application_name\" | grep \"$PID\" | grep -v grep | grep -v $original_command | awk '{ print $2 }')\n" +
|
||||
" if [[ -z \"$ID\" ]]; then\n" +
|
||||
" ID=$(ps -C java -f --width 1000 | grep \"$application_name\" | grep \"$PID\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
|
||||
" if [[ -z \"$ID\" ]]; then\n" +
|
||||
" ID=$(ps aux | grep \"$application_name\" | grep \"$PID\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
|
||||
" fi\n" +
|
||||
" fi\n" +
|
||||
" else\n" +
|
||||
" ID=$(ps -ef | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{ print $2 }')\n" +
|
||||
" if [[ -z \"$ID\" ]]; then\n" +
|
||||
" ID=$(ps -C java -f --width 1000 | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
|
||||
" if [[ -z \"$ID\" ]]; then\n" +
|
||||
" ID=$(ps aux | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
|
||||
" fi\n" +
|
||||
" fi\n" +
|
||||
" fi\n" +
|
||||
" echo ${ID}\n" +
|
||||
"}\n" +
|
||||
"\n" +
|
||||
"pid=$(get_pid)\n" +
|
||||
"if [[ -z \"$pid\" ]]; then\n" +
|
||||
" echo \"Application is already stopped.\"\n" +
|
||||
"else\n" +
|
||||
" echo \"Stopping $pid...\"\n" +
|
||||
" kill" + (isForce ? " -9 " : " ") + "${pid}\n" +
|
||||
"fi\n" +
|
||||
"\n" +
|
||||
"LOOPS=0\n" +
|
||||
"while (true); do\n" +
|
||||
" gpid=$(get_pid \"$pid\")\n" +
|
||||
" if [[ \"$gpid\" == \"\" ]]; then\n" +
|
||||
" echo \"Oook! cost:$LOOPS\"\n" +
|
||||
" break\n" +
|
||||
" fi\n" +
|
||||
" let LOOPS=LOOPS+1\n" +
|
||||
" sleep 1\n" +
|
||||
"done\n";
|
||||
"application_name=\"$1\"\n" +
|
||||
"function get_pid() {\n" +
|
||||
" PID=$1\n" +
|
||||
" if [[ ! -z \"$PID\" ]]; then\n" +
|
||||
" ID=$(ps -ef | grep \"$application_name\" | grep \"$PID\" | grep -v grep | grep -v $original_command | awk '{ print $2 }')\n" +
|
||||
" if [[ -z \"$ID\" ]]; then\n" +
|
||||
" ID=$(ps -C java -f --width 1000 | grep \"$application_name\" | grep \"$PID\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
|
||||
" if [[ -z \"$ID\" ]]; then\n" +
|
||||
" ID=$(ps aux | grep \"$application_name\" | grep \"$PID\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
|
||||
" fi\n" +
|
||||
" fi\n" +
|
||||
" else\n" +
|
||||
" ID=$(ps -ef | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{ print $2 }')\n" +
|
||||
" if [[ -z \"$ID\" ]]; then\n" +
|
||||
" ID=$(ps -C java -f --width 1000 | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
|
||||
" if [[ -z \"$ID\" ]]; then\n" +
|
||||
" ID=$(ps aux | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
|
||||
" fi\n" +
|
||||
" fi\n" +
|
||||
" fi\n" +
|
||||
" echo ${ID}\n" +
|
||||
"}\n" +
|
||||
"\n" +
|
||||
"pid=$(get_pid)\n" +
|
||||
"if [[ -z \"$pid\" ]]; then\n" +
|
||||
" echo \"Application is already stopped.\"\n" +
|
||||
"else\n" +
|
||||
" echo \"Stopping $pid...\"\n" +
|
||||
" kill" + (isForce ? " -9 " : " ") + "${pid}\n" +
|
||||
"fi\n" +
|
||||
"\n" +
|
||||
"LOOPS=0\n" +
|
||||
"while (true); do\n" +
|
||||
" gpid=$(get_pid \"$pid\")\n" +
|
||||
" if [[ \"$gpid\" == \"\" ]]; then\n" +
|
||||
" echo \"Oook! cost:$LOOPS\"\n" +
|
||||
" break\n" +
|
||||
" fi\n" +
|
||||
" let LOOPS=LOOPS+1\n" +
|
||||
" sleep 1\n" +
|
||||
"done\n";
|
||||
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());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user