Authored by qinchao

docker多集群发布

... ... @@ -131,23 +131,6 @@ public class DockerProjectCtrl {
}
/**
* 发布工单_jenkins:发起镜像制作
*/
@RequestMapping("/createMirror")
@ResponseBody
public BaseResponse createMirror(@RequestBody DockerJenkinsReq dockerJenkinsReq) {
String buildId=dockerProjectService.jenkinsBuild(dockerJenkinsReq);
BaseResponse rnt=new BaseResponse();
if(StringUtils.isBlank(buildId)){
rnt.setCode(201);
rnt.setMessage("请求镜像失败,返回jobId = " +buildId);
}else{
rnt.setData(buildId);
}
return rnt;
}
/**
* 发布工单_jenkins:自动发布的项目
*/
@RequestMapping("/mirrorAndDeploy")
... ... @@ -457,6 +440,7 @@ public class DockerProjectCtrl {
String clusterIds=dockerJenkinsReq.getClusterId();
String namespace=dockerJenkinsReq.getNamespace()==null?"":dockerJenkinsReq.getNamespace();
String serviceName=dockerJenkinsReq.getServiceName();
String imageStore=dockerJenkinsReq.getImageSource();
for(String clusterId:clusterIds.split(",")){
if(StringUtils.isBlank(clusterId)){
continue;
... ... @@ -476,7 +460,7 @@ public class DockerProjectCtrl {
return rtn;
}
JSONArray containerJa=(JSONArray)statusMap.get("containerJa");
boolean currentImageLatestFlag=dockerServerDeployService.checkImageLatestWithModifyFlag(clusterId, namespace, serviceName, containerJa,false);
boolean currentImageLatestFlag=dockerServerDeployService.checkImageLatestWithModifyFlag(clusterId, namespace, serviceName,imageStore, containerJa,false);
if(currentImageLatestFlag){
//开始重新发布
boolean deploySuccess=dockerServerDeployService.redeployClusterService(clusterId, namespace, serviceName);
... ... @@ -487,7 +471,7 @@ public class DockerProjectCtrl {
}
}else{
//更改路径,更改之后自动重新部署
currentImageLatestFlag=dockerServerDeployService.checkImageLatestWithModifyFlag(clusterId, namespace, serviceName, containerJa,true);
currentImageLatestFlag=dockerServerDeployService.checkImageLatestWithModifyFlag(clusterId, namespace, serviceName,imageStore, containerJa,true);
if(!currentImageLatestFlag){
rtn.setCode(201);
rtn.setMessage("Docker服务修改镜像服务发生异常");
... ... @@ -509,11 +493,17 @@ public class DockerProjectCtrl {
public String getRollbackList(String projectId, String environment) {
DockerProjectModel model=dockerProjectMapper.selectById(Integer.parseInt(projectId));
if(model!=null){
//把灰度和online的版本都搞出来
String imageStore=model.getDockerOnlineImageStore();
if(environment!=null&&environment.toUpperCase().indexOf("GRAY")>0){
List<String> ls =dockerServerDeployService.queryImageListWithImageStore(imageStore,model.getDockerProjectName(),0);
if(StringUtils.isNotBlank(model.getDockerGrayImageStore())){
imageStore=model.getDockerGrayImageStore();
ls.addAll(dockerServerDeployService.queryImageListWithImageStore(imageStore,model.getDockerProjectName(),0));
}
return JSON.toJSONString(dockerServerDeployService.queryImageList(imageStore,model.getDockerProjectName(),0));
return JSON.toJSONString(ls);
}
return "";
}
... ... @@ -559,7 +549,7 @@ public class DockerProjectCtrl {
JSONArray containerJa=(JSONArray)statusMap.get("containerJa");
//更改路径,更改之后自动重新部署
boolean currentImageLatestFlag=dockerServerDeployService.modifyImageStore(clusterId, namespace, serviceName, containerJa,rollbackImageStore);
boolean currentImageLatestFlag=dockerServerDeployService.rollbackImageStore(clusterId, namespace, serviceName, containerJa,rollbackImageStore);
if(!currentImageLatestFlag){
rtn.setCode(201);
rtn.setMessage("Docker服务修改镜像服务发生异常");
... ...
... ... @@ -90,6 +90,15 @@ public class DockerServerDeployService {
/**
* 查询回滚版本
*/
public List<String> queryImageListWithImageStore(String imageStore, String serviceName,int offset) {
List<String> storesWithImagePath=new ArrayList<>();
List<String> stores = this.queryImageList(imageStore,serviceName,offset);
for(String ver:stores){
storesWithImagePath.add("/"+imageStore+"/"+serviceName+":"+ver);
}
return storesWithImagePath;
}
public List<String> queryImageList(String imageStore, String serviceName,int offset) {
List<String> stores=new ArrayList<>();
JSONObject searchResult = txServer.describeImageList(imageStore,serviceName,offset );
... ... @@ -181,7 +190,7 @@ public class DockerServerDeployService {
* 检测服务的container的镜像文件是否是latest,
* 如果不是需要修改镜像的版本为latest
*/
public boolean checkImageLatestWithModifyFlag(String clusterId, String namespace, String serviceName,JSONArray containerJa,boolean modifyFlag){
public boolean checkImageLatestWithModifyFlag(String clusterId, String namespace, String serviceName,String imageStore,JSONArray containerJa,boolean modifyFlag){
if(containerJa==null||containerJa.size()<=0){
logger.info("modifyClusterServiceImageFailed: container is empty !");
return false;
... ... @@ -194,8 +203,9 @@ public class DockerServerDeployService {
if(StringUtils.isNotBlank(image)&&image.indexOf("/"+serviceName+":")>0){
int index=image.lastIndexOf(":");
String imageLeft=image.substring(0,index);
String imageLeft_to_imageStore=image.substring(0,image.lastIndexOf("/"+serviceName+":"));
String imageRight=image.substring(index+1);
if("latest".equals(imageRight)){
if("latest".equals(imageRight)&&imageLeft_to_imageStore.indexOf(imageStore)>0){
correctImageFlag=true;
break;
}else{
... ... @@ -203,7 +213,13 @@ public class DockerServerDeployService {
String containersIndexName="containers."+i+".containerName";
String containersIndexNameValue=jo.getString("containerName");
String containersIndexImage="containers."+i+".image";
String containersIndexImageValue=imageLeft+":latest";
String containersIndexImageValue;
if(imageLeft_to_imageStore.indexOf(imageStore)>0){
containersIndexImageValue=imageLeft+":latest";
}else{
containersIndexImageValue=imageLeft_to_imageStore.substring(0,imageLeft_to_imageStore.lastIndexOf("/"))+"/"+imageStore+"/"+containersIndexNameValue+":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;
... ... @@ -220,7 +236,7 @@ public class DockerServerDeployService {
/**
* 回滚
*/
public boolean modifyImageStore(String clusterId, String namespace, String serviceName,JSONArray containerJa,String rollbackImageStore){
public boolean rollbackImageStore(String clusterId, String namespace, String serviceName,JSONArray containerJa,String rollbackImageStore){
try{
for(int i=0;i<containerJa.size();i++){
JSONObject jo=containerJa.getJSONObject(i);
... ... @@ -228,12 +244,14 @@ public class DockerServerDeployService {
if(StringUtils.isNotBlank(image)&&image.indexOf("/"+serviceName+":")>0){
int index=image.lastIndexOf(":");
String imageLeft=image.substring(0,index);
String imageLeft_to_imageStore=image.substring(0,image.lastIndexOf("/"+serviceName+":"));
String imageLeft_without_imageStore =imageLeft_to_imageStore.substring(0,imageLeft_to_imageStore.lastIndexOf("/"));
//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;
String containersIndexImageValue=imageLeft_without_imageStore+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;
... ... @@ -260,11 +278,14 @@ public class DockerServerDeployService {
}
public static void main(String args[]){
String image="xxxxxxx/fddfdf:wwwxxx";
int index=image.lastIndexOf(":");
String image="xxxxxxx/yoho-online/fddfdf:wwwxxx";
int index=image.lastIndexOf("/"+"fddfdf"+":");
String imageLeft=image.substring(0,index);
String imageRight=image.substring(index+1);
System.out.println(imageLeft);
System.out.println(imageLeft.substring(0,imageLeft.lastIndexOf("/")));
System.out.println(imageRight);
}
}
... ...