luci-app-lxc: Download list of available distributions
authorMichal Hrusecky <michal.hrusecky@nic.cz>
Thu, 16 Jun 2016 14:18:59 +0000 (16:18 +0200)
committerMichal Hrusecky <michal.hrusecky@nic.cz>
Thu, 16 Jun 2016 14:18:59 +0000 (16:18 +0200)
There was harcoded list of available images. Instead of it, LuCI LXC app will
now try to download list of available container images.

Signed-off-by: Michal Hrusecky <michal.hrusecky@nic.cz>
utils/luci-app-lxc/files/controller/lxc.lua
utils/luci-app-lxc/files/view/lxc.htm

index 3d1b23df857efa1ea68fc5fe88683c957603c5e7..3b7635d965a368829de3ae4ce1024daa4e89cb98 100644 (file)
@@ -59,6 +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
 
@@ -67,6 +70,27 @@ 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 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")
 
index db216fc28ef56bea2f3b9adb0298ea60d65df4ff..bc767f3664d57952f197525ad5dac9608447b5e6 100644 (file)
@@ -47,7 +47,6 @@ Author: Petar Koretic <petar.koretic@sartura.hr>
                                </td>
                                <td>
                                        <select id="s_template" class="cbi-input-select cbi-button">
-                                               <option value="openwrt:bb">OpenWrt BB</option>
                                        </select>
                                </td>
                                <td>
@@ -438,4 +437,22 @@ Author: Petar Koretic <petar.koretic@sartura.hr>
        }
 
        lxc_list_update()
+
+       new XHR().get('<%=luci.dispatcher.build_url("admin", "services")%>/lxc_get_downloadable', null,
+       function(x, data)
+       {
+               if (!x) return;
+
+               var lxc_count = Object.keys(data).length
+               if (!data || !lxc_count) return;
+               var select = document.getElementById("s_template");
+               for(var key in data)
+               {
+                       var option = document.createElement('option');
+                       option.value = data[key];
+                       option.text = data[key].replace(/[_:]/, ' ');
+                       select.add(option, -1);
+               }
+       })
+
 //]]></script>