Authored by qinchao

dock发布功能

package com.monitor.model.request;
import lombok.Data;
/**
* jenkins发布镜像
*/
@Data
public class DockerJenkinsReq {
private String appNames;
private String branch;
private String nodeEnv;
private String userName;
private String userMail;
private String releaseWorkId;
}
... ...
package com.model;
import lombok.Data;
import java.util.Date;
@Data
public class DockerJenkinsRecord {
private String id;
private String projectName;
private String jenkinsJobId;
private String jenkinsBuildStatus;
private String ReleaseWorkId;
/**
* 创建时间
*/
private Date createTime;
}
... ...
package com.model;
import lombok.Data;
@Data
public class DockerProjectModel {
private int id;
//项目名称
private String projectName;
//项目类型
private String projectType;
private String dockerProjectName;
private String dockerGrayClusterId;
private String dockerGrayNameSpace;
private String dockerOnlineClusterId;
private String dockerOnlineNameSpace;
}
... ...
package com.monitor.mysql.mapper;
import com.model.DockerJenkinsRecord;
import com.model.DockerProjectModel;
import com.monitor.model.domain.PageBean;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Created by craig.qin
*/
public interface DockerJenkinsRecordMapper {
DockerJenkinsRecord selectById(String id);
int insert(DockerJenkinsRecord dockerJenkinsRecord);
int updateJob(@Param("id") String id ,@Param("jobId") String jobId,@Param("jobStatus")String jobStatus);
}
... ...
package com.monitor.mysql.mapper;
import com.model.DockerProjectModel;
import com.monitor.model.domain.PageBean;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Created by craig.qin
*/
public interface DockerProjectMapper {
int selectCount();
List<DockerProjectModel> selectByPage(PageBean page);
DockerProjectModel selectById(int id);
List<DockerProjectModel> selectByIdList(@Param("idList") List<Integer> idList);
List<DockerProjectModel> selectByProjectType(@Param("projectType") String projectType);
}
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.monitor.mysql.mapper.DockerJenkinsRecordMapper" >
<resultMap id="BaseResultMap" type="com.model.DockerJenkinsRecord" >
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="projectname" property="projectName" jdbcType="VARCHAR" />
<result column="jenkins_job_id" property="jenkinsJobId" jdbcType="VARCHAR" />
<result column="jenkins_build_status" property="jenkinsBuildStatus" jdbcType="VARCHAR" />
<result column="release_work_id" property="ReleaseWorkId" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
id, projectname, jenkins_job_id,jenkins_build_status,release_work_id,create_time
</sql>
<insert id="insert" parameterType="com.model.DockerJenkinsRecord" >
insert into docker_jenkins_record (id, projectname, release_work_id)
values (#{id,jdbcType=VARCHAR}, #{projectName,jdbcType=VARCHAR}, #{ReleaseWorkId,jdbcType=VARCHAR})
</insert>
<update id="updateJob" >
update docker_jenkins_record
set jenkins_job_id = #{jobId} ,jenkins_build_status = #{jobStatus}
where id = #{id}
</update>
</mapper>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.monitor.mysql.mapper.DockerProjectMapper" >
<resultMap id="BaseResultMap" type="com.model.DockerProjectModel" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="projectname" property="projectName" jdbcType="VARCHAR" />
<result column="projecttype" property="projectType" jdbcType="VARCHAR" />
<result column="dockerprojectname" property="dockerProjectName" jdbcType="VARCHAR" />
<result column="dockergrayclusterid" property="dockerGrayClusterId" jdbcType="VARCHAR" />
<result column="dockergraynamespace" property="dockerGrayNameSpace" jdbcType="VARCHAR" />
<result column="dockeronlineclusterid" property="dockerOnlineClusterId" jdbcType="VARCHAR" />
<result column="dockeronlinenamespace" property="dockerOnlineNameSpace" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, projectname,projecttype,dockerprojectName,dockergrayclusterid,dockergraynamespace,dockeronlineclusterid,dockeronlinenamespace
</sql>
<select id="selectCount" resultType="java.lang.Integer">
select
count(1)
from docker_project
</select>
<select id="selectByPage" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from docker_project
where 1=1
<if test="params.projectType != null" >
and projecttype = #{params.projectType}
</if>
order by id desc
limit #{startIndex},#{pageSize}
</select>
<select id="selectById" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from docker_project
where
id = #{id}
</select>
<select id="selectByProjectType" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from docker_project
where 1=1
<choose>
<when test="projectType != null &amp;&amp; projectType !='' " >
and projecttype = #{projectType}
</when>
<otherwise>
and 1=2
</otherwise>
</choose>
</select>
<select id="selectByIdList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from docker_project
where 1=1
<choose>
<when test="idList != null &amp;&amp; idList.size > 0" >
and
<foreach open="(" close=")" item="item" index="index" collection="idList" separator="or">
id = #{item}
</foreach>
</when>
<otherwise>
and 1=2
</otherwise>
</choose>
</select>
</mapper>
\ No newline at end of file
... ...
package com.monitor.other.worksystem.dock;
package com.monitor.other.worksystem.ctrl;
import com.alibaba.fastjson.JSONArray;
import com.model.ReleaseWorkJob;
import com.model.DockerProjectModel;
import com.monitor.model.request.DockerJenkinsReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.other.worksystem.dock.qq.DockerServerDeployService;
import com.monitor.other.worksystem.service.ReleaseService;
import com.monitor.other.worksystem.service.DockerProjectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/work/docker")
public class DockerServerDeployCtrl {
@RequestMapping("/dockerProject")
public class DockerProjectCtrl {
@Autowired
private ReleaseService releaseService;
private DockerProjectService dockerProjectService;
@Autowired
private DockerServerDeployService dockerServerDeployService;
/**
* 查询docker项目
*/
@RequestMapping("/getDockerProjectByType")
@ResponseBody
public BaseResponse<List<DockerProjectModel>> getDockerProjectByType(String projectType) {
return new BaseResponse<>(dockerProjectService.selectByProjectType(projectType));
}
/**
* 查询docker项目
*/
@RequestMapping("/getDockerProjectByIds")
@ResponseBody
public BaseResponse<List<DockerProjectModel>> getDockerProjectByIds(String ids) {
List<Integer> idList=new ArrayList<>();
for(String id:ids.split(",")){
if(StringUtils.isNotBlank(id)){
idList.add(Integer.parseInt(id.trim()));
}
}
return new BaseResponse<>(dockerProjectService.selectByIdList(idList));
}
/**
* 发布工单_jenkins:发起镜像制作
*/
@RequestMapping("/createMirror")
@ResponseBody
public BaseResponse createMirror() {
ReleaseWorkJob releaseWorkJob=new ReleaseWorkJob();
int jobId=releaseService.jenkinsBuild(releaseWorkJob);
public BaseResponse createMirror(@RequestBody DockerJenkinsReq dockerJenkinsReq) {
String id=dockerProjectService.jenkinsBuild(dockerJenkinsReq);
BaseResponse rnt=new BaseResponse();
if(id==null||id.length()<=0){
rnt.setCode(201);
rnt.setMessage("请求镜像失败,返回jobId = " +id);
}else{
rnt.setData(id);
}
return rnt;
}
/**
* 发布工单_jenkins:更新镜像的状态
*/
@RequestMapping("/updateJenkinsStatus")
@ResponseBody
public BaseResponse updateJenkinsStatus(String ticketID,String jobID,String status) {
dockerProjectService.jenkinsStatusUpdate(ticketID,jobID,status);
BaseResponse rnt=new BaseResponse();
rnt.setData(jobId);
return rnt;
}
/**
* 发布工单_jenkins:更新镜像的状态
*/
@RequestMapping("/queryJenkinsStatus")
@ResponseBody
public BaseResponse queryJenkinsStatus(String ticketID,String jobID,String status) {
dockerProjectService.jenkinsStatusUpdate(ticketID,jobID,status);
BaseResponse rnt=new BaseResponse();
return rnt;
}
/**
* 发布docker服务
*/
@RequestMapping("/deployMirror")
... ...
package com.monitor.other.worksystem.dock;
import com.model.ReleaseWorkJob;
import com.monitor.model.response.BaseResponse;
import com.monitor.other.worksystem.service.ReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -14,15 +13,6 @@ public class JenkinsCtrl {
@Autowired
private ReleaseService releaseService;
/**
* 发布工单_jenkins:更新镜像的状态
*/
@RequestMapping("/updateStatus")
@ResponseBody
public BaseResponse updateStatus(String ticketID,String status,String jobID) {
BaseResponse rnt=new BaseResponse();
rnt.setData(releaseService.jenkinsStatusUpdate(ticketID,status,jobID));
return rnt;
}
}
... ...
package com.monitor.other.worksystem.service;
import com.model.DockerProjectModel;
import com.model.PendingJob;
import com.monitor.model.request.DockerJenkinsReq;
import com.monitor.model.request.WorkSystemReq;
import com.monitor.model.response.PageResponse;
import java.util.List;
/**
* Created by craig.qin
*/
public interface DockerProjectService {
List<DockerProjectModel> selectByIdList(List<Integer> idList);
List<DockerProjectModel> selectByProjectType(String projectType);
String jenkinsBuild(DockerJenkinsReq dockerJenkinsReq);
void jenkinsStatusUpdate(String ticketID,String jobID,String status);
String queryJenkinsStatus(String id);
}
... ...
... ... @@ -27,8 +27,4 @@ public interface ReleaseService {
int updateByTestId(String testId, String testMessage);
int jenkinsBuild(ReleaseWorkJob releaseWorkJob);
int jenkinsStatusUpdate(String ticketID,String status,String jobID);
}
... ...
package com.monitor.other.worksystem.service.impl;
import com.model.*;
import com.monitor.common.service.MailService;
import com.monitor.model.domain.Mail;
import com.monitor.model.request.DockerJenkinsReq;
import com.monitor.mysql.mapper.*;
import com.monitor.other.worksystem.contants.HandleType;
import com.monitor.other.worksystem.contants.HandlerRole;
import com.monitor.other.worksystem.contants.WorkStatus;
import com.monitor.other.worksystem.contants.WorkType;
import com.monitor.other.worksystem.dock.FastJenkinsUtils;
import com.monitor.other.worksystem.service.DBService;
import com.monitor.other.worksystem.service.DockerProjectService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* Created by zhengyouwei on 2016/9/2.
*/
@Service
public class DockerProjectServiceImpl implements DockerProjectService {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private DockerProjectMapper dockerProjectMapper;
@Autowired
private DockerJenkinsRecordMapper dockerJenkinsRecordMapper;
public List<DockerProjectModel> selectByIdList(List<Integer> idList){
if(idList==null||idList.size()<=0){
return new ArrayList<>();
}
return dockerProjectMapper.selectByIdList(idList);
}
public List<DockerProjectModel> selectByProjectType(String projectType){
if(StringUtils.isBlank(projectType)){
return new ArrayList<>();
}
return dockerProjectMapper.selectByProjectType(projectType);
}
@Override
public String jenkinsBuild(DockerJenkinsReq dockerJenkinsReq){
String appNames=dockerJenkinsReq.getAppNames();
String uuid = UUID.randomUUID().toString();
uuid=appNames+"-"+uuid.replace("-", "");
Map params= new HashMap();
params.put("ticketID", uuid);
params.put("appNames",appNames);
params.put("branch",dockerJenkinsReq.getBranch());
params.put("nodeEnv",dockerJenkinsReq.getNodeEnv());
params.put("userMail",dockerJenkinsReq.getUserMail());
try{
DockerJenkinsRecord record=new DockerJenkinsRecord();
record.setId(uuid);
record.setProjectName(dockerJenkinsReq.getAppNames());
record.setReleaseWorkId(dockerJenkinsReq.getReleaseWorkId());
dockerJenkinsRecordMapper.insert(record);
FastJenkinsUtils fastJenkinsUtils=new FastJenkinsUtils();
fastJenkinsUtils.createJob(FastJenkinsUtils.JENKINS_JOB_NODE,params);
}catch (Exception e){
logger.error("jenkinsBuild error",e);
return "";
}
return uuid;
}
@Override
public void jenkinsStatusUpdate(String ticketID,String jobID,String status){
dockerJenkinsRecordMapper.updateJob(ticketID,jobID,status);
}
@Override
public String queryJenkinsStatus(String id){
dockerJenkinsRecordMapper.s
}
}
... ...
... ... @@ -15,16 +15,12 @@ import com.monitor.other.worksystem.contants.HandleType;
import com.monitor.other.worksystem.contants.HandlerRole;
import com.monitor.other.worksystem.contants.WorkStatus;
import com.monitor.other.worksystem.contants.WorkType;
import com.monitor.other.worksystem.dock.FastJenkinsUtils;
import com.monitor.other.worksystem.service.ReleaseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* Created by zhengyouwei on 2016/9/2.
... ... @@ -267,35 +263,4 @@ public class ReleaseServiceImpl implements ReleaseService {
public int updateByTestId(String testId, String testMessage) {
return releaseWorkJobMapper.updateByTestId(testId, testMessage);
}
@Override
public int jenkinsBuild(ReleaseWorkJob releaseWorkJob){
releaseWorkJob.setId("release0004377");
Map params= new HashMap();
params.put("appNames","yohoblk-wap");
params.put("branch","feature/docker");
params.put("nodeEnv","ONLINE-QCLOUD");
params.put("userMail","craig.qin@yoho.cn");
String uuid = UUID.randomUUID().toString();
uuid=params.get("appNames")+"-"+uuid.replace("-", "");
params.put("ticketID", uuid);
//更新发布工单的jenkins_build_id
releaseWorkJobMapper.updateJenkinsBuildId(releaseWorkJob);
FastJenkinsUtils fastJenkinsUtils=new FastJenkinsUtils();
int jobId=fastJenkinsUtils.createJob(FastJenkinsUtils.JENKINS_JOB_NODE,params);
return jobId;
}
@Override
public int jenkinsStatusUpdate(String ticketID,String status,String jobID){
ReleaseWorkJob releaseWorkJob=new ReleaseWorkJob();
releaseWorkJob.setJenkinsBuildId(ticketID);
releaseWorkJob.setJenkinsBuildStatus(status);
releaseWorkJob.setJenkinsJobId(jobID);
return releaseWorkJobMapper.updateJenkinsBuildStatus(releaseWorkJob);
}
}
... ...