feature(cli): 增加命令行生成保留上次主机分布
This commit is contained in:
@@ -26,6 +26,10 @@
|
|||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-json</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.freemarker</groupId>
|
<groupId>org.freemarker</groupId>
|
||||||
<artifactId>freemarker</artifactId>
|
<artifactId>freemarker</artifactId>
|
||||||
|
|||||||
@@ -16,10 +16,19 @@ import org.springframework.stereotype.Component;
|
|||||||
@ConfigurationProperties("deploy")
|
@ConfigurationProperties("deploy")
|
||||||
@Component
|
@Component
|
||||||
public class DeployInformationProperties {
|
public class DeployInformationProperties {
|
||||||
|
private Boolean shuffler = false;
|
||||||
private RuntimeInfo runtime;
|
private RuntimeInfo runtime;
|
||||||
private List<HostInfo> hosts;
|
private List<HostInfo> hosts;
|
||||||
private List<ServiceInfo> services;
|
private List<ServiceInfo> services;
|
||||||
|
|
||||||
|
public Boolean getShuffler() {
|
||||||
|
return shuffler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setShuffler(Boolean shuffler) {
|
||||||
|
this.shuffler = shuffler;
|
||||||
|
}
|
||||||
|
|
||||||
public RuntimeInfo getRuntime() {
|
public RuntimeInfo getRuntime() {
|
||||||
return runtime;
|
return runtime;
|
||||||
}
|
}
|
||||||
@@ -47,7 +56,8 @@ public class DeployInformationProperties {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "DeployInformationProperties{" +
|
return "DeployInformationProperties{" +
|
||||||
"runtime=" + runtime +
|
"shuffler=" + shuffler +
|
||||||
|
", runtime=" + runtime +
|
||||||
", hosts=" + hosts +
|
", hosts=" + hosts +
|
||||||
", services=" + services +
|
", services=" + services +
|
||||||
'}';
|
'}';
|
||||||
|
|||||||
@@ -8,8 +8,20 @@ import cn.hutool.extra.template.Template;
|
|||||||
import cn.hutool.extra.template.TemplateConfig;
|
import cn.hutool.extra.template.TemplateConfig;
|
||||||
import cn.hutool.extra.template.TemplateEngine;
|
import cn.hutool.extra.template.TemplateEngine;
|
||||||
import cn.hutool.extra.template.TemplateUtil;
|
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.HostInfo;
|
||||||
import com.lanyuanxiaoyao.service.cli.core.ServiceInfo;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationArguments;
|
||||||
@@ -17,17 +29,6 @@ import org.springframework.boot.ApplicationRunner;
|
|||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
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 {
|
public class RunnerApplication implements ApplicationRunner {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(RunnerApplication.class);
|
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) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(RunnerApplication.class, args);
|
SpringApplication.run(RunnerApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Resource
|
|
||||||
private DeployInformationProperties deployInformationProperties;
|
|
||||||
|
|
||||||
private Map<String, Object> escape(Map<String, Object> map) {
|
private Map<String, Object> escape(Map<String, Object> map) {
|
||||||
// https://github.com/spring-projects/spring-framework/issues/9628
|
// https://github.com/spring-projects/spring-framework/issues/9628
|
||||||
// spring boot 的配置文件没有办法转义 $,使用 $\{ -> ${ 来绕过一下
|
// spring boot 的配置文件没有办法转义 $,使用 $\{ -> ${ 来绕过一下
|
||||||
@@ -61,6 +67,13 @@ public class RunnerApplication implements ApplicationRunner {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) throws IOException {
|
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("");
|
Path root = Paths.get("");
|
||||||
Files.createDirectories(root);
|
Files.createDirectories(root);
|
||||||
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
|
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
|
||||||
@@ -69,7 +82,11 @@ public class RunnerApplication implements ApplicationRunner {
|
|||||||
logger.info("Generate script for {}", serviceInfo.getName());
|
logger.info("Generate script for {}", serviceInfo.getName());
|
||||||
serviceInfo.setEnvironments(escape(serviceInfo.getEnvironments()));
|
serviceInfo.setEnvironments(escape(serviceInfo.getEnvironments()));
|
||||||
serviceInfo.setArguments(escape(serviceInfo.getArguments()));
|
serviceInfo.setArguments(escape(serviceInfo.getArguments()));
|
||||||
List<String> selectedHosts = serviceInfo.getReplicas() == 0
|
List<String> selectedHosts;
|
||||||
|
if (deployPlans.containsKey(serviceInfo.getName())) {
|
||||||
|
selectedHosts = deployPlans.get(serviceInfo.getName());
|
||||||
|
} else {
|
||||||
|
selectedHosts = serviceInfo.getReplicas() == 0
|
||||||
? deployInformationProperties.getHosts()
|
? deployInformationProperties.getHosts()
|
||||||
.stream()
|
.stream()
|
||||||
.map(HostInfo::getIp)
|
.map(HostInfo::getIp)
|
||||||
@@ -83,6 +100,8 @@ public class RunnerApplication implements ApplicationRunner {
|
|||||||
.collect(Collectors.toList()
|
.collect(Collectors.toList()
|
||||||
), serviceInfo.getReplicas()
|
), serviceInfo.getReplicas()
|
||||||
), Comparator.naturalOrder());
|
), Comparator.naturalOrder());
|
||||||
|
deployPlans.put(serviceInfo.getName(), selectedHosts);
|
||||||
|
}
|
||||||
String deployScript = deployTemplate.render(MapUtil.builder()
|
String deployScript = deployTemplate.render(MapUtil.builder()
|
||||||
.put("hosts", deployInformationProperties.getHosts()
|
.put("hosts", deployInformationProperties.getHosts()
|
||||||
.stream()
|
.stream()
|
||||||
@@ -205,5 +224,8 @@ public class RunnerApplication implements ApplicationRunner {
|
|||||||
"done\n";
|
"done\n";
|
||||||
Files.write(Paths.get(root.toString(), "stop.sh"), stopScript.apply(false).getBytes());
|
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.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