|
@@ -0,0 +1,292 @@
|
|
|
+package com.uas.erp.manage.client.service;
|
|
|
+
|
|
|
+import com.uas.erp.manage.client.entity.Container;
|
|
|
+import com.uas.erp.manage.client.entity.Project;
|
|
|
+import com.uas.erp.manage.client.repository.ContainerRepository;
|
|
|
+import com.uas.erp.manage.client.util.ShellUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.*;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by Pro1 on 2017/6/19.
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ContainerService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ContainerRepository containerRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProjectService projectService;
|
|
|
+
|
|
|
+ public Container getContainerByPath(String path) throws Exception {
|
|
|
+ Container container = new Container(path);
|
|
|
+ File dir = new File(path);
|
|
|
+ if (dir.exists() && dir.isDirectory()) {
|
|
|
+ if (new File(dir.getAbsolutePath() + "/bin/catalina.sh").exists()) {
|
|
|
+ container.setType(Container.Type.TOMCAT.name());
|
|
|
+ container.setStartCommand(dir.getAbsolutePath() + "/bin/catalina.sh start");
|
|
|
+ container.setStopCommand(dir.getAbsolutePath() + "/bin/catalina.sh stop");
|
|
|
+ container.setStatusCommand(dir.getAbsolutePath() + "/bin/catalina.sh status");
|
|
|
+ container.setProcessCommand(String.format("ps aux | grep '%s' | grep -v 'grep' | awk '{print $2}'", dir.getAbsolutePath()));
|
|
|
+ container.setWebappsHome(dir.getAbsolutePath() + "/webapps");
|
|
|
+ } else if (new File(dir.getAbsolutePath() + "/bin/standalone.sh").exists()) {
|
|
|
+ container.setType(Container.Type.JBOSS.name());
|
|
|
+ container.setWebappsHome(dir.getAbsolutePath() + "/standalone/deployment");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return container;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getPid(Container container) throws Exception {
|
|
|
+ if (!StringUtils.isEmpty(container.getProcessCommand())) {
|
|
|
+ if (container.isRemote()) {
|
|
|
+ return ShellUtils.execute(String.format("ssh root@%s '%s'", container.getHost(),
|
|
|
+ container.getProcessCommand()));
|
|
|
+ } else {
|
|
|
+ return ShellUtils.execute(container.getProcessCommand());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getStatus(Container container) throws Exception{
|
|
|
+ if (container.isCluster()) {
|
|
|
+ if (!CollectionUtils.isEmpty(container.getNodes())) {
|
|
|
+ for (Container node:container.getNodes()) {
|
|
|
+ if (!Container.Status.RUNNING.name().equals(getStatus(node))) {
|
|
|
+ return Container.Status.UNRECOGNIZED.name();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Container.Status.RUNNING.name();
|
|
|
+ }
|
|
|
+ return Container.Status.UNRECOGNIZED.name();
|
|
|
+ } else {
|
|
|
+ if (!StringUtils.isEmpty(getPid(container)))
|
|
|
+ return Container.Status.RUNNING.name();
|
|
|
+ return Container.Status.STOPPED.name();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void safetySave(Container container) throws Exception{
|
|
|
+ Container oldOne = null;
|
|
|
+ if (null != container.getId()){
|
|
|
+ oldOne = containerRepository.findOne(container.getId());
|
|
|
+ if (null != oldOne) {
|
|
|
+ oldOne.setDescription(container.getDescription());
|
|
|
+ oldOne.setHome(container.getHome());
|
|
|
+ oldOne.setType(container.getType());
|
|
|
+ oldOne.setPort(container.getPort());
|
|
|
+ oldOne.setStatus(getStatus(oldOne));
|
|
|
+ oldOne.setWebappsHome(container.getWebappsHome());
|
|
|
+ oldOne.setProcessCommand(container.getProcessCommand());
|
|
|
+ oldOne.setStatusCommand(container.getStatusCommand());
|
|
|
+ oldOne.setStartCommand(container.getStartCommand());
|
|
|
+ oldOne.setStopCommand(container.getStopCommand());
|
|
|
+ oldOne.setNodes(container.getNodes());
|
|
|
+ } else {
|
|
|
+ oldOne = container;
|
|
|
+ oldOne.setId(null);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ oldOne = containerRepository.findByHome(container.getHome());
|
|
|
+ if (null != oldOne) {
|
|
|
+ oldOne.setDescription(container.getDescription());
|
|
|
+ oldOne.setType(container.getType());
|
|
|
+ oldOne.setPort(container.getPort());
|
|
|
+ oldOne.setStatus(getStatus(oldOne));
|
|
|
+ oldOne.setWebappsHome(container.getWebappsHome());
|
|
|
+ oldOne.setProcessCommand(container.getProcessCommand());
|
|
|
+ oldOne.setStatusCommand(container.getStatusCommand());
|
|
|
+ oldOne.setStartCommand(container.getStartCommand());
|
|
|
+ oldOne.setStopCommand(container.getStopCommand());
|
|
|
+ oldOne.setNodes(container.getNodes());
|
|
|
+ } else {
|
|
|
+ oldOne = container;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ containerRepository.save(oldOne);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void save(Container container) {
|
|
|
+ containerRepository.save(container);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void save(List<Container> containers) {
|
|
|
+ containerRepository.save(containers);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void delete(Container container){
|
|
|
+ List<Project> projects = projectService.findByContainerId(container.getId());
|
|
|
+ Assert.isTrue(CollectionUtils.isEmpty(projects), "有项目正在使用该容器,无法删除");
|
|
|
+ if (container.isCluster() && !CollectionUtils.isEmpty(container.getNodes())) {
|
|
|
+ for (Container node:container.getNodes()) {
|
|
|
+ delete(node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ containerRepository.delete(container);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void deleteAll() {
|
|
|
+ containerRepository.deleteAll();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Container findOne(long containerId) {
|
|
|
+ return containerRepository.findOne(containerId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Container> findAll() {
|
|
|
+ return containerRepository.findAll();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void start(Container container) throws Exception{
|
|
|
+ if (!StringUtils.isEmpty(container.getStartCommand())) {
|
|
|
+ if (container.isRemote()) {
|
|
|
+ ShellUtils.execute(String.format("ssh root@%s '%s'", container.getHost(),
|
|
|
+ container.getStartCommand()));
|
|
|
+ } else {
|
|
|
+ ShellUtils.execute(container.getStartCommand());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (container.isCluster()) {
|
|
|
+ if (!CollectionUtils.isEmpty(container.getNodes())) {
|
|
|
+ for (Container node : container.getNodes()) {
|
|
|
+ start(node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void stop(Container container) throws Exception {
|
|
|
+ if (!StringUtils.isEmpty(container.getStopCommand())) {
|
|
|
+ if (container.isRemote()) {
|
|
|
+ ShellUtils.execute(String.format("ssh root@%s '%s'", container.getHost(),
|
|
|
+ container.getStopCommand()));
|
|
|
+ } else {
|
|
|
+ ShellUtils.execute(container.getStopCommand());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (container.isCluster()) {
|
|
|
+ if (!CollectionUtils.isEmpty(container.getNodes())) {
|
|
|
+ for (Container node : container.getNodes()) {
|
|
|
+ stop(node);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String pid = getPid(container);
|
|
|
+ if (!StringUtils.isEmpty(pid)) {
|
|
|
+ if (container.isRemote()) {
|
|
|
+ ShellUtils.execute(String.format("ssh root@%s 'kill -9 %s'", container.getHost(), pid));
|
|
|
+ } else {
|
|
|
+ ShellUtils.execute("kill -9 " + pid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void restart(Container container) throws Exception {
|
|
|
+ stop(container);
|
|
|
+ start(container);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清除旧文件目录
|
|
|
+ * @param container
|
|
|
+ * @param context
|
|
|
+ */
|
|
|
+ public void clearByContext(Container container, String context) throws Exception{
|
|
|
+ if (container.isCluster()) {
|
|
|
+ if (!CollectionUtils.isEmpty(container.getNodes())) {
|
|
|
+ for (Container node : container.getNodes()) {
|
|
|
+ if (node.isRemote())
|
|
|
+ clearRemoteByContext(node, context);
|
|
|
+ else
|
|
|
+ clearLocalByContext(container, context);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (container.isRemote())
|
|
|
+ clearRemoteByContext(container, context);
|
|
|
+ else
|
|
|
+ clearLocalByContext(container, context);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void clearLocalByContext(Container container, String context) throws Exception{
|
|
|
+ File deployDir = new File(container.getWebappsHome());
|
|
|
+ File dir = new File(deployDir, context);
|
|
|
+ if (dir.exists()) {
|
|
|
+ FileSystemUtils.deleteRecursively(dir);
|
|
|
+ }
|
|
|
+ File file = new File(deployDir, context + ".war");
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ file = new File(deployDir, context + ".deployed");
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ file = new File(deployDir, context + ".undeployed");
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ file = new File(deployDir, context + ".deploying");
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void clearRemoteByContext(Container container, String context) throws Exception{
|
|
|
+ File deployDir = new File(container.getWebappsHome());
|
|
|
+ ShellUtils.execute(String.format("ssh root@%s 'rm -rf %s/%s'", container.getHost(), deployDir.getAbsolutePath(),
|
|
|
+ context));
|
|
|
+ ShellUtils.execute(String.format("ssh root@%s 'rm -rf %s/%s.war'", container.getHost(), deployDir.getAbsolutePath(),
|
|
|
+ context));
|
|
|
+ ShellUtils.execute(String.format("ssh root@%s 'rm -rf %s/%s.deployed'", container.getHost(), deployDir.getAbsolutePath(),
|
|
|
+ context));
|
|
|
+ ShellUtils.execute(String.format("ssh root@%s 'rm -rf %s/%s.undeployed'", container.getHost(), deployDir.getAbsolutePath(),
|
|
|
+ context));
|
|
|
+ ShellUtils.execute(String.format("ssh root@%s 'rm -rf %s/%s.deploying'", container.getHost(), deployDir.getAbsolutePath(),
|
|
|
+ context));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void deployByFile(Container container, File buildFile) throws Exception{
|
|
|
+ if (!StringUtils.isEmpty(container.getDeployCommand())) {
|
|
|
+ if (container.isRemote()) {
|
|
|
+ // send file
|
|
|
+ ShellUtils.execute(String.format("scp %s root@%s:/tmp/", buildFile.getAbsolutePath(),
|
|
|
+ container.getHost(), buildFile.getName()));
|
|
|
+ // deploy
|
|
|
+ ShellUtils.execute(String.format("ssh root@%s '%s %s'", container.getHost(),
|
|
|
+ container.getDeployCommand(), "/tmp/" + buildFile.getName()));
|
|
|
+ } else {
|
|
|
+ ShellUtils.execute(container.getDeployCommand() + " " + buildFile.getAbsolutePath());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (container.isCluster()) {
|
|
|
+ if (!CollectionUtils.isEmpty(container.getNodes())) {
|
|
|
+ for (Container node : container.getNodes()) {
|
|
|
+ deployByFile(node, buildFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (container.isRemote()) {
|
|
|
+ // send file only
|
|
|
+ ShellUtils.execute(String.format("scp %s root@%s:", buildFile.getAbsolutePath(),
|
|
|
+ container.getHost(), container.getWebappsHome()));
|
|
|
+ } else {
|
|
|
+ FileCopyUtils.copy(buildFile, new File(container.getWebappsHome()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|