|
|
@@ -1,13 +1,33 @@
|
|
|
package cn.reghao.bnt.web.devops.deployer.service;
|
|
|
|
|
|
+import cn.reghao.bnt.web.devops.app.db.query.AppBuildQuery;
|
|
|
+import cn.reghao.bnt.web.devops.app.model.po.AppBuilding;
|
|
|
+import cn.reghao.bnt.web.devops.app.model.po.config.AppConfig;
|
|
|
+import cn.reghao.bnt.web.devops.builder.model.LocalBuildDir;
|
|
|
+import cn.reghao.bnt.web.devops.deployer.db.RemoteAgentConfigRepository;
|
|
|
import cn.reghao.bnt.web.devops.deployer.db.RemoteHostRepository;
|
|
|
+import cn.reghao.bnt.web.devops.deployer.model.constant.NodeType;
|
|
|
+import cn.reghao.bnt.web.devops.deployer.model.dto.RemoteMachine;
|
|
|
+import cn.reghao.bnt.web.devops.deployer.model.dto.UpdateApp;
|
|
|
+import cn.reghao.bnt.web.devops.deployer.model.po.RemoteAgentConfig;
|
|
|
import cn.reghao.bnt.web.devops.deployer.model.po.RemoteHost;
|
|
|
import cn.reghao.bnt.web.devops.deployer.util.Sftp;
|
|
|
+import cn.reghao.jutil.jdk.io.TextFile;
|
|
|
+import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
+import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
|
|
|
+import cn.reghao.jutil.jdk.web.result.Result;
|
|
|
+import cn.reghao.jutil.jdk.web.result.ResultStatus;
|
|
|
+import com.jcraft.jsch.Session;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -16,26 +36,81 @@ import java.util.List;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class DeployApp {
|
|
|
+ private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("update-pool", 5);
|
|
|
private final RemoteHostRepository remoteHostRepository;
|
|
|
+ private final RemoteAgentConfigRepository remoteAgentConfigRepository;
|
|
|
+ private final AppBuildQuery appBuildQuery;
|
|
|
private final Sftp sftp = new Sftp();
|
|
|
+ private final TextFile textFile = new TextFile();
|
|
|
+ private final String appId = "bnt";
|
|
|
|
|
|
- public DeployApp(RemoteHostRepository remoteHostRepository) {
|
|
|
+ public DeployApp(RemoteHostRepository remoteHostRepository, RemoteAgentConfigRepository remoteAgentConfigRepository,
|
|
|
+ AppBuildQuery appBuildQuery) {
|
|
|
this.remoteHostRepository = remoteHostRepository;
|
|
|
+ this.remoteAgentConfigRepository = remoteAgentConfigRepository;
|
|
|
+ this.appBuildQuery = appBuildQuery;
|
|
|
}
|
|
|
|
|
|
- public void addRemoteHost(RemoteHost remoteHost) {
|
|
|
- String host = remoteHost.getHost();
|
|
|
- RemoteHost remoteHostEntity = remoteHostRepository.findByHost(host);
|
|
|
- if (remoteHostEntity == null) {
|
|
|
- remoteHostRepository.save(remoteHost);
|
|
|
+ public void addAgentConfig(RemoteAgentConfig remoteAgentConfig) {
|
|
|
+ RemoteAgentConfig entity = remoteAgentConfigRepository.findByMgrHost(remoteAgentConfig.getMgrHost());
|
|
|
+ if (entity == null) {
|
|
|
+ remoteAgentConfigRepository.save(remoteAgentConfig);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void deleteRemoteHost(String host) {
|
|
|
- RemoteHost remoteHostEntity = remoteHostRepository.findByHost(host);
|
|
|
- if (remoteHostEntity != null) {
|
|
|
- //remoteHostRepository.delete(remoteHostEntity);
|
|
|
+ public void deleteAgentConfig(int id) {
|
|
|
+ remoteAgentConfigRepository.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<RemoteAgentConfig> getAgentConfigs() {
|
|
|
+ PageRequest pageRequest = PageRequest.of(0, 100);
|
|
|
+ return remoteAgentConfigRepository.findAll(pageRequest).getContent();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result addRemoteHost(RemoteMachine remoteMachine) {
|
|
|
+ Integer id = remoteMachine.getRemoteAgentConfig();
|
|
|
+ String nodeType = remoteMachine.getNodeType();
|
|
|
+ RemoteHost remoteHost;
|
|
|
+ if (NodeType.mgr.name().equals(nodeType)) {
|
|
|
+ remoteHost = new RemoteHost(remoteMachine, null);
|
|
|
+ } else if (id == null) {
|
|
|
+ return Result.fail("Remote agent config is required for agent node");
|
|
|
+ } else {
|
|
|
+ RemoteAgentConfig remoteAgentConfig = remoteAgentConfigRepository.findById(id).orElse(null);
|
|
|
+ if (remoteAgentConfig == null) {
|
|
|
+ return Result.fail("Remote agent config not exists");
|
|
|
+ }
|
|
|
+ remoteHost = new RemoteHost(remoteMachine, remoteAgentConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ Result result = Result.success();
|
|
|
+ try {
|
|
|
+ Session session = sftp.getSession(remoteHost);
|
|
|
+ if (session.isConnected()) {
|
|
|
+ boolean exist = sftp.mkdir(session, remoteHost.getAppDir());
|
|
|
+ if (exist) {
|
|
|
+ log.error(remoteHost.getAppDir() + " exists");
|
|
|
+ //result = Result.fail(remoteHost.getAppDir() + " exists");
|
|
|
+ }
|
|
|
+ session.disconnect();
|
|
|
+
|
|
|
+ String host = remoteHost.getHost();
|
|
|
+ RemoteHost remoteHostEntity = remoteHostRepository.findByHost(host);
|
|
|
+ if (remoteHostEntity == null) {
|
|
|
+ remoteHostRepository.save(remoteHost);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ result = Result.fail("Connect to " + remoteHost.getHost() + " failed");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ result = Result.error(e.getMessage());
|
|
|
}
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void deleteRemoteHost(int id) {
|
|
|
+ remoteHostRepository.deleteById(id);
|
|
|
}
|
|
|
|
|
|
public List<RemoteHost> getRemoteHosts(String nodeType) {
|
|
|
@@ -43,18 +118,93 @@ public class DeployApp {
|
|
|
return remoteHostRepository.findByNodeType(nodeType, pageRequest).getContent();
|
|
|
}
|
|
|
|
|
|
- public void deployApp(String host) {
|
|
|
- RemoteHost remoteHost = remoteHostRepository.findByHost(host);
|
|
|
- if (remoteHost == null) {
|
|
|
- return;
|
|
|
+ public Result deployApp(UpdateApp updateApp) {
|
|
|
+ AppConfig appConfig = appBuildQuery.getAppConfig(appId);
|
|
|
+ if (appConfig == null) {
|
|
|
+ return Result.fail(appId + " not exists");
|
|
|
}
|
|
|
|
|
|
- String localDir = "";
|
|
|
- String remoteDir = remoteHost.getAppDir();
|
|
|
- try {
|
|
|
- sftp.deploy(localDir, remoteDir, remoteHost);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ AppBuilding appBuilding = appBuildQuery.getAppBuilding(appId);
|
|
|
+ if (appBuilding == null) {
|
|
|
+ return Result.fail(appId + " not built");
|
|
|
+ }
|
|
|
+
|
|
|
+ updateApp.getHostList().forEach(host -> {
|
|
|
+ /*UpdateTask updateTask = new UpdateTask(host, appBuilding);
|
|
|
+ threadPool.submit(updateTask);*/
|
|
|
+ RemoteHost remoteHost = remoteHostRepository.findByHost(host);
|
|
|
+ if (remoteHost == null) {
|
|
|
+ log.error("host {} not exists", host);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String commitId = appBuilding.getCommitId();
|
|
|
+ String nodeType = remoteHost.getNodeType();
|
|
|
+ String localDir = String.format("%s/%s/%s_%s/%s", LocalBuildDir.packDir, appId, appId, commitId, nodeType);
|
|
|
+ String remoteDir = remoteHost.getAppDir();
|
|
|
+ try {
|
|
|
+ String agentConfigPath = String.format("%s/%s", localDir, "devopsagent.json");
|
|
|
+ RemoteAgentConfig remoteAgentConfig = remoteHost.getRemoteAgentConfig();
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("protocol", remoteAgentConfig.getMgrProtocol());
|
|
|
+ map.put("host", remoteAgentConfig.getMgrHost());
|
|
|
+ map.put("port", remoteAgentConfig.getMgrPort() + "");
|
|
|
+ textFile.write(new File(agentConfigPath), JsonConverter.objectToJson(map));
|
|
|
+
|
|
|
+ Result result = sftp.deploy(localDir, remoteDir, remoteHost);
|
|
|
+ if (result.getCode() == ResultStatus.SUCCESS.getCode()) {
|
|
|
+ remoteHost.setAppVersion(commitId);
|
|
|
+ remoteHost.setUpdateTime(LocalDateTime.now());
|
|
|
+ remoteHostRepository.save(remoteHost);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ class UpdateTask implements Runnable {
|
|
|
+ private final String host;
|
|
|
+ private final AppBuilding appBuilding;
|
|
|
+
|
|
|
+ public UpdateTask(String host, AppBuilding appBuilding) {
|
|
|
+ this.host = host;
|
|
|
+ this.appBuilding = appBuilding;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ RemoteHost remoteHost = remoteHostRepository.findByHost(host);
|
|
|
+ if (remoteHost == null) {
|
|
|
+ log.error("host {} not exists", host);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String commitId = appBuilding.getCommitId();
|
|
|
+ String nodeType = remoteHost.getNodeType();
|
|
|
+ String localDir = String.format("%s/%s/%s_%s/%s", LocalBuildDir.packDir, appId, appId, commitId, nodeType);
|
|
|
+ String remoteDir = remoteHost.getAppDir();
|
|
|
+
|
|
|
+ String agentConfigPath = String.format("%s/%s", localDir, "devopsagent.json");
|
|
|
+ RemoteAgentConfig remoteAgentConfig = remoteHost.getRemoteAgentConfig();
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("protocol", remoteAgentConfig.getMgrProtocol());
|
|
|
+ map.put("host", remoteAgentConfig.getMgrHost());
|
|
|
+ map.put("port", remoteAgentConfig.getMgrPort() + "");
|
|
|
+ textFile.write(new File(agentConfigPath), JsonConverter.objectToJson(map));
|
|
|
+
|
|
|
+ Result result = sftp.deploy(localDir, remoteDir, remoteHost);
|
|
|
+ if (result.getCode() == ResultStatus.SUCCESS.getCode()) {
|
|
|
+ remoteHost.setAppVersion(commitId);
|
|
|
+ remoteHost.setUpdateTime(LocalDateTime.now());
|
|
|
+ remoteHostRepository.save(remoteHost);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|