X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;ds=sidebyside;f=applications%2Fluci-app-lxc%2Fluasrc%2Fcontroller%2Flxc.lua;h=e15915df5df7cd2a2f579fcdaff1c22c1fbd0244;hb=1b2cd8d1c41daaf3bb4ba50a82835a890f5c06bf;hp=ea7adbafbba8884734855e27d0d42b065dc35830;hpb=d40a939d65397580ceb7209d9c1ba01a396c6220;p=project%2Fluci.git diff --git a/applications/luci-app-lxc/luasrc/controller/lxc.lua b/applications/luci-app-lxc/luasrc/controller/lxc.lua index ea7adbafb..e15915df5 100644 --- a/applications/luci-app-lxc/luasrc/controller/lxc.lua +++ b/applications/luci-app-lxc/luasrc/controller/lxc.lua @@ -14,36 +14,33 @@ Author: Petar Koretic ]]-- -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".cursor() +local util = require "luci.util" +local fs = require "nixio" +module("luci.controller.lxc", package.seeall) function fork_exec(command) - local pid = nixio.fork() + local pid = fs.fork() if pid > 0 then return elseif pid == 0 then -- change to root dir - nixio.chdir("/") + fs.chdir("/") -- patch stdin, out, err to /dev/null - local null = nixio.open("/dev/null", "w+") + local null = fs.open("/dev/null", "w+") if null then - nixio.dup(null, nixio.stderr) - nixio.dup(null, nixio.stdout) - nixio.dup(null, nixio.stdin) + fs.dup(null, fs.stderr) + fs.dup(null, fs.stdout) + fs.dup(null, fs.stdin) if null:fileno() > 2 then null:close() end end -- replace with target command - nixio.exec("/bin/sh", "-c", command) + fs.exec("/bin/sh", "-c", command) end end @@ -71,55 +68,51 @@ 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 2>/dev/null' + % util.shellquote(uci: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: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 @@ -127,6 +120,7 @@ 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 .. "/" @@ -165,3 +159,22 @@ function lxc_configuration_set(lxc_name) luci.http.write("0") end +function lxc_get_arch_target() + local target = fs.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