refactor(cli): 优化脚本 增加command脚本的生成

This commit is contained in:
2024-02-27 10:44:20 +08:00
parent 5ecc3daac0
commit 73f0c4c82c
12 changed files with 146 additions and 52 deletions

View File

@@ -1,9 +1,12 @@
#!/bin/bash
jars_path=/data/datalake/jars
jdk_path=/opt/jdk8u252-b09/bin/java
jdk_path=/opt/jdk1.8.0_162/bin/java
arguments=$@
curl ftp://yyy:QeY\!68\)4nH1@132.121.122.15:2222/service-cli-runner-1.0.0-SNAPSHOT.jar -o ${jars_path}/service-cli-runner.jar
${jdk_path} -jar ${jars_path}/service-cli-runner.jar --spring.profiles.active=b12 $arguments
${jdk_path} -jar ${jars_path}/service-cli-runner.jar --spring.profiles.active=b12 --deploy.generate.command=true $arguments
chmod +x cloud/*.sh
chmod +x command/cli*
chmod +x command/*.sh

View File

@@ -17,11 +17,20 @@ import org.springframework.stereotype.Component;
@Configuration
@ConfigurationProperties("deploy")
public class DeployInformationProperties {
private Generate generate;
private Boolean shuffler = false;
private RuntimeInfo runtime;
private Map<String, HostInfo> hosts;
private Map<String, ServiceInfo> services;
public Generate getGenerate() {
return generate;
}
public void setGenerate(Generate generate) {
this.generate = generate;
}
public Boolean getShuffler() {
return shuffler;
}
@@ -57,10 +66,40 @@ public class DeployInformationProperties {
@Override
public String toString() {
return "DeployInformationProperties{" +
"shuffler=" + shuffler +
"generate=" + generate +
", shuffler=" + shuffler +
", runtime=" + runtime +
", hosts=" + hosts +
", services=" + services +
'}';
}
public static final class Generate {
private Boolean cloud = true;
private Boolean command = true;
public Boolean getCloud() {
return cloud;
}
public void setCloud(Boolean cloud) {
this.cloud = cloud;
}
public Boolean getCommand() {
return command;
}
public void setCommand(Boolean command) {
this.command = command;
}
@Override
public String toString() {
return "Generate{" +
"cloud=" + cloud +
", command=" + command +
'}';
}
}
}

View File

@@ -1,6 +1,7 @@
package com.lanyuanxiaoyao.service.cli.runner;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
@@ -26,6 +27,7 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;
/**
* 启动类
@@ -83,8 +85,10 @@ public class RunnerApplication implements ApplicationRunner {
public void run(ApplicationArguments args) throws IOException {
logger.info("Loading runtime info: {}", mapper.writeValueAsString(runtimeInfo));
generateCloud(Paths.get("cloud"));
generateCloud(Paths.get("command"));
if (deployInformationProperties.getGenerate().getCloud())
generateCloud(Paths.get("cloud"));
if (deployInformationProperties.getGenerate().getCommand())
generateCommand(Paths.get("command"));
}
private void generateCloud(Path root) throws IOException {
@@ -99,7 +103,7 @@ public class RunnerApplication implements ApplicationRunner {
}
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
Template deployTemplate = engine.getTemplate("deploy.ftl");
Template deployTemplate = engine.getTemplate("cloud/deploy.ftl");
for (ServiceInfoWrapper serviceInfo : serviceInfoList) {
logger.info("Generate script for {}", serviceInfo.getName());
List<String> selectedHosts;
@@ -133,7 +137,7 @@ public class RunnerApplication implements ApplicationRunner {
Files.deleteIfExists(deployScriptFile);
Files.write(deployScriptFile, deployScript.getBytes());
Template stopTemplate = engine.getTemplate("stop.ftl");
Template stopTemplate = engine.getTemplate("cloud/stop.ftl");
String stopScript = stopTemplate.render(MapUtil.builder()
.put("currentPath", absolutRootPath)
.put("hosts", hostInfoList
@@ -153,7 +157,7 @@ public class RunnerApplication implements ApplicationRunner {
Files.deleteIfExists(stopScriptFile);
Files.write(stopScriptFile, stopScript.getBytes());
Template logTemplate = engine.getTemplate("log.ftl");
Template logTemplate = engine.getTemplate("cloud/log.ftl");
String logScript = logTemplate.render(MapUtil.builder()
.put("currentPath", absolutRootPath)
.put("hosts", hostInfoList
@@ -175,46 +179,8 @@ public class RunnerApplication implements ApplicationRunner {
Files.write(logScriptFile, logScript.getBytes());
}
// language=JavaScript
String stopScript =
"original_command=\"$0\"\n" +
"application_name=\"$1\"\n" +
"\n" +
"function get_pid() {\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" +
" echo ${ID}\n" +
"}\n" +
"\n" +
"pid=$(get_pid)\n" +
"\n" +
"if [[ -z \"$pid\" ]]; then\n" +
" echo \"Application is already stopped.\"\n" +
" exit 0\n" +
"fi\n" +
"\n" +
"echo \"Stopping $pid...\"\n" +
"kill ${pid}\n" +
"\n" +
"LOOPS=0\n" +
"while (true); do\n" +
" cpid=$(get_pid)\n" +
" if [[ -z \"$cpid\" ]]; then\n" +
" echo \"Oook! cost:$LOOPS\"\n" +
" break\n" +
" fi\n" +
" LOOPS=$(($LOOPS+1))\n" +
" if [[ $(($LOOPS%15)) -eq 0 ]]; then\n" +
" echo \"Wait timeout, try to force kill ${pid}...\"\n" +
" kill -9 ${pid}\n" +
" fi\n" +
" sleep 1s\n" +
"done";
ClassPathResource stopFile = new ClassPathResource("script/stop.sh");
String stopScript = new String(IoUtil.readBytes(stopFile.getInputStream()));
Files.write(Paths.get(root.toString(), "stop.sh"), stopScript.getBytes());
Map<String, List<ServiceInfoWrapper>> groups = new HashMap<>();
@@ -232,7 +198,7 @@ public class RunnerApplication implements ApplicationRunner {
String group = entry.getKey();
List<ServiceInfoWrapper> infos = entry.getValue();
Template batchDeployTemplate = engine.getTemplate("batch-deploy.ftl");
Template batchDeployTemplate = engine.getTemplate("cloud/batch-deploy.ftl");
String batchDeployScript = batchDeployTemplate.render(MapUtil.builder()
.put("currentPath", absolutRootPath)
.put("services", infos.stream().map(ServiceInfoWrapper::getName).collect(Collectors.toList()))
@@ -244,7 +210,7 @@ public class RunnerApplication implements ApplicationRunner {
Files.deleteIfExists(batchDeployScriptFile);
Files.write(batchDeployScriptFile, batchDeployScript.getBytes());
Template batchStopTemplate = engine.getTemplate("batch-stop.ftl");
Template batchStopTemplate = engine.getTemplate("cloud/batch-stop.ftl");
String batchStopScript = batchStopTemplate.render(MapUtil.builder()
.put("currentPath", absolutRootPath)
.put("services", infos.stream().map(ServiceInfoWrapper::getName).collect(Collectors.toList()))
@@ -262,5 +228,38 @@ public class RunnerApplication implements ApplicationRunner {
}
private void generateCommand(Path root) throws IOException {
Files.createDirectories(root);
String absolutRootPath = root.toAbsolutePath().toString();
logger.info("Current path: {}", absolutRootPath);
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
Template commandTemplate = engine.getTemplate("command/cli.ftl");
String commandScript = commandTemplate.render(MapUtil.builder()
.put("currentPath", absolutRootPath)
.put("runtime", runtimeInfo)
.put("directly", false)
.build());
Path commandScriptFile = Paths.get(root.toString(), "cli");
Files.deleteIfExists(commandScriptFile);
Files.write(commandScriptFile, commandScript.getBytes());
Template commandDirectlyTemplate = engine.getTemplate("command/cli.ftl");
String commandDirectlyScript = commandDirectlyTemplate.render(MapUtil.builder()
.put("currentPath", absolutRootPath)
.put("runtime", runtimeInfo)
.put("directly", true)
.build());
Path commandDirectlyScriptFile = Paths.get(root.toString(), "cli_d");
Files.deleteIfExists(commandDirectlyScriptFile);
Files.write(commandDirectlyScriptFile, commandDirectlyScript.getBytes());
Template updateTemplate = engine.getTemplate("command/update.ftl");
String updateScript = updateTemplate.render(MapUtil.builder()
.put("currentPath", absolutRootPath)
.put("runtime", runtimeInfo)
.build());
Path updateScriptFile = Paths.get(root.toString(), "update.sh");
Files.deleteIfExists(updateScriptFile);
Files.write(updateScriptFile, updateScript.getBytes());
}
}

View File

@@ -0,0 +1,40 @@
#!/bin/bash
original_command="$0"
application_name="$1"
function get_pid() {
ID=$(ps -ef | grep "$application_name" | grep -v grep | grep -v $original_command | awk '{ print $2 }')
if [[ -z "$ID" ]]; then
ID=$(ps -C java -f --width 1000 | grep "$application_name" | grep -v grep | grep -v $original_command | awk '{print $2}')
if [[ -z "$ID" ]]; then
ID=$(ps aux | grep "$application_name" | grep -v grep | grep -v $original_command | awk '{print $2}')
fi
fi
echo ${ID}
}
pid=$(get_pid)
if [[ -z "$pid" ]]; then
echo "Application is already stopped."
exit 0
fi
echo "Stopping $pid..."
kill ${pid}
LOOPS=0
while (true); do
cpid=$(get_pid)
if [[ -z "$cpid" ]]; then
echo "Oook! cost:$LOOPS"
break
fi
LOOPS=$(($LOOPS+1))
if [[ $(($LOOPS%15)) -eq 0 ]]; then
echo "Wait timeout, try to force kill ${pid}..."
kill -9 ${pid}
fi
sleep 1s
done

View File

@@ -0,0 +1,10 @@
#!/bin/bash
datetime=`date +%Y%m%d%H%M%S`
log_path='${runtime.logPath}'
loki_url='${runtime.loki.servicePushUrl}'
mkdir -p ${runtime.jarPath}
export JASYPT_ENCRYPTOR_PASSWORD='r#(R,P\"Dp^A47>WSn:Wn].gs/+\"v:q_Q*An~zF*g-@j@jtSTv5H/,S-3:R?r9R}.'
${runtime.jdkPath} <#noparse>-Ddatetime=${datetime} -Dhostname=${HOSTNAME} -Dlogging.parent=${log_path} -Dloki.url=${loki_url}</#noparse> -Dspring.profiles.include=default,b12 -jar ${runtime.jarPath}/service-command.jar<#if directly> $@</#if>

View File

@@ -0,0 +1,3 @@
#!/bin/bash
curl ftp://yyy:QeY\!68\)4nH1@132.121.122.15:2222/command-1.0.0-SNAPSHOT.jar -o ${runtime.jarPath}/service-command.jar

View File

@@ -35,14 +35,14 @@ public class LaunchersService {
serviceMap = Maps.immutable.ofAll(map);
}
private LauncherService getService(String cluster) {
public LauncherService getService(String cluster) {
if (serviceMap.containsKey(cluster)) {
return serviceMap.get(cluster);
}
throw new RuntimeException(StrUtil.format("Cluster {} not found", cluster));
}
private ImmutableList<LauncherService> getServices() {
public ImmutableList<LauncherService> getServices() {
return serviceMap.valuesView().toList().toImmutable();
}