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,19 +147,99 @@ @@ -147,19 +147,99 @@
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); 153 + builderDocker_createMirror(dockerProject);
156 154
157 - //轮询镜像是否成功  
158 -alert("轮询开始");  
159 - //开始deploy 155 + }
160 156
161 - //发布是否成功查询 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() {
  163 + $.ajax({
  164 + url: url,
  165 + type: 'POST',
  166 + data:getDockerRequestParam(dockerProject),
  167 + dataType: 'json',
  168 + timeout : 0,
  169 + success: function (data) {
  170 + if (!data || data.code != 200) {
  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);
  176 + responseError(data);
  177 + }else{
  178 + writeToText(dockerProject.id,"发生错误data is null or data code !=200,查询服务启动,继续获取数据,请耐心等待, errorTimes =."+errorTimes);
  179 + writeToText(dockerProject.id,"data is "+data);
  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;
  190 + },
  191 + error: function (e) {
  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);
  201 + }
  202 + }
  203 + });
  204 + }, 10000);
  205 + }
162 206
  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;
  216 + }
  217 + return frontParam;
  218 + }
  219 +
  220 + function builderDocker_deploy(dockerProject){
  221 + writeToText(dockerProject.id,"开始docker发布....");
  222 + var url= contextPath + 'dockerBuild/deployMirror';
  223 + $.ajax({
  224 + url: url,
  225 + type: 'POST',
  226 + dataType: 'json',
  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);
  236 + }
  237 + },
  238 + error: function (e) {
  239 + writeToText(dockerProject.id,"....docker发布请求失败,发生异常!");
  240 + requestError(dockerProject.id,url);
  241 + }
  242 + });
163 } 243 }
164 244
165 function builderDocker_createMirror(dockerProject){ 245 function builderDocker_createMirror(dockerProject){
@@ -170,6 +250,7 @@ alert("轮询开始"); @@ -170,6 +250,7 @@ alert("轮询开始");
170 type: 'POST', 250 type: 'POST',
171 dataType: 'json', 251 dataType: 'json',
172 data:{ 252 data:{
  253 + projectType:dockerProject.projectType,
173 appNames:dockerProject.projectName, 254 appNames:dockerProject.projectName,
174 nodeEnv:$("#environment_name").val(), 255 nodeEnv:$("#environment_name").val(),
175 branch:$("#branch_name").val(), 256 branch:$("#branch_name").val(),
@@ -179,18 +260,74 @@ alert("轮询开始"); @@ -179,18 +260,74 @@ alert("轮询开始");
179 success: function (data) { 260 success: function (data) {
180 if (!data || data.code != 200) { 261 if (!data || data.code != 200) {
181 writeToText(dockerProject.id,"....jenkins制作镜像请求失败!"); 262 writeToText(dockerProject.id,"....jenkins制作镜像请求失败!");
182 - responseError(data); 263 + responseError(dockerProject.id,data);
183 }else{ 264 }else{
184 - writeToText(dockerProject.id,"....jenkins制作镜像请求成功"); 265 + writeToText(dockerProject.id,"....jenkins制作镜像请求成功,buildId is "+data.data);
  266 +
  267 + builderDocker_queryJenkinsStatus(data.data,dockerProject);
185 } 268 }
186 }, 269 },
187 error: function (e) { 270 error: function (e) {
188 writeToText(dockerProject.id,"....jenkins制作镜像请求失败,发生异常!"); 271 writeToText(dockerProject.id,"....jenkins制作镜像请求失败,发生异常!");
189 - requestError(url); 272 + requestError(dockerProject.id,url);
190 } 273 }
191 }); 274 });
192 } 275 }
193 276
  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() {
  283 + $.ajax({
  284 + url: url,
  285 + type: 'POST',
  286 + data:{
  287 + buildId:buildId
  288 + },
  289 + dataType: 'json',
  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);
  302 + }
  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;
  314 + },
  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 + }
  327 + });
  328 + }, 10000);
  329 + }
  330 +
194 //写入消息 331 //写入消息
195 function writeToText(id,msg){ 332 function writeToText(id,msg){
196 var d = $("#resultArea_"+id).val(); 333 var d = $("#resultArea_"+id).val();
@@ -199,8 +336,12 @@ alert("轮询开始"); @@ -199,8 +336,12 @@ alert("轮询开始");
199 $("#resultArea_"+id).scrollTop(scrollTop); 336 $("#resultArea_"+id).scrollTop(scrollTop);
200 } 337 }
201 338
  339 + //请求接口异常
  340 + function requestError(id,api){
  341 + writeToText(id,'系统异常:' + api);
  342 + }
  343 +
202 $(function () { 344 $(function () {
203 - console.log(dockerProjects);  
204 if(dockerProjects!=null&&dockerProjects.length>0){ 345 if(dockerProjects!=null&&dockerProjects.length>0){
205 console.log(dockerProjects); 346 console.log(dockerProjects);
206 var dockerProjectsJson=JSON.parse(dockerProjects); 347 var dockerProjectsJson=JSON.parse(dockerProjects);
@@ -210,165 +351,9 @@ alert("轮询开始"); @@ -210,165 +351,9 @@ alert("轮询开始");
210 buildDocker(dockerProjectsJson[i]); 351 buildDocker(dockerProjectsJson[i]);
211 } 352 }
212 } 353 }
213 -  
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();  
228 - $.ajax({  
229 - url: contextPath + 'project/getbuildmsg?messageid=' + val+"&project="+cproject,  
230 - type: 'POST',  
231 - 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();  
257 - }  
258 -  
259 - },  
260 - error: function (e) {  
261 - }  
262 - });  
263 - });  
264 - }, 3000);  
265 }); 354 });
266 355
267 - /**  
268 - * 取消  
269 - */  
270 - function cancelBuild(messageid) {  
271 -  
272 - var jsondata = {"id": messageid};  
273 - $.ajax({  
274 - url: contextPath + 'project/cancelBuild',  
275 - type: 'POST',  
276 - dataType: 'json',  
277 - data: jsondata,  
278 - success: function (data) {  
279 - },  
280 - error: function (e) {  
281 - }  
282 - });  
283 - }  
284 -  
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 - }  
309 - $.ajax({  
310 - url: contextPath + 'project/testexecute?env=' + value,  
311 - type: 'POST',  
312 - 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;  
319 - }  
320 - var json = returnVal.data;  
321 - //获取到id  
322 - var id = json.id;  
323 - setIntervalIndex = setInterval(function(){testget(id);}, 1000 * 60);  
324 - },  
325 - error: function (e) {  
326 - }  
327 - });  
328 - }  
329 356
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;  
349 - }  
350 - var message = returnVal.message;  
351 - var code = returnVal.code;  
352 357
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 - });  
361 - }  
362 </script> 358 </script>
363 359
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;  
370 - }  
371 - }  
372 - document.oncontextmenu = function() {event.returnValue = false;}  
373 -  
374 -</script>  
@@ -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>