Showing
8 changed files
with
194 additions
and
1 deletions
@@ -95,4 +95,10 @@ public class ReleaseWorkJob { | @@ -95,4 +95,10 @@ public class ReleaseWorkJob { | ||
95 | 95 | ||
96 | private String updateTime; | 96 | private String updateTime; |
97 | 97 | ||
98 | + /** | ||
99 | + * jenkins发布id和会写状态信息 | ||
100 | + */ | ||
101 | + private String jenkinsBuildId; | ||
102 | + private String jenkinsBuildStatus; | ||
103 | + | ||
98 | } | 104 | } |
@@ -32,4 +32,7 @@ public interface ReleaseWorkJobMapper { | @@ -32,4 +32,7 @@ public interface ReleaseWorkJobMapper { | ||
32 | List<ReleaseWorkJob> selectForAlarm(); | 32 | List<ReleaseWorkJob> selectForAlarm(); |
33 | 33 | ||
34 | int updateByTestId(@Param("testId") String testId, @Param("testMessage") String testMessage); | 34 | int updateByTestId(@Param("testId") String testId, @Param("testMessage") String testMessage); |
35 | + | ||
36 | + int updateJenkinsBuildId(ReleaseWorkJob releaseWorkJob); | ||
37 | + int updateJenkinsBuildStatus(ReleaseWorkJob releaseWorkJob); | ||
35 | } | 38 | } |
@@ -21,10 +21,13 @@ | @@ -21,10 +21,13 @@ | ||
21 | <result column="createTime" property="createTime" jdbcType="VARCHAR" /> | 21 | <result column="createTime" property="createTime" jdbcType="VARCHAR" /> |
22 | <result column="updateTime" property="updateTime" jdbcType="VARCHAR" /> | 22 | <result column="updateTime" property="updateTime" jdbcType="VARCHAR" /> |
23 | 23 | ||
24 | + <result column="jenkins_build_id" property="jenkinsBuildId" jdbcType="VARCHAR" /> | ||
25 | + <result column="jenkins_build_status" property="jenkinsBuildStatus" jdbcType="VARCHAR" /> | ||
26 | + | ||
24 | </resultMap> | 27 | </resultMap> |
25 | 28 | ||
26 | <sql id="Base_Column_List" > | 29 | <sql id="Base_Column_List" > |
27 | - 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 | 30 | + 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 |
28 | </sql> | 31 | </sql> |
29 | 32 | ||
30 | <select id="selectCount" resultType="java.lang.Integer"> | 33 | <select id="selectCount" resultType="java.lang.Integer"> |
@@ -150,4 +153,22 @@ | @@ -150,4 +153,22 @@ | ||
150 | where | 153 | where |
151 | testId = #{testId,jdbcType=VARCHAR} | 154 | testId = #{testId,jdbcType=VARCHAR} |
152 | </update> | 155 | </update> |
156 | + | ||
157 | + <update id="updateJenkinsBuildId" parameterType="com.model.ReleaseWorkJob"> | ||
158 | + UPDATE release_work_job | ||
159 | + set | ||
160 | + jenkins_build_id = #{jenkinsBuildId,jdbcType=VARCHAR} | ||
161 | + where | ||
162 | + id = #{id,jdbcType=VARCHAR} | ||
163 | + </update> | ||
164 | + | ||
165 | + <update id="updateJenkinsBuildStatus" parameterType="com.model.ReleaseWorkJob"> | ||
166 | + UPDATE release_work_job | ||
167 | + set | ||
168 | + jenkins_build_status = #{jenkinsBuildStatus,jdbcType=VARCHAR} | ||
169 | + where | ||
170 | + jenkins_build_id = #{jenkinsBuildId,jdbcType=VARCHAR} | ||
171 | + </update> | ||
172 | + | ||
173 | + | ||
153 | </mapper> | 174 | </mapper> |
@@ -15,6 +15,12 @@ | @@ -15,6 +15,12 @@ | ||
15 | <dependencies> | 15 | <dependencies> |
16 | 16 | ||
17 | <dependency> | 17 | <dependency> |
18 | + <groupId>com.offbytwo.jenkins</groupId> | ||
19 | + <artifactId>jenkins-client</artifactId> | ||
20 | + <version>0.3.7</version> | ||
21 | + </dependency> | ||
22 | + | ||
23 | + <dependency> | ||
18 | <groupId>monitor-service</groupId> | 24 | <groupId>monitor-service</groupId> |
19 | <artifactId>monitor-service-common</artifactId> | 25 | <artifactId>monitor-service-common</artifactId> |
20 | </dependency> | 26 | </dependency> |
monitor-service-other/src/main/java/com/monitor/other/worksystem/dock/FastJenkinsUtils.java
0 → 100644
1 | +package com.monitor.other.worksystem.dock; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSON; | ||
4 | +import com.offbytwo.jenkins.JenkinsServer; | ||
5 | +import com.offbytwo.jenkins.model.Build; | ||
6 | +import com.offbytwo.jenkins.model.Job; | ||
7 | +import com.offbytwo.jenkins.model.JobWithDetails; | ||
8 | +import com.offbytwo.jenkins.model.QueueItem; | ||
9 | + | ||
10 | +import java.net.URI; | ||
11 | +import java.util.Map; | ||
12 | + | ||
13 | + | ||
14 | +public class FastJenkinsUtils { | ||
15 | + public final static String JENKINS_JOB_JAVA="Docker-Deploy-Java"; | ||
16 | + public final static String JENKINS_JOB_NODE="Docker-Deploy-Node"; | ||
17 | + | ||
18 | + private final String JENKINS_SERVER_URI="http://140.143.118.227/"; | ||
19 | + private final String JENKINS_SERVER_USER="yoho"; | ||
20 | + private final String JENKINS_SERVER_PWD="yoho9646"; | ||
21 | + | ||
22 | + /** | ||
23 | + * 发起一个job | ||
24 | + * buildNumber 再并发请求的状态,返回的值是一样的,因此不能作为 | ||
25 | + */ | ||
26 | + public int createJob(String jobName,Map<String, String> params) { | ||
27 | + | ||
28 | + int buildNumber = 0; | ||
29 | + try{ | ||
30 | + JenkinsServer jenkinsServer = new JenkinsServer(new URI(JENKINS_SERVER_URI), JENKINS_SERVER_USER, JENKINS_SERVER_PWD); | ||
31 | + | ||
32 | + Map<String, Job> jobs = jenkinsServer.getJobs(); | ||
33 | + | ||
34 | + JobWithDetails job = jobs.get(jobName).details(); | ||
35 | + | ||
36 | + // 即将执行任务的jobId | ||
37 | + buildNumber = job.getNextBuildNumber(); | ||
38 | + // 参数化构建 | ||
39 | + job.build(params); | ||
40 | + }catch (Exception e){ | ||
41 | + e.printStackTrace(); | ||
42 | + } finally { | ||
43 | + | ||
44 | + } | ||
45 | + return buildNumber; | ||
46 | + } | ||
47 | + | ||
48 | + | ||
49 | + /** | ||
50 | + * 查看job是否执行结束 | ||
51 | + */ | ||
52 | + /*public boolean isJobFinish(String jobName,int jobId) { | ||
53 | + if(jobId <=0){ | ||
54 | + throw new IllegalArgumentException("jodId must greater than 0!"); | ||
55 | + } | ||
56 | + try{ | ||
57 | + JenkinsServer jenkinsServer = new JenkinsServer(new URI(JENKINS_SERVER_URI), JENKINS_SERVER_USER, JENKINS_SERVER_PWD); | ||
58 | + Map<String, Job> jobs = jenkinsServer.getJobs(); | ||
59 | + JobWithDetails job = jobs.get(jobName).details(); | ||
60 | + QueueItem time= job.getQueueItem(); | ||
61 | + System.out.println(JSON.toJSONString(time)); | ||
62 | + | ||
63 | + Build build=job.getBuildByNumber(jobId); | ||
64 | + boolean isBuilding=false; | ||
65 | + if(build!=null){ | ||
66 | + isBuilding=build.details().isBuilding(); | ||
67 | + } | ||
68 | + return !isBuilding; | ||
69 | + | ||
70 | + } catch (Exception e){ | ||
71 | + e.printStackTrace(); | ||
72 | + } finally{ | ||
73 | + } | ||
74 | + return false; | ||
75 | + }*/ | ||
76 | +} |
1 | +package com.monitor.other.worksystem.dock; | ||
2 | + | ||
3 | +import com.model.ReleaseWorkJob; | ||
4 | +import com.monitor.model.response.BaseResponse; | ||
5 | +import com.monitor.other.worksystem.service.ReleaseService; | ||
6 | +import org.springframework.beans.factory.annotation.Autowired; | ||
7 | +import org.springframework.stereotype.Controller; | ||
8 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
9 | +import org.springframework.web.bind.annotation.ResponseBody; | ||
10 | + | ||
11 | +@Controller | ||
12 | +@RequestMapping("/work/jenkins") | ||
13 | +public class JenkinsCtrl { | ||
14 | + @Autowired | ||
15 | + private ReleaseService releaseService; | ||
16 | + | ||
17 | + | ||
18 | + /** | ||
19 | + * 发布工单_jenkins:发起镜像制作 | ||
20 | + */ | ||
21 | + @RequestMapping("/createMirror") | ||
22 | + @ResponseBody | ||
23 | + public BaseResponse createMirror() { | ||
24 | + ReleaseWorkJob releaseWorkJob=new ReleaseWorkJob(); | ||
25 | + int jobId=releaseService.jenkinsBuild(releaseWorkJob); | ||
26 | + BaseResponse rnt=new BaseResponse(); | ||
27 | + rnt.setData(jobId); | ||
28 | + System.out.println("jobId is "+jobId); | ||
29 | + return rnt; | ||
30 | + } | ||
31 | + | ||
32 | + /** | ||
33 | + * 发布工单_jenkins:更新镜像的状态 | ||
34 | + */ | ||
35 | + @RequestMapping("/updateStatus") | ||
36 | + @ResponseBody | ||
37 | + public BaseResponse updateStatus(String ticketID,String status) { | ||
38 | + BaseResponse rnt=new BaseResponse(); | ||
39 | + rnt.setData(releaseService.jenkinsStatusUpdate(ticketID,status)); | ||
40 | + return rnt; | ||
41 | + } | ||
42 | + | ||
43 | +} |
@@ -27,4 +27,8 @@ public interface ReleaseService { | @@ -27,4 +27,8 @@ public interface ReleaseService { | ||
27 | 27 | ||
28 | int updateByTestId(String testId, String testMessage); | 28 | int updateByTestId(String testId, String testMessage); |
29 | 29 | ||
30 | + int jenkinsBuild(ReleaseWorkJob releaseWorkJob); | ||
31 | + | ||
32 | + int jenkinsStatusUpdate(String ticketID,String status); | ||
33 | + | ||
30 | } | 34 | } |
@@ -15,12 +15,16 @@ import com.monitor.other.worksystem.contants.HandleType; | @@ -15,12 +15,16 @@ import com.monitor.other.worksystem.contants.HandleType; | ||
15 | import com.monitor.other.worksystem.contants.HandlerRole; | 15 | import com.monitor.other.worksystem.contants.HandlerRole; |
16 | import com.monitor.other.worksystem.contants.WorkStatus; | 16 | import com.monitor.other.worksystem.contants.WorkStatus; |
17 | import com.monitor.other.worksystem.contants.WorkType; | 17 | import com.monitor.other.worksystem.contants.WorkType; |
18 | +import com.monitor.other.worksystem.dock.FastJenkinsUtils; | ||
18 | import com.monitor.other.worksystem.service.ReleaseService; | 19 | import com.monitor.other.worksystem.service.ReleaseService; |
19 | import org.apache.commons.lang.StringUtils; | 20 | import org.apache.commons.lang.StringUtils; |
20 | import org.springframework.beans.factory.annotation.Autowired; | 21 | import org.springframework.beans.factory.annotation.Autowired; |
21 | import org.springframework.stereotype.Service; | 22 | import org.springframework.stereotype.Service; |
22 | 23 | ||
24 | +import java.util.HashMap; | ||
23 | import java.util.List; | 25 | import java.util.List; |
26 | +import java.util.Map; | ||
27 | +import java.util.UUID; | ||
24 | 28 | ||
25 | /** | 29 | /** |
26 | * Created by zhengyouwei on 2016/9/2. | 30 | * Created by zhengyouwei on 2016/9/2. |
@@ -263,4 +267,34 @@ public class ReleaseServiceImpl implements ReleaseService { | @@ -263,4 +267,34 @@ public class ReleaseServiceImpl implements ReleaseService { | ||
263 | public int updateByTestId(String testId, String testMessage) { | 267 | public int updateByTestId(String testId, String testMessage) { |
264 | return releaseWorkJobMapper.updateByTestId(testId, testMessage); | 268 | return releaseWorkJobMapper.updateByTestId(testId, testMessage); |
265 | } | 269 | } |
270 | + | ||
271 | + @Override | ||
272 | + public int jenkinsBuild(ReleaseWorkJob releaseWorkJob){ | ||
273 | + releaseWorkJob.setId("release0004377"); | ||
274 | + | ||
275 | + Map params= new HashMap(); | ||
276 | + params.put("appNames","yohoblk-wap"); | ||
277 | + params.put("branch","feature/docker"); | ||
278 | + params.put("nodeEnv","ONLINE-QCLOUD"); | ||
279 | + params.put("userMail","craig.qin@yoho.cn"); | ||
280 | + | ||
281 | + String uuid = UUID.randomUUID().toString(); | ||
282 | + uuid=params.get("appNames")+"-"+uuid.replace("-", ""); | ||
283 | + params.put("ticketID", uuid); | ||
284 | + | ||
285 | + //更新发布工单的jenkins_build_id | ||
286 | + releaseWorkJobMapper.updateJenkinsBuildId(releaseWorkJob); | ||
287 | + | ||
288 | + FastJenkinsUtils fastJenkinsUtils=new FastJenkinsUtils(); | ||
289 | + int jobId=fastJenkinsUtils.createJob(FastJenkinsUtils.JENKINS_JOB_NODE,params); | ||
290 | + return jobId; | ||
291 | + } | ||
292 | + | ||
293 | + @Override | ||
294 | + public int jenkinsStatusUpdate(String ticketID,String status){ | ||
295 | + ReleaseWorkJob releaseWorkJob=new ReleaseWorkJob(); | ||
296 | + releaseWorkJob.setJenkinsBuildId(ticketID); | ||
297 | + releaseWorkJob.setJenkinsBuildStatus(status); | ||
298 | + return releaseWorkJobMapper.updateJenkinsBuildStatus(releaseWorkJob); | ||
299 | + } | ||
266 | } | 300 | } |
-
Please register or login to post a comment