Authored by qinchao

docker发布

... ... @@ -95,4 +95,10 @@ public class ReleaseWorkJob {
private String updateTime;
/**
* jenkins发布id和会写状态信息
*/
private String jenkinsBuildId;
private String jenkinsBuildStatus;
}
... ...
... ... @@ -32,4 +32,7 @@ public interface ReleaseWorkJobMapper {
List<ReleaseWorkJob> selectForAlarm();
int updateByTestId(@Param("testId") String testId, @Param("testMessage") String testMessage);
int updateJenkinsBuildId(ReleaseWorkJob releaseWorkJob);
int updateJenkinsBuildStatus(ReleaseWorkJob releaseWorkJob);
}
... ...
... ... @@ -21,10 +21,13 @@
<result column="createTime" property="createTime" jdbcType="VARCHAR" />
<result column="updateTime" property="updateTime" jdbcType="VARCHAR" />
<result column="jenkins_build_id" property="jenkinsBuildId" jdbcType="VARCHAR" />
<result column="jenkins_build_status" property="jenkinsBuildStatus" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, title,demand,environment,platform,project,branch,testId,testMessage,develop,test,operator,status,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%S') AS createTime,DATE_FORMAT(update_time,'%Y-%m-%d %H:%i:%S') AS updateTime
id, title,demand,environment,platform,project,branch,testId,testMessage,develop,test,operator,status,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%S') AS createTime,DATE_FORMAT(update_time,'%Y-%m-%d %H:%i:%S') AS updateTime,jenkins_build_id,jenkins_build_status
</sql>
<select id="selectCount" resultType="java.lang.Integer">
... ... @@ -150,4 +153,22 @@
where
testId = #{testId,jdbcType=VARCHAR}
</update>
<update id="updateJenkinsBuildId" parameterType="com.model.ReleaseWorkJob">
UPDATE release_work_job
set
jenkins_build_id = #{jenkinsBuildId,jdbcType=VARCHAR}
where
id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateJenkinsBuildStatus" parameterType="com.model.ReleaseWorkJob">
UPDATE release_work_job
set
jenkins_build_status = #{jenkinsBuildStatus,jdbcType=VARCHAR}
where
jenkins_build_id = #{jenkinsBuildId,jdbcType=VARCHAR}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -15,6 +15,12 @@
<dependencies>
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.7</version>
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-common</artifactId>
</dependency>
... ...
package com.monitor.other.worksystem.dock;
import com.alibaba.fastjson.JSON;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.Build;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobWithDetails;
import com.offbytwo.jenkins.model.QueueItem;
import java.net.URI;
import java.util.Map;
public class FastJenkinsUtils {
public final static String JENKINS_JOB_JAVA="Docker-Deploy-Java";
public final static String JENKINS_JOB_NODE="Docker-Deploy-Node";
private final String JENKINS_SERVER_URI="http://140.143.118.227/";
private final String JENKINS_SERVER_USER="yoho";
private final String JENKINS_SERVER_PWD="yoho9646";
/**
* 发起一个job
* buildNumber 再并发请求的状态,返回的值是一样的,因此不能作为
*/
public int createJob(String jobName,Map<String, String> params) {
int buildNumber = 0;
try{
JenkinsServer jenkinsServer = new JenkinsServer(new URI(JENKINS_SERVER_URI), JENKINS_SERVER_USER, JENKINS_SERVER_PWD);
Map<String, Job> jobs = jenkinsServer.getJobs();
JobWithDetails job = jobs.get(jobName).details();
// 即将执行任务的jobId
buildNumber = job.getNextBuildNumber();
// 参数化构建
job.build(params);
}catch (Exception e){
e.printStackTrace();
} finally {
}
return buildNumber;
}
/**
* 查看job是否执行结束
*/
/*public boolean isJobFinish(String jobName,int jobId) {
if(jobId <=0){
throw new IllegalArgumentException("jodId must greater than 0!");
}
try{
JenkinsServer jenkinsServer = new JenkinsServer(new URI(JENKINS_SERVER_URI), JENKINS_SERVER_USER, JENKINS_SERVER_PWD);
Map<String, Job> jobs = jenkinsServer.getJobs();
JobWithDetails job = jobs.get(jobName).details();
QueueItem time= job.getQueueItem();
System.out.println(JSON.toJSONString(time));
Build build=job.getBuildByNumber(jobId);
boolean isBuilding=false;
if(build!=null){
isBuilding=build.details().isBuilding();
}
return !isBuilding;
} catch (Exception e){
e.printStackTrace();
} finally{
}
return false;
}*/
}
... ...
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;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/work/jenkins")
public class JenkinsCtrl {
@Autowired
private ReleaseService releaseService;
/**
* 发布工单_jenkins:发起镜像制作
*/
@RequestMapping("/createMirror")
@ResponseBody
public BaseResponse createMirror() {
ReleaseWorkJob releaseWorkJob=new ReleaseWorkJob();
int jobId=releaseService.jenkinsBuild(releaseWorkJob);
BaseResponse rnt=new BaseResponse();
rnt.setData(jobId);
System.out.println("jobId is "+jobId);
return rnt;
}
/**
* 发布工单_jenkins:更新镜像的状态
*/
@RequestMapping("/updateStatus")
@ResponseBody
public BaseResponse updateStatus(String ticketID,String status) {
BaseResponse rnt=new BaseResponse();
rnt.setData(releaseService.jenkinsStatusUpdate(ticketID,status));
return rnt;
}
}
... ...
... ... @@ -27,4 +27,8 @@ public interface ReleaseService {
int updateByTestId(String testId, String testMessage);
int jenkinsBuild(ReleaseWorkJob releaseWorkJob);
int jenkinsStatusUpdate(String ticketID,String status);
}
... ...
... ... @@ -15,12 +15,16 @@ 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.
... ... @@ -263,4 +267,34 @@ 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){
ReleaseWorkJob releaseWorkJob=new ReleaseWorkJob();
releaseWorkJob.setJenkinsBuildId(ticketID);
releaseWorkJob.setJenkinsBuildStatus(status);
return releaseWorkJobMapper.updateJenkinsBuildStatus(releaseWorkJob);
}
}
... ...