5 Copyright (C)
2014, Cisco Systems, Inc.
7 Licensed under the Apache License, Version
2.0 (the
"License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-
2.0
13 Author: Petar Koretic
<petar.koretic@sartura.hr
>
18 local nx = require
"nixio"
19 local target = nx.uname().machine
22 <div class=
"cbi-section">
23 <h3><%:Available Containers%
></h3>
24 <div class=
"cbi-section-node">
25 <div class=
"table cbi-section-table" id=
"t_lxc_list">
26 <div class=
"tr cbi-section-table-titles">
27 <div class=
"th cbi-section-table-cell"><%:Name%
></div>
28 <div class=
"th cbi-section-table-cell"><%:Status%
></div>
29 <div class=
"th cbi-section-table-cell"><%:Actions%
></div>
35 <div class=
"cbi-section">
36 <span id=
"lxc-list-output"></span>
40 <div class=
"cbi-section">
41 <h3><%:Create New Container%
></h3>
42 <div class=
"cbi-section-node">
43 <div class=
"table cbi-section-table" id=
"t_lxc_create">
44 <div class=
"tr cbi-section-table-titles">
45 <div class=
"th cbi-section-table-cell"><%:Name%
></div>
46 <div class=
"th cbi-section-table-cell"><%:Template%
></div>
47 <div class=
"th cbi-section-table-cell"><%:Actions%
></div>
49 <div class=
"tr cbi-section-table-row" id=
"div_create">
50 <div class=
"td"><input class=
"cbi-input-text" type=
"text" id=
"tx_name" placeholder=
"<%:Enter new name%>" value=''
/></div>
51 <div class=
"td"><select id=
"s_template" class=
"cbi-input-select cbi-button"></select></div>
53 <input type=
"button" id=
"bt_create" value=
"<%:Create%>" onclick=
"lxc_create()" class=
"cbi-button cbi-button-add" />
54 <span id=
"lxc-add-loader" style=
"display:inline-block; width:16px; height:16px; margin:0 5px"></span>
61 <div class=
"cbi-section">
62 <span id=
"lxc-add-output"></span>
67 <script type=
"text/javascript" src=
"<%=resource%>/cbi.js"></script>
68 <script type=
"text/javascript">
70 window.img = {
"red" :
"<%=resource%>/cbi/red.gif",
"green" :
"<%=resource%>/cbi/green.gif",
"purple" :
"<%=resource%>/cbi/purple.gif" };
71 window.states = {
"STOPPED" :
"red",
"RUNNING" :
"green",
"FROZEN" :
"purple" };
73 var t_lxc_list = document.getElementById('t_lxc_list');
74 var loader_html = '
<img src=
"<%=resource%>/icons/loading.gif" alt=
"<%:Loading%>" width=
"16" height=
"16" style=
"vertical-align:middle" />';
75 var output_list = document.getElementById(
"lxc-list-output");
76 var output_add = document.getElementById(
"lxc-add-output");
77 var loader_add = document.getElementById(
"lxc-add-loader");
78 var div_create = document.getElementById(
"div_create");
79 var bt_create = div_create.querySelector(
"#bt_create");
81 bt_create.disabled = true;
82 info_message(output_add,
"Template download in progress, please be patient!");
86 var lxc_name = div_create.querySelector(
"#tx_name").value.replace(/[\s!@#$%^&*()+=\[\]{};':
"\\|,<>\/?]/g,'');
87 var lxc_template = div_create.querySelector("#s_template
").value;
89 if (t_lxc_list.querySelector("[data-id='
" + lxc_name + "']
") != null)
91 return info_message(output_add, "Container with that name already exists!
", 2000);
94 bt_create.disabled = true;
95 output_add.innerHTML = '';
99 return set_no_template();
102 if (!lxc_name || !lxc_name.length)
104 bt_create.disabled = false;
105 return info_message(output_add, "The 'Name' field must not be empty!
", 2000);
110 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_create/' + '%h/%h'.format(lxc_name, lxc_template) , null,
113 bt_create.disabled = false;
114 loading(loader_add, 0);
118 info_message(output_add, "Container creation failed!
", 2000);
123 function lxc_create_template(lxc_name, lxc_state)
125 if (document.getElementById(lxc_name))
130 info_message(output_list, "");
132 actions += '<input type="button
" onclick="action_handler(this)
" data-action="start
" value="<%:Start%
>" class="cbi-button cbi-button-apply
" />';
133 actions += ' <input type="button
" onclick="action_handler(this)
" data-action="stop
" value="<%:Stop%
>" class="cbi-button cbi-button-reset
" />';
134 actions += ' <input type="button
" onclick="action_handler(this)
" data-action="destroy
" value="<%:Delete%
>" class="cbi-button cbi-button-remove
" />';
135 actions += ' <select class="cbi-input-select cbi-button
" style="width:
10em
" onchange="action_more_handler(this)
">\
136 <option selected="selected
" disabled="disabled
">more</option>\
137 <option>configure</option>\
138 <option>freeze</option>\
139 <option>unfreeze</option>\
140 <option>reboot</option>\
142 actions += '<span data-loader="" style="display:inline-block; width:
16px; height:
16px; margin:
0 5px
"></span>';
144 var div0 = document.createElement("div
");
145 div0.className = "tr cbi-section-table-row
";
147 div0.setAttribute("data-id
", lxc_name);
149 var div1 = document.createElement("div
");
150 div1.className = "td
";
151 div1.style.width = "30%
";
152 div1.innerHTML = '%q%h%q'.format("<strong>", lxc_name, "</strong>");
154 var div2 = document.createElement("div
");
155 div2.className = "td
";
156 div2.style.width = "20%
";
157 div2.innerHTML = "<img src='
"+window.img[lxc_state]+"'
/>";
159 var div3 = document.createElement("div
");
160 div3.className = "td
";
161 div3.style.width = "50%
";
162 div3.innerHTML = actions;
164 document.getElementById("t_lxc_list
").appendChild(div0);
165 div0.appendChild(div1);
166 div0.appendChild(div2);
167 div0.appendChild(div3);
170 function action_handler(self)
172 var bt_action = self;
173 var action = self.dataset['action'];
174 var lxc_name = self.parentNode.parentNode.dataset['id'];
175 var status_img = self.parentNode.parentNode.querySelector('img');
176 var loader = self.parentNode.querySelector('[data-loader]');
178 bt_action.disabled = true;
180 if (action == "stop
")
184 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
188 bt_action.disabled = false;
192 return info_message(output_list,"Action failed!
", 2000);
194 set_status(status_img, "red
");
197 else if (action == "start
")
201 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
205 bt_action.disabled = false;
209 return info_message(output_list,"Action failed!
", 2000);
211 set_status(status_img, "green
");
214 else if (action == "destroy
")
216 var div = self.parentNode.parentNode;
217 var img = div.querySelector('img');
219 if (img.getAttribute('src') != window.img["red
"])
221 bt_action.disabled = false;
222 return info_message(output_list,"Container is still running!
", 2000);
225 if (!confirm("This will completely remove a stopped LXC container from disk. Are you sure?
"))
227 bt_action.disabled = false;
232 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
236 bt_action.disabled = false;
240 return info_message(output_list,"Action failed!
", 2000);
242 var div = self.parentNode.parentNode;
243 div.parentNode.removeChild(div);
248 function lxc_configure_handler(self)
250 var div = self.parentNode;
251 var textarea = div.querySelector('[data-id]');
252 var lxc_name = textarea.dataset['id'];
253 var lxc_conf = textarea.value;
255 new XHR().post('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_configuration_set/' + lxc_name, "lxc_conf=
" + encodeURIComponent(lxc_conf),
258 if (!x || x.responseText != "0")
260 return info_message(output_list,"Action failed!
", 2000);
262 info_message(output_list,"LXC configuration updated
", 2000);
263 var rmdiv = div.parentNode;
264 rmdiv.parentNode.removeChild(rmdiv);
268 function lxc_configure_template(lxc_name, lxc_conf)
271 <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> \
272 <input data-id="bt_confirm
" onclick="lxc_configure_handler(this)
" type="button
" class="cbi-button
" value="Confirm
" />';
276 function action_more_handler(self)
278 var lxc_name = self.parentNode.parentNode.dataset['id'];
279 var loader = self.parentNode.querySelector('[data-loader]');
280 var option = self.options[self.selectedIndex].text;
286 var div0 = document.createElement('div');
287 var div1 = self.parentNode.parentNode;
288 var next_div = div1.nextSibling;
290 if (next_div && next_div.dataset['action'] !== null)
292 div1.parentNode.removeChild(next_div);
295 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_configuration_get/' + lxc_name, null,
298 div0.innerHTML="<div>" + lxc_configure_template(lxc_name, x.responseText) + "</div>";
299 div0.setAttribute('data-action','');
300 div1.parentNode.insertBefore(div0, div1.nextSibling);
305 var img = self.parentNode.parentNode.querySelector('img');
306 if(img.getAttribute('src') != window.img["green
"])
308 return info_message(output_list,"Container is not running!
", 2000);
313 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
319 return info_message(output_list,"Action failed!
", 2000);
321 set_status(img, "purple
");
326 var img = self.parentNode.parentNode.querySelector('img');
327 if(img.getAttribute('src') != window.img["purple
"])
329 return info_message(output_list,"Container is not frozen!
", 2000);
334 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
340 return info_message(output_list,"Action failed!
", 2000);
342 set_status(img, "green
");
347 var img = self.parentNode.parentNode.querySelector('img');
348 if(img.getAttribute('src') != window.img["green
"])
350 return info_message(output_list,"Container is not running!
", 2000);
353 if (!confirm("Are you sure?
"))
360 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
366 return info_message(output_list,"Action failed!
", 2000);
368 info_message(output_list,"LXC container rebooted
", 2000);
374 function set_no_container()
376 info_message(output_list, "There are no containers available yet.
");
379 function set_no_template()
381 bt_create.disabled = true;
382 info_message(output_add, "There are no templates for your architecture (<%=target%
>) available, please select another containers URL.
");
385 function lxc_list_update()
387 XHR.poll(4, '<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/list', null,
395 var lxc_count = Object.keys(data).length;
398 return set_no_container();
401 var lxcs = t_lxc_list.querySelectorAll('td[data-id]');
402 var lxc_name_div = {};
403 for (var i = 0, len = lxcs.length; i < len; i++)
405 var lxc_name = lxcs[i].dataset['id'];
406 if (!(lxc_name in data))
408 var div = t_lxc_list.querySelector("[data-id='
" + lxc_name + "']
").parentNode;
409 div.parentNode.removeChild(div);
412 lxc_name_div[lxc_name] = lxcs[i].parentNode.querySelector('img');
418 var state = window.states[data[key]];
420 if (!(lxc_name in lxc_name_div))
422 lxc_create_template(lxc_name, state);
424 else if (state != get_status(lxc_name_div[lxc_name]))
426 set_status(lxc_name_div[lxc_name], state);
432 function loading(elem, state)
434 state = (typeof state === 'undefined') ? 1 : state;
437 elem.innerHTML = loader_html;
441 setTimeout(function() { elem.innerHTML = ''}, 2000);
445 function set_status(elem, state)
447 state = (typeof state === 'undefined') ? 1 : state;
448 setTimeout(function() { elem.setAttribute('src', window.img[state])}, 300);
451 function get_status(elem)
453 var src = elem.getAttribute('src');
463 function info_message(output, msg, timeout)
465 timeout = timeout || 0;
466 output.innerHTML = '<em>' + msg + '</em>';
469 setTimeout(function(){ output.innerHTML=""}, timeout);
473 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_get_downloadable', null,
483 return set_no_template();
486 var lxc_count = Object.keys(data).length;
489 return set_no_template();
492 var select = document.getElementById("s_template
");
495 var option = document.createElement('option');
496 option.value = data[key];
497 option.text = data[key].replace(/[_:]/g, ' ');
498 select.add(option, -1);
501 info_message(output_add, "");
502 bt_create.disabled = false;