summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-lxc/luasrc/view/lxc.htm
blob: b45e27f51f3fd514fa625a062bac5dd0a62470ed (plain)
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
<%#

LuCI LXC module

Copyright (C) 2014, Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Author: Petar Koretic <petar.koretic@sartura.hr>

-%>

<%-
local nx     = require "nixio"
local target = nx.uname().machine
-%>

<fieldset class="cbi-section">
	<legend><%:Available Containers%></legend>
	<div class="cbi-section-node">
		<div class="table cbi-section-table" id="t_lxc_list">
			<div class="tr cbi-section-table-titles">
				<div class="th cbi-section-table-cell"><%:Name%></div>
				<div class="th cbi-section-table-cell"><%:Status%></div>
				<div class="th cbi-section-table-cell"><%:Actions%></div>
			</div>
		</div>
	</div>
</fieldset>

<fieldset class="cbi-section">
	<span id="lxc-list-output"></span>
</fieldset>

<hr />
<fieldset class="cbi-section">
	<legend><%:Create New Container%></legend>
	<div class="cbi-section-node">
		<div class="table cbi-section-table" id="t_lxc_create">
			<div class="tr cbi-section-table-titles">
				<div class="th cbi-section-table-cell"><%:Name%></div>
				<div class="th cbi-section-table-cell"><%:Template%></div>
				<div class="th cbi-section-table-cell"><%:Actions%></div>
			</div>
			<div class="tr cbi-section-table-row" id="div_create">
				<div class="td"><input class="cbi-input-text" type="text" id="tx_name" placeholder="<%:Enter new name%>" value='' /></div>
				<div class="td"><select id="s_template" class="cbi-input-select cbi-button"></select></div>
				<div class="td">
					<input type="button" id="bt_create" value="<%:Create%>" onclick="lxc_create()" class="cbi-button cbi-button-add" />
					<span id="lxc-add-loader" style="display:inline-block; width:16px; height:16px; margin:0 5px"></span>
				</div>
			</div>
		</div>
	</div>
</fieldset>

<fieldset class="cbi-section">
	<span id="lxc-add-output"></span>
</fieldset>

<hr />

<script type="text/javascript" src="<%=resource%>/cbi.js"></script>
<script type="text/javascript">
//<![CDATA[
	window.img    = { "red" : "<%=resource%>/cbi/red.gif", "green" : "<%=resource%>/cbi/green.gif", "purple" : "<%=resource%>/cbi/purple.gif" };
	window.states = { "STOPPED" : "red", "RUNNING" : "green", "FROZEN" : "purple" };

	var t_lxc_list  = document.getElementById('t_lxc_list');
	var loader_html = '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" width="16" height="16" style="vertical-align:middle" />';
	var output_list = document.getElementById("lxc-list-output");
	var output_add  = document.getElementById("lxc-add-output");
	var loader_add  = document.getElementById("lxc-add-loader");
	var div_create  = document.getElementById("div_create");
	var bt_create   = div_create.querySelector("#bt_create");

	bt_create.disabled = true;
	info_message(output_add, "Template download in progress, please be patient!");

	function lxc_create()
	{
		var lxc_name     = div_create.querySelector("#tx_name").value.replace(/[\s!@#$%^&*()+=\[\]{};':"\\|,<>\/?]/g,'');
		var lxc_template = div_create.querySelector("#s_template").value;

		if (t_lxc_list.querySelector("[data-id='" + lxc_name + "']") != null)
		{
			return info_message(output_add, "Container with that name already exists!", 2000);
		}

		bt_create.disabled = true;
		output_add.innerHTML = '';

		if (!lxc_template)
		{
			return set_no_template();
		}

		if (!lxc_name || !lxc_name.length)
		{
			bt_create.disabled = false;
			return info_message(output_add, "The 'Name' field must not be empty!", 2000);
		}

		loading(loader_add);

		new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_create/' + '%h/%h'.format(lxc_name, lxc_template) , null,
		function(x)
		{
			bt_create.disabled = false;
			loading(loader_add, 0);

			if (!x)
			{
				info_message(output_add, "Container creation failed!", 2000);
			}
		})
	}

	function lxc_create_template(lxc_name, lxc_state)
	{
		if (document.getElementById(lxc_name))
		{
			return;
		}

		info_message(output_list, "");
		var actions = '';
		actions += '<input type="button" onclick="action_handler(this)" data-action="start" value="<%:Start%>" class="cbi-button cbi-button-apply" />';
		actions += '&#160;<input type="button" onclick="action_handler(this)" data-action="stop" value="<%:Stop%>" class="cbi-button cbi-button-reset" />';
		actions += '&#160;<input type="button" onclick="action_handler(this)" data-action="destroy" value="<%:Delete%>" class="cbi-button cbi-button-remove" />';
		actions += '&#160;<select class="cbi-input-select cbi-button" style="width:10em" onchange="action_more_handler(this)">\
						<option selected="selected" disabled="disabled">more</option>\
						<option>configure</option>\
						<option>freeze</option>\
						<option>unfreeze</option>\
						<option>reboot</option>\
					</select>';
		actions += '<span data-loader="" style="display:inline-block; width:16px; height:16px; margin:0 5px"></span>';

		var div0 = document.createElement("div");
		div0.className = "tr cbi-section-table-row";
		div0.id = lxc_name;
		div0.setAttribute("data-id", lxc_name);

		var div1 = document.createElement("div");
		div1.className = "td";
		div1.style.width = "30%";
		div1.innerHTML = '%q%h%q'.format("<strong>", lxc_name, "</strong>");

		var div2 = document.createElement("div");
		div2.className = "td";
		div2.style.width = "20%";
		div2.innerHTML = "<img src='"+window.img[lxc_state]+"'/>";

		var div3 = document.createElement("div");
		div3.className = "td";
		div3.style.width = "50%";
		div3.innerHTML = actions;

		document.getElementById("t_lxc_list").appendChild(div0);
		div0.appendChild(div1);
		div0.appendChild(div2);
		div0.appendChild(div3);
	}

	function action_handler(self)
	{
		var bt_action  = self;
		var action     = self.dataset['action'];
		var lxc_name   = self.parentNode.parentNode.dataset['id'];
		var status_img = self.parentNode.parentNode.querySelector('img');
		var loader     = self.parentNode.querySelector('[data-loader]');

		bt_action.disabled = true;

		if (action == "stop")
		{
			loading(loader);

			new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
			function(x, ec)
			{
				loading(loader, 0);
				bt_action.disabled = false;

				if (!x || ec)
				{
					return info_message(output_list,"Action failed!", 2000);
				}
				set_status(status_img, "red");
			});
		}
		else if (action == "start")
		{
			loading(loader);

			new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
			function(x, data)
			{
				loading(loader, 0);
				bt_action.disabled = false;

				if (!x || data)
				{
					return info_message(output_list,"Action failed!", 2000);
				}
				set_status(status_img, "green");
			});
		}
		else if (action == "destroy")
		{
			var div = self.parentNode.parentNode;
			var img = div.querySelector('img');

			if (img.getAttribute('src') != window.img["red"])
			{
				bt_action.disabled = false;
				return info_message(output_list,"Container is still running!", 2000);
			}

			if (!confirm("This will completely remove a stopped LXC container from disk. Are you sure?"))
			{
				bt_action.disabled = false;
				return;
			}
			loading(loader);

			new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
			function(x, ec)
			{
				loading(loader, 0);
				bt_action.disabled = false;

				if (!x || ec)
				{
					return info_message(output_list,"Action failed!", 2000);
				}
				var div = self.parentNode.parentNode;
				div.parentNode.removeChild(div);
			});
		}
	}

	function lxc_configure_handler(self)
	{
		var div      = self.parentNode;
		var textarea = div.querySelector('[data-id]');
		var lxc_name = textarea.dataset['id'];
		var lxc_conf = textarea.value;

	new XHR().post('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_configuration_set/' + lxc_name, "lxc_conf=" + encodeURIComponent(lxc_conf),
		function(x)
		{
			if (!x || x.responseText != "0")
			{
				return info_message(output_list,"Action failed!", 2000);
			}
			info_message(output_list,"LXC configuration updated", 2000);
			var rmdiv = div.parentNode;
			rmdiv.parentNode.removeChild(rmdiv);
		})
	}

	function lxc_configure_template(lxc_name, lxc_conf)
	{
		var h = '\
			<textarea data-id="' + lxc_name + '" rows="20" style="width:600px;font-family:monospace;white-space:pre;overflow-wrap:normal;overflow-x:scroll;">'+ lxc_conf +'</textarea> \
			<input data-id="bt_confirm" onclick="lxc_configure_handler(this)" type="button" class="cbi-button" value="Confirm" />';
		return h;
	}

	function action_more_handler(self)
	{
		var lxc_name = self.parentNode.parentNode.dataset['id'];
		var loader   = self.parentNode.querySelector('[data-loader]');
		var option   = self.options[self.selectedIndex].text;
		self.value   = "more";

		switch(option)
		{
			case "configure":
				var div0 = document.createElement('div');
				var div1 = self.parentNode.parentNode;
				var next_div = div1.nextSibling;

				if (next_div && next_div.dataset['action'] !== null)
				{
					div1.parentNode.removeChild(next_div);
				}

				new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_configuration_get/' + lxc_name, null,
				function(x)
				{
					div0.innerHTML="<div>" + lxc_configure_template(lxc_name, x.responseText) + "</div>";
					div0.setAttribute('data-action','');
					div1.parentNode.insertBefore(div0, div1.nextSibling);
				})
			break;

			case "freeze":
				var img = self.parentNode.parentNode.querySelector('img');
				if(img.getAttribute('src') != window.img["green"])
				{
					return info_message(output_list,"Container is not running!", 2000);
				}

				loading(loader);

				new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
				function(x, ec)
				{
					loading(loader, 0)
					if (!x || ec)
					{
						return info_message(output_list,"Action failed!", 2000);
					}
					set_status(img, "purple");
				})
			break;

			case "unfreeze":
				var img = self.parentNode.parentNode.querySelector('img');
				if(img.getAttribute('src') != window.img["purple"])
				{
					return info_message(output_list,"Container is not frozen!", 2000);
				}

				loading(loader);

				new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
				function(x, ec)
				{
					loading(loader, 0);
					if (!x || ec)
					{
						return info_message(output_list,"Action failed!", 2000);
					}
					set_status(img, "green");
				})
			break;

			case "reboot":
				var img = self.parentNode.parentNode.querySelector('img');
				if(img.getAttribute('src') != window.img["green"])
				{
					return info_message(output_list,"Container is not running!", 2000);
				}

				if (!confirm("Are you sure?"))
				{
					return;
				}

				loading(loader);

				new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
				function(x, ec)
				{
					loading(loader, 0)
					if (!x || ec)
					{
						return info_message(output_list,"Action failed!", 2000);
					}
					info_message(output_list,"LXC container rebooted", 2000);
				})
			break;
		}
	}

	function set_no_container()
	{
		info_message(output_list, "There are no containers available yet.");
	}

	function set_no_template()
	{
		bt_create.disabled = true;
		info_message(output_add, "There are no templates for your architecture (<%=target%>) available, please select another containers URL.");
	}

	function lxc_list_update()
	{
		XHR.poll(4, '<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/list', null,
		function(x, data)
		{
			if (!x || !data)
			{
				return;
			}

			var lxc_count = Object.keys(data).length;
			if (!lxc_count)
			{
				return set_no_container();
			}

			var lxcs = t_lxc_list.querySelectorAll('td[data-id]');
			var lxc_name_div = {};
			for (var i = 0, len = lxcs.length; i < len; i++)
			{
				var lxc_name = lxcs[i].dataset['id'];
				if (!(lxc_name in data))
				{
					var div = t_lxc_list.querySelector("[data-id='" + lxc_name + "']").parentNode;
					div.parentNode.removeChild(div);
					continue;
				}
				lxc_name_div[lxc_name] = lxcs[i].parentNode.querySelector('img');
			}

			for(var key in data)
			{
				var lxc_name = key;
				var state = window.states[data[key]];

				if (!(lxc_name in lxc_name_div))
				{
					lxc_create_template(lxc_name, state);
				}
				else if (state != get_status(lxc_name_div[lxc_name]))
				{
					set_status(lxc_name_div[lxc_name], state);
				}
			}
		})
	}

	function loading(elem, state)
	{
		state = (typeof state === 'undefined') ? 1 : state;
		if (state === 1)
		{
			elem.innerHTML = loader_html;
		}
		else
		{
			setTimeout(function() { elem.innerHTML = ''}, 2000);
		}
	}

	function set_status(elem, state)
	{
		state = (typeof state === 'undefined') ? 1 : state;
		setTimeout(function() { elem.setAttribute('src', window.img[state])}, 300);
	}

	function get_status(elem)
	{
		var src = elem.getAttribute('src');
		for (var i in img)
		{
			if (img[i] == src)
			{
				return i;
			}
		}
	}

	function info_message(output, msg, timeout)
	{
		timeout = timeout || 0;
		output.innerHTML = '<em>' + msg + '</em>';
		if (timeout > 0)
		{
			setTimeout(function(){ output.innerHTML=""}, timeout);
		}
	}

	new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_get_downloadable', null,
	function(x, data)
	{
		if (!x)
		{
			return;
		}

		if (!data)
		{
			return set_no_template();
		}

		var lxc_count = Object.keys(data).length;
		if (!lxc_count)
		{
			return set_no_template();
		}

		var select = document.getElementById("s_template");
		for(var key in data)
		{
			var option = document.createElement('option');
			option.value = data[key];
			option.text = data[key].replace(/[_:]/g, ' ');
			select.add(option, -1);
		}

		info_message(output_add, "");
		bt_create.disabled = false;
	})

	lxc_list_update();
//]]>
</script>