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 9d72147..b54e59d 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 @@ -21,7 +21,6 @@ import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.Function; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -150,25 +149,6 @@ 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() @@ -190,50 +170,47 @@ public class RunnerApplication implements ApplicationRunner { Files.write(logScriptFile, logScript.getBytes()); } - Function 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"; - 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()); + // 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 ${pid}\n" + + " fi\n" + + " sleep 1s\n" + + "done"; + Files.write(Paths.get(root.toString(), "stop.sh"), stopScript.getBytes()); Files.deleteIfExists(planPath); Files.write(planPath, mapper.writeValueAsString(deployPlans).getBytes()); diff --git a/service-cli/service-cli-runner/src/main/resources/template/force-stop.ftl b/service-cli/service-cli-runner/src/main/resources/template/force-stop.ftl deleted file mode 100644 index 80b8755..0000000 --- a/service-cli/service-cli-runner/src/main/resources/template/force-stop.ftl +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -hosts=( -<#list hosts as host> - ${host} - -) - -for host in <#noparse>${hosts[@]} -do - hostname=`ssh $host 'echo $HOSTNAME'` - echo "$host $hostname" - ssh $host 'bash -s' < force-stop.sh ${runtime.jarPath}/${info.name}.jar -done