|
|
package com.monitor.other.worksystem.ctrl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.contants.AlarmGroupContants;
|
|
|
import com.model.DockerJenkinsRecord;
|
|
|
import com.model.DockerProjectModel;
|
|
|
<<<<<<< HEAD
|
|
|
import com.monitor.model.request.DockerInfoReq;
|
|
|
=======
|
|
|
import com.monitor.common.service.AlarmMsgService;
|
|
|
>>>>>>> e9700f8c84e809432697c320db0ccd40c3f55ebb
|
|
|
import com.monitor.model.request.DockerJenkinsReq;
|
|
|
import com.monitor.model.response.BaseResponse;
|
|
|
import com.monitor.model.response.PageResponse;
|
|
|
import com.monitor.mysql.mapper.DockerProjectMapper;
|
|
|
import com.monitor.other.maliciousip.ctrl.MaliciousIpController;
|
|
|
import com.monitor.other.worksystem.dock.qq.DockerServerDeployService;
|
|
|
import com.monitor.other.worksystem.model.WebHooksReleaseBO;
|
|
|
import com.monitor.other.worksystem.service.DockerProjectService;
|
|
|
import com.offbytwo.jenkins.model.JobWithDetails;
|
|
|
import com.util.GetUsersInfoUtil;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -26,11 +33,15 @@ import org.springframework.web.bind.annotation.ResponseBody; |
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
@Controller
|
|
|
@RequestMapping("/dockerProject")
|
|
|
public class DockerProjectCtrl {
|
|
|
|
|
|
|
|
|
Logger log = LoggerFactory.getLogger(DockerProjectCtrl.class);
|
|
|
|
|
|
@Autowired
|
|
|
private DockerProjectService dockerProjectService;
|
|
|
|
...
|
...
|
@@ -42,6 +53,16 @@ public class DockerProjectCtrl { |
|
|
@Autowired
|
|
|
private DockerProjectMapper dockerProjectMapper;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private AlarmMsgService alarmMsgService;
|
|
|
|
|
|
/**
|
|
|
* 根据报警组名称取成员的手机号码
|
|
|
*/
|
|
|
@Autowired
|
|
|
private GetUsersInfoUtil getUsersInfoUtil;
|
|
|
|
|
|
/**
|
|
|
* 查询docker项目
|
|
|
*/
|
...
|
...
|
@@ -92,23 +113,132 @@ public class DockerProjectCtrl { |
|
|
public BaseResponse gitWebHook(@RequestBody WebHooksReleaseBO webHooksReleaseBO) {
|
|
|
BaseResponse rtn=new BaseResponse<>();
|
|
|
if (!webHooksReleaseBO.getRef().contains("master")&&StringUtils.isBlank(webHooksReleaseBO.getProjectName())) {
|
|
|
rtn.setData("");
|
|
|
log.info("gitWebHook 非master分支或者项目名为空");
|
|
|
return rtn;
|
|
|
}
|
|
|
|
|
|
String mobile_yunwei = getUsersInfoUtil.getMobileByAlarmGroup(AlarmGroupContants.GROUP_NAME_DOCKER);
|
|
|
//log.info("gitWebHook begin");
|
|
|
DockerProjectModel model=dockerProjectMapper.selectByProjectName(webHooksReleaseBO.getProjectName());
|
|
|
//log.info("gitWebHook model {} ",model);
|
|
|
if (isRealChange(webHooksReleaseBO)&&model!=null) {
|
|
|
//Docker-Deploy-GitHook
|
|
|
DockerJenkinsReq dockerJenkinsReq=new DockerJenkinsReq();
|
|
|
dockerJenkinsReq.setDeployName(model.getProjectName());
|
|
|
dockerJenkinsReq.setProjectGitGroup(model.getProjectGitGroup());
|
|
|
dockerJenkinsReq.setDeployName(model.getDockerProjectName());
|
|
|
dockerJenkinsReq.setBranch("master");
|
|
|
dockerJenkinsReq.setUserMail(webHooksReleaseBO.getUser_email());
|
|
|
dockerJenkinsReq.setImageSource(model.getDockerOnlineImageStore());
|
|
|
dockerJenkinsReq.setNodeEnv("yoho-online");
|
|
|
String id=dockerProjectService.jenkinsBuild(dockerJenkinsReq);
|
|
|
rtn.setData(id);
|
|
|
//异步
|
|
|
log.info("gitWebHook isRealChange is true ");
|
|
|
CompletableFuture.runAsync(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
//Docker-Deploy-GitHook
|
|
|
DockerJenkinsReq dockerJenkinsReq=new DockerJenkinsReq();
|
|
|
dockerJenkinsReq.setProjectName(model.getProjectName());
|
|
|
dockerJenkinsReq.setProjectJobType(model.getProjectJobType());
|
|
|
dockerJenkinsReq.setProjectGitGroup(model.getProjectGitGroup());
|
|
|
dockerJenkinsReq.setDeployName(model.getDockerProjectName());
|
|
|
dockerJenkinsReq.setBranch("master");
|
|
|
dockerJenkinsReq.setUserMail(webHooksReleaseBO.getUser_email());
|
|
|
dockerJenkinsReq.setImageSource(model.getDockerOnlineImageStore());
|
|
|
dockerJenkinsReq.setNodeEnv("yoho-online");
|
|
|
dockerJenkinsReq.setProjectPort(model.getPort());
|
|
|
dockerJenkinsReq.setClusterId(model.getDockerOnlineClusterId());
|
|
|
|
|
|
//开始创建镜像
|
|
|
log.info("gitWebHook run {} ",dockerJenkinsReq);
|
|
|
String buildId=dockerProjectService.jenkinsBuild(dockerJenkinsReq);
|
|
|
|
|
|
//等待镜像创建完成:查询状态
|
|
|
try {
|
|
|
Thread.sleep(2*60*1000);//等待2分钟再查询
|
|
|
} catch (InterruptedException e) {
|
|
|
log.error("gitWebHook thread sleep error",e);
|
|
|
}
|
|
|
|
|
|
//查询镜像
|
|
|
boolean okFlag=false;
|
|
|
String alarmMsg="镜像制作失败";
|
|
|
String alarmTypeSuccess="service_exception";//"docker_auto_success";
|
|
|
String alarmTypeFail="service_exception";//"docker_auto_error";
|
|
|
for(int i=0;i<20;i++){
|
|
|
DockerJenkinsRecord record=dockerProjectService.queryJenkinsById(buildId);
|
|
|
if(record==null){
|
|
|
//发送短信
|
|
|
alarmMsg="获取镜像状态信息为空,镜像buildId is"+buildId;
|
|
|
break ;
|
|
|
}
|
|
|
String status=record.getJenkinsBuildStatus();
|
|
|
if (StringUtils.isBlank(status)){
|
|
|
//继续查询
|
|
|
try {
|
|
|
Thread.sleep(1*60*1000);//等待1分钟再查询
|
|
|
} catch (InterruptedException e) {
|
|
|
log.error("gitWebHook thread sleep error",e);
|
|
|
}
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if ("SUCCESS".equalsIgnoreCase(status.trim())){
|
|
|
//成功
|
|
|
okFlag=true;
|
|
|
break;
|
|
|
}else{
|
|
|
okFlag=false;
|
|
|
alarmMsg="镜像制作失败,镜像状态为 "+status+",镜像buildId is"+buildId;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//镜像制作失败
|
|
|
if(!okFlag){
|
|
|
//sendMsg
|
|
|
alarmMsg=("["+model.getProjectName()+"]"+alarmMsg);
|
|
|
alarmMsgService.sendSms(alarmTypeFail, alarmMsg , mobile_yunwei);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
//调用发布
|
|
|
dockerJenkinsReq.setClusterId(model.getDockerOnlineClusterId());
|
|
|
dockerJenkinsReq.setNamespace(model.getDockerOnlineNameSpace());
|
|
|
dockerJenkinsReq.setServiceName(model.getDockerProjectName());
|
|
|
BaseResponse deployResp=deployMirror(dockerJenkinsReq);
|
|
|
if(deployResp==null){
|
|
|
//发布失败
|
|
|
alarmMsg=("["+model.getProjectName()+"]调用腾讯云发布接口失败");
|
|
|
alarmMsgService.sendSms(alarmTypeFail,alarmMsg , mobile_yunwei);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if(deployResp.getCode()!=200){
|
|
|
//发布失败
|
|
|
alarmMsg=("["+model.getProjectName()+"]查询腾讯云发布状态接口失败");
|
|
|
alarmMsgService.sendSms(alarmTypeFail,alarmMsg , mobile_yunwei);
|
|
|
return;
|
|
|
}else{
|
|
|
//发布成功
|
|
|
boolean projectStatus=false;
|
|
|
for(int i=0;i<60;i++){
|
|
|
BaseResponse deployStatusResp=queryDockerServerStatus(dockerJenkinsReq);
|
|
|
if(deployStatusResp!=null&&"NORMAL".equalsIgnoreCase((String)deployStatusResp.getData())){
|
|
|
//发布之后的状态成功
|
|
|
projectStatus=true;
|
|
|
alarmMsg=("["+model.getProjectName()+"]发布成功!");
|
|
|
alarmMsgService.sendSms(alarmTypeSuccess,alarmMsg , mobile_yunwei);
|
|
|
break;
|
|
|
}else{
|
|
|
try {
|
|
|
Thread.sleep(1*20*1000);//等待20秒再查询
|
|
|
} catch (InterruptedException e) {
|
|
|
log.error("gitWebHook queryDockerServerStatus thread sleep error",e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(!projectStatus){
|
|
|
alarmMsg=("["+model.getProjectName()+"]发布状态为非normal,请检查!");
|
|
|
alarmMsgService.sendSms(alarmTypeFail,alarmMsg , mobile_yunwei);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
return rtn;
|
...
|
...
|
@@ -203,7 +333,6 @@ public class DockerProjectCtrl { |
|
|
return rtn;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 发布docker服务
|
|
|
*/
|
...
|
...
|
|