autoscaling_new.js 23.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
//页面初始化
$(function() {
    refreshAutosSalingTable();
});

function changeByCloudType(){
    refreshAutosSalingTable();
}

//刷新表格数据
function refreshAutosSalingTable() {
    var columns = getColumns();
    $("#autoscalingTable").table(
        {
            columnAutoWidth : false,
            url : contextPath + "autoScalingTool/getAutoScalingGroups?cloudType=" + $("#cloudType").val(),
            striped : true,
            dataType: "json",
            pagination: false,
            loadFilter : function(data) {
                return defaultLoadFilter(data);
            },
            columns : columns
        });

}

//根据类型获取需要显示的列
function getColumns(){
    var columns = [
        {
            title : "索引",
            width : "50px",
            formatter : function(value, rowData, rowIndex) {
                return (rowIndex + 1);
            },
            styler : function(value, rowData, rowIndex){
                return {
                    "vertical-align" : "middle"
                };
            }
        },
        {
            title : "组名",
            field : "name",
            width : "20%",
            styler : function(value, rowData, rowIndex){
                return {
                    "vertical-align" : "middle"
                };
            }
        },
        {
            title : "主机",
            field : "ip",
            width : "30%",
            align : "center",
            formatter : function(value, rowData, rowIndex) {
                var instances = rowData.autoScalingHosts;
                if(instances == null || instances.length == 0){
                    return;
                }
                var ips = "";
                for(var index = 0;index < instances.length;index++){
                    var healthCss=' style="background-color: #5bc0de;font-weight:normal;" ';
                    var runningStatusMessage = '&nbsp;&nbsp;<span class="badge" style="background-color: #f37b1d;font-weight:normal;">健康</span>';
                    if("Healthy"!=instances[index].runningStatus){
                        healthCss=' style="background-color: #5bc0de;font-weight:normal;" ';
                        runningStatusMessage='&nbsp;&nbsp;<span class="badge" style="background-color: #dd514c;font-weight:normal;">异常</span>';
                       // runningStatusMessage='<span class="glyphicon glyphicon-align-left">fdf</span>';
                    }

                    var protectedB = instances[index].protected;
                    var canScaleMessage='' ;
                    if(protectedB != null && protectedB != undefined){
                        if(protectedB){
                            canScaleMessage = '&nbsp;&nbsp;<a href="#"  title="该主机免于缩减"><span class="badge" style="background-color:#5bc0de;font-weight:normal;">免</span></a>';
                        }else{
                            canScaleMessage = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                        }
                    }

                    var hostip='<span  class="label" style="font-weight:normal;">';
                    hostip+= ('<span class="badge" '+healthCss+'>'+instances[index].ip);


                    for(var i=0;i<=(15-instances[index].ip.length);i++){
                        hostip+="&nbsp;&nbsp;"
                    }
                    hostip += runningStatusMessage;

                    hostip += '</span>';
                    hostip += canScaleMessage;
                    hostip += '</span>';
                    //<button class="btn_java_api btn btn-lg btn-default btn-success">'+value.hostIp+'&nbsp;<span class="badge">好</span></button>
                    var onehost='<h4>'+hostip+'</h4>';
                    if(protectedB){
                        ips=onehost+ips;
                    }else{
                        ips += onehost;
                    }
                }
                return ips;
            },
            styler : function(value, rowData, rowIndex){
                return {
                    "vertical-align" : "middle"
                };
            }
        },
        {
            title : "期望容量",
            field : "desired",
            width : "50px",
            styler : function(value, rowData, rowIndex){
                return {
                    "vertical-align" : "middle"
                };
            }
        },
        {
            title : "当前容量",
            field : "size",
            width : "50px",
            styler : function(value, rowData, rowIndex){
                return {
                    "vertical-align" : "middle"
                };
            }
        },
        {
            title : "最少实例数",
            field : "min",
            width : "75px",
            styler : function(value, rowData, rowIndex){
                return {
                    "vertical-align" : "middle"
                };
            }
        },
        {
            title : "最多实例数",
            field : "max",
            width : "75px",
            styler : function(value, rowData, rowIndex){
                return {
                    "vertical-align" : "middle"
                };
            }
        },
        {
            title : "操作",
            width : "100px",
            formatter : function(value, rowData, rowIndex) {
                var protectedFromCount=0;
                var instances = rowData.autoScalingHosts;
                if(instances != null&& instances.length > 0){
                    for(var index = 0;index < instances.length;index++){
                        var protectedB = instances[index].protected;
                        var message = "";
                        if(protectedB != null && protectedB != undefined){
                            if(protectedB){
                                protectedFromCount++; // 免于缩减
                            }
                        }
                    }
                }

                var div = $("<div>");
                $("<button onclick=\"changeServer(\'"
                    + rowData.id + "\',\'"
                    + rowData.name + "\',\'"
                    + protectedFromCount + "\',\'"
                    + rowData.desired + "\',\'"
                    + rowData.min + "\',\'"
                    + rowData.max + "\')\">").addClass(
                    "btn btn-xs btn-info").html("扩充或缩减")
                    .appendTo(div);
                return div;
            },
            styler : function(value, rowData, rowIndex){
                return {
                    "vertical-align" : "middle"
                };
            }
        } ];
    return columns;
}

//修改前原始数据,用于判断是否需要调用修改接口
var scalingGroupName;
var souScalingGroupId;
var souProtectedFromCount;
var souDesiredCapacity;
var souMinSize;
var souMaxSize;
//需要发布的版本
var souBranch;
//修改伸缩组
function changeServer(scalingGroupId, groupName, protectedFromCount, desiredCapacity, minSize, maxSize){
    scalingGroupName = groupName;
    souScalingGroupId = scalingGroupId;
    souProtectedFromCount = protectedFromCount;
    souDesiredCapacity = desiredCapacity;
    //souDesiredCapacity = 2;
    souMinSize = minSize;
    souMaxSize = maxSize;

    $("#myModalLabel").text("");
    $("#protectedFromCount").val("");
    $("#minSize").val("");
    $("#maxSize").val("");
    $("#desiredCapacity").val("");

    $("#myModalLabel").text(groupName + " : 扩充或缩减");
    $("#protectedFromCount").val(protectedFromCount);
    $("#minSize").val(minSize);
    $("#maxSize").val(maxSize);
    $("#desiredCapacity").val(desiredCapacity);
    $("#myModal").modal('show');
}

function changeOk(){
    //分支版本
    souBranch = $("#branch").val();
    if(souBranch == "" || souBranch == undefined){
        alert("需要发布的分支版本不能为空");
        return;
    }
    //异常处理
    if(souScalingGroupId == "" || souScalingGroupId == undefined
                || souProtectedFromCount == "" || souProtectedFromCount == undefined
                || souMinSize == "" || souMinSize == undefined
                || souMaxSize == "" || souMaxSize == undefined
                || souDesiredCapacity == "" || souDesiredCapacity == undefined){
        alert("异常,请重新点击 扩充或缩减 开始");
        return;
    }
    //受保护的实例数
    var protectedFromCount = $("#protectedFromCount").val();
    //最少实例数,大于等于受保护的实例数
    var minSize = $("#minSize").val();
    //最多实例数
    var maxSize = $("#maxSize").val();
    //期望实例数,大于等于最小实例数 小于等于最多实例数
    var desiredCapacity = $("#desiredCapacity").val();
    if((protectedFromCount == souProtectedFromCount) && (minSize == souMinSize)
                                                     && (maxSize == souMaxSize)
                                                     && (desiredCapacity == souDesiredCapacity)){
        alert("数据未做修改!");
        $("#myModal").modal('hide');
        return;
    }
    if(minSize < protectedFromCount){
        alert("期望调整的最小实例数 小于 受保护的实例数!");
        return;
    }else if(maxSize < minSize){
        alert("期望调整的最大实例数 小于 最小实例数!");
        return;
    }else if((desiredCapacity < minSize) || (desiredCapacity > maxSize)){
        alert("期望调整的容量必须在最小和最大实例数之间,最大<=容量>=最小!");
        return;
    }
    var showMessage;
    if(desiredCapacity == souDesiredCapacity){
        //基础信息修改,不涉及到缩容或者扩容
        showMessage = "";
    }else{
        if(desiredCapacity < souDesiredCapacity){
            showMessage = "缩容";
        }else if(desiredCapacity > souDesiredCapacity){
            showMessage = "扩容";
        }
    }

    if(confirm("是否确定要执行!")){
        $("#myModal").modal('hide');
        var parms = {};
        if(2 != $("#cloudType").val()){
            alert("暂时只提供腾讯云的操作!");
            return;
        }else{
            var qCloudParms = {};
            parms.cloudType = parseInt($("#cloudType").val());
            qCloudParms.scalingGroupId = souScalingGroupId;
            qCloudParms.minSize = parseInt(minSize);
            qCloudParms.maxSize = parseInt(maxSize);
            qCloudParms.desiredCapacity = parseInt(desiredCapacity);
            parms.qCloudParms = qCloudParms;
        }
        $.ajax({
            url: contextPath + 'autoScalingTool/modifyScalingGroup?cloudType=' + parseInt($("#cloudType").val()),
            type: 'POST',
            data: qCloudParms,
            dataType: 'json',
            success: function (data) {
                if (!data || data.code != 200) {
                    alert('修改伸缩组失败:'+data.message);
                }else{
                    if(showMessage == "" || showMessage == undefined){
                        alert("修改基础信息成功!");
                        clear_Sour();
                        return;
                    }
                    $("#myModalMessageLabel").text(scalingGroupName + ":正在 " + showMessage);
                    $("#myModalMessage").modal('show');
                    $("#myModalMessage").unbind("keyup");
                    $(".modal-backdrop").unbind("click");
                    //清空
                    $("#resultScalingMessage").html("");
                    $("#closeButton").attr("disabled","disabled");
                    //1:开始伸缩通知,成功扩容后才能执行下一步
                    refCloudMessage(showMessage, Math.abs(souDesiredCapacity - desiredCapacity));
                }
            },
            error: function (e) {
                alert('系统异常');
                clear_Sour();
            }
        });
    }
}

function clear_Sour(){
    //清空
    souProtectedFromCount = "";
    souDesiredCapacity = "";
    souMinSize = "";
    souMaxSize = "";
}

//腾讯云:修改伸缩组后的下一个流程是查询伸缩活动
function refCloudMessage(showMessage, serverCount){
    var timestamp = Date.parse(new Date());
    var count = 0;
    var url = contextPath + 'autoScalingTool/describeScalingActivity?scalingGroupId=' + souScalingGroupId + '&startTime=' + timestamp;
    writeToText("开始..........");
    var intervalIndex = setInterval(function() {
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                if (!data || data.code != 200) {
                    responseError(data);
                }else{
                    var dataJson = data.data[0];
                    if(dataJson.succInsList == undefined || dataJson.succInsList.length != serverCount){
                        if(count == 0){
                            writeToText(dataJson.desciption);
                        }else{
                            writeToText("......");
                        }
                    }else{
                        writeToText("第 1 步:" + showMessage + " 成功!");
                        writeToText("...");
                        clearInterval(intervalIndex);
                        if(showMessage == "缩容"){
                            unDeploy(showMessage);
                            writeToText("结束..........");
                            return;
                        }else{
                            //扩容成功后执行
                            deploy(dataJson.succInsList);
                        }
                    }
                }
                count += 1;
            },
            error: function (e) {
                requestError(url);
            }
        });
    }, 10000);
}

//写入消息
function writeToText(msg){
    var d = $("#resultScalingMessage").val();
    $("#resultScalingMessage").html(d + "&#13" + msg);
    var scrollTop = $("#resultScalingMessage")[0].scrollHeight;
    $("#resultScalingMessage").scrollTop(scrollTop);
}

//1:扩容成功后执行
function deploy(instanceIds){
    if(instanceIds == undefined){
        //发布完之后,显示关闭按钮
        $("#closeButton").removeAttr("disabled");
        return;
    }
    var instanceIdStr = "";
    for (var i = 0; i < instanceIds.length; i ++) {
        if(i == instanceIds.length - 1){
            instanceIdStr += instanceIds[i];
        }else{
            instanceIdStr += instanceIds[i] + ",";
        }
    }
    writeToText("实例ID为:" + instanceIdStr);
    writeToText(">>>正在通过实例ID获取实例的内网ip:请稍后...");
    //2:根据实例id获取内网ip地址
    var ips = getIpByInstanceIds(instanceIdStr);
    writeToText("第 2 步:获取内网成功!");
    if(ips == undefined || ips == null || ips == ""){
        writeToText("系统异常,执行失败....");
        deployLaster();
        return;
    }

    writeToText(">>>正在部署系统,请稍后...");
    if(scalingGroupName.indexOf("nginx")){
        $.ajax({
            url: contextPath  + "nginxSync/pushTask",
            type: "post",
            dataType: "json",
            data: {
                type: 1,
                select: $("#cloudType").val()
            },
            success: function (response) {
                var intervalNginx = setInterval(function () {
                    $.ajax({
                        url: contextPath + "nginxSync/getTaskLog",
                        type: 'POST',
                        dataType: 'json',
                        data: {
                            taskId: response.data.taskId
                        },
                        success: function (data3) {
                            //console.log(data3);
                            var messagedata = data3.data;
                            var logList = messagedata.log;
                            if (logList != "") {
                                writeToText(logList);
                            }
                            //code为2 ,则结束
                            var code = messagedata.isFinished;
                            if (code == 1) {
                                clearInterval(intervalNginx);
                                writeToText("完毕..........");

                            }
                        },
                        error: function (e) {
                            requestError(url);
                        }
                    });
                }, 3000);
            },
            error: function (e) {
                requestError(url);
            }
        });
    }else{
        //3:根据ip部署项目,得到消息id
        var messageId = deployProject(ips);
        writeToText("第 3 步:项目正在部署!")
        //3.1:发布项目的同时获取发布的消息,由于定时器是异步行为,故下一步骤智能在内部进行
        deployProjectGetMsg(messageId,ips);
    }
}

//发布成功和失败都执行
function deployLaster(){
    //发布完之后,显示关闭按钮
    $("#closeButton").removeAttr("disabled");
    //成功后清空
    clear_Sour();
}

//2:根据实例id获取内网ip地址
function getIpByInstanceIds(instanceIds){
    var returnIps = "";
    var url = contextPath + 'autoScalingTool/getQcloudDescribeInstances?ids=' + instanceIds;
    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        async: false,
        success: function (data) {
            if (!data || data.code != 200) {
                responseError(data);
            }else{
                //组装内网ip
                var dataJson = data.data;
                if(dataJson == undefined){
                    writeToText("系统异常,执行失败....");
                    //发布完之后,显示关闭按钮
                    $("#closeButton").removeAttr("disabled");
                    return;
                }
                //展现
                var ips = "";
                for(var i = 0;i < dataJson.length;i++){
                    if(i == dataJson.length - 1){
                        ips += dataJson[i].lanIp;
                    }else{
                        ips += dataJson[i].lanIp + ",";
                    }
                    writeToText("实例id:" + dataJson[i].unInstanceId + " - ip:" + dataJson[i].lanIp);
                }
                returnIps = ips;
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
    return returnIps;
}

//3:根据ip部署项目,返回部署的消息id,同步获取部署的进展
function deployProject(ips){
    var returnMessageId = "";
    var url = contextPath + 'autoScalingTool/deployCloudProject?projectName='+scalingGroupName+'&ips='+ips+'&branch=' + souBranch + '&environment=qcloud';
    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        async: false,
        success: function (data) {
            if (!data || data.code != 200) {
                responseError(data);
            }else{
                returnMessageId = data.data.messageids.split(',')[0];
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
    return returnMessageId;
}

//3.1:发布项目的同时获取发布的消息
function deployProjectGetMsg(msgId, ips){
    var intervalIndexMsg = setInterval(function() {
        var url = contextPath + 'project/getbuildmsg?messageid=' + msgId+"&project="+scalingGroupName;
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            success: function (data3) {
                var obj2 = eval("(" + data3 + ")");
                var messagedata = obj2.data;
                if ((messagedata.message != "") && (messagedata.message != null) && (messagedata.message != undefined)) {
                    writeToText(messagedata.message);
                }
                //code为2 ,则结束
                var code = obj2.code;
                //只有状态2,3,4才表示系统部署成功与否
                if (code == 2 || code == 3 || code == 4) {
                    writeToText("第 3 步:系统部署成功!");
                    clearInterval(intervalIndexMsg);

                    writeToText(">>>正在进行集成测试,请稍后...");
                    //4:集成测试
                    integrationTest(ips);
                }
            },
            error: function (e) {
                requestError(url);
                clearInterval(intervalIndexMsg);
            }
        });
    }, 3000);
}

//4:集成测试
function integrationTest(ips){
    var url = contextPath + 'autoScalingTool/integrationTest?cloudType='+$("#cloudType").val()+'&projectName='+scalingGroupName+'&ips='+ips;
    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        async: false,
        success: function (data) {
            if (!data || data.code != 200) {
                responseError(data);
            }else{
                writeToText(" ");
                var dataJson = data.data;
                for (var i = 0; i < dataJson.length; i ++) {
                    writeToText(dataJson[i].ip + ":请求url:" + dataJson[i].url);
                    var state = dataJson[i].state;
                    var stateMsg;
                    if(state == 200){
                        stateMsg = "成功访问";
                        writeToText("状态为:" + state + " - " + stateMsg);
                        writeToText("第 4 步:集成测试完毕!");

                        writeToText(">>>正在更新git-iptables,请稍后...");
                        //5:更新git文件
                        editToGit();
                    }else{
                        stateMsg = "访问失败";
                        writeToText("状态为:" + state + " - " + stateMsg);
                        deployLaster();
                    }
                }
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
}

//5:ip写入git
function editToGit(showMessage){
    var url = contextPath+'autoScalingTool/updateIpToGit?cloudType='+$("#cloudType").val()+'&projectName='+scalingGroupName+'&scalingGroupId=' + souScalingGroupId;
    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        async: false,
        success: function (data) {
            if (!data || data.code != 200) {
                responseError(data);
            }else{
                writeToText(" ");
                writeToText("更新之后的ip集合是:"+data.message);
                if(showMessage != null && showMessage != undefined){
                    writeToText("更新git - app-iptable文件成功!");
                }else{
                    writeToText("第 5 步:更新git - app-iptable文件成功!");
                }
                //扩容成功后刷新数据
                refreshAutosSalingTable();
                //部署全部成功后执行
                deployLaster();
                writeToText("完毕..........");
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
}

//缩容成功后执行
function unDeploy(showMessage){
    writeToText("正在更新git-iptables,请稍后...");
    //5:更新git文件
    editToGit(showMessage);

    //发布完之后,显示关闭按钮
    $("#closeButton").removeAttr("disabled");

    //成功后清空
    clear_Sour();
}

//请求接口异常
function requestError(api){
    writeToText('系统异常:' + api);
    deployLaster();
}

//接口返回异常
function responseError(data){
    if(!data){
        writeToText('返回数据异常:data为空');
    }else{
        writeToText('返回数据异常:code=' + data.code + ',message=' + data.message);
    }
    deployLaster();
}