Authored by qinchao

docker回滚

... ... @@ -161,6 +161,7 @@ public class DockerProjectCtrl {
String userMail=dockerJenkinsReq.getUserMail();
String branch=dockerJenkinsReq.getBranch();
String nodeEnv=dockerJenkinsReq.getNodeEnv();
String work_release_id=dockerJenkinsReq.getReleaseWorkId();
StringBuilder buildIdSb=new StringBuilder();
StringBuilder failSb=new StringBuilder();
for(String id:ids.split(",")){
... ... @@ -177,6 +178,7 @@ public class DockerProjectCtrl {
dockerJenkinsReqSingle.setProjectPort(model.getPort());
//页面传过来的参数
dockerJenkinsReqSingle.setReleaseWorkId(work_release_id);
dockerJenkinsReqSingle.setUserMail(userMail);
dockerJenkinsReqSingle.setBranch(branch);
dockerJenkinsReqSingle.setNodeEnv(nodeEnv);
... ... @@ -457,6 +459,69 @@ public class DockerProjectCtrl {
}
/**
* 获取发布docker的回滚版本
*/
@RequestMapping(value = "/getRollbackList")
@ResponseBody
public String getRollbackList(String projectId, String environment) {
DockerProjectModel model=dockerProjectMapper.selectById(Integer.parseInt(projectId));
if(model!=null){
String imageStore=model.getDockerOnlineImageStore();
if(environment!=null&&environment.toUpperCase().indexOf("GRAY")>0){
imageStore=model.getDockerGrayImageStore();
}
return JSON.toJSONString(dockerServerDeployService.queryImageList(imageStore,model.getDockerProjectName()));
}
return "";
}
/**
* docker的回滚
*/
@RequestMapping(value = "/rollback")
@ResponseBody
public BaseResponse rollback(String projectId, String environment, String rollbackImageStore) {
BaseResponse rtn=new BaseResponse();
DockerProjectModel model=dockerProjectMapper.selectById(Integer.parseInt(projectId));
if(model==null){
rtn.setCode(201);
rtn.setMessage("找不到docker project配置信息!");
return rtn;
}
String clusterId=model.getDockerOnlineClusterId();
String namespace=model.getDockerOnlineNameSpace();
if(environment!=null&&environment.toUpperCase().indexOf("GRAY")>0){
clusterId=model.getDockerGrayClusterId();
namespace=model.getDockerGrayNameSpace();
}
String serviceName=model.getDockerProjectName();
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.modifyImageStore(clusterId, namespace, serviceName, containerJa,rollbackImageStore);
if(!currentImageLatestFlag){
rtn.setCode(201);
rtn.setMessage("Docker服务修改镜像服务发生异常");
return rtn;
}
return rtn;
}
/**
* 发布docker服务
*/
@RequestMapping("/queryDockerServerStatus")
... ...
... ... @@ -15,6 +15,16 @@ import java.util.*;
public class CcsApiQcloud {
public String pushTest(Map<String, String> paraMap) {
String uri="GETccs.api.qcloud.com/v2/index.php?";
return pushTestWithUrl(paraMap,uri);
}
public String pushTestForImageStore(Map<String, String> paraMap) {
String uri="GETccr.api.qcloud.com/v2/index.php?";
return pushTestWithUrl(paraMap,uri);
}
private String pushTestWithUrl(Map<String, String> paraMap,String uri){
String secretId = "AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK";
//用于加密签名字符串和服务器端验证签名字符串的密钥
String secretKey = "ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW";
... ... @@ -45,7 +55,7 @@ public class CcsApiQcloud {
buffer.append(mykey + "=" + paraMap.get(mykey));
}
}
String url = "GETccs.api.qcloud.com/v2/index.php?" + buffer.toString();
String url = uri + buffer.toString();
String signature = getSignature(url);
return buffer.toString() + "&Signature=" + signature;
}
... ...
... ... @@ -10,7 +10,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
... ... @@ -42,6 +44,33 @@ public class DockerServerDeployService {
}
/**
* 查询回滚版本
*/
public List<String> queryImageList(String imageStore, String serviceName) {
List<String> stores=new ArrayList<>();
JSONObject searchResult = txServer.describeImageList(imageStore,serviceName );
if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){
JSONArray ja=searchResult.getJSONObject("data").getJSONArray("tagInfo");
if(ja!=null&&ja.size()>0){
for(int i=0;i<ja.size();i++){
String tagName=ja.getJSONObject(i).getString("tagName");
if(!"latest".equals(tagName)){
stores.add(tagName);
}
}
}
}
return stores;
}
/**
* 回滚
*/
public void rollback(String imageStore){
}
/**
* 检测服务状态
*/
public String queryAppStatus(String clusterId, String namespace, String serviceName) {
... ... @@ -134,6 +163,35 @@ public class DockerServerDeployService {
}
/**
* 回滚
*/
public boolean modifyImageStore(String clusterId, String namespace, String serviceName,JSONArray containerJa,String rollbackImageStore){
try{
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);
String containersIndexName="containers."+i+".containerName";
String containersIndexNameValue=jo.getString("containerName");
String containersIndexImage="containers."+i+".image";
String containersIndexImageValue=imageLeft+":"+rollbackImageStore;
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)){
return true;
}
}
}
}catch (Exception e){
return false;
}
return false;
}
/**
* 重新发布
*/
public boolean redeployClusterService(String clusterId, String namespace, String serviceName){
... ...
... ... @@ -19,6 +19,7 @@ public class QqDockerServiceUtils {
private static final Logger logger = LoggerFactory.getLogger(QqDockerServiceUtils.class);
private final String requestUrl = "https://ccs.api.qcloud.com/v2/index.php";
private final String requestUrl_image_store = "https://ccr.api.qcloud.com/v2/index.php";
@Autowired
private CcsApiQcloud ccsApiQcloud;
... ... @@ -138,6 +139,32 @@ public class QqDockerServiceUtils {
return resultJson;
}
/**
* 用于查询镜像列表
*/
public JSONObject describeImageList(String imageStore, String serviceName) {
if(StringUtils.isBlank(imageStore)||StringUtils.isBlank(serviceName)){
return null;
}
JSONObject resultJson=null;
try{
Map<String, String> paraMap = Maps.newHashMap();
paraMap.put("reponame", imageStore+"/"+serviceName);
paraMap.put("Action", "GetTagList");
String backString = ccsApiQcloud.pushTestForImageStore(paraMap);
String uri=requestUrl_image_store+"?"+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 createClusterService(Map<String, String> paraMap) {
paraMap.put("Action", "CreateClusterService");
String backString = ccsApiQcloud.pushTest(paraMap);
... ...