Merge branch 'dev_dock_release'
Showing
19 changed files
with
1106 additions
and
16 deletions
@@ -2,12 +2,7 @@ package com.monitor.javaserver.schedule; | @@ -2,12 +2,7 @@ package com.monitor.javaserver.schedule; | ||
2 | 2 | ||
3 | import com.alibaba.fastjson.JSON; | 3 | import com.alibaba.fastjson.JSON; |
4 | import com.alibaba.fastjson.JSONObject; | 4 | import com.alibaba.fastjson.JSONObject; |
5 | -import com.contants.AlarmGroupContants; | ||
6 | -import com.monitor.common.config.SnsMobileConfig; | ||
7 | import com.monitor.common.service.AlarmMsgService; | 5 | import com.monitor.common.service.AlarmMsgService; |
8 | -import com.monitor.common.util.HttpRestClient; | ||
9 | -import com.monitor.javaserver.bigdata.Vdata; | ||
10 | -import com.monitor.javaserver.bigdata.ctrl.MonitAlarmCtrl; | ||
11 | import com.util.GetUsersInfoUtil; | 6 | import com.util.GetUsersInfoUtil; |
12 | import org.slf4j.Logger; | 7 | import org.slf4j.Logger; |
13 | import org.slf4j.LoggerFactory; | 8 | import org.slf4j.LoggerFactory; |
@@ -19,8 +14,6 @@ import org.springframework.web.util.UriComponentsBuilder; | @@ -19,8 +14,6 @@ import org.springframework.web.util.UriComponentsBuilder; | ||
19 | 14 | ||
20 | import javax.annotation.Resource; | 15 | import javax.annotation.Resource; |
21 | import java.net.URI; | 16 | import java.net.URI; |
22 | -import java.util.HashMap; | ||
23 | -import java.util.Map; | ||
24 | 17 | ||
25 | /** | 18 | /** |
26 | * Created by craig.qin on 2017/10/16. | 19 | * Created by craig.qin on 2017/10/16. |
1 | +package com.monitor.model.request; | ||
2 | + | ||
3 | + | ||
4 | +import lombok.Data; | ||
5 | + | ||
6 | +/** | ||
7 | + * jenkins发布镜像 | ||
8 | + */ | ||
9 | +@Data | ||
10 | +public class DockerJenkinsReq { | ||
11 | + private String appNames; | ||
12 | + private String branch; | ||
13 | + private String nodeEnv; | ||
14 | + private String userName; | ||
15 | + private String userMail; | ||
16 | + private String releaseWorkId; | ||
17 | + private String projectType; | ||
18 | + | ||
19 | + //docker qlcoud | ||
20 | + private String clusterId; | ||
21 | + private String namespace; | ||
22 | + private String serviceName; | ||
23 | +} |
1 | +package com.model; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +import java.util.Date; | ||
6 | + | ||
7 | +@Data | ||
8 | +public class DockerJenkinsRecord { | ||
9 | + private String id; | ||
10 | + private String projectName; | ||
11 | + private String jenkinsJobId; | ||
12 | + private String jenkinsBuildStatus; | ||
13 | + private String ReleaseWorkId; | ||
14 | + /** | ||
15 | + * 创建时间 | ||
16 | + */ | ||
17 | + private Date createTime; | ||
18 | +} |
1 | +package com.model; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +@Data | ||
6 | +public class DockerProjectModel { | ||
7 | + private int id; | ||
8 | + //项目名称 | ||
9 | + private String projectName; | ||
10 | + | ||
11 | + //项目类型 | ||
12 | + private String projectType; | ||
13 | + | ||
14 | + private String dockerProjectName; | ||
15 | + | ||
16 | + private String dockerGrayClusterId; | ||
17 | + private String dockerGrayNameSpace; | ||
18 | + | ||
19 | + private String dockerOnlineClusterId; | ||
20 | + private String dockerOnlineNameSpace; | ||
21 | + | ||
22 | +} |
monitor-service-mysql/src/main/java/com/monitor/mysql/mapper/DockerJenkinsRecordMapper.java
0 → 100644
1 | +package com.monitor.mysql.mapper; | ||
2 | + | ||
3 | +import com.model.DockerJenkinsRecord; | ||
4 | +import com.model.DockerProjectModel; | ||
5 | +import com.monitor.model.domain.PageBean; | ||
6 | +import org.apache.ibatis.annotations.Param; | ||
7 | + | ||
8 | +import java.util.List; | ||
9 | + | ||
10 | +/** | ||
11 | + * Created by craig.qin | ||
12 | + */ | ||
13 | +public interface DockerJenkinsRecordMapper { | ||
14 | + DockerJenkinsRecord selectById(@Param("id") String id); | ||
15 | + int insert(DockerJenkinsRecord dockerJenkinsRecord); | ||
16 | + int updateJob(@Param("id") String id ,@Param("jobId") String jobId,@Param("jobStatus")String jobStatus); | ||
17 | +} |
1 | +package com.monitor.mysql.mapper; | ||
2 | + | ||
3 | +import com.model.DockerProjectModel; | ||
4 | +import com.monitor.model.domain.PageBean; | ||
5 | +import org.apache.ibatis.annotations.Param; | ||
6 | + | ||
7 | +import java.util.List; | ||
8 | + | ||
9 | +/** | ||
10 | + * Created by craig.qin | ||
11 | + */ | ||
12 | +public interface DockerProjectMapper { | ||
13 | + int selectCount(); | ||
14 | + | ||
15 | + List<DockerProjectModel> selectByPage(PageBean page); | ||
16 | + | ||
17 | + DockerProjectModel selectById(int id); | ||
18 | + | ||
19 | + List<DockerProjectModel> selectByIdList(@Param("idList") List<Integer> idList); | ||
20 | + | ||
21 | + List<DockerProjectModel> selectByProjectType(@Param("projectType") String projectType); | ||
22 | +} |
1 | +<?xml version="1.0" encoding="UTF-8" ?> | ||
2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > | ||
3 | +<mapper namespace="com.monitor.mysql.mapper.DockerJenkinsRecordMapper" > | ||
4 | + <resultMap id="BaseResultMap" type="com.model.DockerJenkinsRecord" > | ||
5 | + <id column="id" property="id" jdbcType="VARCHAR" /> | ||
6 | + <result column="projectname" property="projectName" jdbcType="VARCHAR" /> | ||
7 | + <result column="jenkins_job_id" property="jenkinsJobId" jdbcType="VARCHAR" /> | ||
8 | + <result column="jenkins_build_status" property="jenkinsBuildStatus" jdbcType="VARCHAR" /> | ||
9 | + <result column="release_work_id" property="ReleaseWorkId" jdbcType="VARCHAR" /> | ||
10 | + <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> | ||
11 | + </resultMap> | ||
12 | + | ||
13 | + <sql id="Base_Column_List" > | ||
14 | + id, projectname, jenkins_job_id,jenkins_build_status,release_work_id,create_time | ||
15 | + </sql> | ||
16 | + | ||
17 | + <select id="selectById" resultMap="BaseResultMap"> | ||
18 | + select | ||
19 | + <include refid="Base_Column_List" /> | ||
20 | + from docker_jenkins_record | ||
21 | + where id= #{id} | ||
22 | + </select> | ||
23 | + | ||
24 | + | ||
25 | + <insert id="insert" parameterType="com.model.DockerJenkinsRecord" > | ||
26 | + insert into docker_jenkins_record (id, projectname, release_work_id) | ||
27 | + values (#{id,jdbcType=VARCHAR}, #{projectName,jdbcType=VARCHAR}, #{ReleaseWorkId,jdbcType=VARCHAR}) | ||
28 | + </insert> | ||
29 | + | ||
30 | + <update id="updateJob" > | ||
31 | + update docker_jenkins_record | ||
32 | + set jenkins_job_id = #{jobId} ,jenkins_build_status = #{jobStatus} | ||
33 | + where id = #{id} | ||
34 | + </update> | ||
35 | + | ||
36 | + | ||
37 | + | ||
38 | +</mapper> |
1 | +<?xml version="1.0" encoding="UTF-8" ?> | ||
2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > | ||
3 | +<mapper namespace="com.monitor.mysql.mapper.DockerProjectMapper" > | ||
4 | + <resultMap id="BaseResultMap" type="com.model.DockerProjectModel" > | ||
5 | + <id column="id" property="id" jdbcType="INTEGER" /> | ||
6 | + <result column="projectname" property="projectName" jdbcType="VARCHAR" /> | ||
7 | + <result column="projecttype" property="projectType" jdbcType="VARCHAR" /> | ||
8 | + <result column="dockerprojectname" property="dockerProjectName" jdbcType="VARCHAR" /> | ||
9 | + <result column="dockergrayclusterid" property="dockerGrayClusterId" jdbcType="VARCHAR" /> | ||
10 | + <result column="dockergraynamespace" property="dockerGrayNameSpace" jdbcType="VARCHAR" /> | ||
11 | + <result column="dockeronlineclusterid" property="dockerOnlineClusterId" jdbcType="VARCHAR" /> | ||
12 | + <result column="dockeronlinenamespace" property="dockerOnlineNameSpace" jdbcType="VARCHAR" /> | ||
13 | + | ||
14 | + </resultMap> | ||
15 | + | ||
16 | + <sql id="Base_Column_List" > | ||
17 | + id, projectname,projecttype,dockerprojectName,dockergrayclusterid,dockergraynamespace,dockeronlineclusterid,dockeronlinenamespace | ||
18 | + </sql> | ||
19 | + | ||
20 | + <select id="selectCount" resultType="java.lang.Integer"> | ||
21 | + select | ||
22 | + count(1) | ||
23 | + from docker_project | ||
24 | + </select> | ||
25 | + | ||
26 | + <select id="selectByPage" resultMap="BaseResultMap"> | ||
27 | + select | ||
28 | + <include refid="Base_Column_List" /> | ||
29 | + from docker_project | ||
30 | + where 1=1 | ||
31 | + <if test="params.projectType != null" > | ||
32 | + and projecttype = #{params.projectType} | ||
33 | + </if> | ||
34 | + order by id desc | ||
35 | + limit #{startIndex},#{pageSize} | ||
36 | + </select> | ||
37 | + | ||
38 | + <select id="selectById" resultMap="BaseResultMap"> | ||
39 | + select | ||
40 | + <include refid="Base_Column_List" /> | ||
41 | + from docker_project | ||
42 | + where | ||
43 | + id = #{id} | ||
44 | + </select> | ||
45 | + | ||
46 | + <select id="selectByProjectType" resultMap="BaseResultMap"> | ||
47 | + select | ||
48 | + <include refid="Base_Column_List" /> | ||
49 | + from docker_project | ||
50 | + where 1=1 | ||
51 | + <choose> | ||
52 | + <when test="projectType != null && projectType !='' " > | ||
53 | + and projecttype = #{projectType} | ||
54 | + </when> | ||
55 | + <otherwise> | ||
56 | + and 1=2 | ||
57 | + </otherwise> | ||
58 | + </choose> | ||
59 | + </select> | ||
60 | + | ||
61 | + <select id="selectByIdList" resultMap="BaseResultMap"> | ||
62 | + select | ||
63 | + <include refid="Base_Column_List" /> | ||
64 | + from docker_project | ||
65 | + where 1=1 | ||
66 | + <choose> | ||
67 | + <when test="idList != null && idList.size > 0" > | ||
68 | + and | ||
69 | + <foreach open="(" close=")" item="item" index="index" collection="idList" separator="or"> | ||
70 | + id = #{item} | ||
71 | + </foreach> | ||
72 | + </when> | ||
73 | + <otherwise> | ||
74 | + and 1=2 | ||
75 | + </otherwise> | ||
76 | + </choose> | ||
77 | + </select> | ||
78 | + | ||
79 | +</mapper> |
@@ -20,7 +20,6 @@ | @@ -20,7 +20,6 @@ | ||
20 | <result column="status" property="status" jdbcType="INTEGER" /> | 20 | <result column="status" property="status" jdbcType="INTEGER" /> |
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 | - | ||
24 | </resultMap> | 23 | </resultMap> |
25 | 24 | ||
26 | <sql id="Base_Column_List" > | 25 | <sql id="Base_Column_List" > |
@@ -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/ctrl/DockerProjectCtrl.java
0 → 100644
1 | +package com.monitor.other.worksystem.ctrl; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSONArray; | ||
4 | +import com.model.DockerJenkinsRecord; | ||
5 | +import com.model.DockerProjectModel; | ||
6 | +import com.monitor.model.request.DockerJenkinsReq; | ||
7 | +import com.monitor.model.response.BaseResponse; | ||
8 | +import com.monitor.other.worksystem.dock.qq.DockerServerDeployService; | ||
9 | +import com.monitor.other.worksystem.service.DockerProjectService; | ||
10 | +import org.apache.commons.lang.StringUtils; | ||
11 | +import org.springframework.beans.factory.annotation.Autowired; | ||
12 | +import org.springframework.stereotype.Controller; | ||
13 | +import org.springframework.web.bind.annotation.RequestBody; | ||
14 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
15 | +import org.springframework.web.bind.annotation.ResponseBody; | ||
16 | + | ||
17 | +import java.util.ArrayList; | ||
18 | +import java.util.List; | ||
19 | +import java.util.Map; | ||
20 | + | ||
21 | +@Controller | ||
22 | +@RequestMapping("/dockerProject") | ||
23 | +public class DockerProjectCtrl { | ||
24 | + | ||
25 | + @Autowired | ||
26 | + private DockerProjectService dockerProjectService; | ||
27 | + | ||
28 | + @Autowired | ||
29 | + private DockerServerDeployService dockerServerDeployService; | ||
30 | + | ||
31 | + /** | ||
32 | + * 查询docker项目 | ||
33 | + */ | ||
34 | + @RequestMapping("/getDockerProjectByType") | ||
35 | + @ResponseBody | ||
36 | + public BaseResponse<List<DockerProjectModel>> getDockerProjectByType(String projectType) { | ||
37 | + return new BaseResponse<>(dockerProjectService.selectByProjectType(projectType)); | ||
38 | + } | ||
39 | + | ||
40 | + /** | ||
41 | + * 查询docker项目 | ||
42 | + */ | ||
43 | + @RequestMapping("/getDockerProjectByIds") | ||
44 | + @ResponseBody | ||
45 | + public BaseResponse<List<DockerProjectModel>> getDockerProjectByIds(String ids) { | ||
46 | + List<Integer> idList=new ArrayList<>(); | ||
47 | + for(String id:ids.split(",")){ | ||
48 | + if(StringUtils.isNotBlank(id)){ | ||
49 | + idList.add(Integer.parseInt(id.trim())); | ||
50 | + } | ||
51 | + } | ||
52 | + List<DockerProjectModel> models= dockerProjectService.selectByIdList(idList); | ||
53 | + return new BaseResponse<>(models); | ||
54 | + } | ||
55 | + | ||
56 | + /** | ||
57 | + * 发布工单_jenkins:发起镜像制作 | ||
58 | + */ | ||
59 | + @RequestMapping("/createMirror") | ||
60 | + @ResponseBody | ||
61 | + public BaseResponse createMirror(@RequestBody DockerJenkinsReq dockerJenkinsReq) { | ||
62 | + String id=dockerProjectService.jenkinsBuild(dockerJenkinsReq); | ||
63 | + BaseResponse rnt=new BaseResponse(); | ||
64 | + if(id==null||id.length()<=0){ | ||
65 | + rnt.setCode(201); | ||
66 | + rnt.setMessage("请求镜像失败,返回jobId = " +id); | ||
67 | + }else{ | ||
68 | + rnt.setData(id); | ||
69 | + } | ||
70 | + return rnt; | ||
71 | + } | ||
72 | + | ||
73 | + /** | ||
74 | + * 发布工单_jenkins:更新镜像的状态 | ||
75 | + */ | ||
76 | + @RequestMapping("/updateJenkinsStatus") | ||
77 | + @ResponseBody | ||
78 | + public BaseResponse updateJenkinsStatus(String ticketID,String jobID,String status) { | ||
79 | + dockerProjectService.jenkinsStatusUpdate(ticketID,jobID,status); | ||
80 | + BaseResponse rnt=new BaseResponse(); | ||
81 | + return rnt; | ||
82 | + } | ||
83 | + | ||
84 | + /** | ||
85 | + * 发布工单_jenkins:更新镜像的状态 | ||
86 | + */ | ||
87 | + @RequestMapping("/queryJenkinsStatus") | ||
88 | + @ResponseBody | ||
89 | + public BaseResponse queryJenkinsStatus(String id) { | ||
90 | + DockerJenkinsRecord record=dockerProjectService.queryJenkinsById(id); | ||
91 | + BaseResponse rnt=new BaseResponse(); | ||
92 | + if (record==null){ | ||
93 | + rnt.setCode(201); | ||
94 | + rnt.setMessage("找不到Jenkins要更新的记录"); | ||
95 | + }else{ | ||
96 | + rnt.setMessage("Jenkins更新jobid:"+record.getJenkinsJobId()); | ||
97 | + rnt.setData(record.getJenkinsBuildStatus()==null?"":record.getJenkinsBuildStatus()); | ||
98 | + } | ||
99 | + | ||
100 | + return rnt; | ||
101 | + } | ||
102 | + | ||
103 | + | ||
104 | + | ||
105 | + /** | ||
106 | + * 发布docker服务 | ||
107 | + */ | ||
108 | + @RequestMapping("/deployMirror") | ||
109 | + @ResponseBody | ||
110 | + public BaseResponse deployMirror(@RequestBody DockerJenkinsReq dockerJenkinsReq) { | ||
111 | + BaseResponse rtn=new BaseResponse(); | ||
112 | + | ||
113 | + String clusterId=dockerJenkinsReq.getClusterId(); | ||
114 | + String namespace=dockerJenkinsReq.getNamespace()==null?"":dockerJenkinsReq.getNamespace(); | ||
115 | + String serviceName=dockerJenkinsReq.getServiceName(); | ||
116 | + if(!dockerServerDeployService.checkAppIsExist(clusterId,namespace,serviceName)){ | ||
117 | + rtn.setCode(201); | ||
118 | + rtn.setMessage("Docker服务不存在,请联系运维人员"); | ||
119 | + return rtn; | ||
120 | + } | ||
121 | + | ||
122 | + Map<String,Object> statusMap=dockerServerDeployService.checkAppStatusIsNormal(clusterId,namespace,serviceName); | ||
123 | + boolean normalStatusFlag=(boolean)statusMap.get("result"); | ||
124 | + if(!normalStatusFlag){ | ||
125 | + rtn.setCode(201); | ||
126 | + rtn.setMessage("Docker服务非Normal状态,发布中止!"); | ||
127 | + return rtn; | ||
128 | + } | ||
129 | + JSONArray containerJa=(JSONArray)statusMap.get("containerJa"); | ||
130 | + boolean currentImageLatestFlag=dockerServerDeployService.checkImageLatestWithModifyFlag(clusterId, namespace, serviceName, containerJa,false); | ||
131 | + if(currentImageLatestFlag){ | ||
132 | + //开始重新发布 | ||
133 | + boolean deploySuccess=dockerServerDeployService.redeployClusterService(clusterId, namespace, serviceName); | ||
134 | + if(!deploySuccess){ | ||
135 | + rtn.setCode(201); | ||
136 | + rtn.setMessage("Docker服务重新发布失败,请联系运维人员"); | ||
137 | + return rtn; | ||
138 | + } | ||
139 | + }else{ | ||
140 | + //更改路径,更改之后自动重新部署 | ||
141 | + currentImageLatestFlag=dockerServerDeployService.checkImageLatestWithModifyFlag(clusterId, namespace, serviceName, containerJa,true); | ||
142 | + if(!currentImageLatestFlag){ | ||
143 | + rtn.setCode(201); | ||
144 | + rtn.setMessage("Docker服务修改镜像服务发生异常"); | ||
145 | + return rtn; | ||
146 | + } | ||
147 | + | ||
148 | + | ||
149 | + } | ||
150 | + return rtn; | ||
151 | + } | ||
152 | + | ||
153 | + | ||
154 | + /** | ||
155 | + * 发布docker服务 | ||
156 | + */ | ||
157 | + @RequestMapping("/queryDockerServerStatus") | ||
158 | + @ResponseBody | ||
159 | + public BaseResponse queryDockerServerStatus(@RequestBody DockerJenkinsReq dockerJenkinsReq) { | ||
160 | + BaseResponse rtn=new BaseResponse(); | ||
161 | + String clusterId=dockerJenkinsReq.getClusterId(); | ||
162 | + String namespace=dockerJenkinsReq.getNamespace()==null?"":dockerJenkinsReq.getNamespace(); | ||
163 | + String serviceName=dockerJenkinsReq.getServiceName(); | ||
164 | + String status=dockerServerDeployService.queryAppStatus(clusterId,namespace,serviceName); | ||
165 | + rtn.setData(status); | ||
166 | + return rtn; | ||
167 | + } | ||
168 | +} |
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.monitor.model.response.BaseResponse; | ||
4 | +import com.monitor.other.worksystem.service.ReleaseService; | ||
5 | +import org.springframework.beans.factory.annotation.Autowired; | ||
6 | +import org.springframework.stereotype.Controller; | ||
7 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
8 | +import org.springframework.web.bind.annotation.ResponseBody; | ||
9 | + | ||
10 | +@Controller | ||
11 | +@RequestMapping("/work/jenkins") | ||
12 | +public class JenkinsCtrl { | ||
13 | + @Autowired | ||
14 | + private ReleaseService releaseService; | ||
15 | + | ||
16 | + | ||
17 | + | ||
18 | +} |
monitor-service-other/src/main/java/com/monitor/other/worksystem/dock/qq/CcsApiQcloud.java
0 → 100644
1 | +package com.monitor.other.worksystem.dock.qq; | ||
2 | + | ||
3 | +import org.apache.commons.codec.binary.Base64; | ||
4 | +import org.springframework.stereotype.Component; | ||
5 | + | ||
6 | +import javax.crypto.Mac; | ||
7 | +import javax.crypto.spec.SecretKeySpec; | ||
8 | +import java.net.URLEncoder; | ||
9 | +import java.util.*; | ||
10 | + | ||
11 | +/** | ||
12 | + * Created by jimi on 2017/9/14. | ||
13 | + */ | ||
14 | +@Component | ||
15 | +public class CcsApiQcloud { | ||
16 | + | ||
17 | + public String pushTest(Map<String, String> paraMap) { | ||
18 | + String secretId = "AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK"; | ||
19 | + //用于加密签名字符串和服务器端验证签名字符串的密钥 | ||
20 | + String secretKey = "ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW"; | ||
21 | + //当前时间戳 | ||
22 | + long timestamp = System.currentTimeMillis() / 1000; | ||
23 | + //随机的正整数 | ||
24 | + int nonce = (new Random()).nextInt(100000); | ||
25 | + //实例所在区域 | ||
26 | + String region = "bj"; | ||
27 | + //签名方式 | ||
28 | + String signatureMethod = "HmacSHA256"; | ||
29 | + paraMap.put("Nonce", String.valueOf(nonce)); | ||
30 | + paraMap.put("Region", region); | ||
31 | + paraMap.put("SecretId", secretId); | ||
32 | + paraMap.put("SignatureMethod", signatureMethod); | ||
33 | + paraMap.put("Timestamp", String.valueOf(timestamp)); | ||
34 | + | ||
35 | + List<String> listPara = new ArrayList<String> (); | ||
36 | + StringBuffer buffer = new StringBuffer(); | ||
37 | + | ||
38 | + listPara.addAll(paraMap.keySet()); | ||
39 | + Collections.sort(listPara); | ||
40 | + for (String mykey :listPara) { | ||
41 | + if (buffer.length() > 0) { | ||
42 | + buffer.append("&"); | ||
43 | + buffer.append(mykey + "=" + paraMap.get(mykey)); | ||
44 | + } else { | ||
45 | + buffer.append(mykey + "=" + paraMap.get(mykey)); | ||
46 | + } | ||
47 | + } | ||
48 | + String url = "GETccs.api.qcloud.com/v2/index.php?" + buffer.toString(); | ||
49 | + String signature = getSignature(url); | ||
50 | + return buffer.toString() + "&Signature=" + signature; | ||
51 | + } | ||
52 | + | ||
53 | + | ||
54 | + private String getSignature(String url) { | ||
55 | + String returnValue = ""; | ||
56 | + try { | ||
57 | + String secretKey = "ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW"; | ||
58 | + Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); | ||
59 | + SecretKeySpec secret_key = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256"); | ||
60 | + sha256_HMAC.init(secret_key); | ||
61 | + String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(url.getBytes())); | ||
62 | + returnValue = URLEncoder.encode(hash, "GBK"); | ||
63 | + } catch (Exception e) { | ||
64 | + e.printStackTrace(); | ||
65 | + } | ||
66 | + return returnValue; | ||
67 | + } | ||
68 | + | ||
69 | + | ||
70 | + public static void main(String[] args) { | ||
71 | + String imageName="yoho-core/es"; | ||
72 | + String imageNameShow="es"; | ||
73 | + CcsApiQcloud ccsApiQcloud = new CcsApiQcloud(); | ||
74 | + Map<String, String> paraMap = new HashMap<>(); | ||
75 | + paraMap.put("clusterId", "cls-qz2lezjt"); | ||
76 | + paraMap.put("Action", "CreateClusterService"); | ||
77 | + paraMap.put("serviceName", imageNameShow); | ||
78 | + paraMap.put("serviceDesc", "testyoho-" + imageNameShow); | ||
79 | + paraMap.put("replicas", "1"); | ||
80 | + paraMap.put("accessType", "ClusterIP"); | ||
81 | + paraMap.put("portMappings.0.protocol", "TCP"); | ||
82 | + String port = "9200"; | ||
83 | + paraMap.put("portMappings.0.containerPort", port); | ||
84 | + paraMap.put("portMappings.0.lbPort", port);//防止端口冲突 | ||
85 | + paraMap.put("portMappings.0.nodePort", "0");//防止端口冲突 | ||
86 | + paraMap.put("containers.0.containerName", imageNameShow); | ||
87 | + paraMap.put("namespace", "default"); | ||
88 | + paraMap.put("containers.0.image", "ccr.ccs.tencentyun.com/" + imageName); | ||
89 | + String jsonObject = ccsApiQcloud.pushTest(paraMap); | ||
90 | + System.out.println(jsonObject); | ||
91 | + } | ||
92 | + | ||
93 | +} |
1 | +package com.monitor.other.worksystem.dock.qq; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSON; | ||
4 | +import com.alibaba.fastjson.JSONArray; | ||
5 | +import com.alibaba.fastjson.JSONObject; | ||
6 | +import org.apache.commons.lang.StringUtils; | ||
7 | +import org.apache.commons.lang.math.NumberUtils; | ||
8 | +import org.slf4j.Logger; | ||
9 | +import org.slf4j.LoggerFactory; | ||
10 | +import org.springframework.beans.factory.annotation.Autowired; | ||
11 | +import org.springframework.stereotype.Service; | ||
12 | + | ||
13 | +import java.util.HashMap; | ||
14 | +import java.util.Map; | ||
15 | + | ||
16 | +/** | ||
17 | + * Created by jimi on 2017/11/6. | ||
18 | + */ | ||
19 | +@Service | ||
20 | +public class DockerServerDeployService { | ||
21 | + | ||
22 | + @Autowired | ||
23 | + private QqDockerServiceUtils txServer; | ||
24 | + | ||
25 | + private static final Logger logger = LoggerFactory.getLogger(DockerServerDeployService.class); | ||
26 | + | ||
27 | + /** | ||
28 | + * 检查是否存在服务 | ||
29 | + * @param checkedServiceName 需要检查的服务名称 | ||
30 | + */ | ||
31 | + public boolean checkAppIsExist(String clusterId, String namespace,String checkedServiceName){ | ||
32 | + JSONObject searchResult=txServer.describeClusterService(clusterId,namespace); | ||
33 | + if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){ | ||
34 | + JSONArray jaServices=searchResult.getJSONObject("data").getJSONArray("services"); | ||
35 | + for(int i=0;i<jaServices.size();i++){ | ||
36 | + if(checkedServiceName.equals(jaServices.getJSONObject(i).getString("serviceName"))){ | ||
37 | + return true; | ||
38 | + } | ||
39 | + } | ||
40 | + } | ||
41 | + return false; | ||
42 | + } | ||
43 | + | ||
44 | + /** | ||
45 | + * 检测服务状态 | ||
46 | + */ | ||
47 | + public String queryAppStatus(String clusterId, String namespace, String serviceName) { | ||
48 | + String status=""; | ||
49 | + JSONObject searchResult = txServer.describeClusterServiceInfo(clusterId, namespace,serviceName ); | ||
50 | + if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){ | ||
51 | + status=searchResult.getJSONObject("data").getJSONObject("service").getString("status"); | ||
52 | + }else{ | ||
53 | + if(searchResult!=null){ | ||
54 | + status= JSON.toJSONString(searchResult); | ||
55 | + } | ||
56 | + } | ||
57 | + return status; | ||
58 | + } | ||
59 | + | ||
60 | + /** | ||
61 | + * 检测服务状态是否是Normal | ||
62 | + * 返回标志信息和container信息 | ||
63 | + * | ||
64 | + * serviceName.replace("_","-") | ||
65 | + */ | ||
66 | + public Map<String,Object> checkAppStatusIsNormal(String clusterId, String namespace, String serviceName) { | ||
67 | + int requestTime=3; | ||
68 | + return checkAppStatusIsNormal(clusterId,namespace,serviceName,requestTime); | ||
69 | + } | ||
70 | + | ||
71 | + private Map<String,Object> checkAppStatusIsNormal(String clusterId, String namespace, String serviceName, int requestTime) { | ||
72 | + //List<String> myString = Splitter.on(",").omitEmptyStrings().splitToList(serviceNames); | ||
73 | + boolean normalFlag=false; | ||
74 | + JSONArray containerJa=null; | ||
75 | + for (int i = 0; i < requestTime; i++) { | ||
76 | + JSONObject searchResult = txServer.describeClusterServiceInfo(clusterId, namespace,serviceName ); | ||
77 | + logger.info("第 {} 次检测服务 {} 是否正常 ,返回结果 {}", i,serviceName,searchResult); | ||
78 | + boolean singleResult = searchResult!=null | ||
79 | + &&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO) | ||
80 | + &&searchResult.getJSONObject("data").getJSONObject("service").getString("status").equals("Normal"); | ||
81 | + boolean result = true; | ||
82 | + result = result && singleResult; | ||
83 | + | ||
84 | + if (result == true) { | ||
85 | + normalFlag=result; | ||
86 | + containerJa=searchResult.getJSONObject("data").getJSONObject("service").getJSONArray("containers"); | ||
87 | + break; | ||
88 | + } | ||
89 | + } | ||
90 | + Map<String,Object> map=new HashMap<>(); | ||
91 | + map.put("result",normalFlag); | ||
92 | + map.put("containerJa",containerJa); | ||
93 | + return map; | ||
94 | + } | ||
95 | + | ||
96 | + /** | ||
97 | + * 检测服务的container的镜像文件是否是latest, | ||
98 | + * 如果不是需要修改镜像的版本为latest | ||
99 | + */ | ||
100 | + public boolean checkImageLatestWithModifyFlag(String clusterId, String namespace, String serviceName,JSONArray containerJa,boolean modifyFlag){ | ||
101 | + if(containerJa==null||containerJa.size()<=0){ | ||
102 | + logger.info("modifyClusterServiceImageFailed: container is empty !"); | ||
103 | + return false; | ||
104 | + } | ||
105 | + boolean correctImageFlag=false; | ||
106 | + for(int i=0;i<containerJa.size();i++){ | ||
107 | + JSONObject jo=containerJa.getJSONObject(i); | ||
108 | + String image=jo.getString("image"); | ||
109 | + | ||
110 | + if(StringUtils.isNotBlank(image)&&image.indexOf("/"+serviceName+":")>0){ | ||
111 | + int index=image.lastIndexOf(":"); | ||
112 | + String imageLeft=image.substring(0,index); | ||
113 | + String imageRight=image.substring(index+1); | ||
114 | + if("latest".equals(imageRight)){ | ||
115 | + correctImageFlag=true; | ||
116 | + break; | ||
117 | + }else{ | ||
118 | + if(modifyFlag){ | ||
119 | + String containersIndexName="containers."+i+".containerName"; | ||
120 | + String containersIndexNameValue=jo.getString("containerName"); | ||
121 | + String containersIndexImage="containers."+i+".image"; | ||
122 | + String containersIndexImageValue=imageLeft+":latest"; | ||
123 | + JSONObject searchResult = txServer.modifyClusterServiceImage(clusterId, namespace,serviceName ,containersIndexName,containersIndexNameValue,containersIndexImage,containersIndexImageValue); | ||
124 | + if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){ | ||
125 | + correctImageFlag=true; | ||
126 | + } | ||
127 | + break; | ||
128 | + } | ||
129 | + } | ||
130 | + } | ||
131 | + | ||
132 | + } | ||
133 | + return correctImageFlag; | ||
134 | + } | ||
135 | + | ||
136 | + /** | ||
137 | + * 重新发布 | ||
138 | + */ | ||
139 | + public boolean redeployClusterService(String clusterId, String namespace, String serviceName){ | ||
140 | + JSONObject searchResult =txServer.redeployClusterService(clusterId,namespace,serviceName); | ||
141 | + if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){ | ||
142 | + return true; | ||
143 | + }else{ | ||
144 | + logger.error("redeployClusterServiceFailed ,response is {} ",searchResult); | ||
145 | + return false; | ||
146 | + } | ||
147 | + } | ||
148 | + | ||
149 | + public static void main(String args[]){ | ||
150 | + String image="xxxxxxx/fddfdf:wwwxxx"; | ||
151 | + int index=image.lastIndexOf(":"); | ||
152 | + String imageLeft=image.substring(0,index); | ||
153 | + String imageRight=image.substring(index+1); | ||
154 | + System.out.println(imageLeft); | ||
155 | + System.out.println(imageRight); | ||
156 | + } | ||
157 | +} |
monitor-service-other/src/main/java/com/monitor/other/worksystem/dock/qq/QqDockerServiceUtils.java
0 → 100644
1 | +package com.monitor.other.worksystem.dock.qq; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSONObject; | ||
4 | +import com.google.common.collect.Maps; | ||
5 | +import com.monitor.common.util.HttpClientUtil; | ||
6 | +import org.apache.commons.lang3.StringUtils; | ||
7 | +import org.slf4j.Logger; | ||
8 | +import org.slf4j.LoggerFactory; | ||
9 | +import org.springframework.beans.factory.annotation.Autowired; | ||
10 | +import org.springframework.stereotype.Service; | ||
11 | + | ||
12 | +import java.util.Map; | ||
13 | + | ||
14 | +/** | ||
15 | + * Created by jimi on 2017/9/14. | ||
16 | + */ | ||
17 | +@Service | ||
18 | +public class QqDockerServiceUtils { | ||
19 | + private static final Logger logger = LoggerFactory.getLogger(QqDockerServiceUtils.class); | ||
20 | + | ||
21 | + private final String requestUrl = "https://ccs.api.qcloud.com/v2/index.php"; | ||
22 | + | ||
23 | + @Autowired | ||
24 | + private CcsApiQcloud ccsApiQcloud; | ||
25 | + | ||
26 | + /** | ||
27 | + * 查询服务列表,该接口返回的列表只包含服务的扼要信息, | ||
28 | + * 如果需要获取详细的服务信息,需要调用 DescribeClusterServiceInfo 接口 | ||
29 | + */ | ||
30 | + public JSONObject describeClusterService(String clusterId, String namespace) { | ||
31 | + JSONObject resultJson=null; | ||
32 | + try{ | ||
33 | + Map<String, String> paraMap = Maps.newHashMap(); | ||
34 | + paraMap.put("Action", "DescribeClusterService"); | ||
35 | + paraMap.put("clusterId", clusterId); | ||
36 | + if (StringUtils.isNotEmpty(namespace)) { | ||
37 | + paraMap.put("namespace", namespace); | ||
38 | + } else { | ||
39 | + paraMap.put("namespace", "default"); | ||
40 | + } | ||
41 | + String backString = ccsApiQcloud.pushTest(paraMap); | ||
42 | + String uri=requestUrl+"?"+backString; | ||
43 | + String qqResponseJsonStr =HttpClientUtil.doget(uri,null,null); | ||
44 | + if(StringUtils.isNotBlank(qqResponseJsonStr)){ | ||
45 | + resultJson = JSONObject.parseObject(qqResponseJsonStr); | ||
46 | + } | ||
47 | + }catch (Exception e){ | ||
48 | + logger.error("describeClusterService error",e); | ||
49 | + } | ||
50 | + return resultJson; | ||
51 | + } | ||
52 | + | ||
53 | + /** | ||
54 | + * 用于查询单个服务详情 | ||
55 | + */ | ||
56 | + public JSONObject describeClusterServiceInfo(String clusterId, String namespace, String serviceName) { | ||
57 | + JSONObject resultJson=null; | ||
58 | + try{ | ||
59 | + Map<String, String> paraMap = Maps.newHashMap(); | ||
60 | + paraMap.put("clusterId", clusterId); | ||
61 | + paraMap.put("serviceName", serviceName); | ||
62 | + paraMap.put("Action", "DescribeClusterServiceInfo"); | ||
63 | + if (StringUtils.isNotEmpty(namespace)) { | ||
64 | + paraMap.put("namespace", namespace); | ||
65 | + } else { | ||
66 | + paraMap.put("namespace", "default"); | ||
67 | + } | ||
68 | + String backString = ccsApiQcloud.pushTest(paraMap); | ||
69 | + String uri=requestUrl+"?"+backString; | ||
70 | + String qqResponseJsonStr =HttpClientUtil.doget(uri,null,null); | ||
71 | + if(StringUtils.isNotBlank(qqResponseJsonStr)){ | ||
72 | + resultJson = JSONObject.parseObject(qqResponseJsonStr); | ||
73 | + } | ||
74 | + }catch (Exception e){ | ||
75 | + logger.error("describeClusterServiceInfo error",e); | ||
76 | + } | ||
77 | + return resultJson; | ||
78 | + } | ||
79 | + | ||
80 | + /** | ||
81 | + * 用于更新镜像文件 | ||
82 | + */ | ||
83 | + public JSONObject modifyClusterServiceImage (String clusterId, String namespace, String serviceName | ||
84 | + ,String containersIndexName,String containersIndexNameValue,String containersIndexImage,String containersIndexImageValue) { | ||
85 | + JSONObject resultJson=null; | ||
86 | + try{ | ||
87 | + Map<String, String> paraMap = Maps.newHashMap(); | ||
88 | + paraMap.put("clusterId", clusterId); | ||
89 | + paraMap.put("serviceName", serviceName); | ||
90 | + paraMap.put("Action", "ModifyClusterServiceImage"); | ||
91 | + paraMap.put(containersIndexName,containersIndexNameValue);// 容器name信息 | ||
92 | + paraMap.put(containersIndexImage,containersIndexImageValue);// 镜像值信息 | ||
93 | + if (StringUtils.isNotEmpty(namespace)) { | ||
94 | + paraMap.put("namespace", namespace); | ||
95 | + } else { | ||
96 | + paraMap.put("namespace", "default"); | ||
97 | + } | ||
98 | + logger.info("modifyClusterServiceImage paraMap is {}",paraMap); | ||
99 | + String backString = ccsApiQcloud.pushTest(paraMap); | ||
100 | + String uri=requestUrl+"?"+backString; | ||
101 | + String qqResponseJsonStr =HttpClientUtil.doget(uri,null,null); | ||
102 | + logger.info("modifyClusterServiceImage backResponse is {}",qqResponseJsonStr); | ||
103 | + if(StringUtils.isNotBlank(qqResponseJsonStr)){ | ||
104 | + resultJson = JSONObject.parseObject(qqResponseJsonStr); | ||
105 | + } | ||
106 | + }catch (Exception e){ | ||
107 | + logger.error("modifyClusterServiceImage error",e); | ||
108 | + } | ||
109 | + return resultJson; | ||
110 | + } | ||
111 | + | ||
112 | + /** | ||
113 | + * 用于将服务实例重新部署,常用场景为镜像仓库中镜像发生改变后, | ||
114 | + * 通过重部署触发服务重新拉取镜像 | ||
115 | + */ | ||
116 | + public JSONObject redeployClusterService(String clusterId, String namespace, String serviceName) { | ||
117 | + JSONObject resultJson=null; | ||
118 | + try{ | ||
119 | + Map<String, String> paraMap = Maps.newHashMap(); | ||
120 | + paraMap.put("clusterId", clusterId); | ||
121 | + paraMap.put("serviceName", serviceName); | ||
122 | + paraMap.put("Action", "RedeployClusterService"); | ||
123 | + if (StringUtils.isNotEmpty(namespace)) { | ||
124 | + paraMap.put("namespace", namespace); | ||
125 | + } else { | ||
126 | + paraMap.put("namespace", "default"); | ||
127 | + } | ||
128 | + String backString = ccsApiQcloud.pushTest(paraMap); | ||
129 | + String uri=requestUrl+"?"+backString; | ||
130 | + String qqResponseJsonStr =HttpClientUtil.doget(uri,null,null); | ||
131 | + if(StringUtils.isNotBlank(qqResponseJsonStr)){ | ||
132 | + resultJson = JSONObject.parseObject(qqResponseJsonStr); | ||
133 | + } | ||
134 | + }catch (Exception e){ | ||
135 | + logger.error("redeployClusterService error",e); | ||
136 | + } | ||
137 | + | ||
138 | + return resultJson; | ||
139 | + } | ||
140 | + | ||
141 | + /* public JSONObject createClusterService(Map<String, String> paraMap) { | ||
142 | + paraMap.put("Action", "CreateClusterService"); | ||
143 | + String backString = ccsApiQcloud.pushTest(paraMap); | ||
144 | + String resultjason = httpRestClientService.doPostStringJson(requestUrl, backString); | ||
145 | + JSONObject resultJson = JSONObject.parseObject(resultjason); | ||
146 | + return resultJson; | ||
147 | + }*/ | ||
148 | + | ||
149 | + | ||
150 | + | ||
151 | + /* public JSONObject deleteClusterService(String clusterId, String namespace, String serviceName) { | ||
152 | + Map<String, String> paraMap = Maps.newHashMap(); | ||
153 | + paraMap.put("clusterId", clusterId); | ||
154 | + paraMap.put("serviceName", serviceName); | ||
155 | + paraMap.put("Action", "DeleteClusterService"); | ||
156 | + if (StringUtils.isNotEmpty(namespace)) { | ||
157 | + paraMap.put("namespace", namespace); | ||
158 | + } else { | ||
159 | + paraMap.put("namespace", "default"); | ||
160 | + } | ||
161 | + String backString = ccsApiQcloud.pushTest(paraMap); | ||
162 | + String resultjason = httpRestClientService.doPostStringJson(requestUrl, backString); | ||
163 | + JSONObject resultJson = JSONObject.parseObject(resultjason); | ||
164 | + return resultJson; | ||
165 | + }*/ | ||
166 | + | ||
167 | + | ||
168 | + | ||
169 | + /* public JSONObject describeServiceEvent(String clusterId, String namespace, String serviceName) { | ||
170 | + Map<String, String> paraMap = Maps.newHashMap(); | ||
171 | + paraMap.put("clusterId", clusterId); | ||
172 | + paraMap.put("serviceName", serviceName); | ||
173 | + paraMap.put("Action", "DescribeServiceEvent"); | ||
174 | + if (StringUtils.isNotEmpty(namespace)) { | ||
175 | + paraMap.put("namespace", namespace); | ||
176 | + } else { | ||
177 | + paraMap.put("namespace", "default"); | ||
178 | + } | ||
179 | + String backString = ccsApiQcloud.pushTest(paraMap); | ||
180 | + String resultjason = httpRestClientService.doPostStringJson(requestUrl, backString); | ||
181 | + JSONObject resultJson = JSONObject.parseObject(resultjason); | ||
182 | + return resultJson; | ||
183 | + }*/ | ||
184 | +/* | ||
185 | + private static Map<String, String> portMap = Maps.newHashMap(); | ||
186 | + | ||
187 | + @PostConstruct | ||
188 | + private void getUser() { | ||
189 | + portMap.put("cobar", "8080"); | ||
190 | + portMap.put("es", "9300"); | ||
191 | + portMap.put("java", "8080"); | ||
192 | + portMap.put("mq", "5672"); | ||
193 | + portMap.put("mq-common", "5672"); | ||
194 | + portMap.put("mq-cluster", "5672"); | ||
195 | + portMap.put("mysql", "3306"); | ||
196 | + portMap.put("mysql-cms", "3306"); | ||
197 | + portMap.put("mysql-orders", "3306"); | ||
198 | + portMap.put("mysql-passport", "3306"); | ||
199 | + portMap.put("mysql-shops", "3306"); | ||
200 | + portMap.put("mysql-cobar-split1", "3306"); | ||
201 | + portMap.put("mysql-cobar-split2", "3306"); | ||
202 | + portMap.put("mysql-cobar-split3", "3306"); | ||
203 | + portMap.put("mysql-cobar-split4", "3306"); | ||
204 | + portMap.put("openresty-java", "80"); | ||
205 | + portMap.put("twemproxy", "63790"); | ||
206 | + portMap.put("twemproxy-nosync", "63790"); | ||
207 | + portMap.put("twemproxy-sync", "63790"); | ||
208 | + portMap.put("twemproxy-brower", "63790"); | ||
209 | + portMap.put("twemproxy-bigdata", "63790"); | ||
210 | + portMap.put("zk", "2181"); | ||
211 | + | ||
212 | + portMap.put("java-yoho-gateway", "8080,21212,21211"); | ||
213 | + portMap.put("java-yoho-users", "8081"); | ||
214 | + portMap.put("java-uic", "8096"); | ||
215 | + portMap.put("java-yoho-message", "8086"); | ||
216 | + portMap.put("java-yohobuy-union", "8088"); | ||
217 | + portMap.put("java-yohobuy-activity", "8090"); | ||
218 | + portMap.put("java-yohobuy-resources", "8087"); | ||
219 | + portMap.put("java-yohobuy-order", "8084"); | ||
220 | + portMap.put("java-yohobuy-wechat", "8094"); | ||
221 | + portMap.put("java-yohobuy-product", "8083"); | ||
222 | + portMap.put("java-yohobuy-platform", "8088"); | ||
223 | + portMap.put("java-yohobuy-bigdata", "8091"); | ||
224 | + portMap.put("java-yoho-social", "8095"); | ||
225 | + portMap.put("java-yohobuy-task", "8084"); | ||
226 | + portMap.put("java-yohobuy-promotion", "8085"); | ||
227 | + portMap.put("java-yoho-sns", "8082"); | ||
228 | + portMap.put("java-yohobuy-brower", "8092"); | ||
229 | + portMap.put("java-yohobuy-crm", "8093"); | ||
230 | + portMap.put("java-yoho-push", "8089"); | ||
231 | + portMap.put("java-cms-push", "8086"); | ||
232 | + portMap.put("java-yoho-search-producer", "8089"); | ||
233 | + portMap.put("java-yoho-search-consumer", "8088"); | ||
234 | + portMap.put("java-yoho-search-service", "8080"); | ||
235 | + portMap.put("java-yoho-search-adaptor", "8085"); | ||
236 | + | ||
237 | + | ||
238 | + } | ||
239 | + | ||
240 | + private String getPort(String key) { | ||
241 | + if (portMap.containsKey(key)) { | ||
242 | + return portMap.get(key); | ||
243 | + } else | ||
244 | + return portMap.get("java"); | ||
245 | + }*/ | ||
246 | + | ||
247 | +} |
monitor-service-other/src/main/java/com/monitor/other/worksystem/service/DockerProjectService.java
0 → 100644
1 | +package com.monitor.other.worksystem.service; | ||
2 | + | ||
3 | +import com.model.DockerJenkinsRecord; | ||
4 | +import com.model.DockerProjectModel; | ||
5 | +import com.model.PendingJob; | ||
6 | +import com.monitor.model.request.DockerJenkinsReq; | ||
7 | +import com.monitor.model.request.WorkSystemReq; | ||
8 | +import com.monitor.model.response.PageResponse; | ||
9 | + | ||
10 | +import java.util.List; | ||
11 | + | ||
12 | +/** | ||
13 | + * Created by craig.qin | ||
14 | + */ | ||
15 | +public interface DockerProjectService { | ||
16 | + List<DockerProjectModel> selectByIdList(List<Integer> idList); | ||
17 | + | ||
18 | + List<DockerProjectModel> selectByProjectType(String projectType); | ||
19 | + | ||
20 | + String jenkinsBuild(DockerJenkinsReq dockerJenkinsReq); | ||
21 | + | ||
22 | + void jenkinsStatusUpdate(String ticketID,String jobID,String status); | ||
23 | + | ||
24 | + DockerJenkinsRecord queryJenkinsById(String id); | ||
25 | +} |
1 | +package com.monitor.other.worksystem.service.impl; | ||
2 | + | ||
3 | +import com.model.*; | ||
4 | +import com.monitor.common.service.MailService; | ||
5 | +import com.monitor.model.domain.Mail; | ||
6 | +import com.monitor.model.request.DockerJenkinsReq; | ||
7 | +import com.monitor.mysql.mapper.*; | ||
8 | +import com.monitor.other.worksystem.contants.HandleType; | ||
9 | +import com.monitor.other.worksystem.contants.HandlerRole; | ||
10 | +import com.monitor.other.worksystem.contants.WorkStatus; | ||
11 | +import com.monitor.other.worksystem.contants.WorkType; | ||
12 | +import com.monitor.other.worksystem.dock.FastJenkinsUtils; | ||
13 | +import com.monitor.other.worksystem.service.DBService; | ||
14 | +import com.monitor.other.worksystem.service.DockerProjectService; | ||
15 | +import org.apache.commons.lang.StringUtils; | ||
16 | +import org.slf4j.Logger; | ||
17 | +import org.slf4j.LoggerFactory; | ||
18 | +import org.springframework.beans.factory.annotation.Autowired; | ||
19 | +import org.springframework.stereotype.Service; | ||
20 | + | ||
21 | +import java.util.*; | ||
22 | + | ||
23 | +/** | ||
24 | + * Created by zhengyouwei on 2016/9/2. | ||
25 | + */ | ||
26 | +@Service | ||
27 | +public class DockerProjectServiceImpl implements DockerProjectService { | ||
28 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
29 | + | ||
30 | + @Autowired | ||
31 | + private DockerProjectMapper dockerProjectMapper; | ||
32 | + | ||
33 | + @Autowired | ||
34 | + private DockerJenkinsRecordMapper dockerJenkinsRecordMapper; | ||
35 | + | ||
36 | + public List<DockerProjectModel> selectByIdList(List<Integer> idList){ | ||
37 | + if(idList==null||idList.size()<=0){ | ||
38 | + return new ArrayList<>(); | ||
39 | + } | ||
40 | + return dockerProjectMapper.selectByIdList(idList); | ||
41 | + } | ||
42 | + | ||
43 | + public List<DockerProjectModel> selectByProjectType(String projectType){ | ||
44 | + if(StringUtils.isBlank(projectType)){ | ||
45 | + return new ArrayList<>(); | ||
46 | + } | ||
47 | + return dockerProjectMapper.selectByProjectType(projectType); | ||
48 | + } | ||
49 | + | ||
50 | + @Override | ||
51 | + public String jenkinsBuild(DockerJenkinsReq dockerJenkinsReq){ | ||
52 | + String appNames=dockerJenkinsReq.getAppNames(); | ||
53 | + String uuid = UUID.randomUUID().toString(); | ||
54 | + uuid=appNames+"-"+uuid.replace("-", ""); | ||
55 | + Map params= new HashMap(); | ||
56 | + params.put("ticketID", uuid); | ||
57 | + params.put("appNames",appNames); | ||
58 | + params.put("branch",dockerJenkinsReq.getBranch()); | ||
59 | + params.put("nodeEnv",dockerJenkinsReq.getNodeEnv()); | ||
60 | + params.put("userMail",dockerJenkinsReq.getUserMail()); | ||
61 | + try{ | ||
62 | + DockerJenkinsRecord record=new DockerJenkinsRecord(); | ||
63 | + record.setId(uuid); | ||
64 | + record.setProjectName(dockerJenkinsReq.getAppNames()); | ||
65 | + record.setReleaseWorkId(dockerJenkinsReq.getReleaseWorkId()); | ||
66 | + dockerJenkinsRecordMapper.insert(record); | ||
67 | + | ||
68 | + FastJenkinsUtils fastJenkinsUtils=new FastJenkinsUtils(); | ||
69 | + String jobName=""; | ||
70 | + if("NODE".equalsIgnoreCase(dockerJenkinsReq.getProjectType())){ | ||
71 | + jobName=FastJenkinsUtils.JENKINS_JOB_NODE; | ||
72 | + }else if("JAVA".equalsIgnoreCase(dockerJenkinsReq.getProjectType())){ | ||
73 | + jobName=FastJenkinsUtils.JENKINS_JOB_JAVA; | ||
74 | + } | ||
75 | + fastJenkinsUtils.createJob(jobName,params); | ||
76 | + }catch (Exception e){ | ||
77 | + logger.error("jenkinsBuild error",e); | ||
78 | + return ""; | ||
79 | + } | ||
80 | + return uuid; | ||
81 | + } | ||
82 | + | ||
83 | + @Override | ||
84 | + public void jenkinsStatusUpdate(String ticketID,String jobID,String status){ | ||
85 | + dockerJenkinsRecordMapper.updateJob(ticketID,jobID,status); | ||
86 | + } | ||
87 | + | ||
88 | + @Override | ||
89 | + public DockerJenkinsRecord queryJenkinsById(String id) { | ||
90 | + DockerJenkinsRecord record = dockerJenkinsRecordMapper.selectById(id); | ||
91 | + return record; | ||
92 | + } | ||
93 | + | ||
94 | +} |
@@ -2,11 +2,6 @@ package com.monitor; | @@ -2,11 +2,6 @@ package com.monitor; | ||
2 | 2 | ||
3 | import com.monitor.common.util.HttpRestClient; | 3 | import com.monitor.common.util.HttpRestClient; |
4 | import org.apache.commons.net.telnet.TelnetClient; | 4 | import org.apache.commons.net.telnet.TelnetClient; |
5 | -import org.apache.http.HttpResponse; | ||
6 | -import org.apache.http.client.methods.HttpGet; | ||
7 | -import org.apache.http.client.params.ClientPNames; | ||
8 | -import org.apache.http.impl.client.BasicResponseHandler; | ||
9 | -import org.apache.http.impl.client.DefaultHttpClient; | ||
10 | import org.junit.Test; | 5 | import org.junit.Test; |
11 | import org.junit.runner.RunWith; | 6 | import org.junit.runner.RunWith; |
12 | import org.springframework.beans.factory.annotation.Autowired; | 7 | import org.springframework.beans.factory.annotation.Autowired; |
@@ -14,9 +9,9 @@ import org.springframework.context.annotation.PropertySource; | @@ -14,9 +9,9 @@ import org.springframework.context.annotation.PropertySource; | ||
14 | import org.springframework.test.context.ContextConfiguration; | 9 | import org.springframework.test.context.ContextConfiguration; |
15 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
16 | 11 | ||
17 | -import java.io.*; | ||
18 | -import java.net.HttpURLConnection; | ||
19 | -import java.net.Socket; | 12 | +import java.io.ByteArrayOutputStream; |
13 | +import java.io.IOException; | ||
14 | +import java.io.InputStream; | ||
20 | 15 | ||
21 | /** | 16 | /** |
22 | * Created by fruwei on 2016/7/5. | 17 | * Created by fruwei on 2016/7/5. |
-
Please register or login to post a comment