diff options
Diffstat (limited to 'applications/luci-app-lxc')
-rw-r--r-- | applications/luci-app-lxc/Makefile | 2 | ||||
-rw-r--r-- | applications/luci-app-lxc/luasrc/controller/lxc.lua | 124 | ||||
-rw-r--r-- | applications/luci-app-lxc/luasrc/model/cbi/lxc.lua | 43 | ||||
-rw-r--r-- | applications/luci-app-lxc/luasrc/view/lxc.htm | 527 |
4 files changed, 380 insertions, 316 deletions
diff --git a/applications/luci-app-lxc/Makefile b/applications/luci-app-lxc/Makefile index 85cae5c405..379cccbfc9 100644 --- a/applications/luci-app-lxc/Makefile +++ b/applications/luci-app-lxc/Makefile @@ -7,7 +7,7 @@ include $(TOPDIR)/rules.mk LUCI_TITLE:=LXC management Web UI -LUCI_DEPENDS:=+luci-mod-admin-full +lxc +lxc-attach +lxc-console +lxc-create +liblxc +rpcd-mod-lxc +getopt +LUCI_DEPENDS:=+luci-mod-admin-full +lxc +lxc-attach +lxc-console +lxc-create +liblxc +rpcd-mod-lxc +getopt +!LXC_BUSYBOX_OPTIONS:tar LUCI_PKGARCH:=all PKG_MAINTAINER:=Petar Koretic <petar.koretic@sartura.hr> diff --git a/applications/luci-app-lxc/luasrc/controller/lxc.lua b/applications/luci-app-lxc/luasrc/controller/lxc.lua index cc490f0b81..c7fe63f87b 100644 --- a/applications/luci-app-lxc/luasrc/controller/lxc.lua +++ b/applications/luci-app-lxc/luasrc/controller/lxc.lua @@ -14,74 +14,43 @@ Author: Petar Koretic <petar.koretic@sartura.hr> ]]-- -local uci = require "luci.model.uci" -local util = require "luci.util" -local nixio = require "nixio" - module("luci.controller.lxc", package.seeall) -function fork_exec(command) - local pid = nixio.fork() - if pid > 0 then - return - elseif pid == 0 then - -- change to root dir - nixio.chdir("/") - - -- patch stdin, out, err to /dev/null - local null = nixio.open("/dev/null", "w+") - if null then - nixio.dup(null, nixio.stderr) - nixio.dup(null, nixio.stdout) - nixio.dup(null, nixio.stdin) - if null:fileno() > 2 then - null:close() - end - end +local uci = require "luci.model.uci".cursor() +local util = require "luci.util" +local fs = require "nixio" - -- replace with target command - nixio.exec("/bin/sh", "-c", command) +function index() + if not nixio.fs.access("/etc/config/lxc") then + return end -end -function index() page = node("admin", "services", "lxc") page.target = cbi("lxc") page.title = _("LXC Containers") page.order = 70 - page = entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil) - page.leaf = true - - page = entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil) - page.leaf = true - - page = entry({"admin", "services", "lxc_get_downloadable"}, call("lxc_get_downloadable"), nil) - page.leaf = true - - page = entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil) - page.leaf = true - - page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil) - page.leaf = true - + entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil).leaf = true + entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil).leaf = true + entry({"admin", "services", "lxc_get_downloadable"}, call("lxc_get_downloadable"), nil).leaf = true + entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil).leaf = true + entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil).leaf = true end function lxc_get_downloadable() local target = lxc_get_arch_target() local templates = {} + local ssl_status = lxc_get_ssl_status() - local f = io.popen('sh /usr/share/lxc/templates/lxc-download --list --no-validate --server %s' - % util.shellquote(uci.cursor():get("lxc", "lxc", "url")), 'r') - + local f = io.popen('sh /usr/share/lxc/templates/lxc-download --list %s --server %s 2>/dev/null' + %{ ssl_status, util.shellquote(uci:get("lxc", "lxc", "url")) }, 'r') local line for line in f:lines() do local dist, version, dist_target = line:match("^(%S+)%s+(%S+)%s+(%S+)%s+default%s+%S+$") - if dist and version and dist_target == target then + if dist and version and dist_target and dist_target == target then templates[#templates+1] = "%s:%s" %{ dist, version } end end - f:close() luci.http.prepare_content("application/json") @@ -91,23 +60,26 @@ end function lxc_create(lxc_name, lxc_template) luci.http.prepare_content("text/plain") - if not pcall(dofile, "/etc/openwrt_release") then - return luci.http.write("1") + local check = lxc_get_config_path() + if not check then + return end - local lxc_dist, lxc_release = lxc_template:match("^(.+):(.+)$") + local ssl_status = lxc_get_ssl_status() + local src_err + local lxc_dist, lxc_release = lxc_template:match("^(.+):(.+)$") luci.http.write(util.ubus("lxc", "create", { name = lxc_name, template = "download", args = { - "--server", uci.cursor():get("lxc", "lxc", "url"), - "--no-validate", + "--server", uci:get("lxc", "lxc", "url"), "--dist", lxc_dist, "--release", lxc_release, - "--arch", lxc_get_arch_target() + "--arch", lxc_get_arch_target(), + ssl_status } - })) + }), src_err) end function lxc_action(lxc_action, lxc_name) @@ -121,11 +93,28 @@ function lxc_get_config_path() local f = io.open("/etc/lxc/lxc.conf", "r") local content = f:read("*all") f:close() + local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)') if ret then - return ret .. "/" + if nixio.fs.access(ret) then + local min_space = tonumber(uci:get("lxc", "lxc", "min_space")) or 100000 + local free_space = tonumber(util.exec("df " ..ret.. " | awk '{if(NR==2)print $4}'")) + if free_space and free_space >= min_space then + local min_temp = tonumber(uci:get("lxc", "lxc", "min_temp")) or 100000 + local free_temp = tonumber(util.exec("df /tmp | awk '{if(NR==2)print $4}'")) + if free_temp and free_temp >= min_temp then + return ret .. "/" + else + util.perror("lxc error: not enough temporary space (< " ..min_temp.. " KB)") + end + else + util.perror("lxc error: not enough space (< " ..min_space.. " KB)") + end + else + util.perror("lxc error: directory not found") + end else - return "/srv/lxc/" + util.perror("lxc error: config path is empty") end end @@ -142,15 +131,16 @@ end function lxc_configuration_set(lxc_name) luci.http.prepare_content("text/plain") - local lxc_configuration = luci.http.formvalue("lxc_configuration") - + local lxc_configuration = luci.http.formvalue("lxc_conf") if lxc_configuration == nil then - return luci.http.write("1") + util.perror("lxc error: config formvalue is empty") + return end local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+") if not f then - return luci.http.write("2") + util.perror("lxc error: config file not found") + return end f:write(lxc_configuration) @@ -160,7 +150,7 @@ function lxc_configuration_set(lxc_name) end function lxc_get_arch_target() - local target = nixio.uname().machine + local target = fs.uname().machine local target_map = { armv5 = "armel", armv6 = "armel", @@ -168,13 +158,21 @@ function lxc_get_arch_target() armv8 = "arm64", x86_64 = "amd64" } - local k, v for k, v in pairs(target_map) do - if target:find(k) then + if target:find("^" ..k.. "$") then return v end end - return target end + +function lxc_get_ssl_status() + local ssl_enabled = uci:get("lxc", "lxc", "ssl_enabled") + local ssl_status = "--no-validate" + + if ssl_enabled and ssl_enabled == "1" then + ssl_status = "" + end + return ssl_status +end diff --git a/applications/luci-app-lxc/luasrc/model/cbi/lxc.lua b/applications/luci-app-lxc/luasrc/model/cbi/lxc.lua index ac0fdff332..8a8fc2be0d 100644 --- a/applications/luci-app-lxc/luasrc/model/cbi/lxc.lua +++ b/applications/luci-app-lxc/luasrc/model/cbi/lxc.lua @@ -14,18 +14,35 @@ Author: Petar Koretic <petar.koretic@sartura.hr> ]]-- -local fs = require "nixio.fs" - -m = Map("lxc", translate("LXC Containers")) - -if fs.access("/etc/config/lxc") then - m:section(SimpleSection).template = "lxc" - - s = m:section(TypedSection, "lxc", translate("Options")) - s.anonymous = true - s.addremove = false - - s:option(Value, "url", translate("Containers URL")) -end +m = Map("lxc", translate("LXC Containers"), + translate("<b>Please note:</b> For LXC Containers you need a custom OpenWrt image.<br />") + .. translate("The image should include at least support for 'kernel cgroups', 'kernel namespaces' and 'miscellaneous LXC related options'.")) +m:section(SimpleSection).template = "lxc" + +s = m:section(TypedSection, "lxc", translate("Options")) +s.anonymous = true + +o1 = s:option(Value, "url", translate("Containers URL")) +o1:value("images.linuxcontainers.org") +o1:value("repo.turris.cz/lxc", "repo.turris.cz/lxc (SSL req.)") +o1.default = "images.linuxcontainers.org" +o1.rmempty = false + +o2 = s:option(Flag, "ssl_enabled", translate("Enable SSL"), + translate("Enable optional SSL encryption support. This requires additional packages like 'wget', 'ca-certificates', 'gnupg' and 'gnupg-utils'.")) +o2.default = o2.disabled +o2.rmempty = false + +o3 = s:option(Value, "min_space", translate("Free Space Threshold"), + translate("Minimum required free space for LXC Container creation in KB")) +o3.default = "100000" +o3.datatype = "min(50000)" +o3.rmempty = false + +o4 = s:option(Value, "min_temp", translate("Free Temp Threshold"), + translate("Minimum required free temp space for LXC Container creation in KB")) +o4.default = "100000" +o4.datatype = "min(50000)" +o4.rmempty = false return m diff --git a/applications/luci-app-lxc/luasrc/view/lxc.htm b/applications/luci-app-lxc/luasrc/view/lxc.htm index edfff8e063..e703d6c882 100644 --- a/applications/luci-app-lxc/luasrc/view/lxc.htm +++ b/applications/luci-app-lxc/luasrc/view/lxc.htm @@ -14,16 +14,21 @@ Author: Petar Koretic <petar.koretic@sartura.hr> -%> +<%- +local fs = require "nixio" +local target = fs.uname().machine +-%> + <fieldset class="cbi-section"> <legend><%:Available Containers%></legend> <div class="cbi-section-node"> - <table id="t_lxc_list" class="cbi-section-table"> - <tr class="cbi-section-table-titles"> - <th class="cbi-section-table-cell"><%:Name%></th> - <th class="cbi-section-table-cell"><%:Status%></th> - <th class="cbi-section-table-cell"><%:Actions%></th> - </tr> - </table> + <div class="table cbi-section-table" id="t_lxc_list"> + <div class="tr cbi-section-table-titles"> + <div class="th cbi-section-table-cell"><%:Name%></div> + <div class="th cbi-section-table-cell"><%:Status%></div> + <div class="th cbi-section-table-cell"><%:Actions%></div> + </div> + </div> </div> </fieldset> @@ -31,30 +36,25 @@ Author: Petar Koretic <petar.koretic@sartura.hr> <span id="lxc-list-output"></span> </fieldset> -<hr/> +<hr /> <fieldset class="cbi-section"> <legend><%:Create New Container%></legend> <div class="cbi-section-node"> - <table id="t_lxc_create" class="cbi-section-table"> - <tr class="cbi-section-table-titles"> - <th class="cbi-section-table-cell"><%:Name%></th> - <th class="cbi-section-table-cell"><%:Template%></th> - <th class="cbi-section-table-cell"><%:Actions%></th> - </tr> - <tr id="tr_holder"> - <td> - <input type="text" id="tx_name" placeholder="<%:Enter new name%>" value='' /> - </td> - <td> - <select id="s_template" class="cbi-input-select cbi-button"> - </select> - </td> - <td> - <input type="button" id="bt_create" value="<%:Create%>" onclick="lxc_create(tr_holder)" class="cbi-button cbi-button-add" /> + <div class="table cbi-section-table" id="t_lxc_create"> + <div class="tr cbi-section-table-titles"> + <div class="th cbi-section-table-cell"><%:Name%></div> + <div class="th cbi-section-table-cell"><%:Template%></div> + <div class="th cbi-section-table-cell"><%:Actions%></div> + </div> + <div class="tr cbi-section-table-row" id="div_create"> + <div class="td"><input class="cbi-input-text" type="text" id="tx_name" placeholder="<%:Enter new name%>" value='' /></div> + <div class="td"><select id="s_template" class="cbi-input-select cbi-button"></select></div> + <div class="td"> + <input type="button" id="bt_create" value="<%:Create%>" onclick="lxc_create()" class="cbi-button cbi-button-add" /> <span id="lxc-add-loader" style="display:inline-block; width:16px; height:16px; margin:0 5px"></span> - </td> - </tr> - </table> + </div> + </div> + </div> </div> </fieldset> @@ -62,298 +62,324 @@ Author: Petar Koretic <petar.koretic@sartura.hr> <span id="lxc-add-output"></span> </fieldset> -<hr/> +<hr /> <script type="text/javascript" src="<%=resource%>/cbi.js"></script> -<script type="text/javascript">//<![CDATA[ - - window.img = { "red" : "<%=resource%>/cbi/red.gif", "green" : "<%=resource%>/cbi/green.gif", "purple" : "<%=resource%>/cbi/purple.gif" } - window.states = { "STOPPED" : "red", "RUNNING" : "green", "FROZEN" : "purple"} - - var t_lxc_list = document.getElementById('t_lxc_list'); - var loader_html = '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" width="16" height="16" style="vertical-align:middle" /> '; - var timeout_msg = 0 - var output_list = document.getElementById("lxc-list-output") - var output_add = document.getElementById("lxc-add-output") - var loader_add = document.getElementById("lxc-add-loader") - - function lxc_create(tr) +<script type="text/javascript"> +//<![CDATA[ + window.img = { "red" : "<%=resource%>/cbi/red.gif", "green" : "<%=resource%>/cbi/green.gif", "purple" : "<%=resource%>/cbi/purple.gif" }; + window.states = { "STOPPED" : "red", "RUNNING" : "green", "FROZEN" : "purple" }; + + var t_lxc_list = document.getElementById('t_lxc_list'); + var loader_html = '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" width="16" height="16" style="vertical-align:middle" />'; + var output_list = document.getElementById("lxc-list-output"); + var output_add = document.getElementById("lxc-add-output"); + var loader_add = document.getElementById("lxc-add-loader"); + var div_create = document.getElementById("div_create"); + var bt_create = div_create.querySelector("#bt_create"); + + bt_create.disabled = true; + info_message(output_add, "Template download in progress, please be patient!"); + + function lxc_create() { - var lxc_name = tr.querySelector("#tx_name").value.trim() - var lxc_template = tr.querySelector("#s_template").value - var bt_create = tr.querySelector("#bt_create") + var lxc_name = div_create.querySelector("#tx_name").value.replace(/[\s!@#$%^&*()+=\[\]{};':"\\|,<>\/?]/g,''); + var lxc_template = div_create.querySelector("#s_template").value; if (t_lxc_list.querySelector("[data-id='" + lxc_name + "']") != null) - return info_message(output_add, "Container with that name already exists!", 4000) + { + return info_message(output_add, "Container with that name already exists!", 2000); + } - bt_create.disabled = true - output_add.innerHTML = '' + bt_create.disabled = true; + output_add.innerHTML = ''; + + if (!lxc_template) + { + return set_no_template(); + } if (!lxc_name || !lxc_name.length) { - bt_create.disabled = false - return info_message(output_add, "Name cannot be empty!", 4000) + bt_create.disabled = false; + return info_message(output_add, "The 'Name' field must not be empty!", 2000); } - loading(loader_add) + loading(loader_add); new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_create/' + '%h/%h'.format(lxc_name, lxc_template) , null, function(x) { - bt_create.disabled = false - loading(loader_add, 0) + bt_create.disabled = false; + loading(loader_add, 0); if (!x) - info_message(output_add, "Container creation failed!") + { + info_message(output_add, "Container creation failed!", 2000); + } }) } function lxc_create_template(lxc_name, lxc_state) { - var info_row = t_lxc_list.querySelector("#empty") - if (info_row) - t_lxc_list.deleteRow(1) - - var actions = '' - actions += '<input type="button" onclick="action_handler(this)" data-action="start" value="<%:Start%>" class="cbi-button cbi-button-apply" />' - actions+= '<input type="button" onclick="action_handler(this)" data-action="stop" value="<%:Stop%>" class="cbi-button cbi-button-reset" />' - actions+= '<input type="button" onclick="action_handler(this)" data-action="destroy" value="<%:Delete%>" class="cbi-button cbi-button-remove" />' - actions+= ' <select class="cbi-input-select cbi-button" onchange="action_more_handler(this)">\ - <option selected disabled>more</option>\ + if (document.getElementById(lxc_name)) + { + return; + } + + info_message(output_list, ""); + var actions = ''; + actions += '<input type="button" onclick="action_handler(this)" data-action="start" value="<%:Start%>" class="cbi-button cbi-button-apply" />'; + actions += ' <input type="button" onclick="action_handler(this)" data-action="stop" value="<%:Stop%>" class="cbi-button cbi-button-reset" />'; + actions += ' <input type="button" onclick="action_handler(this)" data-action="destroy" value="<%:Delete%>" class="cbi-button cbi-button-remove" />'; + actions += ' <select class="cbi-input-select cbi-button" style="width:10em" onchange="action_more_handler(this)">\ + <option selected="selected" disabled="disabled">more</option>\ <option>configure</option>\ <option>freeze</option>\ <option>unfreeze</option>\ <option>reboot</option>\ - </select>' - actions+= '<span data-loader style="display:inline-block; width:16px; height:16px; margin:0 5px"></span>' - - var row = t_lxc_list.insertRow(-1) - var cell = row.insertCell(-1) - cell.innerHTML = '%q%h%q'.format("<strong>", lxc_name, "</strong>") - cell.width = "30%" - cell.setAttribute("data-id", lxc_name) - - cell = row.insertCell(-1) - cell.width = "20%" - cell.innerHTML = "<img src='"+window.img[lxc_state]+"'/>" - - cell = row.insertCell(-1) - cell.width = "50%" - cell.innerHTML = actions + </select>'; + actions += '<span data-loader="" style="display:inline-block; width:16px; height:16px; margin:0 5px"></span>'; + + var div0 = document.createElement("div"); + div0.className = "tr cbi-section-table-row"; + div0.id = lxc_name; + div0.setAttribute("data-id", lxc_name); + + var div1 = document.createElement("div"); + div1.className = "td"; + div1.style.width = "30%"; + div1.innerHTML = '%q%h%q'.format("<strong>", lxc_name, "</strong>"); + + var div2 = document.createElement("div"); + div2.className = "td"; + div2.style.width = "20%"; + div2.innerHTML = "<img src='"+window.img[lxc_state]+"'/>"; + + var div3 = document.createElement("div"); + div3.className = "td"; + div3.style.width = "50%"; + div3.innerHTML = actions + + document.getElementById("t_lxc_list").appendChild(div0); + div0.appendChild(div1); + div0.appendChild(div2); + div0.appendChild(div3); } function action_handler(self) { - var action = self.getAttribute("data-action"); + var bt_action = self; + var action = self.dataset['action']; + var lxc_name = self.parentNode.parentNode.dataset['id']; + var status_img = self.parentNode.parentNode.querySelector('img'); + var loader = self.parentNode.querySelector('[data-loader]'); - var bt_action = self - var lxc_name = self.parentNode.parentNode.children[0].getAttribute('data-id') - var status_img = self.parentNode.parentNode.querySelector('img') - var loader = self.parentNode.querySelector('[data-loader]') - - bt_action.disabled = true + bt_action.disabled = true; if (action == "stop") { - loading(loader) + loading(loader); new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null, function(x, ec) { - loading(loader, 0) - bt_action.disabled = false + loading(loader, 0); + bt_action.disabled = false; if (!x || ec) - return info_message(output_list,"Action failed!") - - set_status(status_img, "red") - + { + return info_message(output_list,"Action failed!", 2000); + } + set_status(status_img, "red"); }); } - else if (action == "start") { - loading(loader) + loading(loader); new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null, function(x, data) { - loading(loader, 0) - bt_action.disabled = false - - //FIXME: uncomment after fixing 'lxc-start' - if (!x /*|| ec */) - return info_message(output_list,"Action failed!") + loading(loader, 0); + bt_action.disabled = false; - //FIXME: uncomment after fixing 'lxc-start' - //set_status(status_img, "green") + if (!x || data) + { + return info_message(output_list,"Action failed!", 2000); + } + set_status(status_img, "green"); }); } - else if (action == "destroy") { - if (!confirm("This will completely remove LXC container from the disk. Are you sure? (container will be stopped if running)")) - return + var div = self.parentNode.parentNode + var img = div.querySelector('img') + + if (img.getAttribute('src') != window.img["red"]) + { + bt_action.disabled = false; + return info_message(output_list,"Container is still running!", 2000); + } - loading(loader) + if (!confirm("This will completely remove a stopped LXC container from disk. Are you sure?")) + { + bt_action.disabled = false; + return; + } + loading(loader); new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(action, lxc_name), null, function(x, ec) { - loading(loader, 0) - bt_action.disabled = false + loading(loader, 0); + bt_action.disabled = false; if (!x || ec) - return info_message(output_list,"Action failed!") - - var row = self.parentNode.parentNode - row.parentNode.removeChild(row) - + { + return info_message(output_list,"Action failed!", 2000); + } + var div = self.parentNode.parentNode; + div.parentNode.removeChild(div); }); } } function lxc_configure_handler(self) { - var td = self.parentNode - var textarea = td.querySelector('[data-id]') - var lxc_name = textarea.getAttribute('data-id') - var lxc_configuration = textarea.value + var div = self.parentNode; + var textarea = div.querySelector('[data-id]'); + var lxc_name = textarea.dataset['id']; + var lxc_conf = textarea.value; - new XHR().post('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_configuration_set/' + lxc_name, "lxc_configuration=" + encodeURIComponent(lxc_configuration) , + new XHR().post('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_configuration_set/' + lxc_name, "lxc_conf=" + encodeURIComponent(lxc_conf), function(x) { if (!x || x.responseText != "0") - return info_message(output_list,"Action failed!") - - info_message(output_list,"LXC configuration updated") - var row = td.parentNode - row.parentNode.removeChild(row) + { + return info_message(output_list,"Action failed!", 2000); + } + info_message(output_list,"LXC configuration updated", 2000); + var rmdiv = div.parentNode; + rmdiv.parentNode.removeChild(rmdiv); }) } - function lxc_rename_template(lxc_name) - { - var h = '\ - <input data-id="'+ lxc_name + '" type="text" placeholder="Enter new name" /> \ - <input data-id="bt_confirm" onclick="lxc_rename_handler(this)" type="button" class="cbi-button" value="Confirm" />' - - return h - } - - function lxc_configure_template(lxc_name, lxc_configuration) + function lxc_configure_template(lxc_name, lxc_conf) { var h = '\ - <textarea data-id="'+ lxc_name + '" rows="20" style="width:100%">'+ lxc_configuration +'</textarea> \ - <input data-id="bt_confirm" onclick="lxc_configure_handler(this)" type="button" class="cbi-button" value="Confirm" />' - - return h + <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> \ + <input data-id="bt_confirm" onclick="lxc_configure_handler(this)" type="button" class="cbi-button" value="Confirm" />'; + return h; } function action_more_handler(self) { - var lxc_name = self.parentNode.parentNode.querySelector('[data-id]').getAttribute('data-id') - var loader = self.parentNode.parentNode.querySelector('[data-loader]') + var lxc_name = self.parentNode.parentNode.dataset['id']; + var loader = self.parentNode.querySelector('[data-loader]'); + var option = self.options[self.selectedIndex].text; + self.value = "more"; - var option = self.options[self.selectedIndex].text - - self.value = "more" - - switch (option) + switch(option) { case "configure": - var tr = document.createElement('tr') - var row = self.parentNode.parentNode - var next_row = row.nextSibling - if (next_row && next_row.getAttribute('data-action') !== null) - row.parentNode.removeChild(next_row) + var div0 = document.createElement('div'); + var div1 = self.parentNode.parentNode; + var next_div = div1.nextSibling; + + if (next_div && next_div.dataset['action'] !== null) + { + div1.parentNode.removeChild(next_div); + } new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_configuration_get/' + lxc_name, null, function(x) { - tr.innerHTML="<td colspan='" + row.cells.length + "'>" + lxc_configure_template(lxc_name, x.responseText) + "</td>" - tr.setAttribute('data-action','') - row.parentNode.insertBefore(tr, row.nextSibling) + div0.innerHTML="<div>" + lxc_configure_template(lxc_name, x.responseText) + "</div>"; + div0.setAttribute('data-action',''); + div1.parentNode.insertBefore(div0, div1.nextSibling); }) - - break + break; case "freeze": - var tr = self.parentNode.parentNode - var img = tr.querySelector('img') + var img = self.parentNode.parentNode.querySelector('img'); if(img.getAttribute('src') != window.img["green"]) - return info_message(output_list,"Container is not running!") + { + return info_message(output_list,"Container is not running!", 2000); + } + + loading(loader); - loading(loader) new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null, function(x, ec) { loading(loader, 0) if (!x || ec) - return info_message(output_list,"Action failed!") - - set_status(img, "purple") + { + return info_message(output_list,"Action failed!", 2000); + } + set_status(img, "purple"); }) - - break + break; case "unfreeze": - var tr = self.parentNode.parentNode - var img = tr.querySelector('img') - + var img = self.parentNode.parentNode.querySelector('img'); if(img.getAttribute('src') != window.img["purple"]) - return info_message(output_list,"Container is not frozen!") + { + return info_message(output_list,"Container is not frozen!", 2000); + } + + loading(loader); - loading(loader) new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null, function(x, ec) { - loading(loader, 0) + loading(loader, 0); if (!x || ec) - return info_message(output_list,"Action failed!") - - set_status(img, "green") + { + return info_message(output_list,"Action failed!", 2000); + } + set_status(img, "green"); }) - - break + break; case "reboot": - var tr = self.parentNode.parentNode - var img = tr.querySelector('img') + var img = self.parentNode.parentNode.querySelector('img'); if(img.getAttribute('src') != window.img["green"]) - return info_message(output_list,"Container is not running!") + { + return info_message(output_list,"Container is not running!", 2000); + } if (!confirm("Are you sure?")) - return + { + return; + } + + loading(loader); - loading(loader) new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/' + '%h/%h'.format(option, lxc_name), null, function(x, ec) { loading(loader, 0) if (!x || ec) - return info_message(output_list,"Action failed!") - - info_message(output_list,"LXC rebooted") + { + return info_message(output_list,"Action failed!", 2000); + } + info_message(output_list,"LXC container rebooted", 2000); }) - break + break; } - } - function set_empty(t_lxc_list) + function set_no_container() { - if (document.getElementById('empty') !== null) - return - - var row_count = t_lxc_list.rows.length; - while(--row_count) t_lxc_list.deleteRow(row_count); + info_message(output_list, "There are no containers available yet."); + } - var row = t_lxc_list.insertRow(-1); - row.id = 'empty' - var cell = row.insertCell(0); - cell.colSpan = 4; - cell.innerHTML = '<em><br />There are no containers available yet.</em>'; + function set_no_template() + { + bt_create.disabled = true; + info_message(output_add, "There are no templates for your architecture (<%=target%>) available, please select another containers URL."); } function lxc_list_update() @@ -361,90 +387,108 @@ Author: Petar Koretic <petar.koretic@sartura.hr> XHR.poll(4, '<%=luci.dispatcher.build_url("admin", "services")%>/lxc_action/list', null, function(x, data) { - if (!x) return; - - var lxc_count = Object.keys(data).length - if (!data || !lxc_count) - return set_empty(t_lxc_list) + if (!x || !data) + { + return; + } - if (document.getElementById('empty') !== null) - t_lxc_list.deleteRow(1); + var lxc_count = Object.keys(data).length; + if (!lxc_count) + { + return set_no_container(); + } - var lxcs = t_lxc_list.querySelectorAll('td[data-id]') - var lxc_name_table = {} + var lxcs = t_lxc_list.querySelectorAll('td[data-id]'); + var lxc_name_div = {}; for (var i = 0, len = lxcs.length; i < len; i++) { - var lxc_name = lxcs[i].getAttribute('data-id') + var lxc_name = lxcs[i].dataset['id']; if (!(lxc_name in data)) { - var row = t_lxc_list.querySelector("[data-id='" + lxc_name + "']").parentNode - row.parentNode.removeChild(row) - continue + var div = t_lxc_list.querySelector("[data-id='" + lxc_name + "']").parentNode; + div.parentNode.removeChild(div); + continue; } - - lxc_name_table[lxc_name] = lxcs[i].parentNode.querySelector('img') + lxc_name_div[lxc_name] = lxcs[i].parentNode.querySelector('img'); } for(var key in data) { - var lxc_name = key - var state = window.states[data[key]] - - if (!(lxc_name in lxc_name_table)) - lxc_create_template(lxc_name, state) + var lxc_name = key; + var state = window.states[data[key]]; - else if (state != get_status(lxc_name_table[lxc_name])) - set_status(lxc_name_table[lxc_name], state) + if (!(lxc_name in lxc_name_div)) + { + lxc_create_template(lxc_name, state); + } + else if (state != get_status(lxc_name_div[lxc_name])) + { + set_status(lxc_name_div[lxc_name], state); + } } - }) } function loading(elem, state) { - state = (typeof state === 'undefined') ? 1 : state - + state = (typeof state === 'undefined') ? 1 : state; if (state === 1) - elem.innerHTML = loader_html + { + elem.innerHTML = loader_html; + } else - setTimeout(function() { elem.innerHTML = ''}, 1000) + { + setTimeout(function() { elem.innerHTML = ''}, 2000); + } } function set_status(elem, state) { - state = (typeof state === 'undefined') ? 1 : state - - setTimeout(function() { elem.setAttribute('src', window.img[state])}, 300) + state = (typeof state === 'undefined') ? 1 : state; + setTimeout(function() { elem.setAttribute('src', window.img[state])}, 300); } function get_status(elem) { - var src = elem.getAttribute('src') - + var src = elem.getAttribute('src'); for (var i in img) { if (img[i] == src) - return i + { + return i; + } } } function info_message(output, msg, timeout) { - timeout = timeout || 3000 - output.innerHTML = msg - clearTimeout(timeout_msg) - timeout_msg = setTimeout(function(){ output.innerHTML=""}, timeout); + timeout = timeout || 0; + output.innerHTML = '<em>' + msg + '</em>'; + if (timeout > 0) + { + setTimeout(function(){ output.innerHTML=""}, timeout); + } } - lxc_list_update() - new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_get_downloadable', null, function(x, data) { - if (!x) return; + if (!x) + { + return; + } + + if (!data) + { + return set_no_template(); + } + + var lxc_count = Object.keys(data).length; + if (!lxc_count) + { + return set_no_template(); + } - var lxc_count = Object.keys(data).length - if (!data || !lxc_count) return; var select = document.getElementById("s_template"); for(var key in data) { @@ -453,6 +497,11 @@ Author: Petar Koretic <petar.koretic@sartura.hr> option.text = data[key].replace(/[_:]/g, ' '); select.add(option, -1); } + + info_message(output_add, ""); + bt_create.disabled = false; }) -//]]></script> + lxc_list_update(); +//]]> +</script> |