Merge pull request #2865 from miska/lxc-luci-features
authorchamptar <champetier.etienne@gmail.com>
Fri, 17 Jun 2016 12:42:23 +0000 (15:42 +0300)
committerGitHub <noreply@github.com>
Fri, 17 Jun 2016 12:42:23 +0000 (15:42 +0300)
Little improvement to LXC LuCI app

1  2 
utils/luci-app-lxc/files/controller/lxc.lua

index de4fa9ff5caf059e7c401fd2d807b61acae8f751,3b7635d965a368829de3ae4ce1024daa4e89cb98..ea7adbafbba8884734855e27d0d42b065dc35830
@@@ -59,6 -59,9 +59,9 @@@ function index(
        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
  
  
  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 templates = {}
+       local f = io.popen('lxc-create -n just_want_to_list_available_lxc_templates -t download -- --list', 'r')
+       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
+       end
+       f:close()
+       luci.http.write_json(templates)
+ end
  function lxc_create(lxc_name, lxc_template)
        luci.http.prepare_content("text/plain")
  
                return luci.http.write("1")
        end
  
-       local target = _G.DISTRIB_TARGET:match('([^/]+)')
+       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_template, "--release", "bb", "--arch", target } } )
+       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)
  end
@@@ -93,22 -123,10 +123,22 @@@ function lxc_action(lxc_action, lxc_nam
        luci.http.write_json(ec and {} or data)
  end
  
 +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 .. "/"
 +      else
 +              return "/srv/lxc/"
 +      end
 +end
 +
  function lxc_configuration_get(lxc_name)
        luci.http.prepare_content("text/plain")
  
 -      local f = io.open("/lxc/" .. lxc_name .. "/config", "r")
 +      local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r")
        local content = f:read("*all")
        f:close()
  
@@@ -124,7 -142,7 +154,7 @@@ function lxc_configuration_set(lxc_name
                return luci.http.write("1")
        end
  
 -      local f, err = io.open("/lxc/" .. lxc_name .. "/config","w+")
 +      local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+")
        if not f then
                return luci.http.write("2")
        end