nginx_switch.js
3.52 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
$(function () {});
var dialog1, dialog2, dialog3;
function initSwitch(cloudName, target, onlineOrGray) {
var arr = getNoChangeIpArr(cloudName, onlineOrGray);
if (undefined === arr || null === arr || 0 === arr.length) {
prompt("提示", "无可切换的gateway!");
return;
}
dialog2 = $("<div>").appendTo($("body"));
dialog2.dialog({
title: "你确定切换吗",
backdrop: "static",
content: "你确定要将" + cloudName + "上的" + onlineOrGray + "流量切向" + target + "吗?",
buttons: [{
text: "否",
className: "btn-danger",
onclick: function () {
$(dialog2).dialog("hide");
}
}, {
text: "是",
className: "btn-success",
onclick: function () {
var param = {
cloudName: cloudName,
target: target,
onlineOrGray: onlineOrGray,
noChangeIps: JSON.stringify(arr)
};
sendAjax("post", "viewToChangeNginxConf", param, "text", viewToChangeSuccess, errorFunc);
}
}]
});
}
function viewToChangeSuccess(resp) {
$(dialog2).dialog("hide");
var data = JSON.parse(resp);
dialog3 = $("<div>").appendTo($("body"));
dialog3.dialog({
title: "切换结果",
backdrop: "static",
content: "<pre>" + data.data.result + "</pre>",
buttons: [{
text: "否",
className: "btn-danger",
onclick: function () {
$(dialog3).dialog("hide");
}
}, {
text: "确定",
className: "btn-success",
onclick: function () {
var param = {
cloudName: data.data.cloudName
};
sendAjax("post", "switchNginxConf", param, "text", switchSuccess, errorFunc);
}
}]
}).find(".modal-body").css({
height: "550px"
});
}
function switchSuccess() {
window.location.href = getUrlBasePath() + "/nginxswitch/toNginxSwitch";
}
/**
* 获取不切换的服务器ip(由于线上和灰度同在nginx.conf配置中,模板须设计两个占位符,所以切换某一处时,须将不切换的ip提交到后台)
* @param cloudName 切换的中心aws/qcloud
* @param onlineOrGray 切换线上/灰度
* @returns {Array} 不切换服务器的ip列表
*/
function getNoChangeIpArr(cloudName, onlineOrGray) {
var list = $("." + cloudName + ("online" === onlineOrGray ? "grayapigateway" : "apigateway") + "Ips");
if (undefined === list || null === list || 0 === list.length) {
return [];
}
var result = [];
list.each(function () {
result.push($(this).text().split(":")[0]);
});
return result;
}
/**
* 提示函数
* @param title
* @param content
*/
function prompt(title, content) {
dialog1 = $("<div>").appendTo($("body"));
dialog1.dialog({
title: title,
backdrop: "static",
content: content,
buttons: [{
text: "确定",
className: "btn-success",
onclick: function () {
$(dialog1).dialog("hide");
}
}]
});
}
function sendAjax(type, url, data, dataType, success, error) {
$.ajax({
type: type,
url: url,
data: data,
dataType: dataType,
success: success,
error: error
});
}
function errorFunc() {
layer.msg("Token异常", {icon: 2});
}