lxc.htm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <%#
  2. LuCI LXC module
  3. Copyright (C) 2014, Cisco Systems, Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Author: Petar Koretic <petar.koretic@sartura.hr>
  9. -%>
  10. <%-
  11. local nx = require "nixio"
  12. local target = nx.uname().machine
  13. -%>
  14. <fieldset class="cbi-section">
  15. <legend><%:Available Containers%></legend>
  16. <div class="cbi-section-node">
  17. <div class="table cbi-section-table" id="t_lxc_list">
  18. <div class="tr cbi-section-table-titles">
  19. <div class="th cbi-section-table-cell"><%:Name%></div>
  20. <div class="th cbi-section-table-cell"><%:Status%></div>
  21. <div class="th cbi-section-table-cell"><%:Actions%></div>
  22. </div>
  23. </div>
  24. </div>
  25. </fieldset>
  26. <fieldset class="cbi-section">
  27. <span id="lxc-list-output"></span>
  28. </fieldset>
  29. <hr />
  30. <fieldset class="cbi-section">
  31. <legend><%:Create New Container%></legend>
  32. <div class="cbi-section-node">
  33. <div class="table cbi-section-table" id="t_lxc_create">
  34. <div class="tr cbi-section-table-titles">
  35. <div class="th cbi-section-table-cell"><%:Name%></div>
  36. <div class="th cbi-section-table-cell"><%:Template%></div>
  37. <div class="th cbi-section-table-cell"><%:Actions%></div>
  38. </div>
  39. <div class="tr cbi-section-table-row" id="div_create">
  40. <div class="td"><input class="cbi-input-text" type="text" id="tx_name" placeholder="<%:Enter new name%>" value='' /></div>
  41. <div class="td"><select id="s_template" class="cbi-input-select cbi-button"></select></div>
  42. <div class="td">
  43. <input type="button" id="bt_create" value="<%:Create%>" onclick="lxc_create()" class="cbi-button cbi-button-add" />
  44. <span id="lxc-add-loader" style="display:inline-block; width:16px; height:16px; margin:0 5px"></span>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. </fieldset>
  50. <fieldset class="cbi-section">
  51. <span id="lxc-add-output"></span>
  52. </fieldset>
  53. <hr />
  54. <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
  55. <script type="text/javascript">
  56. //<![CDATA[
  57. window.img = { "red" : "<%=resource%>/cbi/red.gif", "green" : "<%=resource%>/cbi/green.gif", "purple" : "<%=resource%>/cbi/purple.gif" };
  58. window.states = { "STOPPED" : "red", "RUNNING" : "green", "FROZEN" : "purple" };
  59. var t_lxc_list = document.getElementById('t_lxc_list');
  60. var loader_html = '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" width="16" height="16" style="vertical-align:middle" />';
  61. var output_list = document.getElementById("lxc-list-output");
  62. var output_add = document.getElementById("lxc-add-output");
  63. var loader_add = document.getElementById("lxc-add-loader");
  64. var div_create = document.getElementById("div_create");
  65. var bt_create = div_create.querySelector("#bt_create");
  66. bt_create.disabled = true;
  67. info_message(output_add, "Template download in progress, please be patient!");
  68. function lxc_create()
  69. {
  70. var lxc_name = div_create.querySelector("#tx_name").value.replace(/[\s!@#$%^&*()+=\[\]{};':"\\|,<>\/?]/g,'');
  71. var lxc_template = div_create.querySelector("#s_template").value;
  72. if (t_lxc_list.querySelector("[data-id='" + lxc_name + "']") != null)
  73. {
  74. return info_message(output_add, "Container with that name already exists!", 2000);
  75. }
  76. bt_create.disabled = true;
  77. output_add.innerHTML = '';
  78. if (!lxc_template)
  79. {
  80. return set_no_template();
  81. }
  82. if (!lxc_name || !lxc_name.length)
  83. {
  84. bt_create.disabled = false;
  85. return info_message(output_add, "The 'Name' field must not be empty!", 2000);
  86. }
  87. loading(loader_add);
  88. new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_create/' + '%h/%h'.format(lxc_name, lxc_template) , null,
  89. function(x)
  90. {
  91. bt_create.disabled = false;
  92. loading(loader_add, 0);
  93. if (!x)
  94. {
  95. info_message(output_add, "Container creation failed!", 2000);
  96. }
  97. })
  98. }
  99. function lxc_create_template(lxc_name, lxc_state)
  100. {
  101. if (document.getElementById(lxc_name))
  102. {
  103. return;
  104. }
  105. info_message(output_list, "");
  106. var actions = '';
  107. actions += '<input type="button" onclick="action_handler(this)" data-action="start" value="<%:Start%>" class="cbi-button cbi-button-apply" />';
  108. actions += '&#160;<input type="button" onclick="action_handler(this)" data-action="stop" value="<%:Stop%>" class="cbi-button cbi-button-reset" />';
  109. actions += '&#160;<input type="button" onclick="action_handler(this)" data-action="destroy" value="<%:Delete%>" class="cbi-button cbi-button-remove" />';
  110. actions += '&#160;<select class="cbi-input-select cbi-button" style="width:10em" onchange="action_more_handler(this)">\
  111. <option selected="selected" disabled="disabled">more</option>\
  112. <option>configure</option>\
  113. <option>freeze</option>\
  114. <option>unfreeze</option>\
  115. <option>reboot</option>\
  116. </select>';
  117. actions += '<span data-loader="" style="display:inline-block; width:16px; height:16px; margin:0 5px"></span>';
  118. var div0 = document.createElement("div");
  119. div0.className = "tr cbi-section-table-row";
  120. div0.id = lxc_name;
  121. div0.setAttribute("data-id", lxc_name);
  122. var div1 = document.createElement("div");
  123. div1.className = "td";
  124. div1.style.width = "30%";
  125. div1.innerHTML = '%q%h%q'.format("<strong>", lxc_name, "</strong>");
  126. var div2 = document.createElement("div");
  127. div2.className = "td";
  128. div2.style.width = "20%";
  129. div2.innerHTML = "<img src='"+window.img[lxc_state]+"'/>";
  130. var div3 = document.createElement("div");
  131. div3.className = "td";
  132. div3.style.width = "50%";
  133. div3.innerHTML = actions;
  134. document.getElementById("t_lxc_list").appendChild(div0);
  135. div0.appendChild(div1);
  136. div0.appendChild(div2);
  137. div0.appendChild(div3);
  138. }
  139. function action_handler(self)
  140. {
  141. var bt_action = self;
  142. var action = self.dataset['action'];
  143. var lxc_name = self.parentNode.parentNode.dataset['id'];
  144. var status_img = self.parentNode.parentNode.querySelector('img');
  145. var loader = self.parentNode.querySelector('[data-loader]');
  146. bt_action.disabled = true;
  147. if (action == "stop")
  148. {
  149. loading(loader);
  150. new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
  151. function(x, ec)
  152. {
  153. loading(loader, 0);
  154. bt_action.disabled = false;
  155. if (!x || ec)
  156. {
  157. return info_message(output_list,"Action failed!", 2000);
  158. }
  159. set_status(status_img, "red");
  160. });
  161. }
  162. else if (action == "start")
  163. {
  164. loading(loader);
  165. new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
  166. function(x, data)
  167. {
  168. loading(loader, 0);
  169. bt_action.disabled = false;
  170. if (!x || data)
  171. {
  172. return info_message(output_list,"Action failed!", 2000);
  173. }
  174. set_status(status_img, "green");
  175. });
  176. }
  177. else if (action == "destroy")
  178. {
  179. var div = self.parentNode.parentNode;
  180. var img = div.querySelector('img');
  181. if (img.getAttribute('src') != window.img["red"])
  182. {
  183. bt_action.disabled = false;
  184. return info_message(output_list,"Container is still running!", 2000);
  185. }
  186. if (!confirm("This will completely remove a stopped LXC container from disk. Are you sure?"))
  187. {
  188. bt_action.disabled = false;
  189. return;
  190. }
  191. loading(loader);
  192. new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null,
  193. function(x, ec)
  194. {
  195. loading(loader, 0);
  196. bt_action.disabled = false;
  197. if (!x || ec)
  198. {
  199. return info_message(output_list,"Action failed!", 2000);
  200. }
  201. var div = self.parentNode.parentNode;
  202. div.parentNode.removeChild(div);
  203. });
  204. }
  205. }
  206. function lxc_configure_handler(self)
  207. {
  208. var div = self.parentNode;
  209. var textarea = div.querySelector('[data-id]');
  210. var lxc_name = textarea.dataset['id'];
  211. var lxc_conf = textarea.value;
  212. new XHR().post('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_configuration_set/' + lxc_name, "lxc_conf=" + encodeURIComponent(lxc_conf),
  213. function(x)
  214. {
  215. if (!x || x.responseText != "0")
  216. {
  217. return info_message(output_list,"Action failed!", 2000);
  218. }
  219. info_message(output_list,"LXC configuration updated", 2000);
  220. var rmdiv = div.parentNode;
  221. rmdiv.parentNode.removeChild(rmdiv);
  222. })
  223. }
  224. function lxc_configure_template(lxc_name, lxc_conf)
  225. {
  226. var h = '\
  227. <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> \
  228. <input data-id="bt_confirm" onclick="lxc_configure_handler(this)" type="button" class="cbi-button" value="Confirm" />';
  229. return h;
  230. }
  231. function action_more_handler(self)
  232. {
  233. var lxc_name = self.parentNode.parentNode.dataset['id'];
  234. var loader = self.parentNode.querySelector('[data-loader]');
  235. var option = self.options[self.selectedIndex].text;
  236. self.value = "more";
  237. switch(option)
  238. {
  239. case "configure":
  240. var div0 = document.createElement('div');
  241. var div1 = self.parentNode.parentNode;
  242. var next_div = div1.nextSibling;
  243. if (next_div && next_div.dataset['action'] !== null)
  244. {
  245. div1.parentNode.removeChild(next_div);
  246. }
  247. new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_configuration_get/' + lxc_name, null,
  248. function(x)
  249. {
  250. div0.innerHTML="<div>" + lxc_configure_template(lxc_name, x.responseText) + "</div>";
  251. div0.setAttribute('data-action','');
  252. div1.parentNode.insertBefore(div0, div1.nextSibling);
  253. })
  254. break;
  255. case "freeze":
  256. var img = self.parentNode.parentNode.querySelector('img');
  257. if(img.getAttribute('src') != window.img["green"])
  258. {
  259. return info_message(output_list,"Container is not running!", 2000);
  260. }
  261. loading(loader);
  262. new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
  263. function(x, ec)
  264. {
  265. loading(loader, 0)
  266. if (!x || ec)
  267. {
  268. return info_message(output_list,"Action failed!", 2000);
  269. }
  270. set_status(img, "purple");
  271. })
  272. break;
  273. case "unfreeze":
  274. var img = self.parentNode.parentNode.querySelector('img');
  275. if(img.getAttribute('src') != window.img["purple"])
  276. {
  277. return info_message(output_list,"Container is not frozen!", 2000);
  278. }
  279. loading(loader);
  280. new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
  281. function(x, ec)
  282. {
  283. loading(loader, 0);
  284. if (!x || ec)
  285. {
  286. return info_message(output_list,"Action failed!", 2000);
  287. }
  288. set_status(img, "green");
  289. })
  290. break;
  291. case "reboot":
  292. var img = self.parentNode.parentNode.querySelector('img');
  293. if(img.getAttribute('src') != window.img["green"])
  294. {
  295. return info_message(output_list,"Container is not running!", 2000);
  296. }
  297. if (!confirm("Are you sure?"))
  298. {
  299. return;
  300. }
  301. loading(loader);
  302. new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null,
  303. function(x, ec)
  304. {
  305. loading(loader, 0)
  306. if (!x || ec)
  307. {
  308. return info_message(output_list,"Action failed!", 2000);
  309. }
  310. info_message(output_list,"LXC container rebooted", 2000);
  311. })
  312. break;
  313. }
  314. }
  315. function set_no_container()
  316. {
  317. info_message(output_list, "There are no containers available yet.");
  318. }
  319. function set_no_template()
  320. {
  321. bt_create.disabled = true;
  322. info_message(output_add, "There are no templates for your architecture (<%=target%>) available, please select another containers URL.");
  323. }
  324. function lxc_list_update()
  325. {
  326. XHR.poll(4, '<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/list', null,
  327. function(x, data)
  328. {
  329. if (!x || !data)
  330. {
  331. return;
  332. }
  333. var lxc_count = Object.keys(data).length;
  334. if (!lxc_count)
  335. {
  336. return set_no_container();
  337. }
  338. var lxcs = t_lxc_list.querySelectorAll('td[data-id]');
  339. var lxc_name_div = {};
  340. for (var i = 0, len = lxcs.length; i < len; i++)
  341. {
  342. var lxc_name = lxcs[i].dataset['id'];
  343. if (!(lxc_name in data))
  344. {
  345. var div = t_lxc_list.querySelector("[data-id='" + lxc_name + "']").parentNode;
  346. div.parentNode.removeChild(div);
  347. continue;
  348. }
  349. lxc_name_div[lxc_name] = lxcs[i].parentNode.querySelector('img');
  350. }
  351. for(var key in data)
  352. {
  353. var lxc_name = key;
  354. var state = window.states[data[key]];
  355. if (!(lxc_name in lxc_name_div))
  356. {
  357. lxc_create_template(lxc_name, state);
  358. }
  359. else if (state != get_status(lxc_name_div[lxc_name]))
  360. {
  361. set_status(lxc_name_div[lxc_name], state);
  362. }
  363. }
  364. })
  365. }
  366. function loading(elem, state)
  367. {
  368. state = (typeof state === 'undefined') ? 1 : state;
  369. if (state === 1)
  370. {
  371. elem.innerHTML = loader_html;
  372. }
  373. else
  374. {
  375. setTimeout(function() { elem.innerHTML = ''}, 2000);
  376. }
  377. }
  378. function set_status(elem, state)
  379. {
  380. state = (typeof state === 'undefined') ? 1 : state;
  381. setTimeout(function() { elem.setAttribute('src', window.img[state])}, 300);
  382. }
  383. function get_status(elem)
  384. {
  385. var src = elem.getAttribute('src');
  386. for (var i in img)
  387. {
  388. if (img[i] == src)
  389. {
  390. return i;
  391. }
  392. }
  393. }
  394. function info_message(output, msg, timeout)
  395. {
  396. timeout = timeout || 0;
  397. output.innerHTML = '<em>' + msg + '</em>';
  398. if (timeout > 0)
  399. {
  400. setTimeout(function(){ output.innerHTML=""}, timeout);
  401. }
  402. }
  403. new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_get_downloadable', null,
  404. function(x, data)
  405. {
  406. if (!x)
  407. {
  408. return;
  409. }
  410. if (!data)
  411. {
  412. return set_no_template();
  413. }
  414. var lxc_count = Object.keys(data).length;
  415. if (!lxc_count)
  416. {
  417. return set_no_template();
  418. }
  419. var select = document.getElementById("s_template");
  420. for(var key in data)
  421. {
  422. var option = document.createElement('option');
  423. option.value = data[key];
  424. option.text = data[key].replace(/[_:]/g, ' ');
  425. select.add(option, -1);
  426. }
  427. info_message(output_add, "");
  428. bt_create.disabled = false;
  429. })
  430. lxc_list_update();
  431. //]]>
  432. </script>