summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-lxc/luasrc/controller/lxc.lua
diff options
context:
space:
mode:
authorAdmin Localnet <localnet@users.noreply.github.com>2017-10-30 21:12:25 +0100
committerJo-Philipp Wich <jo@mein.io>2018-04-21 14:35:26 +0200
commitde0d16e4ac77be32b7ab3f9816b72aa053eb5854 (patch)
tree4d97c2b734043294c00ab1b2d4f93ebeac633901 /applications/luci-app-lxc/luasrc/controller/lxc.lua
parentefcca756787d53757e1ce8568ddf8ec5dda10af9 (diff)
luci-app-lxc: various changes
1) Modify dependencies Add dependencies required for to be able use containers created from "images.linuxcontainers.org". Several of them require "lxc-attach" for set password so to be able login. None of them has SSH preinstalled so "lxc-console" is required to be able login and install SSH, for example. Remove dependency "xz", it seems incompatible with LXC_BUSYBOX_OPTIONS when both are enabled happens a build crash. 2) Change container image repository The repository "virtualwrt.org/containers/" seems to not work, I have change it by the official LXC container image repository. 3) Translate the arch Translate the local uname architecture to a valid "images.linuxcontainers.org" arch. Only tested with the platform "mvebu" (armv7l -> armhf). 4) Other minor fixes Use same server to list images and download the image. Disable GPG check when listing images. Reported-by: "Admin Localnet <localnet@users.noreply.github.com>" [Squashed commits, cleaned up whitespace, refactor arch mapping, escape url setting, use system wide ubus helper, use uci model library] Closes: #1422 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'applications/luci-app-lxc/luasrc/controller/lxc.lua')
-rw-r--r--applications/luci-app-lxc/luasrc/controller/lxc.lua83
1 files changed, 48 insertions, 35 deletions
diff --git a/applications/luci-app-lxc/luasrc/controller/lxc.lua b/applications/luci-app-lxc/luasrc/controller/lxc.lua
index ea7adbafbb..1122d7e53b 100644
--- a/applications/luci-app-lxc/luasrc/controller/lxc.lua
+++ b/applications/luci-app-lxc/luasrc/controller/lxc.lua
@@ -14,14 +14,11 @@ Author: Petar Koretic <petar.koretic@sartura.hr>
]]--
-module("luci.controller.lxc", package.seeall)
-
-require "ubus"
-local conn = ubus.connect()
-if not conn then
- error("Failed to connect to ubus")
-end
+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()
@@ -71,55 +68,52 @@ function index()
end
function lxc_get_downloadable()
- luci.http.prepare_content("application/json")
-
- local f = io.popen('uname -m', 'r')
- local target = f:read('*a')
- f:close()
- target = target:gsub("^%s*(.-)%s*$", "%1")
-
+ local target = lxc_get_arch_target()
local templates = {}
- local f = io.popen('lxc-create -n just_want_to_list_available_lxc_templates -t download -- --list', 'r')
+ 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 line
for line in f:lines() do
- local dist,version = line:match("^(%S+)%s+(%S+)%s+" .. target .. "%s+default%s+%S+$")
- if dist~=nil and version~=nil then templates[#templates + 1] = dist .. ":" .. version end
+ 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
+ templates[#templates+1] = "%s:%s" %{ dist, version }
+ end
end
f:close()
+
+ luci.http.prepare_content("application/json")
luci.http.write_json(templates)
end
function lxc_create(lxc_name, lxc_template)
luci.http.prepare_content("text/plain")
- local uci = require("uci").cursor()
-
- local url = uci:get("lxc", "lxc", "url")
-
if not pcall(dofile, "/etc/openwrt_release") then
return luci.http.write("1")
end
- local f = io.popen('uname -m', 'r')
- local target = f:read('*a')
- f:close()
- target = target:gsub("^%s*(.-)%s*$", "%1")
-
- local lxc_dist = lxc_template:gsub("(.*):(.*)", '%1')
- local lxc_release = lxc_template:gsub("(.*):(.*)", '%2')
-
- local data = conn:call("lxc", "create", { name = lxc_name, template = "download", args = { "--server", url, "--no-validate", "--dist", lxc_dist, "--release", lxc_release, "--arch", target } } )
-
- luci.http.write(data)
+ 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",
+ "--dist", lxc_dist,
+ "--release", lxc_release,
+ "--arch", lxc_get_arch_target()
+ }
+ }))
end
function lxc_action(lxc_action, lxc_name)
- luci.http.prepare_content("application/json")
-
- local data, ec = conn:call("lxc", lxc_action, lxc_name and { name = lxc_name} or {} )
+ local data, ec = util.ubus("lxc", lxc_action, lxc_name and { name = lxc_name } or {})
+ luci.http.prepare_content("application/json")
luci.http.write_json(ec and {} or data)
end
@@ -165,3 +159,22 @@ function lxc_configuration_set(lxc_name)
luci.http.write("0")
end
+function lxc_get_arch_target()
+ local target = nixio.uname().machine
+ local target_map {
+ armv5 = "armel",
+ armv6 = "armel",
+ armv7 = "armhf",
+ armv8 = "arm64",
+ x86_64 = "amd64"
+ }
+
+ local k, v
+ for k, v in pairs(target_map) do
+ if target:find(k) then
+ return v
+ end
+ end
+
+ return target
+end