diff --git a/service-cli/service-cli-runner/pom.xml b/service-cli/service-cli-runner/pom.xml index 3481bee..4803a2c 100644 --- a/service-cli/service-cli-runner/pom.xml +++ b/service-cli/service-cli-runner/pom.xml @@ -26,6 +26,10 @@ spring-boot-configuration-processor true + + org.springframework.boot + spring-boot-starter-json + org.freemarker freemarker 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 74a392a..3188f10 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 @@ -16,10 +16,19 @@ import org.springframework.stereotype.Component; @ConfigurationProperties("deploy") @Component public class DeployInformationProperties { + private Boolean shuffler = false; private RuntimeInfo runtime; private List hosts; private List services; + public Boolean getShuffler() { + return shuffler; + } + + public void setShuffler(Boolean shuffler) { + this.shuffler = shuffler; + } + public RuntimeInfo getRuntime() { return runtime; } @@ -47,7 +56,8 @@ public class DeployInformationProperties { @Override public String toString() { return "DeployInformationProperties{" + - "runtime=" + runtime + + "shuffler=" + shuffler + + ", runtime=" + runtime + ", hosts=" + hosts + ", services=" + services + '}'; 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 9906a9a..1182622 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 @@ -8,8 +8,20 @@ import cn.hutool.extra.template.Template; import cn.hutool.extra.template.TemplateConfig; import cn.hutool.extra.template.TemplateEngine; import cn.hutool.extra.template.TemplateUtil; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import com.lanyuanxiaoyao.service.cli.core.HostInfo; import com.lanyuanxiaoyao.service.cli.core.ServiceInfo; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +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; import org.springframework.boot.ApplicationArguments; @@ -17,17 +29,6 @@ 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; -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; - /** * 启动类 * @@ -38,13 +39,18 @@ import java.util.stream.Collectors; public class RunnerApplication implements ApplicationRunner { private static final Logger logger = LoggerFactory.getLogger(RunnerApplication.class); + private final DeployInformationProperties deployInformationProperties; + private final ObjectMapper mapper; + + public RunnerApplication(DeployInformationProperties deployInformationProperties) { + this.deployInformationProperties = deployInformationProperties; + mapper = new ObjectMapper(); + } + public static void main(String[] args) { SpringApplication.run(RunnerApplication.class, args); } - @Resource - private DeployInformationProperties deployInformationProperties; - private Map escape(Map map) { // https://github.com/spring-projects/spring-framework/issues/9628 // spring boot 的配置文件没有办法转义 $,使用 $\{ -> ${ 来绕过一下 @@ -61,6 +67,13 @@ public class RunnerApplication implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws IOException { + Path planPath = Paths.get("deploy.plan"); + Map> deployPlans = new HashMap<>(); + if (Files.exists(planPath) && !deployInformationProperties.getShuffler()) { + deployPlans = mapper.readValue(new String(Files.readAllBytes(planPath)), new TypeReference>>() { + }); + } + Path root = Paths.get(""); Files.createDirectories(root); TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH)); @@ -69,20 +82,26 @@ public class RunnerApplication implements ApplicationRunner { logger.info("Generate script for {}", serviceInfo.getName()); serviceInfo.setEnvironments(escape(serviceInfo.getEnvironments())); serviceInfo.setArguments(escape(serviceInfo.getArguments())); - List 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()); + List selectedHosts; + if (deployPlans.containsKey(serviceInfo.getName())) { + selectedHosts = deployPlans.get(serviceInfo.getName()); + } 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()); + deployPlans.put(serviceInfo.getName(), selectedHosts); + } String deployScript = deployTemplate.render(MapUtil.builder() .put("hosts", deployInformationProperties.getHosts() .stream() @@ -205,5 +224,8 @@ public class RunnerApplication implements ApplicationRunner { "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()); + + Files.deleteIfExists(planPath); + Files.write(planPath, mapper.writeValueAsString(deployPlans).getBytes()); } }