- Config 파일 변경 command 추가

This commit is contained in:
hyunjujeong 2023-09-04 10:27:07 +09:00
parent f30f914283
commit 5401b6687f
15 changed files with 264 additions and 89 deletions

View File

@ -0,0 +1,10 @@
package inc.sdt.blokworks.devicedeployer.application;
import java.util.LinkedHashMap;
public class BashCommand implements CommandInfo{
@Override
public LinkedHashMap<String, Object> put() {
return null;
}
}

View File

@ -0,0 +1,7 @@
package inc.sdt.blokworks.devicedeployer.application;
import java.util.LinkedHashMap;
public interface CommandInfo {
LinkedHashMap<String, Object> put();
}

View File

@ -15,7 +15,6 @@ import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Optional;
@ -42,46 +41,47 @@ public class DefaultDeployerService implements DeployerService{
}
@Override
public void publish(OutboundMessage deployMessage, String assetCode) {
log.info("[publish] deployMessage = {}, assetCode = {}", deployMessage, assetCode);
final String url = filePath + deployMessage.getFileId();
final String deviceType = "ecn";
HashMap<String, Object> cmdInfo = new HashMap<>();
String cmdType = "";
public void publish(OutboundMessage outboundMessage, String assetCode) {
log.info("[publish] deployMessage = {}, assetCode = {}", outboundMessage, assetCode);
final String url = filePath + outboundMessage.getFileId();
final DeviceType deviceType = outboundMessage.getDeviceType();
LinkedHashMap<String, Object> commandInfo = new LinkedHashMap<>();
try {
/*switch (deployMessage.getCommandType()) {
case bash:
break;
case systemd:
break;
case docker:
break;
case deploy:
break;
}*/
if(!deployMessage.getCommand().isEmpty()) {
cmdInfo.put("cmd", deployMessage.getCommand());
cmdInfo.put("fileUrl", url);
cmdInfo.put("fileType", "python");
cmdType = String.valueOf(CommandType.deploy);
}
if(!deployMessage.getEnv().isEmpty()) {
cmdInfo.put("cmd", "run");
cmdInfo.put("image", url);
cmdInfo.put("name", deployMessage.getName());
cmdInfo.put("options", deployMessage.getEnv());
cmdType = String.valueOf(CommandType.docker);
switch (outboundMessage.getCommandType()) {
case bash -> commandInfo.put("cmd", outboundMessage.getCommand());
case systemd -> {
commandInfo.put("cmd", outboundMessage.getCommand());
commandInfo.put("service", outboundMessage.getName());
}
case docker -> {
commandInfo.put("cmd", "run");
commandInfo.put("appName", "");
commandInfo.put("name", outboundMessage.getName());
commandInfo.put("image", outboundMessage.getImage());
commandInfo.put("options", outboundMessage.getOptions());
}
case deploy -> {
commandInfo.put("cmd", outboundMessage.getCommand());
commandInfo.put("appName", outboundMessage.getName());
commandInfo.put("fileUrl", url);
commandInfo.put("fileType", outboundMessage.getFileType());
}
case json -> {
commandInfo.put("cmd", String.valueOf(outboundMessage.getCommandType()));
commandInfo.put("appName", outboundMessage.getName());
commandInfo.put("parameter", outboundMessage.getParameters());
}
}
OutboundMessagePayload payload = new OutboundMessagePayload(
cmdInfo,
cmdType,
assetCode,
commandInfo,
outboundMessage.getCommandType(),
outboundMessage.getSubCommandType(),
deviceType,
deployMessage.getRequestId()
assetCode,
outboundMessage.getRequestId()
);
byte[] bytes = objectMapper.writeValueAsBytes(payload);

View File

@ -4,5 +4,6 @@ public enum CommandType {
bash,
systemd,
docker,
deploy
deploy,
json
}

View File

@ -5,14 +5,20 @@ public class DeployRequest {
private String assetCode;
private String appName;
private OperationType operationType;
private DeviceType deviceType;
private CommandType commandType;
private SubCommandType subCommandType;
protected DeployRequest() {}
public DeployRequest(String requestId, String assetCode, String appName, OperationType operationType) {
public DeployRequest(String requestId, String assetCode, String appName, OperationType operationType, DeviceType deviceType, CommandType commandType, SubCommandType subCommandType) {
this.requestId = requestId;
this.assetCode = assetCode;
this.appName = appName;
this.operationType = operationType;
this.deviceType = deviceType;
this.commandType = commandType;
this.subCommandType = subCommandType;
}
public String getRequestId() {
@ -31,13 +37,28 @@ public class DeployRequest {
return operationType;
}
public DeviceType getDeviceType() {
return deviceType;
}
public CommandType getCommandType() {
return commandType;
}
public SubCommandType getSubCommandType() {
return subCommandType;
}
@Override
public String toString() {
return "DeployRequest{" +
"requestId='" + requestId + '\'' +
", assetCode='" + assetCode + '\'' +
", appName='" + appName + '\'' +
", operationType='" + operationType + '\'' +
", operationType=" + operationType +
", deviceType=" + deviceType +
", commandType=" + commandType +
", subCommandType=" + subCommandType +
'}';
}
@ -50,6 +71,9 @@ public class DeployRequest {
private String assetCode;
private String appName;
private OperationType operationType;
private DeviceType deviceType;
private CommandType commandType;
private SubCommandType subCommandType;
public Builder requestId(String requestId) {
this.requestId = requestId;
@ -71,12 +95,30 @@ public class DeployRequest {
return this;
}
public Builder deviceType(DeviceType deviceType) {
this.deviceType = deviceType;
return this;
}
public Builder commandType(CommandType commandType) {
this.commandType = commandType;
return this;
}
public Builder subCommandType(SubCommandType subCommandType) {
this.subCommandType = subCommandType;
return this;
}
public DeployRequest build() {
DeployRequest deployRequest = new DeployRequest();
deployRequest.requestId = this.requestId;
deployRequest.assetCode = this.assetCode;
deployRequest.appName = this.appName;
deployRequest.operationType = this.operationType;
deployRequest.deviceType = this.deviceType;
deployRequest.commandType = this.commandType;
deployRequest.subCommandType = this.subCommandType;
return deployRequest;
}
}

View File

@ -0,0 +1,6 @@
package inc.sdt.blokworks.devicedeployer.domain;
public enum DeviceType {
ecn,
nodeq
}

View File

@ -4,12 +4,17 @@ import java.util.LinkedHashMap;
public class OutboundMessage {
private String fileId;
private String fileType;
private String appName;
private String name;
private LinkedHashMap<String, Object> env;
private String image;
private LinkedHashMap<String, Object> options;
private String command;
private String requestId;
private String deviceType;
private DeviceType deviceType;
private CommandType commandType;
private SubCommandType subCommandType;
private LinkedHashMap<String, String> parameters;
protected OutboundMessage() {}
@ -17,12 +22,24 @@ public class OutboundMessage {
return fileId;
}
public String getFileType() {
return fileType;
}
public String getAppName() {
return appName;
}
public String getName() {
return name;
}
public LinkedHashMap<String, Object> getEnv() {
return env;
public String getImage() {
return image;
}
public LinkedHashMap<String, Object> getOptions() {
return options;
}
public String getCommand() {
@ -37,7 +54,7 @@ public class OutboundMessage {
return requestId;
}
public String getDeviceType() {
public DeviceType getDeviceType() {
return deviceType;
}
@ -45,16 +62,29 @@ public class OutboundMessage {
return commandType;
}
public SubCommandType getSubCommandType() {
return subCommandType;
}
public LinkedHashMap<String, String> getParameters() {
return parameters;
}
@Override
public String toString() {
return "OutboundMessage{" +
"fileId='" + fileId + '\'' +
", fileType='" + fileType + '\'' +
", appName='" + appName + '\'' +
", name='" + name + '\'' +
", env=" + env +
", image='" + image + '\'' +
", options=" + options +
", command='" + command + '\'' +
", requestId='" + requestId + '\'' +
", deviceType='" + deviceType + '\'' +
", deviceType=" + deviceType +
", commandType=" + commandType +
", subCommandType=" + subCommandType +
", parameters=" + parameters +
'}';
}
@ -64,25 +94,45 @@ public class OutboundMessage {
public static final class Builder {
private String fileId;
private String fileType;
private String appName;
private String name;
private LinkedHashMap<String, Object> env;
private String image;
private LinkedHashMap<String, Object> options;
private String command;
private String requestId;
private String deviceType;
private DeviceType deviceType;
private CommandType commandType;
private SubCommandType subCommandType;
private LinkedHashMap<String, String> parameters;
public Builder fileId(String fileId) {
this.fileId = fileId;
return this;
}
public Builder fileType(String fileType) {
this.fileType = fileType;
return this;
}
public Builder appName(String appName) {
this.appName = appName;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder env(LinkedHashMap<String, Object> env) {
this.env = env;
public Builder image(String image) {
this.image = image;
return this;
}
public Builder options(LinkedHashMap<String, Object> options) {
this.options = options;
return this;
}
@ -96,7 +146,7 @@ public class OutboundMessage {
return this;
}
public Builder deviceType(String deviceType) {
public Builder deviceType(DeviceType deviceType) {
this.deviceType = deviceType;
return this;
}
@ -106,15 +156,30 @@ public class OutboundMessage {
return this;
}
public Builder subCommandType(SubCommandType subCommandType) {
this.subCommandType = subCommandType;
return this;
}
public Builder parameters(LinkedHashMap<String, String> parameters) {
this.parameters = parameters;
return this;
}
public OutboundMessage build() {
OutboundMessage deployMessage = new OutboundMessage();
deployMessage.fileId = this.fileId;
deployMessage.fileType = this.fileType;
deployMessage.appName = this.appName;
deployMessage.name = this.name;
deployMessage.env = this.env;
deployMessage.image = this.image;
deployMessage.options = this.options;
deployMessage.command = this.command;
deployMessage.requestId = this.requestId;
deployMessage.deviceType = this.deviceType;
deployMessage.commandType = this.commandType;
deployMessage.subCommandType = this.subCommandType;
deployMessage.parameters = this.parameters;
return deployMessage;
}
}

View File

@ -0,0 +1,6 @@
package inc.sdt.blokworks.devicedeployer.domain;
public enum SubCommandType {
systemd,
docker
}

View File

@ -1,13 +1,17 @@
package inc.sdt.blokworks.devicedeployer.infrastructure.mqtt;
import inc.sdt.blokworks.devicedeployer.domain.CommandType;
import inc.sdt.blokworks.devicedeployer.domain.DeviceType;
import inc.sdt.blokworks.devicedeployer.domain.SubCommandType;
import java.util.HashMap;
public record OutboundMessagePayload(
HashMap<String, Object> cmdInfo,
String cmdType,
CommandType cmdType,
SubCommandType subCmdType,
DeviceType deviceType,
String assetCode,
String deviceType,
String requestId
) {
}

View File

@ -1,6 +1,9 @@
package inc.sdt.blokworks.devicedeployer.infrastructure.relational;
import inc.sdt.blokworks.devicedeployer.domain.CommandType;
import inc.sdt.blokworks.devicedeployer.domain.DeviceType;
import inc.sdt.blokworks.devicedeployer.domain.OperationType;
import inc.sdt.blokworks.devicedeployer.domain.SubCommandType;
import jakarta.persistence.*;
@Entity(name = "deploy_request")
@ -18,14 +21,26 @@ public class DeployRequestEntity {
@Enumerated(EnumType.STRING)
@Column(name = "operation_type", length = 255)
private OperationType operationType;
@Enumerated(EnumType.STRING)
@Column(name = "device_type", length = 255)
private DeviceType deviceType;
@Enumerated(EnumType.STRING)
@Column(name = "command_type", length = 255)
private CommandType commandType;
@Enumerated(EnumType.STRING)
@Column(name = "sub_command_type", length = 255)
private SubCommandType subCommandType;
protected DeployRequestEntity() {}
public DeployRequestEntity(String requestId, String assetCode, String appName, OperationType operationType) {
public DeployRequestEntity(String requestId, String assetCode, String appName, OperationType operationType, DeviceType deviceType, CommandType commandType, SubCommandType subCommandType) {
this.requestId = requestId;
this.assetCode = assetCode;
this.appName = appName;
this.operationType = operationType;
this.deviceType = deviceType;
this.commandType = commandType;
this.subCommandType = subCommandType;
}
public String getId() {
@ -47,4 +62,16 @@ public class DeployRequestEntity {
public OperationType getOperationType() {
return operationType;
}
public DeviceType getDeviceType() {
return deviceType;
}
public CommandType getCommandType() {
return commandType;
}
public SubCommandType getSubCommandType() {
return subCommandType;
}
}

View File

@ -39,7 +39,10 @@ public class DeployRequestRelationalRepository implements DeployRequestRepositor
deployRequest.getRequestId(),
deployRequest.getAssetCode(),
deployRequest.getAppName(),
deployRequest.getOperationType()
deployRequest.getOperationType(),
deployRequest.getDeviceType(),
deployRequest.getCommandType(),
deployRequest.getSubCommandType()
);
}
@ -49,6 +52,9 @@ public class DeployRequestRelationalRepository implements DeployRequestRepositor
.assetCode(entity.getAssetCode())
.appName(entity.getAppName())
.operationType(entity.getOperationType())
.deviceType(entity.getDeviceType())
.commandType(entity.getCommandType())
.subCommandType(entity.getSubCommandType())
.build();
}
}

View File

@ -33,16 +33,16 @@ public class DeployerController {
/**
*
* @param assetCode
* @param assetAppResource
* @param resource
*/
@ResourceMapping(name = "Deploy_App", method = "POST", uri = "/assets/{code}/apps", description = "앱 배포 명령")
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/assets/{assetCode}/apps")
public void deploy(@PathVariable String assetCode,
@Valid @RequestBody OutboundMessageResource assetAppResource) {
log.info("[deploy] assetCode = {}, assetAppResource = {}", assetCode, assetAppResource);
@Valid @RequestBody OutboundMessageResource resource) {
log.info("[deploy] assetCode = {}, resource = {}", assetCode, resource);
String requestId = UUID.randomUUID().toString();
OutboundMessage outboundMessage = outboundMessageResourceConverter.fromResource(assetAppResource);
OutboundMessage outboundMessage = outboundMessageResourceConverter.fromResource(resource);
outboundMessage.setRequestId(requestId);
DeployRequest deployRequest = DeployRequest.builder()
@ -50,6 +50,9 @@ public class DeployerController {
.assetCode(assetCode)
.appName(outboundMessage.getName())
.operationType(OperationType.DEPLOY)
.deviceType(resource.deviceType())
.commandType(resource.commandType())
.subCommandType(resource.subCommandType())
.build();
DeployRequest request = deployerService.save(deployRequest);

View File

@ -34,7 +34,7 @@ public class MqttMessageHandler {
void handleMessage(Message<String> message) {
log.info("[handleMessage] message={}", message);
if(!message.getPayload().contains("pid")) {
/*if(!message.getPayload().contains("pid")) {
//String topic = String.valueOf(message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
//String id = topic.split("/")[4];
@ -54,8 +54,18 @@ public class MqttMessageHandler {
processMessagePayloadConverter.convertFromByte(message.getPayload(), InboundProcessMessagePayload.class)
.flatMap(processService)
.subscribe();
}
}*/
deployMessagePayloadConverter.convertFromByte(message.getPayload(), InboundDeployMessagePayload.class)
.map(p -> new InboundDeployMessagePayload(
p.assetCode(),
p.deviceType(),
p.status(),
p.result(),
p.requestId()
))
.flatMap(deployerService)
.subscribe();
}
}

View File

@ -1,20 +1,25 @@
package inc.sdt.blokworks.devicedeployer.presentation;
import inc.sdt.blokworks.devicedeployer.domain.CommandType;
import inc.sdt.blokworks.devicedeployer.domain.DeviceType;
import inc.sdt.blokworks.devicedeployer.domain.SubCommandType;
import org.wildfly.common.annotation.NotNull;
import java.util.HashMap;
import java.util.LinkedHashMap;
record OutboundMessageResource(
@NotNull
String fileId,
String fileType,
@NotNull
String name,
String image,
String command,
LinkedHashMap<String, Object> env,
String deviceType,
CommandType commandType
DeviceType deviceType,
CommandType commandType,
SubCommandType subCommandType,
LinkedHashMap<String, Object> options,
LinkedHashMap<String, String> parameters
) {
}

View File

@ -1,44 +1,27 @@
package inc.sdt.blokworks.devicedeployer.presentation;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import inc.sdt.blokworks.devicedeployer.domain.CommandType;
import inc.sdt.blokworks.devicedeployer.domain.DeviceType;
import inc.sdt.blokworks.devicedeployer.domain.OutboundMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.*;
@Component
public class OutboundMessageResourceConverter {
private final ObjectMapper objectMapper;
public OutboundMessageResourceConverter() {
this.objectMapper = new ObjectMapper();
}
public OutboundMessage fromResource(OutboundMessageResource resource) {
//final Map<String, Object> cmdInfo = resource.env().isEmpty() ? new HashMap<>() : convertToMap(resource.env());
return OutboundMessage.builder()
.fileId(resource.fileId())
.fileType(resource.fileType())
.name(resource.name())
.image(resource.image() == null ? "" : resource.image())
.command(resource.command())
.env(resource.env() != null ? resource.env() : new LinkedHashMap<>())
.deviceType(resource.deviceType())
.options(resource.options() == null ? new LinkedHashMap<>() : resource.options())
.deviceType(resource.deviceType() == null ? DeviceType.ecn : resource.deviceType())
.commandType(resource.commandType() == null ? CommandType.deploy : resource.commandType())
.parameters(resource.parameters() == null ? new LinkedHashMap<>() : resource.parameters())
.build();
}
private Map<String, Object> convertToMap(String env) {
try {
return objectMapper.readValue(env, new TypeReference<>() {});
}catch (IOException e) {
throw new IllegalArgumentException();
}
}
}