From 35231fa028f1347252712a1e34ea537fa5683adf Mon Sep 17 00:00:00 2001 From: jiangzeyin Date: Sat, 29 Sep 2018 16:57:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=89=E8=A3=85....?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jiangzeyin/controller/BaseController.java | 5 +++ .../controller/manage/BuildController.java | 42 +++++++++++++++++-- .../cn/jiangzeyin/service/BaseService.java | 14 +++++++ .../service/manage/CommandService.java | 2 +- .../{ => service}/oss/OssManagerService.java | 19 ++++++++- src/main/resources/vm/manage/build.vm | 42 +++++++++++++++++-- src/main/resources/vm/manage/editProject.vm | 1 - src/main/resources/vm/manage/projectInfo.vm | 1 - 8 files changed, 115 insertions(+), 11 deletions(-) rename src/main/java/cn/jiangzeyin/{ => service}/oss/OssManagerService.java (84%) diff --git a/src/main/java/cn/jiangzeyin/controller/BaseController.java b/src/main/java/cn/jiangzeyin/controller/BaseController.java index b5b295ee7..f319385a6 100644 --- a/src/main/java/cn/jiangzeyin/controller/BaseController.java +++ b/src/main/java/cn/jiangzeyin/controller/BaseController.java @@ -2,6 +2,7 @@ package cn.jiangzeyin.controller; import cn.jiangzeyin.common.interceptor.LoginInterceptor; import cn.jiangzeyin.controller.base.AbstractBaseControl; +import org.springframework.web.context.request.RequestAttributes; /** * @author jiangzeyin @@ -16,4 +17,8 @@ public abstract class BaseController extends AbstractBaseControl { userName = getSessionAttribute(LoginInterceptor.SESSION_NAME); userPwd = getSessionAttribute(LoginInterceptor.SESSION_PWD); } + + public static String getUserName() { + return (String) getRequestAttributes().getAttribute(LoginInterceptor.SESSION_NAME, RequestAttributes.SCOPE_SESSION); + } } diff --git a/src/main/java/cn/jiangzeyin/controller/manage/BuildController.java b/src/main/java/cn/jiangzeyin/controller/manage/BuildController.java index d00ee7ec0..c8e8a3be2 100644 --- a/src/main/java/cn/jiangzeyin/controller/manage/BuildController.java +++ b/src/main/java/cn/jiangzeyin/controller/manage/BuildController.java @@ -1,16 +1,23 @@ package cn.jiangzeyin.controller.manage; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.core.util.ZipUtil; +import cn.jiangzeyin.common.JsonMessage; import cn.jiangzeyin.controller.BaseController; import cn.jiangzeyin.model.ProjectInfoModel; -import cn.jiangzeyin.oss.OssManagerService; +import cn.jiangzeyin.service.manage.CommandService; import cn.jiangzeyin.service.manage.ManageService; +import cn.jiangzeyin.service.oss.OssManagerService; import com.alibaba.fastjson.JSONArray; +import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; +import java.io.File; import java.io.IOException; /** @@ -22,17 +29,46 @@ public class BuildController extends BaseController { @Resource private OssManagerService ossManagerService; - @Resource private ManageService manageService; + @Resource + private CommandService commandService; @RequestMapping(value = "build", method = RequestMethod.GET) - public String console(String id) throws IOException { + public String build(String id) throws IOException { ProjectInfoModel projectInfoModel = manageService.getProjectInfo(id); if (projectInfoModel != null && StrUtil.isNotEmpty(projectInfoModel.getBuildTag())) { JSONArray jsonArray = ossManagerService.list(projectInfoModel.getBuildTag()); setAttribute("array", jsonArray); + setAttribute("id", id); } return "manage/build"; } + + @RequestMapping(value = "build_install", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @ResponseBody + public String buildInstall(String id, String key) throws IOException { + ProjectInfoModel projectInfoModel = manageService.getProjectInfo(id); + if (projectInfoModel == null) { + return JsonMessage.getString(400, "没有对应项目"); + } + if (StrUtil.isEmpty(projectInfoModel.getBuildTag())) { + return JsonMessage.getString(400, "项目还不支持构建"); + } + File file = ossManagerService.download(key); + if (!file.exists()) { + return JsonMessage.getString(500, "下载远程文件失败"); + } + File lib = new File(projectInfoModel.getLib()); + FileUtil.mkdir(lib); + if (!FileUtil.clean(lib)) { + return JsonMessage.getString(500, "清楚旧lib失败"); + } + ZipUtil.unzip(file, lib); + String result = commandService.execCommand(CommandService.CommandOp.restart, projectInfoModel, null); + if (result.contains(CommandService.RUNING_TAG)) { + return JsonMessage.getString(200, "ok"); + } + return JsonMessage.getString(505, "安装失败:" + result); + } } diff --git a/src/main/java/cn/jiangzeyin/service/BaseService.java b/src/main/java/cn/jiangzeyin/service/BaseService.java index b46456ec9..0ed226886 100644 --- a/src/main/java/cn/jiangzeyin/service/BaseService.java +++ b/src/main/java/cn/jiangzeyin/service/BaseService.java @@ -1,6 +1,9 @@ package cn.jiangzeyin.service; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.StrUtil; import cn.jiangzeyin.common.spring.SpringUtil; +import cn.jiangzeyin.controller.BaseController; import cn.jiangzeyin.util.JsonUtil; import com.alibaba.fastjson.JSONObject; @@ -27,6 +30,17 @@ public class BaseService { return file; } + protected File getTempPath() throws IOException { + File file = getDataPath(); + String userName = BaseController.getUserName(); + if (StrUtil.isEmpty(userName)) { + throw new RuntimeException("没有登录"); + } + file = new File(file.getPath() + "/temp/", userName); + FileUtil.mkdir(file); + return file; + } + /** * 获取数据文件的路径,如果文件不存在,则创建一个 * diff --git a/src/main/java/cn/jiangzeyin/service/manage/CommandService.java b/src/main/java/cn/jiangzeyin/service/manage/CommandService.java index 23e7538ae..9d7b6b0b5 100644 --- a/src/main/java/cn/jiangzeyin/service/manage/CommandService.java +++ b/src/main/java/cn/jiangzeyin/service/manage/CommandService.java @@ -59,7 +59,6 @@ public class CommandService { if (commandOp == CommandOp.showlog) { return result; } - InputStream is; CommandService commandService = SpringUtil.getBean(CommandService.class); String commandPath = commandService.getCommandPath(); @@ -72,6 +71,7 @@ public class CommandService { String jvm = projectInfoModel.getJvm(); String args = projectInfoModel.getArgs(); try { + InputStream is; // 执行命令 String command = String.format("%s %s %s %s %s %s %s [%s][%s]", commandPath, commandOp.toString(), tag, mainClass, lib, log, token, jvm, args); DefaultSystemLog.LOG().info(command); diff --git a/src/main/java/cn/jiangzeyin/oss/OssManagerService.java b/src/main/java/cn/jiangzeyin/service/oss/OssManagerService.java similarity index 84% rename from src/main/java/cn/jiangzeyin/oss/OssManagerService.java rename to src/main/java/cn/jiangzeyin/service/oss/OssManagerService.java index 0e942fce1..8bedd2efc 100644 --- a/src/main/java/cn/jiangzeyin/oss/OssManagerService.java +++ b/src/main/java/cn/jiangzeyin/service/oss/OssManagerService.java @@ -1,19 +1,23 @@ -package cn.jiangzeyin.oss; +package cn.jiangzeyin.service.oss; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.util.CharsetUtil; import cn.jiangzeyin.common.spring.SpringUtil; +import cn.jiangzeyin.service.BaseService; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aliyun.oss.OSSClient; +import com.aliyun.oss.model.GetObjectRequest; import com.aliyun.oss.model.ListObjectsRequest; import com.aliyun.oss.model.OSSObjectSummary; import com.aliyun.oss.model.ObjectListing; import org.springframework.stereotype.Service; +import java.io.File; +import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -22,7 +26,18 @@ import java.util.List; * Created by jiangzeyin on 2018/9/28. */ @Service -public class OssManagerService { +public class OssManagerService extends BaseService { + + public File download(String key) throws IOException { + File file = getTempPath(); + file = new File(file, key); + OSSClient ossClient = getOSSClient(); + // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。 + ossClient.getObject(new GetObjectRequest(getBucketName(), key), file); + // 关闭OSSClient。 + ossClient.shutdown(); + return file; + } public JSONArray list(String name) { OSSClient ossClient; diff --git a/src/main/resources/vm/manage/build.vm b/src/main/resources/vm/manage/build.vm index a1980ad59..6b9185552 100644 --- a/src/main/resources/vm/manage/build.vm +++ b/src/main/resources/vm/manage/build.vm @@ -29,7 +29,9 @@ $item.time $item.size - + #end @@ -40,9 +42,43 @@ \ No newline at end of file diff --git a/src/main/resources/vm/manage/editProject.vm b/src/main/resources/vm/manage/editProject.vm index 60c25557b..921eb5ca2 100644 --- a/src/main/resources/vm/manage/editProject.vm +++ b/src/main/resources/vm/manage/editProject.vm @@ -110,7 +110,6 @@ // 提交项目表单 form.on('submit(submitProject)', function (data) { - console.log(data.field) $.ajax({ url: data.form.action, type: 'POST', diff --git a/src/main/resources/vm/manage/projectInfo.vm b/src/main/resources/vm/manage/projectInfo.vm index c01faddc9..87a098c96 100644 --- a/src/main/resources/vm/manage/projectInfo.vm +++ b/src/main/resources/vm/manage/projectInfo.vm @@ -173,7 +173,6 @@ } else if ('file' === event) { fileManage(data); } else if ('build' === event) { - console.log(data) layer.open({ type: 2, title: '自动构建',