autoscaling_new.js 40.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 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
//页面初始化
$(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
        });

}

//根据类型获取需要显示的列:特殊的地方,腾讯云的伸缩组有groupid和groupname属性,而亚马逊的伸缩组主键是awsGroupName,但是同时又有一个Name标签
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 : "aws组名",
            field : "awsGroupName",
            hidden:true,
            width : "20%",
            styler : function(value, rowData, rowIndex){
                return {
                    "vertical-align" : "middle"
                };
            }
        },*/
        {
            title : "主机",
            field : "ip",
            width : "30%",
          /*  align : "center",*/
            align:"left",
            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;<a href="#"  title="健康"><span class="badge" style="background-color: #5bc0de;font-weight:normal;"><i class="fa fa-check-circle" aria-hidden="true"></i></span></a>';
                    if("Healthy"!=instances[index].runningStatus){
                        healthCss=' style="background-color: #DD514D;font-weight:normal;" ';
                        runningStatusMessage='&nbsp;&nbsp;<a href="#"  title="异常"><span class="badge" style="background-color: #DD514D;font-weight:normal;"><i class="fa fa-exclamation-circle" aria-hidden="true"></i></span></a>';
                    }
                    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);

                    //在个别机器的chrome版本上instances[index].ip取不到值,很奇怪,因此添加一个if判断,跳过
                    if(instances[index]!=null&&instances[index].ip!=null&& instances[index].ip != undefined&&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>';

                    //新增属性 主机标签 --tags
                    var tags = instances[index].tags;
                    if(tags != null && tags!= ""){//展示标签信息
                        hostip = hostip + "<span class=\"badge\" style=\"background-color: #5bc0de;font-weight:normal;font-size: 13px\";>标签:&nbsp;" + tags +"</span>";
                    }

                    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 tmp_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){
                                tmp_protectedFromCount++; // 免于缩减
                            }
                        }
                    }
                }

                var div = $("<div>");
                $("<button onclick=\"changeServer(\'"
                    + rowData.id + "\',\'"
                    + rowData.name + "\',\'"
                    + rowData.awsGroupName + "\',\'"
                    + tmp_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 aws_scalingGroupName;
var souScalingGroupId;
var souProtectedFromCount;
var souDesiredCapacity;
var souMinSize;
var souMaxSize;
//需要发布的版本
var souBranch;
//原实例ip
var souInstanceIps;
var newInstanceIps;
var souProjectNames;
//修改伸缩组
function changeServer(scalingGroupId, groupName,awsGroupName, protectedFromCount, desiredCapacity, minSize, maxSize){
    scalingGroupName = groupName;
    aws_scalingGroupName=awsGroupName;
    souScalingGroupId = scalingGroupId;
    souProtectedFromCount = protectedFromCount;
    souDesiredCapacity = desiredCapacity;
    souMinSize = minSize;
    souMaxSize = maxSize;
    souInstanceIps="";
    newInstanceIps="";
    souProjectNames="";

    $("#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(maxSize*1 < minSize*1){
        alert("期望调整的最大实例数 小于 最小实例数!");
        return;
    }else if((desiredCapacity*1 < minSize*1) || (desiredCapacity*1 > maxSize*1)){
        alert("期望调整的容量必须在最小和最大实例数之间,最大<=容量>=最小!");
        return;
    }
    var showMessage;
    if(parseInt(desiredCapacity) == parseInt(souDesiredCapacity)){
        //基础信息修改,不涉及到缩容或者扩容
        showMessage = "";
    }else{
        if(parseInt(desiredCapacity) < parseInt(souDesiredCapacity)){
            showMessage = "缩容";
        }else if(parseInt(desiredCapacity) > parseInt(souDesiredCapacity)){
            showMessage = "扩容";
        }
    }

    if(confirm("是否确定要执行?-->"+showMessage)){
        $("#myModal").modal('hide');
        var cloudParms = {};
        if(1 == $("#cloudType").val()||2 == $("#cloudType").val()){
            cloudParms.scalingGroupId = souScalingGroupId;
            cloudParms.scalingGroupName = scalingGroupName;//aws需要以伸缩组名作为主键
            cloudParms.awsScalingGroupName=aws_scalingGroupName;
            cloudParms.minSize = parseInt(minSize);
            cloudParms.maxSize = parseInt(maxSize);
            cloudParms.desiredCapacity = parseInt(desiredCapacity);
        }else{
            alert("只提供腾讯云和亚马逊云的操作!");
            return;
        }

        //2:根据实例id获取内网ip地址
        //清空
        $("#resultScalingMessage").html("");
        writeToText("第 0 步:获取原实例ip-->");
        if(1 == $("#cloudType").val()){
            souInstanceIps = getIpByGroupId(aws_scalingGroupName,souScalingGroupId);
        }else{
            souInstanceIps = getIpByGroupId(scalingGroupName,souScalingGroupId);
        }
        writeToText("获取原实例ip获取完成:"+souInstanceIps);
        writeToText("获取原实例ip获取完成");
        souProjectNames = getDeployProjectNames(scalingGroupName);
        writeToText("该伸缩组部署的项目列表:"+souProjectNames);
        $.ajax({
            url: contextPath + 'autoScalingTool/modifyScalingGroup?cloudType=' + parseInt($("#cloudType").val()),
            type: 'POST',
            data: cloudParms,
            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");

                    $("#closeButton").attr("disabled","disabled");

                    //1:开始伸缩通知,成功扩容后才能执行下一步
                    refCloudMessage($("#cloudType").val(),showMessage, Math.abs(souDesiredCapacity - desiredCapacity),desiredCapacity);
                }
            },
            error: function (e) {
                alert('系统异常');
                clear_Sour();
            }
        });
    }
}

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

//腾讯云:修改伸缩组后的下一个流程是查询伸缩活动
function refCloudMessage(cloudType,showMessage, serverChangeCount,desiredCapacity){
    var tmpGroupName=scalingGroupName;
    if(1 == $("#cloudType").val()){
        tmpGroupName=aws_scalingGroupName;
    }
    var url = contextPath + 'autoScalingTool/getIpsByScalingGroupId?groupName='+tmpGroupName+'&groupId='+souScalingGroupId+"&cloudType="+$("#cloudType").val();
    writeToText("开始..........");
    var count = 0;
    var errorTimes = 0;
    var intervalIndex = setInterval(function() {
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            timeout : 0,
            success: function (data) {
                if (!data || data.code != 200) {
                    errorTimes++;
                    if(errorTimes>5){
                        clearInterval(intervalIndex);
                        writeToText("发生错误data is null or data code !=200,refCloudMessage data 程序终止..........");
                        writeToText("data is "+data);
                        responseError(data);
                    }else{
                        writeToText("发生错误data is null or data code !=200,继续获取数据,请耐心等待,getIpsByScalingGroupId data errorTimes =."+errorTimes);
                        writeToText("data is "+data);
                    }
                }else{
                    //计算变化主机的ip
                    var checkPassFlag=true;
                    var changedIps="";
                    newInstanceIps="";
                    if(data.data.length != desiredCapacity){
                        checkPassFlag=false;
                    }else{
                        for(var tmp=0;tmp<data.data.length;tmp++){
                            var tmpInstIp=data.data[tmp];
                            if(tmpInstIp==null||tmpInstIp==""||tmpInstIp==undefined){
                                checkPassFlag=false;
                                newInstanceIps="";
                                break;
                            }
                            if(newInstanceIps.length>0){
                                newInstanceIps += ",";
                            }
                            newInstanceIps += tmpInstIp;
                            if(souInstanceIps.indexOf(tmpInstIp) < 0){
                                if(changedIps.length>0){
                                    changedIps += ",";
                                }
                                writeToText("扩容机器实例IP为:"+tmpInstIp);
                                changedIps += tmpInstIp;
                            }
                        }
                    }
                    if(!checkPassFlag){
                        writeToText("............"+count);
                    }else{
                        clearInterval(intervalIndex);
                        if(showMessage == "缩容"){
                            writeToText("第 1 步: 缩容成功");
                            writeToText("...");
                            unDeploy(showMessage);
                            writeToText("结束..........");
                            return;
                        }else{
                            pingHost(changedIps);
                        }
                    }
                }
                count += 1;
            },
            error: function (e) {
                errorTimes++;
                if(errorTimes>5){
                    clearInterval(intervalIndex);
                    writeToText("发生错误error,refCloudMessage 程序终止..........");
                    writeToText("error is "+e);
                    requestError(url);
                }else{
                    writeToText("发生错误error,继续获取数据,请耐心等待,getIpsByScalingGroupId data errorTimes =."+errorTimes);
                    writeToText("error is "+e);
                }
            }
        });
    }, 20000);
}

function pingHost(changedIps){
    writeToText("探测host.........."+changedIps);
    var url =contextPath  + "autoScalingTool/checkHostReachable";
    var count = 0;
    //检测ip的22端口是否通,如果全部通,则代表扩容成功

    var intervalIndex = setInterval(function() {
        $.ajax({
            url: url,
            type: "post",
            dataType: "json",
            //async: false,
            //timeout:120000, //超时时间:120秒
            data: {
                changedIps:changedIps
            },
            success: function (checkResponse) {
                if (!checkResponse || checkResponse.code != 200) {
                    clearInterval(intervalIndex);
                    writeToText("发生错误,checkHostReachable code 程序终止..........");
                    responseError(checkResponse);
                }else{
                    if(checkResponse.data=="allReachable"){
                        clearInterval(intervalIndex);
                        writeToText("第 1 步: 扩容成功");
                        writeToText("...");

                        writeToText("新扩容的内网ip合计:"+changedIps);
                        //扩容成功后执行
                        deploy(changedIps);
                    }else{
                        writeToText("..........*."+count+".."+checkResponse.data);
                    }
                }
                count += 1;
            },
            error: function (e) {
                clearInterval(intervalIndex);
                writeToText("发生错误,checkHostReachable  程序终止..........");
                requestError(url);
            }
        });
    }, 20000);
}

//缩容成功后执行
function unDeploy(showMessage){
    if(scalingGroupName=="PCH5-memcache"){
        //pch5的memcache:获得镜像机器之后,把ips保存到mobject_info(监控对象)和internal_domain
        updateMemcacheInfo("PC/H5",showMessage);
    }else if(scalingGroupName=="java-gateway-memcache"){
        //java-gateway的memcache:获得镜像机器之后,把ips保存到mobject_info(监控对象)和internal_domain
        updateMemcacheInfo("java",showMessage);
    }else if(scalingGroupName=="PCH5-AutoScaling"||scalingGroupName=="PCH5-NODE"){
        writeToText("node项目缩,正在更新ansible deploy配置文件,请稍后...");
        updateAnsibleDeployForNode(showMessage);
    }else if(scalingGroupName=="ELASTICSEARCH"){
        updateEsConfig(showMessage);
    }else if(scalingGroupName.indexOf("nginx")>=0){
        updateNginxInfo(showMessage);
    }else{
        writeToText("正在更新git-iptables,请稍后...");
        //5:更新git文件
        editToGit(showMessage);
    }
}

//1:扩容成功后执行
function deploy(ips){
    writeToText("第 2 步:获取内网成功!");
    if(ips == undefined || ips == null || ips == ""){
        writeToText("系统异常,执行失败....");
        deployLaster();
        return;
    }

    writeToText(">>>正在部署系统,请稍后..."+scalingGroupName);
    if(scalingGroupName=="PCH5-memcache"){
        //pch5的memcache:获得镜像机器之后,把ips保存到mobject_info(监控对象)和internal_domain
        updateMemcacheInfo("PC/H5");
    }else if(scalingGroupName=="java-gateway-memcache"){
        //java-gateway的memcache:获得镜像机器之后,把ips保存到mobject_info(监控对象)和internal_domain
        updateMemcacheInfo("java");
    }else if(scalingGroupName=="PCH5-AutoScaling"||scalingGroupName=="PCH5-NODE"){
        //node项目
        writeToText(">>>node项目,请稍后...");
        //3:根据ip部署项目,得到消息id
        var messageId = deployProjectForNode(ips);
        writeToText("第 3 步:node项目正在部署!messageId is "+messageId)
        deployProjectForNodeGetMsg(messageId,ips);
    }else if(scalingGroupName=="ELASTICSEARCH"){
        //伸缩ELASTICSEARCH机器
        updateEsConfig();
    }else if(scalingGroupName.indexOf("nginx")>=0){
        updateNginxInfo();
    }else{
        //普通java项目
        writeToText(">>>普通项目,请稍后...");
        //3:根据ip部署项目,得到消息id
        var messageId = deployProject(ips);
        writeToText("第 3 步:项目正在部署!messageId is "+messageId)
        //3.1:发布项目的同时获取发布的消息,由于定时器是异步行为,故下一步骤智能在内部进行
        deployProjectGetMsg(messageId,ips);
    }
}

/**************************************************************
 * memcache的伸缩:把伸缩之后的机器ip,
 * 随机均匀的分布在几个internal_donmain上
 **************************************************************/

function updateMemcacheInfo(memcacheType,showMessage){
    var paramFront={};
    paramFront.newInstanceIps = newInstanceIps;
    paramFront.cloudType=$("#cloudType").val();
    paramFront.memcacheType=memcacheType;

    //1.更新internal_domain
    writeToText("开始更新内部dns:internal_domain.......");
    var url=contextPath+'autoScalingTool/updateMemcacheInfoForInternalDomain';
    $.ajax({
        url: url,
        type: 'POST',
        data: paramFront,
        dataType: 'json',
        async: false,
        success: function (data) {
            if (!data || data.code != 200) {
                responseError(data);
            }else{
                writeToText(" ");
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
    writeToText("开始更新内部dns结束.......");

    //2.更新监控对象
    upHostMobjectInfo();

    //
    writeToText("开始刷新dns.......");
    var currentCloud="aws";
    if(2==$("#cloudType").val()){
        currentCloud="qcloud";
    }
    $.ajax({
        url: contextPath + "internalDns/refresh",
        type: 'post',
        async: false,
        dataType: "json",
        data: {
            domain: "yohoops.org.",
            environment: currentCloud
        },
        success: function (data) {

        }
    });
    writeToText("刷新dns结束.......");
    //更新主机标签
    upHostCmdbInfo(showMessage);

}

/**************************************************************
 * nginx的伸缩
 **************************************************************/

//nginx类型机器伸之后
function updateNginxInfo(showMessage){
    writeToText(">>>nginx项目,请稍后...");
    //
    writeToText(">>>nginx项目,调用本机脚本更新nginx配置...");
    var url=contextPath+'autoScalingTool/updateNginxConfig';
    var paramFront={};
    paramFront.cloudType=$("#cloudType").val();
    paramFront.newInstanceIps=newInstanceIps;
    if(scalingGroupName.toUpperCase().indexOf("PCH5")>=0){
        paramFront.nginxType="pch5";
    }else{
        paramFront.nginxType="java";
    }

    $.ajax({
        url: url,
        type: 'POST',
        data: paramFront,
        dataType: 'json',
        async: false,
        success: function (data) {
            if (!data || data.code != 200) {
                responseError(data);
            }else{
                writeToText("执行完毕");
            }
        },
        error: function (e) {
            requestError(url);
        }
    });

    upHostMobjectInfo();
    upHostCmdbInfo(showMessage);
}

/**************************************************************
 * es的伸缩
 **************************************************************/

//更新es
function updateEsConfig(showMessage){
    //elasticsearch项目,更新后台ssh
    writeToText(">>>elasticsearch项目,请稍后...");
    var url = contextPath  + "autoScalingTool/updateEsConfig";
    $.ajax({
        url: url,
        type: "post",
        dataType: "json",
        //async: false,
        data: {
            allIps:newInstanceIps
        },
        success: function (response) {
            writeToText("elasticsearch伸缩完毕");
            upHostCmdbInfo(showMessage);
        },
        error: function (e) {
            requestError(url);
        }
    });
}

/**************************************************************
 * java项目和node项目的伸缩
 **************************************************************/

//3:根据ip部署项目,返回部署的消息id,同步获取部署的进展
function deployProject(ips){
    var cloudType = "aws";
    if(2==$("#cloudType").val()){
        cloudType="qcloud";
    }
    var returnMessageId = "";
    var url = contextPath + 'autoScalingTool/deployCloudProject?projectName='+scalingGroupName+'&ips='+ips+'&branch=' + souBranch + '&environment='+cloudType;
    $.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;
}

function deployProjectForNode(ips){
    var cloudType = "aws";
    if(2==$("#cloudType").val()){
        cloudType="qcloud";
    }
    var returnMessageId = "";
    var url = contextPath + 'autoScalingTool/deployCloudProjectForNode?projectName='+scalingGroupName+'&ips='+ips+'&branch=' + souBranch + '&environment='+cloudType;
    $.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 count=0;
    var intervalIndexMsg = setInterval(function() {
        var url = contextPath + 'autoScalingTool/getBuildMsgAfterDeploy?messageid=' + msgId+"&project="+scalingGroupName;
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            success: function (respdata3) {
                count++;
                if(respdata3!=null&&respdata3.data==true){
                    writeToText("第 3 步:系统部署成功!");
                    writeToText(respdata3.message);
                    clearInterval(intervalIndexMsg);

                    writeToText(">>>第 4 步:正在进行集成测试,请稍后...");
                    //4:集成测试
                    //integrationTest(ips);
                    writeToText(">>>集成测试暂时不需要,忽略...");

                    writeToText(">>>更新到git,请稍后...");
                    //5:更新git文件
                    editToGit();


                } else{
                    if(respdata3.message!=null&&respdata3.message.length>0){
                        writeToText(respdata3.message+"......."+count);
                    }else{
                        writeToText("......."+count);
                    }
                }
            },
            error: function (e) {
                requestError(url);
                clearInterval(intervalIndexMsg);
            }
        });
    }, 10000);
}

function deployProjectForNodeGetMsg(msgId, ips){
    var count=0;
    var intervalIndexMsg = setInterval(function() {
        var url = contextPath + 'autoScalingTool/getBuildMsgAfterDeployForNode?messageid=' + msgId+"&project="+scalingGroupName;
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            success: function (respdata3) {
                count++;
                if(respdata3!=null&&respdata3.data==true){
                    writeToText("第 3 步:系统部署成功!");
                    writeToText(respdata3.message);
                    clearInterval(intervalIndexMsg);

                    writeToText(">>>第 4 步:正在进行集成测试,请稍后...");
                    //4:集成测试
                    //integrationTest(ips);
                    writeToText(">>>集成测试暂时不需要,忽略...");

                    writeToText(">>>更新到ansible_deploy,请稍后...");
                    //5:node项目扩容之后,要更新到ansible_deploy
                    updateAnsibleDeployForNode();


                } else{
                    if(respdata3.message!=null&&respdata3.message.length>0){
                        writeToText(respdata3.message+"......."+count);
                    }else{
                        writeToText("......."+count);
                    }
                }
            },
            error: function (e) {
                requestError(url);
                clearInterval(intervalIndexMsg);
            }
        });
    }, 10000);
}

function updateAnsibleDeployForNode(showMessage){
    var upDownFlag="up";//伸缩标志 :1 伸
    if(showMessage != null && showMessage != undefined){
        upDownFlag="down"; //缩减
    }
    var paramFront={};
    paramFront.cloudType=$("#cloudType").val();
    paramFront.scalingGroupName=scalingGroupName;
    paramFront.scalingGroupId=souScalingGroupId;
    paramFront.oldInstanceIps=souInstanceIps;
    paramFront.newInstanceIps=newInstanceIps;
    paramFront.upDownFlag=upDownFlag;
    writeToText("第 5 步:开始更新node git to ansible_deploy.......");
    var url=contextPath+'autoScalingTool/updateAnsibleDeployForNode';

    paramFront.updateType="deploy";
    $.ajax({
        url: url,
        type: 'POST',
        data: paramFront,
        dataType: 'json',
        //async: false,
        success: function (data) {
            if (!data || data.code != 200) {
                responseError(data);
            }else{
                writeToText(" ");
                writeToText("ansible_deploy update info is:"+data.message);
                writeToText("第 5 步: ansible_deploy update 结束.......");
                updateAnsibleDeployForNode_2_gitupstream(paramFront,showMessage);
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
}

function updateAnsibleDeployForNode_2_gitupstream(paramFront,showMessage){
    //
    writeToText("第 6 步:开始更新node git to upstream.......");
    paramFront.updateType="nginx";

    var url=contextPath+'autoScalingTool/updateAnsibleDeployForNode';
    $.ajax({
        url: url,
        type: 'POST',
        data: paramFront,
        dataType: 'json',
        //async: false,
        success: function (data) {
            if (!data || data.code != 200) {
                responseError(data);
            }else{
                writeToText(" ");
                writeToText("git to upstream info is:"+data.message);
                writeToText("第 6 步:更新node git to upstream 结束.......");

                upHostCmdbInfo(showMessage);
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
}
//5 和 6:ip写入git
function editToGit(showMessage){
    var upDownFlag="up";//伸缩标志 :1 伸
    if(showMessage != null && showMessage != undefined){
        upDownFlag="down"; //缩减
    }
    var paramFront={};
    paramFront.cloudType=$("#cloudType").val();
    paramFront.scalingGroupName=scalingGroupName;
    paramFront.scalingGroupId=souScalingGroupId;
    paramFront.oldInstanceIps=souInstanceIps;
    paramFront.newInstanceIps=newInstanceIps;
    paramFront.upDownFlag=upDownFlag;
    var url=contextPath+'autoScalingTool/updateIpToGit';
    writeToText("第 5 步:开始更新git - app-iptable.......");
    var allProjectsArray=souProjectNames.split(",");
    for(var tmp=0;tmp<allProjectsArray.length;tmp++){
        paramFront.projectName=allProjectsArray[tmp];//每次变更的都只是项目名称
        writeToText("开始更新项目【"+paramFront.projectName+"】的iptable信息");
        $.ajax({
            url: url,
            type: 'POST',
            data: paramFront,
            dataType: 'json',
            async: false,
            success: function (data) {
                if (!data || data.code != 200) {
                    responseError(data);
                }else{
                    writeToText(" ");
                    writeToText("更新之后的ip集合是:"+data.message);
                }
            },
            error: function (e) {
                requestError(url);
            }
        });
    }
    writeToText("第 5 步:更新git - app-iptable文件成功!");

    if(scalingGroupName=="gateway"||scalingGroupName=="java-gateway"){
        writeToText("第 6 步:开始更新gateway upstream.......");
        url=contextPath+'autoScalingTool/updateGatewayUpstream';
        $.ajax({
            url: url,
            type: 'POST',
            data: paramFront,
            dataType: 'json',
            async: false,
            success: function (data) {
                if (!data || data.code != 200) {
                    responseError(data);
                }else{
                    writeToText(" ");
                    writeToText("gateway upstream update info is:"+data.message);
                    writeToText("第 6 步: gateway upstream 结束.......");
                }
            },
            error: function (e) {
                requestError(url);
            }
        });
    }

    upHostMobjectInfo();
    upHostCmdbInfo(showMessage);

}

/**************************************************************
 * 伸缩之后都需要调用,伸缩的结束方法:更新cmdb信息
 **************************************************************/

//更新主机信息到监控对象
function upHostMobjectInfo(){
    writeToText("开始更新监控对象:mobject_info.......");
    var paramFront={};
    paramFront.cloudType=$("#cloudType").val();
    paramFront.scalingGroupName=scalingGroupName;
    paramFront.newInstanceIps=newInstanceIps;

    var url=contextPath+'autoScalingTool/updateMobjectInfo';
    $.ajax({
        url: url,
        type: 'POST',
        data: paramFront,
        dataType: 'json',
        async: false,
        success: function (respBack) {
            if (!respBack || respBack.code != 200) {
                responseError(respBack);
            }else{
                writeToText(" ");
                writeToText(respBack.data);
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
    writeToText("开始更新监控对象结束.......");
}

//更新主机信息到cmdb
function upHostCmdbInfo(showMessage){
    writeToText(">>>更新主机信息到cmdb,请稍后...");
    var upDownFlag="up";//伸缩标志 :1 伸
    if(showMessage != null && showMessage != undefined&& showMessage != "扩容"){
        upDownFlag="down"; //缩减
    }
    var paramFront={};
    paramFront.cloudType=$("#cloudType").val();
    paramFront.scalingGroupName=scalingGroupName;
    paramFront.scalingGroupId=souScalingGroupId;
    paramFront.oldInstanceIps=souInstanceIps;
    paramFront.newInstanceIps=newInstanceIps;
    paramFront.upDownFlag=upDownFlag;

    var url=contextPath+'autoScalingTool/updateToHost';
    $.ajax({
        url: url,
        type: 'POST',
        data: paramFront,
        dataType: 'json',
        async: false,
        success: function (data) {
            if (!data || data.code != 200) {
                responseError(data);
            }else{
                writeToText(" ");
                writeToText("更新主机信息到cmdb成功");

                //扩容成功后刷新数据
                refreshAutosSalingTable();
                //部署全部成功后执行
                deployLaster();
                writeToText("完毕..........");
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
}

/**************************************************************
 * 公共方法
 **************************************************************/

//获取伸缩组内的项目
function getDeployProjectNames(groupName){
    var returnProjects = "";
    var tmpEnvironment="aws";
    if(2==$("#cloudType").val()){
        tmpEnvironment="qcloud";
    }
    var url = contextPath + 'autoScalingTool/getDeployProjectNames?scalingGroupName='+groupName+"&environment="+tmpEnvironment;
    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        async: false,
        success: function (data) {
            if (!data || data.code != 200) {
                responseError(data);
            }else{
                //项目信息
                returnProjects = data.data;
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
    return returnProjects;
}


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

//根据伸缩组id获取内网ip地址
function getIpByGroupId(groupName,groupId){
    var returnIps = "";
    var url = contextPath + 'autoScalingTool/getIpsByScalingGroupId?groupName='+groupName+'&groupId='+groupId+"&cloudType="+$("#cloudType").val();
    $.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];
                    }else{
                        ips += dataJson[i] + ",";
                    }
                    writeToText("实例id: - ip:" + dataJson[i]);
                }
                returnIps = ips;
            }
        },
        error: function (e) {
            requestError(url);
        }
    });
    return returnIps;
}


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

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

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