node_project.js 10 KB
var ENV;

jQuery(document).ready(function () {
    getProjects();
});

/**
 * 操作变更
 */
function operateChange() {
    var operate = $("input[name='operate']:checked").val();
    if ("Rollback" == operate) {
        //隐藏Branch输入框
        document.getElementById("branch-div").style.display = "none";
        //展示 回滚选择框
        document.getElementById("rollback-div").style.display = "";
        //隐藏自动执行测试用例
        document.getElementById("autotest-div").style.display = "none";
        getRollbackList();
    } else {
        //展示Branch输入框
        document.getElementById("branch-div").style.display = "";
        //隐藏 回滚选择框
        document.getElementById("rollback-div").style.display = "none";
        //展示自动执行测试用例
        document.getElementById("autotest-div").style.display = "";
        branchdefault();
    }
}

/**
 * 根据操作获取当前的项目列表
 */
function getProjects() {
    //var operate = $("input[name='operate']:checked").val();

    $.ajax({
        url: 'getProjects',
        type: 'POST',
        dataType: 'json',
        contentType: "application/json",
        success: function (data) {
            var obj = eval("(" + data + ")");
            var order1HTML = "";
            var order2HTML = "";

            var order1num = 1;
            var order2num = 1;
            var firstNodeName="";
            for (var i = 0; i < obj.length; i++) {
                var order = obj[i].order;
                var name = obj[i].name;

                if(default_releaseWorkJob_project!=null&&default_releaseWorkJob_project.indexOf(name+",")>=0){
                    if(firstNodeName.length<=0){
                        firstNodeName=name;
                    }
                }


                if ("1" == order) {
                    order1HTML += "<div class='rdio rdio-primary' style='display: inline'>";
                    order1HTML += " <input name='project' type='radio' id='" + name + "' value='" + name + "' onclick='changeProject()'/>";
                    order1HTML += "<label for='" + name + "' style='width: 200px'>" + name + "</label></div>";
                    if (order1num % 4 == 0) {
                        order1HTML += "</br>";
                    }
                    order1num += 1;
                }
                if ("2" == order) {
                    order2HTML += "<div class='rdio rdio-warning' style='display: inline'>";
                    order2HTML += " <input name='project' type='radio' id='" + name + "' value='" + name + "' onclick='changeProject()'/>";
                    order2HTML += "<label for='" + name + "' style='width: 200px'>" + name + "</label></div>";
                    if (order2num % 4 == 0) {
                        order2HTML += "</br>";
                    }
                    order2num += 1;

                }
                document.getElementById("project-div").innerHTML = order1HTML + "<hr>" + order2HTML + "<hr>";
            }

            if(default_releaseWorkJob_nodeProjectCount!=null&&default_releaseWorkJob_nodeProjectCount>=2){
                alert("提醒:工单的发布项目为"+default_releaseWorkJob_project+"当前只选中一个!");
            }
            if(firstNodeName!=null&&firstNodeName.length>0){
                 //选中一个,并触发事件
                $("input:radio[name='project'][value='" + firstNodeName + "']").attr("checked", "checked").click();
            }
        },
        error: function (e) {
            alert("从后台获取数据出错");
        }
    });
}

//确认提交
function comfirmSubmit() {
    var operate = $("input[name='operate']:checked").val();
    var environment = $("input[name='environments']:checked").val();
    var project = $("input[name='project']:checked").val();
    if (!project) {
        alert("请选择项目");
        return;
    }
    var hosts = new Array();
    $("input[name=\""+environment+"-checkbox\"]:checked").each(function () {
        hosts.push($(this).val());//向数组中添加元素
    });


    $("input[name='operate_name']").val(operate);
    $("input[name='project_name']").val(project);
    $("input[name='environment_name']").val(environment);
    $("input[name='hosts_name']").val(hosts);

    if ("Deploy" == operate) {//发布
        var branch = $("input[name='branch']").val();
        if (branch == "") {
            alert("请填写分支名称");
            return;
        }
        var workid = $("input[name='workid']").val();
        if (workid == "") {
            alert("请填写关联工单号");
            return;
        }

        $.ajax({
            url: 'check?workid=' + workid,
            type: 'POST',
            success: function (data) {
                if (data != "1") {
                    alert(data);
                    return;
                } else {
                    $("input[name='branch_name']").val(branch);
                    $("input[name='workid_name']").val(workid);
                    $('#confirmSubmitDivId').modal('show');
                }
            },
            error: function (e) {
                alert("从后台获取数据出错");
            }
        });
    } else {//回滚
        var rollbackfile = $("#rollbackList option:selected").val();
        if (typeof(rollbackfile) == "undefined" || rollbackfile == "") {
            alert("请选择回滚版本");
            return;
        }
        var workid = $("input[name='workid']").val();
        if (workid == "") {
            alert("请填写关联工单号");
            return;
        }
        $("input[name='workid_name']").val(workid);
        $("input[name='rollbackfile_name']").val(rollbackfile);
        $('#confirmSubmitDivId').modal('show');
    }
}

function getRollbackList() {
    var project = $("input[name='project']:checked").val();
    var environment = $("input[name='environments']:checked").val();
    if(!environment){
        return;
    }
    $.ajax({
        url: 'rollbackList?project=' + project + "&environment=" + environment,
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            if ("failed" == data) {
                document.getElementById("select-rollbackList-div").innerHTML = "";
                return;
            }

            var list = eval("(" + data + ")");
            //动态生成rollbackList的select组件
            var innerHTMLContext = "<select id='rollbackList' data-placeholder='Choose One' style='width:350px;'>";
            for (var i = 0; i < list.length; i++) {
                innerHTMLContext += "<option value='" + list[i] + "'>" + list[i] + "</option>";
            }
            innerHTMLContext += "</select>";
            document.getElementById("select-rollbackList-div").innerHTML = innerHTMLContext;

            jQuery('#rollbackList').select2({
                minimumResultsForSearch: -1
            });
        },
        error: function (e) {
            alert("该项目暂无备份,无法回滚");
        }
    });
}

function changeProject(){
    var operate = $("input[name='operate']:checked").val();
    var project = $("input[name='project']:checked").val();

    $.ajax({
        url: 'getProject?name=' + project,
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            $("#select-environment-div").empty();
            if ("failed" == data) {
                return;
            }

            var pythonProject = eval("(" + data + ")");
            //动态生成环境的组件
            var list = pythonProject.envs;
            ENV = list;
            for(var i = 0; i < list.length; i++){
                var env = list[i];
                var div = $("<div>").addClass("rdio rdio-default");
                var redio = $("<input>");
                redio.attr("type","radio");
                redio.attr("name","environments");
                redio.attr("id",env.name);
                redio.attr("value",env.name);
                redio.attr("onclick","changeservers()");
                //如果是新打开发布页面,则默认选中最后一个云,如果是通过工单ID,则选择该工单的云
                if(default_releaseWorkJob_environment!=null&&default_releaseWorkJob_environment.length>0){
                    if(default_releaseWorkJob_environment.indexOf(env.name+",")>=0){
                        redio.attr("checked","checked");
                    }
                }else{
                    redio.attr("checked","checked");
                }


                var label = $("<label for=\""+env.name+"\">"+env.name+"</label>");

                var checkBoxDiv = $("<div>").attr("id",env.name+"-div").attr("display","display");

                var hosts = env.host_ips;
                for(var j = 0; j < hosts.length; j++){
                    var host = hosts[j];
                   checkBoxDiv.append("<input type=\"checkbox\" value=\""+host+"\" name=\""+env.name +"-checkbox\" checked=\"checked\">"+host+"");
                }
                div.append(redio).append(label).append(checkBoxDiv);
                $("#select-environment-div").append(div);
            }
            changeservers();
        },
        error: function (e) {
            alert("获取项目环境失败");
        }
    });
}

function changeservers() {
    var environment = $("input[name='environments']:checked").val();
    for(var i = 0; i < ENV.length; i++){
        $("#"+ENV[i].name+"-div").hide();
    }
    $("#"+environment+"-div").show();

    var operate = $("input[name='operate']:checked").val();
    if (operate == "Rollback") {
        getRollbackList();
    }
    if (operate == "Deploy") {
        branchdefault();
    }

}

function branchdefault() {
    var operate = $("input[name='operate']:checked").val();
    if (operate == "Deploy") {
        $("input[name='branch']").val("master");
    }
}

function refresh(){
    $.ajax({
        url: 'refreshGit',
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            alert("刷新成功");
        },
        error: function (e) {
            alert("刷新失败");
        }
    });

}