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">
69 window.img = {
"red" :
"<%=resource%>/cbi/red.gif",
"green" :
"<%=resource%>/cbi/green.gif",
"purple" :
"<%=resource%>/cbi/purple.gif" };
70 window.states = {
"STOPPED" :
"red",
"RUNNING" :
"green",
"FROZEN" :
"purple" };
72 var t_lxc_list = document.getElementById('t_lxc_list');
73 var loader_html = '
<img src=
"<%=resource%>/icons/loading.gif" alt=
"<%:Loading%>" width=
"16" height=
"16" style=
"vertical-align:middle" />';
74 var output_list = document.getElementById(
"lxc-list-output");
75 var output_add = document.getElementById(
"lxc-add-output");
76 var loader_add = document.getElementById(
"lxc-add-loader");
77 var div_create = document.getElementById(
"div_create");
78 var bt_create = div_create.querySelector(
"#bt_create");
80 bt_create.disabled = true;
81 info_message(output_add,
"Template download in progress, please be patient!");
85 var lxc_name = div_create.querySelector(
"#tx_name").value.replace(/[\s!@#$%^&*()+=\[\]{};':
"\\|,<>\/?]/g,'');
86 var lxc_template = div_create.querySelector("#s_template
").value;
88 if (t_lxc_list.querySelector("[data-id='
" + lxc_name + "']
") != null)
90 return info_message(output_add, "Container with that name already exists!
", 2000);
93 bt_create.disabled = true;
94 output_add.innerHTML = '';
98 return set_no_template();
101 if (!lxc_name || !lxc_name.length)
103 bt_create.disabled = false;
104 return info_message(output_add, "The 'Name' field must not be empty!
", 2000);
109 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_create/' + '%h/%h'.format(lxc_name, lxc_template) , null,
112 bt_create.disabled = false;
113 loading(loader_add, 0);
117 info_message(output_add, "Container creation failed!
", 2000);
122 function lxc_create_template(lxc_name, lxc_state)
124 if (document.getElementById(lxc_name))
129 info_message(output_list, "");
131 actions += '<input type="button
" onclick="action_handler(this)
" data-action="start
" value="<%:Start%
>" class="cbi-button cbi-button-apply
" />';
132 actions += ' <input type="button
" onclick="action_handler(this)
" data-action="stop
" value="<%:Stop%
>" class="cbi-button cbi-button-reset
" />';
133 actions += ' <input type="button
" onclick="action_handler(this)
" data-action="destroy
" value="<%:Delete%
>" class="cbi-button cbi-button-remove
" />';
134 actions += ' <select class="cbi-input-select cbi-button
" style="width:
10em
" onchange="action_more_handler(this)
">\
135 <option selected="selected
" disabled="disabled
">more</option>\
136 <option>configure</option>\
137 <option>freeze</option>\
138 <option>unfreeze</option>\
139 <option>reboot</option>\
141 actions += '<span data-loader="" style="display:inline-block; width:
16px; height:
16px; margin:
0 5px
"></span>';
143 var div0 = document.createElement("div
");
144 div0.className = "tr cbi-section-table-row
";
146 div0.setAttribute("data-id
", lxc_name);
148 var div1 = document.createElement("div
");
149 div1.className = "td
";
150 div1.style.width = "30%
";
151 div1.innerHTML = '%q%h%q'.format("<strong>", lxc_name, "</strong>");
153 var div2 = document.createElement("div
");
154 div2.className = "td
";
155 div2.style.width = "20%
";
156 div2.innerHTML = "<img src='
"+window.img[lxc_state]+"'
/>";
158 var div3 = document.createElement("div
");
159 div3.className = "td
";
160 div3.style.width = "50%
";
161 div3.innerHTML = actions;
163 document.getElementById("t_lxc_list
").appendChild(div0);
164 div0.appendChild(div1);
165 div0.appendChild(div2);
166 div0.appendChild(div3);
169 function action_handler(self)
171 var bt_action = self;
172 var action = self.dataset['action'];
173 var lxc_name = self.parentNode.parentNode.dataset['id'];
174 var status_img = self.parentNode.parentNode.querySelector('img');
175 var loader = self.parentNode.querySelector('[data-loader]');
177 bt_action.disabled = true;
179 if (action == "stop
")
183 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
187 bt_action.disabled = false;
191 return info_message(output_list,"Action failed!
", 2000);
193 set_status(status_img, "red
");
196 else if (action == "start
")
200 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
204 bt_action.disabled = false;
208 return info_message(output_list,"Action failed!
", 2000);
210 set_status(status_img, "green
");
213 else if (action == "destroy
")
215 var div = self.parentNode.parentNode;
216 var img = div.querySelector('img');
218 if (img.getAttribute('src') != window.img["red
"])
220 bt_action.disabled = false;
221 return info_message(output_list,"Container is still running!
", 2000);
224 if (!confirm("This will completely remove a stopped LXC container from disk. Are you sure?
"))
226 bt_action.disabled = false;
231 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
235 bt_action.disabled = false;
239 return info_message(output_list,"Action failed!
", 2000);
241 var div = self.parentNode.parentNode;
242 div.parentNode.removeChild(div);
247 function lxc_configure_handler(self)
249 var div = self.parentNode;
250 var textarea = div.querySelector('[data-id]');
251 var lxc_name = textarea.dataset['id'];
252 var lxc_conf = textarea.value;
254 new XHR().post('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_configuration_set/' + lxc_name, "lxc_conf=
" + encodeURIComponent(lxc_conf),
257 if (!x || x.responseText != "0")
259 return info_message(output_list,"Action failed!
", 2000);
261 info_message(output_list,"LXC configuration updated
", 2000);
262 var rmdiv = div.parentNode;
263 rmdiv.parentNode.removeChild(rmdiv);
267 function lxc_configure_template(lxc_name, lxc_conf)
270 <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> \
271 <input data-id="bt_confirm
" onclick="lxc_configure_handler(this)
" type="button
" class="cbi-button
" value="Confirm
" />';
275 function action_more_handler(self)
277 var lxc_name = self.parentNode.parentNode.dataset['id'];
278 var loader = self.parentNode.querySelector('[data-loader]');
279 var option = self.options[self.selectedIndex].text;
285 var div0 = document.createElement('div');
286 var div1 = self.parentNode.parentNode;
287 var next_div = div1.nextSibling;
289 if (next_div && next_div.dataset['action'] !== null)
291 div1.parentNode.removeChild(next_div);
294 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_configuration_get/' + lxc_name, null,
297 div0.innerHTML="<div>" + lxc_configure_template(lxc_name, x.responseText) + "</div>";
298 div0.setAttribute('data-action','');
299 div1.parentNode.insertBefore(div0, div1.nextSibling);
304 var img = self.parentNode.parentNode.querySelector('img');
305 if(img.getAttribute('src') != window.img["green
"])
307 return info_message(output_list,"Container is not running!
", 2000);
312 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
318 return info_message(output_list,"Action failed!
", 2000);
320 set_status(img, "purple
");
325 var img = self.parentNode.parentNode.querySelector('img');
326 if(img.getAttribute('src') != window.img["purple
"])
328 return info_message(output_list,"Container is not frozen!
", 2000);
333 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
339 return info_message(output_list,"Action failed!
", 2000);
341 set_status(img, "green
");
346 var img = self.parentNode.parentNode.querySelector('img');
347 if(img.getAttribute('src') != window.img["green
"])
349 return info_message(output_list,"Container is not running!
", 2000);
352 if (!confirm("Are you sure?
"))
359 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
365 return info_message(output_list,"Action failed!
", 2000);
367 info_message(output_list,"LXC container rebooted
", 2000);
373 function set_no_container()
375 info_message(output_list, "There are no containers available yet.
");
378 function set_no_template()
380 bt_create.disabled = true;
381 info_message(output_add, "There are no templates for your architecture (<%=target%
>) available, please select another containers URL.
");
384 function lxc_list_update()
386 XHR.poll(4, '<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_action/list', null,
394 var lxc_count = Object.keys(data).length;
397 return set_no_container();
400 var lxcs = t_lxc_list.querySelectorAll('td[data-id]');
401 var lxc_name_div = {};
402 for (var i = 0, len = lxcs.length; i < len; i++)
404 var lxc_name = lxcs[i].dataset['id'];
405 if (!(lxc_name in data))
407 var div = t_lxc_list.querySelector("[data-id='
" + lxc_name + "']
").parentNode;
408 div.parentNode.removeChild(div);
411 lxc_name_div[lxc_name] = lxcs[i].parentNode.querySelector('img');
417 var state = window.states[data[key]];
419 if (!(lxc_name in lxc_name_div))
421 lxc_create_template(lxc_name, state);
423 else if (state != get_status(lxc_name_div[lxc_name]))
425 set_status(lxc_name_div[lxc_name], state);
431 function loading(elem, state)
433 state = (typeof state === 'undefined') ? 1 : state;
436 elem.innerHTML = loader_html;
440 setTimeout(function() { elem.innerHTML = ''}, 2000);
444 function set_status(elem, state)
446 state = (typeof state === 'undefined') ? 1 : state;
447 setTimeout(function() { elem.setAttribute('src', window.img[state])}, 300);
450 function get_status(elem)
452 var src = elem.getAttribute('src');
462 function info_message(output, msg, timeout)
464 timeout = timeout || 0;
465 output.innerHTML = '<em>' + msg + '</em>';
468 setTimeout(function(){ output.innerHTML=""}, timeout);
472 new XHR().get('<%=luci.dispatcher.build_url("admin
", "services
")%>/lxc_get_downloadable', null,
482 return set_no_template();
485 var lxc_count = Object.keys(data).length;
488 return set_no_template();
491 var select = document.getElementById("s_template
");
494 var option = document.createElement('option');
495 option.value = data[key];
496 option.text = data[key].replace(/[_:]/g, ' ');
497 select.add(option, -1);
500 info_message(output_add, "");
501 bt_create.disabled = false;