Authored by zhengyouwei

add app

... ... @@ -18,4 +18,6 @@ public class ProjectBuild extends AppBaseReq {
private String rollbackfile;
private String hosts;
}
... ...
package com.ui.app.req;
import lombok.Data;
/**
* Created by zhengyouwei on 2016/11/21.
*/
@Data
public class ProjectInfo extends AppBaseReq{
private String project;
}
... ...
package com.ui.ctrl.app;
import com.alibaba.fastjson.JSONArray;
import com.ui.app.common.ErrorCode;
import com.ui.app.req.*;
import com.ui.app.resp.NodeBuildState;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.domain.BuildMessage;
import com.ui.model.domain.PythonProject;
import com.ui.model.req.BuildRequest;
import com.ui.project.Project;
import com.ui.project.ProjectEnvironment;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Created by zhengyouwei on 2016/11/14.
* app 服务端,重写一个
*/
@RestController
@RequestMapping(value = "/app_nodeBuild")
public class AppNodeBuildCtrl {
@Autowired
private HttpRestClient httpClient;
private String NODE_URL = "http://172.31.16.167:8883/node/";
/**
* 获取项目列表
*
* @return
*/
@RequestMapping(value = "app_getnodeprojectlist", method = RequestMethod.POST)
public BaseResponse getnodeprojectlist(@RequestBody AppBaseReq appBaseReq) {
BaseResponse baseResponse = new BaseResponse();
String result = httpClient.get(NODE_URL + "getProjects", String.class, null);
List<Project> projectList = JSONArray.parseArray(result,Project.class);
List<String> stringList = projectList.stream().map(Project::getName).collect(Collectors.toList());
baseResponse.setData(stringList);
return baseResponse;
}
/**
* 获取项目信息
*
* @return
*/
@RequestMapping(value = "app_getnodeprojectinfo", method = RequestMethod.POST)
public BaseResponse getnodeprojectinfo(@RequestBody ProjectInfo projectInfo) {
BaseResponse baseResponse = new BaseResponse();
if (StringUtils.isBlank(projectInfo.getProject())) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
PythonProject pythonProject = httpClient.get(NODE_URL + "getProject?name="+projectInfo.getProject() , PythonProject.class, null);
baseResponse.setData(pythonProject);
return baseResponse;
}
/**
* 获取回滚列表
*
* @return
*/
@RequestMapping(value = "app_getnoderollbacklist", method = RequestMethod.POST)
public BaseResponse getnoderollbacklist(@RequestBody RollbackList rollbackListReq) {
BaseResponse baseResponse = new BaseResponse();
if (StringUtils.isBlank(rollbackListReq.getProject()) || StringUtils.isBlank(rollbackListReq.getEnvironment())) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
Map<String, String> map = new HashMap<>();
map.put("project", rollbackListReq.getProject());
map.put("environment", rollbackListReq.getEnvironment());
String result = httpClient.get(NODE_URL + "rollbackList", String.class, map);
if ("failed".equals(result)) {//没有文件
baseResponse.setCode(ErrorCode.ROLLBACK_FILE_ERROR);
baseResponse.setMessage(ErrorCode.ROLLBACK_FILE_ERROR_MESSAGE);
return baseResponse;
}
baseResponse.setData(JSONArray.parse(result));
return baseResponse;
}
/**
* 获取回滚列表
*
* @return
*/
@RequestMapping(value = "app_nodebuild", method = RequestMethod.POST)
public BaseResponse build(@RequestBody ProjectBuild projectBuild) {
BaseResponse baseResponse = new BaseResponse();
String project = projectBuild.getProject();
String environment = projectBuild.getEnvironment();
String operate = projectBuild.getOperate();
String branch = projectBuild.getBranch();
String rollbackFile = projectBuild.getRollbackfile();
if (StringUtils.isBlank(project) || StringUtils.isBlank(environment) || StringUtils.isBlank(operate)) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
if ("Deploy".equals(operate)) {
if (StringUtils.isBlank(branch)) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
//check branch
if (!"master".equals(branch)) {
Map<String, String> map = new HashMap<>();
map.put("projects", project);
map.put("branch", branch);
String result = httpClient.get(ProjectEnvironment.getUrl(environment) + "checkBranch", String.class, map);
if (!"1".equals(result)) {
baseResponse.setCode(ErrorCode.BRANCH_ERROR);
baseResponse.setMessage(ErrorCode.BRANCH_ERROR_MESSAGE);
return baseResponse;
}
}
} else {
if (StringUtils.isBlank(rollbackFile)) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
}
BuildRequest buildRequest = new BuildRequest();
buildRequest.setBranch(branch);
buildRequest.setUser(projectBuild.getAccountname());
buildRequest.setEnvironment(environment);
buildRequest.setRollbackfile(rollbackFile);
buildRequest.setOperate(operate);
buildRequest.setProject(project);
buildRequest.setHost(projectBuild.getHosts());
BuildMessage buildMessage = httpClient.post(NODE_URL + "build", buildRequest, BuildMessage.class);
baseResponse.setData(buildMessage.getId());
return baseResponse;
}
/**
* 获取执行结果
*
* @return
*/
@RequestMapping(value = "app_getnodebuildmsg", method = RequestMethod.POST)
public BaseResponse getbuildmsg(@RequestBody Buildmsg buildmsg) {
BaseResponse baseResponse = new BaseResponse();
if (StringUtils.isBlank(buildmsg.getProject()) || StringUtils.isBlank(buildmsg.getMessageid())) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
Map<String, String> map = new HashMap<>();
map.put("messageid", buildmsg.getMessageid());
map.put("project", buildmsg.getProject());
BaseResponse<BuildMessage> result = httpClient.otherOxchangeForget(ProjectEnvironment.getUrl(buildmsg.getMessageid().split("_")[0]) + "getbuildmsg", new ParameterizedTypeReference<BaseResponse<BuildMessage>>() {
}, map);
if (result.getCode() == 1) {
baseResponse.setData(result.getData().getMessage());
} else {
baseResponse.setCode(201);
baseResponse.setMessage(result.getMessage());
baseResponse.setData(result.getData().getMessage());
}
return baseResponse;
}
/**
* 取消执行
*
* @return
*/
@RequestMapping(value = "app_nodecancelbuild", method = RequestMethod.POST)
public BaseResponse cancelbuild(@RequestBody CancleBuild cancleBuild) {
BaseResponse baseResponse = new BaseResponse();
if (StringUtils.isBlank(cancleBuild.getMessageid())) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
Map<String, String> map = new HashMap<>();
map.put("id", cancleBuild.getMessageid());
String result = httpClient.get(ProjectEnvironment.getUrl(cancleBuild.getMessageid().split("_")[0]) + "cancelBuild", String.class, map);
baseResponse.setData(result);
return baseResponse;
}
}
... ...
package com.ui.ctrl.app;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ui.app.common.ErrorCode;
import com.ui.app.req.*;
import com.ui.app.resp.NodeBuildResp;
import com.ui.app.resp.NodeBuildState;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.domain.BuildMessage;
import com.ui.model.domain.PythonProject;
import com.ui.model.req.BuildRequest;
import com.ui.project.Project;
import com.ui.project.ProjectEnvironment;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Created by zhengyouwei on 2016/11/14.
* app 服务端,重写一个
*/
@RestController
@RequestMapping(value = "/app_phpBuild")
public class AppPhpBuildCtrl {
@Autowired
private HttpRestClient httpClient;
private String PHP_URL = "http://172.31.16.167:8883/php/";
/**
* 获取项目列表
*
* @return
*/
@RequestMapping(value = "app_getphpprojectlist", method = RequestMethod.POST)
public BaseResponse getphpprojectlist(@RequestBody AppBaseReq appBaseReq) {
BaseResponse baseResponse = new BaseResponse();
String result = httpClient.get(PHP_URL + "getProjects", String.class, null);
List<Project> projectList = JSONArray.parseArray(result,Project.class);
List<String> stringList = projectList.stream().map(Project::getName).collect(Collectors.toList());
baseResponse.setData(stringList);
return baseResponse;
}
/**
* 获取项目信息
*
* @return
*/
@RequestMapping(value = "app_getphpprojectinfo", method = RequestMethod.POST)
public BaseResponse getphpprojectinfo(@RequestBody ProjectInfo projectInfo) {
BaseResponse baseResponse = new BaseResponse();
if (StringUtils.isBlank(projectInfo.getProject())) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
PythonProject pythonProject = httpClient.get(PHP_URL + "getProject?name="+projectInfo.getProject() , PythonProject.class, null);
baseResponse.setData(pythonProject);
return baseResponse;
}
/**
* 获取回滚列表
*
* @return
*/
@RequestMapping(value = "app_getphprollbacklist", method = RequestMethod.POST)
public BaseResponse getphprollbacklist(@RequestBody RollbackList rollbackListReq) {
BaseResponse baseResponse = new BaseResponse();
if (StringUtils.isBlank(rollbackListReq.getProject()) || StringUtils.isBlank(rollbackListReq.getEnvironment())) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
Map<String, String> map = new HashMap<>();
map.put("project", rollbackListReq.getProject());
map.put("environment", rollbackListReq.getEnvironment());
String result = httpClient.get(PHP_URL + "rollbackList", String.class, map);
if ("failed".equals(result)) {//没有文件
baseResponse.setCode(ErrorCode.ROLLBACK_FILE_ERROR);
baseResponse.setMessage(ErrorCode.ROLLBACK_FILE_ERROR_MESSAGE);
return baseResponse;
}
baseResponse.setData(JSONArray.parse(result));
return baseResponse;
}
/**
* 获取回滚列表
*
* @return
*/
@RequestMapping(value = "app_phpbuild", method = RequestMethod.POST)
public BaseResponse build(@RequestBody ProjectBuild projectBuild) {
BaseResponse baseResponse = new BaseResponse();
String project = projectBuild.getProject();
String environment = projectBuild.getEnvironment();
String operate = projectBuild.getOperate();
String branch = projectBuild.getBranch();
String rollbackFile = projectBuild.getRollbackfile();
if (StringUtils.isBlank(project) || StringUtils.isBlank(environment) || StringUtils.isBlank(operate)) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
if ("Deploy".equals(operate)) {
if (StringUtils.isBlank(branch)) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
//check branch
if (!"master".equals(branch)) {
Map<String, String> map = new HashMap<>();
map.put("projects", project);
map.put("branch", branch);
String result = httpClient.get(ProjectEnvironment.getUrl(environment) + "checkBranch", String.class, map);
if (!"1".equals(result)) {
baseResponse.setCode(ErrorCode.BRANCH_ERROR);
baseResponse.setMessage(ErrorCode.BRANCH_ERROR_MESSAGE);
return baseResponse;
}
}
} else {
if (StringUtils.isBlank(rollbackFile)) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
}
BuildRequest buildRequest = new BuildRequest();
buildRequest.setBranch(branch);
buildRequest.setUser(projectBuild.getAccountname());
buildRequest.setEnvironment(environment);
buildRequest.setRollbackfile(rollbackFile);
buildRequest.setOperate(operate);
buildRequest.setProject(project);
buildRequest.setHost(projectBuild.getHosts());
BuildMessage buildMessage = httpClient.post(PHP_URL + "build", buildRequest, BuildMessage.class);
baseResponse.setData(buildMessage.getId());
return baseResponse;
}
/**
* 获取执行结果
*
* @return
*/
@RequestMapping(value = "app_getphpbuildmsg", method = RequestMethod.POST)
public BaseResponse getbuildmsg(@RequestBody Buildmsg buildmsg) {
BaseResponse baseResponse = new BaseResponse();
if (StringUtils.isBlank(buildmsg.getProject()) || StringUtils.isBlank(buildmsg.getMessageid())) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
Map<String, String> map = new HashMap<>();
map.put("messageid", buildmsg.getMessageid());
map.put("project", buildmsg.getProject());
BaseResponse<BuildMessage> result = httpClient.otherOxchangeForget(ProjectEnvironment.getUrl(buildmsg.getMessageid().split("_")[0]) + "getbuildmsg", new ParameterizedTypeReference<BaseResponse<BuildMessage>>() {
}, map);
if (result.getCode() == 1) {
baseResponse.setData(result.getData().getMessage());
} else {
baseResponse.setCode(201);
baseResponse.setMessage(result.getMessage());
baseResponse.setData(result.getData().getMessage());
}
return baseResponse;
}
/**
* 取消执行
*
* @return
*/
@RequestMapping(value = "app_phpcancelbuild", method = RequestMethod.POST)
public BaseResponse cancelbuild(@RequestBody CancleBuild cancleBuild) {
BaseResponse baseResponse = new BaseResponse();
if (StringUtils.isBlank(cancleBuild.getMessageid())) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
Map<String, String> map = new HashMap<>();
map.put("id", cancleBuild.getMessageid());
String result = httpClient.get(ProjectEnvironment.getUrl(cancleBuild.getMessageid().split("_")[0]) + "cancelBuild", String.class, map);
baseResponse.setData(result);
return baseResponse;
}
}
... ...
... ... @@ -193,116 +193,5 @@ public class AppProjectBuildCtrl {
return baseResponse;
}
private static final String PHP_URL = "http://node-ci.yohops.com/api/";
/**
* 获取项目列表
*
* @return
*/
@RequestMapping(value = "app_getnodeprojectlist", method = RequestMethod.POST)
public BaseResponse getnodeprojectlist(@RequestBody NodeProjectList nodeProjectList) {
BaseResponse baseResponse = new BaseResponse();
if (StringUtils.isBlank(nodeProjectList.getEnvironment())) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
String environment = nodeProjectList.getEnvironment();
if ("qcloud_gray".equals(environment)) {
environment = "qcloud";
}
String result = httpClient.get(PHP_URL + "projects?cloud=" + environment, String.class, null);
if (result == null) {
baseResponse.setCode(ErrorCode.SYSTEM_ERROR);
baseResponse.setMessage("fail to get node/php project list");
return baseResponse;
}
baseResponse.setData(JSONArray.parse(result));
return baseResponse;
}
/**
* 获取回滚列表
*
* @return
*/
@RequestMapping(value = "app_getnoderollbacklist", method = RequestMethod.POST)
public BaseResponse getnoderollbacklist(@RequestBody NodeRollbackList nodeRollbackList) {
BaseResponse baseResponse = new BaseResponse();
if (StringUtils.isBlank(nodeRollbackList.getEnvironment()) || StringUtils.isBlank(nodeRollbackList.getId())) {
baseResponse.setCode(ErrorCode.PARAM_ERROR);
baseResponse.setMessage(ErrorCode.PARAM_ERROR_MESSAGE);
return baseResponse;
}
String env = "production";
String environment = nodeRollbackList.getEnvironment();
if ("qcloud_gray".equals(environment)) {
env = "preview";
}
Map<String, Object> map = new HashMap<>();
map.put("id", nodeRollbackList.getId());
map.put("env", env);
String result = httpClient.get(PHP_URL + "builds", String.class, map);
if (result == null) {
baseResponse.setCode(ErrorCode.SYSTEM_ERROR);
baseResponse.setMessage("fail to get biuld list");
return baseResponse;
}
baseResponse.setData(JSONArray.parse(result));
return baseResponse;
}
/**
* 发布
*
* @return
*/
@RequestMapping(value = "app_nodebuild", method = RequestMethod.POST)
public BaseResponse nodebuild(@RequestBody NodeBuild nodeBuild) {
BaseResponse baseResponse = new BaseResponse();
Map<String, Object> map = new HashMap<>();
if ("Deploy".equals(nodeBuild.getOperate())) {
String env = "production";
String environment = nodeBuild.getEnvironment();
if ("qcloud_gray".equals(environment)) {
env = "preview";
}
map.put("id", nodeBuild.getId());
map.put("env", env);
map.put("branch", nodeBuild.getBranch());
NodeBuildResp result = httpClient.get(PHP_URL + "publish", NodeBuildResp.class, map);
baseResponse.setData(result.getBid());
} else {
map.put("id", nodeBuild.getRollbackfile());
NodeBuildResp result = httpClient.get(PHP_URL + "rollback", NodeBuildResp.class, map);
baseResponse.setData(result.getBid());
}
return baseResponse;
}
/**
* 状态查询
*
* @return
*/
@RequestMapping(value = "app_getnoderesult", method = RequestMethod.POST)
public BaseResponse getnoderesult(@RequestBody NodeMsg nodeMsg) {
BaseResponse baseResponse = new BaseResponse();
Map<String, Object> map = new HashMap<>();
map.put("bid", nodeMsg.getBuildNo());
NodeBuildState result = httpClient.get(PHP_URL + "state", NodeBuildState.class, map);
baseResponse.setData(result);
return baseResponse;
}
}
... ...