diff --git a/bin/generate b/bin/generate index c814ebc..2eabd06 100644 --- a/bin/generate +++ b/bin/generate @@ -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 diff --git a/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/DeployInformationProperties.java b/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/DeployInformationProperties.java index 1cc8a41..6f4eff6 100644 --- a/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/DeployInformationProperties.java +++ b/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/DeployInformationProperties.java @@ -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 hosts; private Map 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 + + '}'; + } + } } 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 db7421a..88a6a07 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 @@ -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 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> groups = new HashMap<>(); @@ -232,7 +198,7 @@ public class RunnerApplication implements ApplicationRunner { String group = entry.getKey(); List 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()); } } diff --git a/service-cli/service-cli-runner/src/main/resources/script/stop.sh b/service-cli/service-cli-runner/src/main/resources/script/stop.sh new file mode 100644 index 0000000..b11ec64 --- /dev/null +++ b/service-cli/service-cli-runner/src/main/resources/script/stop.sh @@ -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 \ No newline at end of file diff --git a/service-cli/service-cli-runner/src/main/resources/template/batch-deploy.ftl b/service-cli/service-cli-runner/src/main/resources/template/cloud/batch-deploy.ftl similarity index 100% rename from service-cli/service-cli-runner/src/main/resources/template/batch-deploy.ftl rename to service-cli/service-cli-runner/src/main/resources/template/cloud/batch-deploy.ftl diff --git a/service-cli/service-cli-runner/src/main/resources/template/batch-stop.ftl b/service-cli/service-cli-runner/src/main/resources/template/cloud/batch-stop.ftl similarity index 100% rename from service-cli/service-cli-runner/src/main/resources/template/batch-stop.ftl rename to service-cli/service-cli-runner/src/main/resources/template/cloud/batch-stop.ftl diff --git a/service-cli/service-cli-runner/src/main/resources/template/deploy.ftl b/service-cli/service-cli-runner/src/main/resources/template/cloud/deploy.ftl similarity index 100% rename from service-cli/service-cli-runner/src/main/resources/template/deploy.ftl rename to service-cli/service-cli-runner/src/main/resources/template/cloud/deploy.ftl diff --git a/service-cli/service-cli-runner/src/main/resources/template/log.ftl b/service-cli/service-cli-runner/src/main/resources/template/cloud/log.ftl similarity index 100% rename from service-cli/service-cli-runner/src/main/resources/template/log.ftl rename to service-cli/service-cli-runner/src/main/resources/template/cloud/log.ftl diff --git a/service-cli/service-cli-runner/src/main/resources/template/stop.ftl b/service-cli/service-cli-runner/src/main/resources/template/cloud/stop.ftl similarity index 100% rename from service-cli/service-cli-runner/src/main/resources/template/stop.ftl rename to service-cli/service-cli-runner/src/main/resources/template/cloud/stop.ftl diff --git a/service-cli/service-cli-runner/src/main/resources/template/command/cli.ftl b/service-cli/service-cli-runner/src/main/resources/template/command/cli.ftl new file mode 100644 index 0000000..bf72b9c --- /dev/null +++ b/service-cli/service-cli-runner/src/main/resources/template/command/cli.ftl @@ -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} -Dspring.profiles.include=default,b12 -jar ${runtime.jarPath}/service-command.jar<#if directly> $@ diff --git a/service-cli/service-cli-runner/src/main/resources/template/command/update.ftl b/service-cli/service-cli-runner/src/main/resources/template/command/update.ftl new file mode 100644 index 0000000..40ab693 --- /dev/null +++ b/service-cli/service-cli-runner/src/main/resources/template/command/update.ftl @@ -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 \ No newline at end of file diff --git a/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/service/launcher/LaunchersService.java b/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/service/launcher/LaunchersService.java index f3a6538..fefe03d 100644 --- a/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/service/launcher/LaunchersService.java +++ b/service-forest/src/main/java/com/lanyuanxiaoyao/service/forest/service/launcher/LaunchersService.java @@ -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 getServices() { + public ImmutableList getServices() { return serviceMap.valuesView().toList().toImmutable(); }