-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusters.html
402 lines (322 loc) · 9.84 KB
/
clusters.html
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
<html>
<head>
<style>
.formcap {
//float: left;
display: table-cell;
}
.forminput {
//float: left;
display: table-cell;
}
.formrow {
//clear: both;
display: table-row;
}
#cluster_list th {
border-bottom: 1px solid;
background: GhostWhite;
}
#servers th {
border-bottom: 1px solid;
}
form {
// display: table;
background: inherit;
}
div[c_level="editor"] {
background: WhiteSmoke;
}
#input_area {
// border: 1px dashed;
}
tr[id="cluster_spacer"] td {
height: 25px;
border-top: 1px dashed;
}
table {
// border-collapse: collapse;
}
.clicky {
color: blue;
cursor: pointer;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<template id="cluster_row">
<tr id="cluster">
<td id="proto"></td>
<td id="addr"></td>
<td id="port"></td>
<td id="scheduler"></td>
<td onclick="h_onclick(event)"><span action="add_server_form" class="clicky">[Add Server]</span><span action="delete_cluster" class="clicky">[Del Cluster]</span></td>
</tr>
<tr>
<td valign=top><h4>Servers<h4></td> <td colspan=4>
<table id="servers" class="servers">
<tr>
<th>Server</th>
<th>Port</th>
<th>Forward</th>
<th>Weight</th>
<th>Action</th>
</tr>
<template id="server_row">
<tr id="server">
<td id="addr"></td>
<td id="port"></td>
<td id="forward"></td>
<td id="weight"></td>
<td id="action" onclick="h_onclick(event)"><span action="delete_server" class="clicky">[Del Server]</span></td>
</tr>
</template>
</table>
</td>
</tr>
<tr id="cluster_spacer">
<td colspan=5></td>
</tr>
</template>
<div id="input_area">
<div c_level="editor" id="add_server" class="table" style="display:none">
<form name="add_server_form">
<h3 id="add_server_cap">Add Server: <label id="addr"></label>:<label id="port"></label></h3>
<div class="formrow">
<div class="formcap">Address:</div>
<div class="forminput"><input type="text" id="addr" size=10></div>
</div>
<div class="formrow">
<div class="formcap">Port:</div>
<div class="forminput"><input type="text" id="port" size=5></div>
</div>
<div class="formrow">
<div class="formcap">Forward:</div>
<div class="forminput"><select id="forward"><option value="Route">Direct Route (DSR)</option><option value="Masq">Masq/Nat</option><option value="Tunnel">Tunneling</option></select></div>
</div>
<div class="formrow">
<div class="formcap">Weight:</div>
<div class="forminput"><input type="text" id="weight" size=5></div>
</div>
<div class="formrow"></div>
<div class="formrow">
<div class="formcap"></div>
<div align=left class="forminput"><input action="cancel_form" onclick="h_onclick(event)" type="button" id="cancel" value="Cancel"> <input action="add_server" type="button" onclick="h_onclick(event)" id="add" value="Add"></div>
</div>
</form>
</div>
<div c_level="editor" id="add_cluster" class="table" style="display:none">
<form name="add_cluster_form">
<h3 id="add_server_cap">Add Cluster</h3>
<div class="formrow">
<div class="formcap">Protocol:</div>
<div class="forminput">
<select id="proto">
<option value="TCP">TCP</option>
<option value="UDP">UDP</option>
<option value="FWMARK">FWMARK</option>
</select>
</div>
</div>
<div class="formrow">
<div class="formcap">Address:</div>
<div class="forminput"><input type="text" id="addr" size=10></div>
</div>
<div class="formrow">
<div class="formcap">Port:</div>
<div class="forminput"><input type="text" id="port" size=5></div>
</div>
<div class="formrow">
<div class="formcap">Scheduler:</div>
<div class="forminput">
<select id="scheduler">
<option value="rr">Round Robin</option>
<option value="wrr">Weighted Round Robin</option>
<option value="lc">Least-Connection</option>
<option value="wlc">Weighted Least-Connection</option>
<option value="lblc">Locality-Based Least-Connection</option>
<option value="lblcr">Locality-Based Least-Connection with Replication</option>
<option value="dh">Destination Hashing</option>
<option value="sh">Source Hashing</option>
<option value="sed">Shortest Expected Delay</option>
<option value="nq">Never Queue</option>
</select>
</div>
</div>
<div class="formrow"></div>
<div class="formrow">
<div class="formcap"></div>
<div align=left class="forminput"><input action="cancel_form" onclick="h_onclick(event)" type="button" id="cancel" value="Cancel"> <input action="add_cluster" type="button" onclick="h_onclick(event)" id="add" value="Add"></div>
</div>
</form>
</div>
</div>
<script>
var lvs;
var backend = '.php';
var please_wait = true;
function h_onclick(e)
{
var target = $(e.target);
action = { 'action': $(target).attr('action') };
switch ( $(target).attr('action') ) {
case 'delete_server':
var ref = $(e.target).closest('tr').data('ref');
action['ref'] = ref;
$.ajax({url: 'api/' + action.action + backend , method: 'GET', data: action, dataType: 'json'}).done(function(c)
{
console.log(c);
load_list();
if ( req_error(c) ) return;
});
break;
case 'delete_cluster':
var ref = $(e.target).closest('tr').data('ref');
action['ref'] = ref;
$.ajax({url: 'api/' + action.action + backend, method: 'GET', data: action, dataType: 'json'}).done(function(c){
console.log(c);
load_list();
if ( req_error(c) ) return;
});
break;
case 'add_server_form':
var ref = $(e.target).closest('tr').data('ref');
$("div[id='add_server']").find('form').data('ref', ref);
fill_fields(ref.cluster, $("#add_server_cap label, div[id='add_server'] input[id='port']"));
$("div[id='add_server'").css('display', 'inline');
if ( ref.cluster.servers.length ) {
fill_fields(ref.cluster.servers[0], $("div[id='add_server'] select[id='forward'],div[id='add_server'] input[id='addr']"));
var addr = $("div[id='add_server'] input[id='addr']").val();
var regExp = /^(.+\.)/;
var match = regExp.exec(addr);
if ( match.length > 1 ) {
$("div[id='add_server'] input[id='addr']").val(match[1]);
}
$("div[id='add_server'] input[id='addr']").focus();
}
break;
case 'add_server':
var input = collect_values($(target).closest('form').find("input[type='text'], select"));
action['input'] = input;
action['ref'] = $(target).closest("form").data('ref');
$.ajax({url: 'api/' +action.action + backend , method: 'GET', data: action, dataType: 'json'}).done(function(c){
console.log(c);
load_list();
if ( req_error(c) ) return;
//make it easy to enter lots of ips quick
var addr = $(target).closest('form').find("input[id='addr']").val();
var regExp = /^(.+\.)/;
var match = regExp.exec(addr);
if ( match.length > 1 ) {
$(target).closest('form').find("input[id='addr']").val(match[1]);
}
$(target).closest('form').find("input[id='addr']").trigger('focus');
});
break;
case 'add_cluster_form':
$("div[id='add_cluster'").css('display', 'inline');
break;
case 'add_cluster':
var input = collect_values($(target).closest('form').find("input[type='text'], select"));
action['input'] = input;
$.ajax({url: 'api/' + action.action + backend, method: 'GET', data: action, dataType: 'json'}).done(function(c){
console.log(c);
load_list();
if ( req_error(c) ) return;
$("div[id='add_cluster'] input[id='cancel']").trigger('click');
});
break;
case 'cancel_form':
var form = $(target).closest('form');
var div = $(target).closest("div[c_level='editor']");
$(div).css('display', 'none');
$(form).removeData('ref');
$(form).find('input[type=text],select').each(function(){
$(this).val('')
});
break;
}
console.log(action);
}
function req_error(c)
{
var tab = ' ';
if ( c.status ) {
var errmsg = "";
if ( c.hasOwnProperty('msg') ) {
for(var x in c.msg) {
errmsg += tab + c.msg[x] + "\n";
}
}
if ( c.hasOwnProperty('cmd') ) {
errmsg += "CMD:\n" + tab + c.cmd + "\n";
}
alert("ERROR:\n" + errmsg);
return true;
}
return false;
}
function collect_values(dom)
{
var input = {};
$(dom).each(function(){
input[$(this).attr('id')] = $(this).val();
});
return input;
}
function fill_fields(obj, dom)
{
dom.each(function() {
var id = $(this).attr('id');
if ( obj.hasOwnProperty(id) ) {
if ( $(this).is('input') || $(this).is('select') ) {
$(this).val(obj[id]);
} else {
$(this).text(obj[id]);
}
}
});
}
function load_list()
{
$("#cluster_list tr[id!='heading']").remove();
$.ajax({url: 'api/list' + backend, method: 'GET', data: {action: 'list'}, dataType: 'json'}).done(function(c){
lvs = c;
var t_cluster_row = document.querySelector('#cluster_row');
lvs.clusters.forEach(function(x){
var c_clone = document.importNode(t_cluster_row.content, true);
fill_fields(x ,$(c_clone).find("tr[id='cluster'] td"));
$(c_clone).find('#cluster').data('ref', {'cluster': x});
var t_server_row = $(c_clone).find('#server_row').get(0);
x.servers.forEach(function(y){
var s_clone = document.importNode(t_server_row.content, true);
var small_cluster = $.extend({}, x);
delete small_cluster.servers;
$(s_clone).find('#server').data('ref', {'server': y, 'cluster': small_cluster});
fill_fields(y, $(s_clone).find("tr[id='server'] td"));
$(c_clone).find('#servers').append(s_clone);
});
$('#cluster_list').append(c_clone);
});
please_wait = false;
});
}
$(document).ready(function()
{
load_list();
});
</script>
<input onclick="load_list()" value="reload" type="button">
<input onclick="h_onclick(event)" action="add_cluster_form" value="Add Cluster" type="button">
<table id="cluster_list">
<tr id="heading">
<th>Protocol</th>
<th>Address</th>
<th>Port</th>
<th>Scheduler</th>
<th>Action</th>
</tr>
</table>
</body>
</html>