lxc.htm 14 KB

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