feature(cli): 增加命令行生成保留上次主机分布
This commit is contained in:
@@ -16,10 +16,19 @@ import org.springframework.stereotype.Component;
|
||||
@ConfigurationProperties("deploy")
|
||||
@Component
|
||||
public class DeployInformationProperties {
|
||||
private Boolean shuffler = false;
|
||||
private RuntimeInfo runtime;
|
||||
private List<HostInfo> hosts;
|
||||
private List<ServiceInfo> 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 +
|
||||
'}';
|
||||
|
||||
@@ -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<String, Object> escape(Map<String, Object> 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<String, List<String>> deployPlans = new HashMap<>();
|
||||
if (Files.exists(planPath) && !deployInformationProperties.getShuffler()) {
|
||||
deployPlans = mapper.readValue(new String(Files.readAllBytes(planPath)), new TypeReference<Map<String, List<String>>>() {
|
||||
});
|
||||
}
|
||||
|
||||
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<String> 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<String> 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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user