Authored by qinchao

dock发布功能

@@ -14,4 +14,11 @@ public class DockerJenkinsReq { @@ -14,4 +14,11 @@ public class DockerJenkinsReq {
14 private String userName; 14 private String userName;
15 private String userMail; 15 private String userMail;
16 private String releaseWorkId; 16 private String releaseWorkId;
  17 + private String projectType;
  18 +
  19 +
  20 + //docker qlcoud
  21 + private String clusterId;
  22 + private String namespace;
  23 + private String serviceName;
17 } 24 }
@@ -65,9 +65,9 @@ public class DockerBuildCtrl { @@ -65,9 +65,9 @@ public class DockerBuildCtrl {
65 * 65 *
66 * @return 66 * @return
67 */ 67 */
68 - @RequestMapping(value = "/deploy") 68 + @RequestMapping(value = "/build")
69 @ResponseBody 69 @ResponseBody
70 - public ModelAndView deploy(String project_id, String project_name, String environment_name, String operate_name, String branch_name, String rollbackfile_name,String workid_name, Model model, HttpSession session) { 70 + public ModelAndView build(String project_id, String project_name, String environment_name, String operate_name, String branch_name, String rollbackfile_name,String workid_name, Model model, HttpSession session) {
71 Map map = new HashMap<>(); 71 Map map = new HashMap<>();
72 map.put("ids",project_id); 72 map.put("ids",project_id);
73 BaseResponse response=httpRestClient.defaultGet("/dockerProject/getDockerProjectByIds", BaseResponse.class, map); 73 BaseResponse response=httpRestClient.defaultGet("/dockerProject/getDockerProjectByIds", BaseResponse.class, map);
@@ -83,7 +83,7 @@ public class DockerBuildCtrl { @@ -83,7 +83,7 @@ public class DockerBuildCtrl {
83 /** 83 /**
84 * 镜像制作 84 * 镜像制作
85 */ 85 */
86 - @RequestMapping(value = "createMirror") 86 + @RequestMapping(value = "/createMirror")
87 @ResponseBody 87 @ResponseBody
88 public BaseResponse createMirror(DockerJenkinsReq dockerJenkinsReq,HttpSession session) { 88 public BaseResponse createMirror(DockerJenkinsReq dockerJenkinsReq,HttpSession session) {
89 User user = (User) session.getAttribute("user"); 89 User user = (User) session.getAttribute("user");
@@ -91,4 +91,34 @@ public class DockerBuildCtrl { @@ -91,4 +91,34 @@ public class DockerBuildCtrl {
91 dockerJenkinsReq.setUserMail(user.getEmail()); 91 dockerJenkinsReq.setUserMail(user.getEmail());
92 return httpRestClient.defaultPost("/dockerProject/createMirror",dockerJenkinsReq,BaseResponse.class); 92 return httpRestClient.defaultPost("/dockerProject/createMirror",dockerJenkinsReq,BaseResponse.class);
93 } 93 }
  94 +
  95 + /**
  96 + * 镜像制作
  97 + */
  98 + @RequestMapping(value = "/queryJenkinsStatus")
  99 + @ResponseBody
  100 + public BaseResponse queryJenkinsStatus(String buildId) {
  101 + Map map = new HashMap<>();
  102 + map.put("id",buildId);
  103 + BaseResponse response=httpRestClient.defaultGet("/dockerProject/queryJenkinsStatus", BaseResponse.class, map);
  104 + return response;
  105 + }
  106 +
  107 + /**
  108 + * qq发布
  109 + */
  110 + @RequestMapping(value = "/deployMirror")
  111 + @ResponseBody
  112 + public BaseResponse deployMirror(DockerJenkinsReq dockerJenkinsReq) {
  113 + return httpRestClient.defaultPost("/dockerProject/deployMirror",dockerJenkinsReq,BaseResponse.class);
  114 + }
  115 +
  116 + /**
  117 + * qq发布
  118 + */
  119 + @RequestMapping(value = "/queryDockerServerStatus")
  120 + @ResponseBody
  121 + public BaseResponse queryDockerServerStatus(DockerJenkinsReq dockerJenkinsReq) {
  122 + return httpRestClient.defaultPost("/dockerProject/queryDockerServerStatus",dockerJenkinsReq,BaseResponse.class);
  123 + }
94 } 124 }
@@ -147,228 +147,213 @@ @@ -147,228 +147,213 @@
147 </script> 147 </script>
148 <script> 148 <script>
149 var dockerProjects = '${docker_project_list}'; 149 var dockerProjects = '${docker_project_list}';
150 - var myArray;  
151 - var removeArray = new Array();  
152 150
153 function buildDocker(dockerProject){ 151 function buildDocker(dockerProject){
154 //发起创建镜像的请求 152 //发起创建镜像的请求
155 - // builderDocker_createMirror(dockerProject);  
156 -  
157 - //轮询镜像是否成功  
158 -alert("轮询开始");  
159 - //开始deploy  
160 -  
161 - //发布是否成功查询 153 + builderDocker_createMirror(dockerProject);
162 154
163 } 155 }
164 156
165 - function builderDocker_createMirror(dockerProject){  
166 - writeToText(dockerProject.id,"开始jenkins制作镜像....");  
167 - var url= contextPath + 'dockerBuild/createMirror'; 157 + function builderDocker_queryServiceStatus(dockerProject){
  158 + var url = contextPath + 'dockerBuild/queryDockerServerStatus';
  159 + writeToText(dockerProject.id,"开始查询服务启动..........");
  160 + var count = 0;
  161 + var errorTimes = 0;
  162 + var intervalIndex = setInterval(function() {
168 $.ajax({ 163 $.ajax({
169 url: url, 164 url: url,
170 type: 'POST', 165 type: 'POST',
  166 + data:getDockerRequestParam(dockerProject),
171 dataType: 'json', 167 dataType: 'json',
172 - data:{  
173 - appNames:dockerProject.projectName,  
174 - nodeEnv:$("#environment_name").val(),  
175 - branch:$("#branch_name").val(),  
176 - releaseWorkId:$("#workid_name").val()  
177 - },  
178 - async: false, 168 + timeout : 0,
179 success: function (data) { 169 success: function (data) {
180 if (!data || data.code != 200) { 170 if (!data || data.code != 200) {
181 - writeToText(dockerProject.id,"....jenkins制作镜像请求失败!"); 171 + errorTimes++;
  172 + if(errorTimes>5){
  173 + clearInterval(intervalIndex);
  174 + writeToText(dockerProject.id,"发生错误data is null or data code !=200,查询服务启动程序终止..........");
  175 + writeToText(dockerProject.id,"data is "+data);
182 responseError(data); 176 responseError(data);
183 }else{ 177 }else{
184 - writeToText(dockerProject.id,"....jenkins制作镜像请求成功"); 178 + writeToText(dockerProject.id,"发生错误data is null or data code !=200,查询服务启动,继续获取数据,请耐心等待, errorTimes =."+errorTimes);
  179 + writeToText(dockerProject.id,"data is "+data);
185 } 180 }
  181 + }else{
  182 + if(data.data!=null&&((data.data).toUpperCase()=="NORMAL")){
  183 + clearInterval(intervalIndex);
  184 + writeToText(dockerProject.id,"查询服务启动完成..........over!");
  185 + }else{
  186 + writeToText(dockerProject.id,"............查询次数............"+count+"......当前状态---->"+data.data);
  187 + }
  188 + }
  189 + count += 1;
186 }, 190 },
187 error: function (e) { 191 error: function (e) {
188 - writeToText(dockerProject.id,"....jenkins制作镜像请求失败,发生异常!");  
189 - requestError(url); 192 + errorTimes++;
  193 + if(errorTimes>5){
  194 + clearInterval(intervalIndex);
  195 + writeToText(dockerProject.id,"发生错误error,查询服务启动程序终止..........");
  196 + writeToText(dockerProject.id,"error is "+e);
  197 + requestError(dockerProject.id,url);
  198 + }else{
  199 + writeToText(dockerProject.id,"发生错误error,查询服务启动,继续获取数据,请耐心等待,errorTimes =."+errorTimes);
  200 + writeToText(dockerProject.id,"error is "+e);
190 } 201 }
191 - });  
192 } 202 }
193 -  
194 - //写入消息  
195 - function writeToText(id,msg){  
196 - var d = $("#resultArea_"+id).val();  
197 - $("#resultArea_"+id).html(d + "&#13" + msg);  
198 - var scrollTop = $("#resultArea_"+id)[0].scrollHeight;  
199 - $("#resultArea_"+id).scrollTop(scrollTop); 203 + });
  204 + }, 10000);
200 } 205 }
201 206
202 - $(function () {  
203 - console.log(dockerProjects);  
204 - if(dockerProjects!=null&&dockerProjects.length>0){  
205 - console.log(dockerProjects);  
206 - var dockerProjectsJson=JSON.parse(dockerProjects);  
207 - console.log(dockerProjectsJson);  
208 - //docek发布  
209 - for(var i=0;i<dockerProjectsJson.length;i++){  
210 - buildDocker(dockerProjectsJson[i]); 207 + function getDockerRequestParam(dockerProject){
  208 + var frontParam={};
  209 + frontParam.serviceName=dockerProject.dockerProjectName;
  210 + if($("#environment_name").val().indexOf("GRAY")>=0){
  211 + frontParam.clusterId=dockerProject.dockerGrayClusterId;
  212 + frontParam.namespace =dockerProject.dockerGrayNameSpace;
  213 + }else{
  214 + frontParam.clusterId=dockerProject.dockerOnlineClusterId;
  215 + frontParam.namespace =dockerProject.dockerOnlineNameSpace;
211 } 216 }
  217 + return frontParam;
212 } 218 }
213 219
214 - return ;  
215 -  
216 - var messageListHidden = $("#messageListHidden").val();  
217 - myArray = messageListHidden.split(",");  
218 -  
219 - var interval = setInterval(function () {//查后台,展示数据  
220 - $.each(myArray, function (idx, val) {  
221 - if(jQuery.inArray(val, removeArray) != -1){  
222 - return;  
223 - }  
224 - if(val == ""){  
225 - return;  
226 - }  
227 - var cproject = $("#currentProject_"+val).val(); 220 + function builderDocker_deploy(dockerProject){
  221 + writeToText(dockerProject.id,"开始docker发布....");
  222 + var url= contextPath + 'dockerBuild/deployMirror';
228 $.ajax({ 223 $.ajax({
229 - url: contextPath + 'project/getbuildmsg?messageid=' + val+"&project="+cproject, 224 + url: url,
230 type: 'POST', 225 type: 'POST',
231 dataType: 'json', 226 dataType: 'json',
232 - success: function (data3) {  
233 - var obj2 = eval("(" + data3 + ")");  
234 - var messagedata = obj2.data;  
235 -  
236 - var currproject = messagedata.currentProject;  
237 - if (currproject != cproject) {  
238 - $("#currentProject_"+val).val(currproject);//更换当前的项目  
239 - $("#resultArea_" + val).val("")  
240 - }  
241 -  
242 - if ((messagedata.message != "") && (messagedata.message != null) && (messagedata.message != undefined)) {  
243 - var d = $("#resultArea_" + val).val();  
244 - $("#resultArea_"+ val).val(d + messagedata.message);  
245 - var scrollTop = $("#resultArea_" + val)[0].scrollHeight;  
246 - $("#resultArea_" + val).scrollTop(scrollTop);  
247 - }  
248 -  
249 - //code为2 ,则结束  
250 - var code = obj2.code;  
251 - var message = obj2.message;  
252 - //只有状态2,3,4才表示系统部署成功与否  
253 - if (code == 2 || code == 3 || code == 4) {  
254 - document.getElementById("task-info-div_" + val).innerHTML = "<strong>'" + message + "'</strong>";  
255 - removeArray.push(val);  
256 - $("#cancel-btn_"+val).hide(); 227 + data:getDockerRequestParam(dockerProject),
  228 + async: false,
  229 + success: function (data) {
  230 + if (!data || data.code != 200) {
  231 + writeToText(dockerProject.id,"....docker发布请求失败!");
  232 + responseError(dockerProject.id,data);
  233 + }else{
  234 + writeToText(dockerProject.id,"....docker发布请求成功");
  235 + builderDocker_queryServiceStatus(dockerProject);
257 } 236 }
258 -  
259 }, 237 },
260 error: function (e) { 238 error: function (e) {
  239 + writeToText(dockerProject.id,"....docker发布请求失败,发生异常!");
  240 + requestError(dockerProject.id,url);
261 } 241 }
262 }); 242 });
263 - });  
264 - }, 3000);  
265 - });  
266 -  
267 - /**  
268 - * 取消  
269 - */  
270 - function cancelBuild(messageid) { 243 + }
271 244
272 - var jsondata = {"id": messageid}; 245 + function builderDocker_createMirror(dockerProject){
  246 + writeToText(dockerProject.id,"开始jenkins制作镜像....");
  247 + var url= contextPath + 'dockerBuild/createMirror';
273 $.ajax({ 248 $.ajax({
274 - url: contextPath + 'project/cancelBuild', 249 + url: url,
275 type: 'POST', 250 type: 'POST',
276 dataType: 'json', 251 dataType: 'json',
277 - data: jsondata, 252 + data:{
  253 + projectType:dockerProject.projectType,
  254 + appNames:dockerProject.projectName,
  255 + nodeEnv:$("#environment_name").val(),
  256 + branch:$("#branch_name").val(),
  257 + releaseWorkId:$("#workid_name").val()
  258 + },
  259 + async: false,
278 success: function (data) { 260 success: function (data) {
  261 + if (!data || data.code != 200) {
  262 + writeToText(dockerProject.id,"....jenkins制作镜像请求失败!");
  263 + responseError(dockerProject.id,data);
  264 + }else{
  265 + writeToText(dockerProject.id,"....jenkins制作镜像请求成功,buildId is "+data.data);
  266 +
  267 + builderDocker_queryJenkinsStatus(data.data,dockerProject);
  268 + }
279 }, 269 },
280 error: function (e) { 270 error: function (e) {
  271 + writeToText(dockerProject.id,"....jenkins制作镜像请求失败,发生异常!");
  272 + requestError(dockerProject.id,url);
281 } 273 }
282 }); 274 });
283 } 275 }
284 276
285 - //做停止用  
286 - var setIntervalIndex;  
287 - //调用30次后停止调用  
288 - var setIntervalCount = 0;  
289 -  
290 - /**  
291 - * 集成测试  
292 - */  
293 - function testexecute(){  
294 - $("#testingdiv").hide();  
295 - setIntervalCount = 0;  
296 - var value;  
297 - var envi = $("#environment_name").val();  
298 - if(envi == "aws"){  
299 - value = 6;  
300 - }else if(envi == "qcloud"){  
301 - value = 7;  
302 - }else{  
303 - value = 5;  
304 - }  
305 - if(setIntervalIndex != null && setIntervalIndex != undefined){  
306 - clearInterval(setIntervalIndex);  
307 - setIntervalIndex = null;  
308 - } 277 + function builderDocker_queryJenkinsStatus(buildId,dockerProject) {
  278 + var url = contextPath + 'dockerBuild/queryJenkinsStatus';
  279 + writeToText(dockerProject.id,"开始查询镜像制作是否完成..........");
  280 + var count = 0;
  281 + var errorTimes = 0;
  282 + var intervalIndex = setInterval(function() {
309 $.ajax({ 283 $.ajax({
310 - url: contextPath + 'project/testexecute?env=' + value, 284 + url: url,
311 type: 'POST', 285 type: 'POST',
  286 + data:{
  287 + buildId:buildId
  288 + },
312 dataType: 'json', 289 dataType: 'json',
313 - success: function (returnVal) {  
314 - $("#btnTest").attr('disabled',true);//设置disabled属性为true,按钮不可用  
315 - if(returnVal == null || returnVal.data == null || returnVal.data == ""){  
316 - alert("http://qmc.yohops.com:9999/autoTask/executeApiAutoTask,参数env="+value+",post请求失败!");  
317 - $("#btnTest").removeAttr("disabled"); //移除disabled属性  
318 - return; 290 + timeout : 0,
  291 + success: function (data) {
  292 + if (!data || data.code != 200) {
  293 + errorTimes++;
  294 + if(errorTimes>5){
  295 + clearInterval(intervalIndex);
  296 + writeToText(dockerProject.id,"发生错误data is null or data code !=200,查询镜像制作程序终止..........");
  297 + writeToText(dockerProject.id,"data is "+data);
  298 + responseError(data);
  299 + }else{
  300 + writeToText(dockerProject.id,"发生错误data is null or data code !=200,继续获取数据,请耐心等待, errorTimes =."+errorTimes);
  301 + writeToText(dockerProject.id,"data is "+data);
319 } 302 }
320 - var json = returnVal.data;  
321 - //获取到id  
322 - var id = json.id;  
323 - setIntervalIndex = setInterval(function(){testget(id);}, 1000 * 60); 303 + }else{
  304 + if(!("SUCCESS"==data.data)){
  305 + writeToText(dockerProject.id,"............查询次数............"+count);
  306 + }else{
  307 + clearInterval(intervalIndex);
  308 + writeToText(dockerProject.id,"镜像制作完成..........");
  309 + //开始deploy
  310 + builderDocker_deploy(dockerProject);
  311 + }
  312 + }
  313 + count += 1;
324 }, 314 },
325 error: function (e) { 315 error: function (e) {
  316 + errorTimes++;
  317 + if(errorTimes>5){
  318 + clearInterval(intervalIndex);
  319 + writeToText(dockerProject.id,"发生错误error,查询镜像制作程序终止..........");
  320 + writeToText(dockerProject.id,"error is "+e);
  321 + requestError(dockerProject.id,url);
  322 + }else{
  323 + writeToText(dockerProject.id,"发生错误error,继续获取数据,请耐心等待,errorTimes =."+errorTimes);
  324 + writeToText(dockerProject.id,"error is "+e);
  325 + }
326 } 326 }
327 }); 327 });
  328 + }, 10000);
328 } 329 }
329 330
330 - /**  
331 - * 集成测试  
332 - */  
333 - function testget(id){  
334 - if(id == null || id == undefined){  
335 - return;  
336 - }  
337 - $.ajax({  
338 - url: contextPath + 'project/testget?id=' + id,  
339 - type: 'POST',  
340 - dataType: 'json',  
341 - success: function (returnVal) {  
342 - if(returnVal == null || returnVal.data == null || returnVal.data == ""){  
343 - setIntervalCount = setIntervalCount + 1;  
344 - if(setIntervalCount >= 30){  
345 - $("#btnTest").removeAttr("disabled"); //移除disabled属性  
346 - clearInterval(setIntervalIndex);  
347 - }  
348 - return; 331 + //写入消息
  332 + function writeToText(id,msg){
  333 + var d = $("#resultArea_"+id).val();
  334 + $("#resultArea_"+id).html(d + "&#13" + msg);
  335 + var scrollTop = $("#resultArea_"+id)[0].scrollHeight;
  336 + $("#resultArea_"+id).scrollTop(scrollTop);
349 } 337 }
350 - var message = returnVal.message;  
351 - var code = returnVal.code;  
352 338
353 - $("#testingdiv").show();  
354 - $("#testingresult").val("id:" + id + ";message:" + message + ";code:"+code);  
355 - $("#btnTest").removeAttr("disabled"); //移除disabled属性  
356 - clearInterval(setIntervalIndex);  
357 - },  
358 - error: function (e) {  
359 - }  
360 - }); 339 + //请求接口异常
  340 + function requestError(id,api){
  341 + writeToText(id,'系统异常:' + api);
361 } 342 }
362 -</script>  
363 343
364 -<script>  
365 - document.onkeydown = function()  
366 - {  
367 - if(event.keyCode==116 || event.keyCode==8 || (event.ctrlKey && event.keyCode==82) || event.keyCode == 13) {  
368 - event.keyCode=0;  
369 - event.returnValue = false; 344 + $(function () {
  345 + if(dockerProjects!=null&&dockerProjects.length>0){
  346 + console.log(dockerProjects);
  347 + var dockerProjectsJson=JSON.parse(dockerProjects);
  348 + console.log(dockerProjectsJson);
  349 + //docek发布
  350 + for(var i=0;i<dockerProjectsJson.length;i++){
  351 + buildDocker(dockerProjectsJson[i]);
370 } 352 }
371 } 353 }
372 - document.oncontextmenu = function() {event.returnValue = false;} 354 + });
  355 +
  356 +
373 357
374 </script> 358 </script>
  359 +
@@ -149,7 +149,7 @@ @@ -149,7 +149,7 @@
149 </h2> 149 </h2>
150 </div> 150 </div>
151 <div class="modal-body"> 151 <div class="modal-body">
152 - <form class="bs-example bs-example-form" action="<%=basePath %>dockerBuild/deploy" role="form" 152 + <form class="bs-example bs-example-form" action="<%=basePath %>dockerBuild/build" role="form"
153 id="GitBranchCreateForm" method="post"> 153 id="GitBranchCreateForm" method="post">
154 <div class="input-group"> 154 <div class="input-group">
155 <span class="input-group-addon">操作类型</span> 155 <span class="input-group-addon">操作类型</span>