Authored by qinchao

Merge branch 'dev_dock_release'

Showing 19 changed files with 1106 additions and 16 deletions
... ... @@ -2,12 +2,7 @@ package com.monitor.javaserver.schedule;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.contants.AlarmGroupContants;
import com.monitor.common.config.SnsMobileConfig;
import com.monitor.common.service.AlarmMsgService;
import com.monitor.common.util.HttpRestClient;
import com.monitor.javaserver.bigdata.Vdata;
import com.monitor.javaserver.bigdata.ctrl.MonitAlarmCtrl;
import com.util.GetUsersInfoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -19,8 +14,6 @@ import org.springframework.web.util.UriComponentsBuilder;
import javax.annotation.Resource;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
/**
* Created by craig.qin on 2017/10/16.
... ...
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;
private String projectType;
//docker qlcoud
private String clusterId;
private String namespace;
private String serviceName;
}
... ...
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(@Param("id") 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>
<select id="selectById" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from docker_jenkins_record
where id= #{id}
</select>
<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
... ...
... ... @@ -20,7 +20,6 @@
<result column="status" property="status" jdbcType="INTEGER" />
<result column="createTime" property="createTime" jdbcType="VARCHAR" />
<result column="updateTime" property="updateTime" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
... ...
... ... @@ -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.ctrl;
import com.alibaba.fastjson.JSONArray;
import com.model.DockerJenkinsRecord;
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.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.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/dockerProject")
public class DockerProjectCtrl {
@Autowired
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()));
}
}
List<DockerProjectModel> models= dockerProjectService.selectByIdList(idList);
return new BaseResponse<>(models);
}
/**
* 发布工单_jenkins:发起镜像制作
*/
@RequestMapping("/createMirror")
@ResponseBody
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();
return rnt;
}
/**
* 发布工单_jenkins:更新镜像的状态
*/
@RequestMapping("/queryJenkinsStatus")
@ResponseBody
public BaseResponse queryJenkinsStatus(String id) {
DockerJenkinsRecord record=dockerProjectService.queryJenkinsById(id);
BaseResponse rnt=new BaseResponse();
if (record==null){
rnt.setCode(201);
rnt.setMessage("找不到Jenkins要更新的记录");
}else{
rnt.setMessage("Jenkins更新jobid:"+record.getJenkinsJobId());
rnt.setData(record.getJenkinsBuildStatus()==null?"":record.getJenkinsBuildStatus());
}
return rnt;
}
/**
* 发布docker服务
*/
@RequestMapping("/deployMirror")
@ResponseBody
public BaseResponse deployMirror(@RequestBody DockerJenkinsReq dockerJenkinsReq) {
BaseResponse rtn=new BaseResponse();
String clusterId=dockerJenkinsReq.getClusterId();
String namespace=dockerJenkinsReq.getNamespace()==null?"":dockerJenkinsReq.getNamespace();
String serviceName=dockerJenkinsReq.getServiceName();
if(!dockerServerDeployService.checkAppIsExist(clusterId,namespace,serviceName)){
rtn.setCode(201);
rtn.setMessage("Docker服务不存在,请联系运维人员");
return rtn;
}
Map<String,Object> statusMap=dockerServerDeployService.checkAppStatusIsNormal(clusterId,namespace,serviceName);
boolean normalStatusFlag=(boolean)statusMap.get("result");
if(!normalStatusFlag){
rtn.setCode(201);
rtn.setMessage("Docker服务非Normal状态,发布中止!");
return rtn;
}
JSONArray containerJa=(JSONArray)statusMap.get("containerJa");
boolean currentImageLatestFlag=dockerServerDeployService.checkImageLatestWithModifyFlag(clusterId, namespace, serviceName, containerJa,false);
if(currentImageLatestFlag){
//开始重新发布
boolean deploySuccess=dockerServerDeployService.redeployClusterService(clusterId, namespace, serviceName);
if(!deploySuccess){
rtn.setCode(201);
rtn.setMessage("Docker服务重新发布失败,请联系运维人员");
return rtn;
}
}else{
//更改路径,更改之后自动重新部署
currentImageLatestFlag=dockerServerDeployService.checkImageLatestWithModifyFlag(clusterId, namespace, serviceName, containerJa,true);
if(!currentImageLatestFlag){
rtn.setCode(201);
rtn.setMessage("Docker服务修改镜像服务发生异常");
return rtn;
}
}
return rtn;
}
/**
* 发布docker服务
*/
@RequestMapping("/queryDockerServerStatus")
@ResponseBody
public BaseResponse queryDockerServerStatus(@RequestBody DockerJenkinsReq dockerJenkinsReq) {
BaseResponse rtn=new BaseResponse();
String clusterId=dockerJenkinsReq.getClusterId();
String namespace=dockerJenkinsReq.getNamespace()==null?"":dockerJenkinsReq.getNamespace();
String serviceName=dockerJenkinsReq.getServiceName();
String status=dockerServerDeployService.queryAppStatus(clusterId,namespace,serviceName);
rtn.setData(status);
return rtn;
}
}
... ...
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.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;
}
... ...
package com.monitor.other.worksystem.dock.qq;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.*;
/**
* Created by jimi on 2017/9/14.
*/
@Component
public class CcsApiQcloud {
public String pushTest(Map<String, String> paraMap) {
String secretId = "AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK";
//用于加密签名字符串和服务器端验证签名字符串的密钥
String secretKey = "ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW";
//当前时间戳
long timestamp = System.currentTimeMillis() / 1000;
//随机的正整数
int nonce = (new Random()).nextInt(100000);
//实例所在区域
String region = "bj";
//签名方式
String signatureMethod = "HmacSHA256";
paraMap.put("Nonce", String.valueOf(nonce));
paraMap.put("Region", region);
paraMap.put("SecretId", secretId);
paraMap.put("SignatureMethod", signatureMethod);
paraMap.put("Timestamp", String.valueOf(timestamp));
List<String> listPara = new ArrayList<String> ();
StringBuffer buffer = new StringBuffer();
listPara.addAll(paraMap.keySet());
Collections.sort(listPara);
for (String mykey :listPara) {
if (buffer.length() > 0) {
buffer.append("&");
buffer.append(mykey + "=" + paraMap.get(mykey));
} else {
buffer.append(mykey + "=" + paraMap.get(mykey));
}
}
String url = "GETccs.api.qcloud.com/v2/index.php?" + buffer.toString();
String signature = getSignature(url);
return buffer.toString() + "&Signature=" + signature;
}
private String getSignature(String url) {
String returnValue = "";
try {
String secretKey = "ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW";
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(url.getBytes()));
returnValue = URLEncoder.encode(hash, "GBK");
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public static void main(String[] args) {
String imageName="yoho-core/es";
String imageNameShow="es";
CcsApiQcloud ccsApiQcloud = new CcsApiQcloud();
Map<String, String> paraMap = new HashMap<>();
paraMap.put("clusterId", "cls-qz2lezjt");
paraMap.put("Action", "CreateClusterService");
paraMap.put("serviceName", imageNameShow);
paraMap.put("serviceDesc", "testyoho-" + imageNameShow);
paraMap.put("replicas", "1");
paraMap.put("accessType", "ClusterIP");
paraMap.put("portMappings.0.protocol", "TCP");
String port = "9200";
paraMap.put("portMappings.0.containerPort", port);
paraMap.put("portMappings.0.lbPort", port);//防止端口冲突
paraMap.put("portMappings.0.nodePort", "0");//防止端口冲突
paraMap.put("containers.0.containerName", imageNameShow);
paraMap.put("namespace", "default");
paraMap.put("containers.0.image", "ccr.ccs.tencentyun.com/" + imageName);
String jsonObject = ccsApiQcloud.pushTest(paraMap);
System.out.println(jsonObject);
}
}
... ...
package com.monitor.other.worksystem.dock.qq;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* Created by jimi on 2017/11/6.
*/
@Service
public class DockerServerDeployService {
@Autowired
private QqDockerServiceUtils txServer;
private static final Logger logger = LoggerFactory.getLogger(DockerServerDeployService.class);
/**
* 检查是否存在服务
* @param checkedServiceName 需要检查的服务名称
*/
public boolean checkAppIsExist(String clusterId, String namespace,String checkedServiceName){
JSONObject searchResult=txServer.describeClusterService(clusterId,namespace);
if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){
JSONArray jaServices=searchResult.getJSONObject("data").getJSONArray("services");
for(int i=0;i<jaServices.size();i++){
if(checkedServiceName.equals(jaServices.getJSONObject(i).getString("serviceName"))){
return true;
}
}
}
return false;
}
/**
* 检测服务状态
*/
public String queryAppStatus(String clusterId, String namespace, String serviceName) {
String status="";
JSONObject searchResult = txServer.describeClusterServiceInfo(clusterId, namespace,serviceName );
if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){
status=searchResult.getJSONObject("data").getJSONObject("service").getString("status");
}else{
if(searchResult!=null){
status= JSON.toJSONString(searchResult);
}
}
return status;
}
/**
* 检测服务状态是否是Normal
* 返回标志信息和container信息
*
* serviceName.replace("_","-")
*/
public Map<String,Object> checkAppStatusIsNormal(String clusterId, String namespace, String serviceName) {
int requestTime=3;
return checkAppStatusIsNormal(clusterId,namespace,serviceName,requestTime);
}
private Map<String,Object> checkAppStatusIsNormal(String clusterId, String namespace, String serviceName, int requestTime) {
//List<String> myString = Splitter.on(",").omitEmptyStrings().splitToList(serviceNames);
boolean normalFlag=false;
JSONArray containerJa=null;
for (int i = 0; i < requestTime; i++) {
JSONObject searchResult = txServer.describeClusterServiceInfo(clusterId, namespace,serviceName );
logger.info("第 {} 次检测服务 {} 是否正常 ,返回结果 {}", i,serviceName,searchResult);
boolean singleResult = searchResult!=null
&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)
&&searchResult.getJSONObject("data").getJSONObject("service").getString("status").equals("Normal");
boolean result = true;
result = result && singleResult;
if (result == true) {
normalFlag=result;
containerJa=searchResult.getJSONObject("data").getJSONObject("service").getJSONArray("containers");
break;
}
}
Map<String,Object> map=new HashMap<>();
map.put("result",normalFlag);
map.put("containerJa",containerJa);
return map;
}
/**
* 检测服务的container的镜像文件是否是latest,
* 如果不是需要修改镜像的版本为latest
*/
public boolean checkImageLatestWithModifyFlag(String clusterId, String namespace, String serviceName,JSONArray containerJa,boolean modifyFlag){
if(containerJa==null||containerJa.size()<=0){
logger.info("modifyClusterServiceImageFailed: container is empty !");
return false;
}
boolean correctImageFlag=false;
for(int i=0;i<containerJa.size();i++){
JSONObject jo=containerJa.getJSONObject(i);
String image=jo.getString("image");
if(StringUtils.isNotBlank(image)&&image.indexOf("/"+serviceName+":")>0){
int index=image.lastIndexOf(":");
String imageLeft=image.substring(0,index);
String imageRight=image.substring(index+1);
if("latest".equals(imageRight)){
correctImageFlag=true;
break;
}else{
if(modifyFlag){
String containersIndexName="containers."+i+".containerName";
String containersIndexNameValue=jo.getString("containerName");
String containersIndexImage="containers."+i+".image";
String containersIndexImageValue=imageLeft+":latest";
JSONObject searchResult = txServer.modifyClusterServiceImage(clusterId, namespace,serviceName ,containersIndexName,containersIndexNameValue,containersIndexImage,containersIndexImageValue);
if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){
correctImageFlag=true;
}
break;
}
}
}
}
return correctImageFlag;
}
/**
* 重新发布
*/
public boolean redeployClusterService(String clusterId, String namespace, String serviceName){
JSONObject searchResult =txServer.redeployClusterService(clusterId,namespace,serviceName);
if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){
return true;
}else{
logger.error("redeployClusterServiceFailed ,response is {} ",searchResult);
return false;
}
}
public static void main(String args[]){
String image="xxxxxxx/fddfdf:wwwxxx";
int index=image.lastIndexOf(":");
String imageLeft=image.substring(0,index);
String imageRight=image.substring(index+1);
System.out.println(imageLeft);
System.out.println(imageRight);
}
}
... ...
package com.monitor.other.worksystem.dock.qq;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.monitor.common.util.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* Created by jimi on 2017/9/14.
*/
@Service
public class QqDockerServiceUtils {
private static final Logger logger = LoggerFactory.getLogger(QqDockerServiceUtils.class);
private final String requestUrl = "https://ccs.api.qcloud.com/v2/index.php";
@Autowired
private CcsApiQcloud ccsApiQcloud;
/**
* 查询服务列表,该接口返回的列表只包含服务的扼要信息,
* 如果需要获取详细的服务信息,需要调用 DescribeClusterServiceInfo 接口
*/
public JSONObject describeClusterService(String clusterId, String namespace) {
JSONObject resultJson=null;
try{
Map<String, String> paraMap = Maps.newHashMap();
paraMap.put("Action", "DescribeClusterService");
paraMap.put("clusterId", clusterId);
if (StringUtils.isNotEmpty(namespace)) {
paraMap.put("namespace", namespace);
} else {
paraMap.put("namespace", "default");
}
String backString = ccsApiQcloud.pushTest(paraMap);
String uri=requestUrl+"?"+backString;
String qqResponseJsonStr =HttpClientUtil.doget(uri,null,null);
if(StringUtils.isNotBlank(qqResponseJsonStr)){
resultJson = JSONObject.parseObject(qqResponseJsonStr);
}
}catch (Exception e){
logger.error("describeClusterService error",e);
}
return resultJson;
}
/**
* 用于查询单个服务详情
*/
public JSONObject describeClusterServiceInfo(String clusterId, String namespace, String serviceName) {
JSONObject resultJson=null;
try{
Map<String, String> paraMap = Maps.newHashMap();
paraMap.put("clusterId", clusterId);
paraMap.put("serviceName", serviceName);
paraMap.put("Action", "DescribeClusterServiceInfo");
if (StringUtils.isNotEmpty(namespace)) {
paraMap.put("namespace", namespace);
} else {
paraMap.put("namespace", "default");
}
String backString = ccsApiQcloud.pushTest(paraMap);
String uri=requestUrl+"?"+backString;
String qqResponseJsonStr =HttpClientUtil.doget(uri,null,null);
if(StringUtils.isNotBlank(qqResponseJsonStr)){
resultJson = JSONObject.parseObject(qqResponseJsonStr);
}
}catch (Exception e){
logger.error("describeClusterServiceInfo error",e);
}
return resultJson;
}
/**
* 用于更新镜像文件
*/
public JSONObject modifyClusterServiceImage (String clusterId, String namespace, String serviceName
,String containersIndexName,String containersIndexNameValue,String containersIndexImage,String containersIndexImageValue) {
JSONObject resultJson=null;
try{
Map<String, String> paraMap = Maps.newHashMap();
paraMap.put("clusterId", clusterId);
paraMap.put("serviceName", serviceName);
paraMap.put("Action", "ModifyClusterServiceImage");
paraMap.put(containersIndexName,containersIndexNameValue);// 容器name信息
paraMap.put(containersIndexImage,containersIndexImageValue);// 镜像值信息
if (StringUtils.isNotEmpty(namespace)) {
paraMap.put("namespace", namespace);
} else {
paraMap.put("namespace", "default");
}
logger.info("modifyClusterServiceImage paraMap is {}",paraMap);
String backString = ccsApiQcloud.pushTest(paraMap);
String uri=requestUrl+"?"+backString;
String qqResponseJsonStr =HttpClientUtil.doget(uri,null,null);
logger.info("modifyClusterServiceImage backResponse is {}",qqResponseJsonStr);
if(StringUtils.isNotBlank(qqResponseJsonStr)){
resultJson = JSONObject.parseObject(qqResponseJsonStr);
}
}catch (Exception e){
logger.error("modifyClusterServiceImage error",e);
}
return resultJson;
}
/**
* 用于将服务实例重新部署,常用场景为镜像仓库中镜像发生改变后,
* 通过重部署触发服务重新拉取镜像
*/
public JSONObject redeployClusterService(String clusterId, String namespace, String serviceName) {
JSONObject resultJson=null;
try{
Map<String, String> paraMap = Maps.newHashMap();
paraMap.put("clusterId", clusterId);
paraMap.put("serviceName", serviceName);
paraMap.put("Action", "RedeployClusterService");
if (StringUtils.isNotEmpty(namespace)) {
paraMap.put("namespace", namespace);
} else {
paraMap.put("namespace", "default");
}
String backString = ccsApiQcloud.pushTest(paraMap);
String uri=requestUrl+"?"+backString;
String qqResponseJsonStr =HttpClientUtil.doget(uri,null,null);
if(StringUtils.isNotBlank(qqResponseJsonStr)){
resultJson = JSONObject.parseObject(qqResponseJsonStr);
}
}catch (Exception e){
logger.error("redeployClusterService error",e);
}
return resultJson;
}
/* public JSONObject createClusterService(Map<String, String> paraMap) {
paraMap.put("Action", "CreateClusterService");
String backString = ccsApiQcloud.pushTest(paraMap);
String resultjason = httpRestClientService.doPostStringJson(requestUrl, backString);
JSONObject resultJson = JSONObject.parseObject(resultjason);
return resultJson;
}*/
/* public JSONObject deleteClusterService(String clusterId, String namespace, String serviceName) {
Map<String, String> paraMap = Maps.newHashMap();
paraMap.put("clusterId", clusterId);
paraMap.put("serviceName", serviceName);
paraMap.put("Action", "DeleteClusterService");
if (StringUtils.isNotEmpty(namespace)) {
paraMap.put("namespace", namespace);
} else {
paraMap.put("namespace", "default");
}
String backString = ccsApiQcloud.pushTest(paraMap);
String resultjason = httpRestClientService.doPostStringJson(requestUrl, backString);
JSONObject resultJson = JSONObject.parseObject(resultjason);
return resultJson;
}*/
/* public JSONObject describeServiceEvent(String clusterId, String namespace, String serviceName) {
Map<String, String> paraMap = Maps.newHashMap();
paraMap.put("clusterId", clusterId);
paraMap.put("serviceName", serviceName);
paraMap.put("Action", "DescribeServiceEvent");
if (StringUtils.isNotEmpty(namespace)) {
paraMap.put("namespace", namespace);
} else {
paraMap.put("namespace", "default");
}
String backString = ccsApiQcloud.pushTest(paraMap);
String resultjason = httpRestClientService.doPostStringJson(requestUrl, backString);
JSONObject resultJson = JSONObject.parseObject(resultjason);
return resultJson;
}*/
/*
private static Map<String, String> portMap = Maps.newHashMap();
@PostConstruct
private void getUser() {
portMap.put("cobar", "8080");
portMap.put("es", "9300");
portMap.put("java", "8080");
portMap.put("mq", "5672");
portMap.put("mq-common", "5672");
portMap.put("mq-cluster", "5672");
portMap.put("mysql", "3306");
portMap.put("mysql-cms", "3306");
portMap.put("mysql-orders", "3306");
portMap.put("mysql-passport", "3306");
portMap.put("mysql-shops", "3306");
portMap.put("mysql-cobar-split1", "3306");
portMap.put("mysql-cobar-split2", "3306");
portMap.put("mysql-cobar-split3", "3306");
portMap.put("mysql-cobar-split4", "3306");
portMap.put("openresty-java", "80");
portMap.put("twemproxy", "63790");
portMap.put("twemproxy-nosync", "63790");
portMap.put("twemproxy-sync", "63790");
portMap.put("twemproxy-brower", "63790");
portMap.put("twemproxy-bigdata", "63790");
portMap.put("zk", "2181");
portMap.put("java-yoho-gateway", "8080,21212,21211");
portMap.put("java-yoho-users", "8081");
portMap.put("java-uic", "8096");
portMap.put("java-yoho-message", "8086");
portMap.put("java-yohobuy-union", "8088");
portMap.put("java-yohobuy-activity", "8090");
portMap.put("java-yohobuy-resources", "8087");
portMap.put("java-yohobuy-order", "8084");
portMap.put("java-yohobuy-wechat", "8094");
portMap.put("java-yohobuy-product", "8083");
portMap.put("java-yohobuy-platform", "8088");
portMap.put("java-yohobuy-bigdata", "8091");
portMap.put("java-yoho-social", "8095");
portMap.put("java-yohobuy-task", "8084");
portMap.put("java-yohobuy-promotion", "8085");
portMap.put("java-yoho-sns", "8082");
portMap.put("java-yohobuy-brower", "8092");
portMap.put("java-yohobuy-crm", "8093");
portMap.put("java-yoho-push", "8089");
portMap.put("java-cms-push", "8086");
portMap.put("java-yoho-search-producer", "8089");
portMap.put("java-yoho-search-consumer", "8088");
portMap.put("java-yoho-search-service", "8080");
portMap.put("java-yoho-search-adaptor", "8085");
}
private String getPort(String key) {
if (portMap.containsKey(key)) {
return portMap.get(key);
} else
return portMap.get("java");
}*/
}
\ No newline at end of file
... ...
package com.monitor.other.worksystem.service;
import com.model.DockerJenkinsRecord;
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);
DockerJenkinsRecord queryJenkinsById(String id);
}
... ...
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();
String jobName="";
if("NODE".equalsIgnoreCase(dockerJenkinsReq.getProjectType())){
jobName=FastJenkinsUtils.JENKINS_JOB_NODE;
}else if("JAVA".equalsIgnoreCase(dockerJenkinsReq.getProjectType())){
jobName=FastJenkinsUtils.JENKINS_JOB_JAVA;
}
fastJenkinsUtils.createJob(jobName,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 DockerJenkinsRecord queryJenkinsById(String id) {
DockerJenkinsRecord record = dockerJenkinsRecordMapper.selectById(id);
return record;
}
}
... ...
... ... @@ -2,11 +2,6 @@ package com.monitor;
import com.monitor.common.util.HttpRestClient;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -14,9 +9,9 @@ import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by fruwei on 2016/7/5.
... ...