Authored by qinchao

dock发布功能

package com.monitor.other.worksystem.dock;
import com.alibaba.fastjson.JSONArray;
import com.model.ReleaseWorkJob;
import com.monitor.model.response.BaseResponse;
import com.monitor.other.worksystem.dock.qq.DockerServerDeployService;
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;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/work/docker")
public class DockerServerDeployCtrl {
@Autowired
private ReleaseService releaseService;
@Autowired
private DockerServerDeployService dockerServerDeployService;
/**
* 发布工单_jenkins:发起镜像制作
*/
@RequestMapping("/createMirror")
@ResponseBody
public BaseResponse createMirror() {
ReleaseWorkJob releaseWorkJob=new ReleaseWorkJob();
int jobId=releaseService.jenkinsBuild(releaseWorkJob);
BaseResponse rnt=new BaseResponse();
rnt.setData(jobId);
return rnt;
}
/**
* 发布docker服务
*/
@RequestMapping("/deployMirror")
@ResponseBody
public BaseResponse deployMirror() {
BaseResponse rtn=new BaseResponse();
String clusterId="cls-ro6kl3cp";
String namespace="";
String serviceName="node-yohoblk-wap";
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;
}
}
... ...
... ... @@ -14,21 +14,6 @@ public class JenkinsCtrl {
@Autowired
private ReleaseService releaseService;
/**
* 发布工单_jenkins:发起镜像制作
*/
@RequestMapping("/createMirror")
@ResponseBody
public BaseResponse createMirror() {
ReleaseWorkJob releaseWorkJob=new ReleaseWorkJob();
int jobId=releaseService.jenkinsBuild(releaseWorkJob);
BaseResponse rnt=new BaseResponse();
rnt.setData(jobId);
System.out.println("jobId is "+jobId);
return rnt;
}
/**
* 发布工单_jenkins:更新镜像的状态
*/
... ...
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);
System.out.println("https://ccs.api.qcloud.com/v2/index.php?" + buffer.toString() + "&Signature=" + signature);
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.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;
}
/**
* 检测服务状态是否是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
... ...