lxc.htm 14 KB

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