5 Copyright (C) 2014, Cisco Systems, Inc.
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
13 Author: Petar Koretic <petar.koretic@sartura.hr>
17 local uci = require "luci.model.uci".cursor()
18 local util = require "luci.util"
19 local fs = require "nixio"
21 module("luci.controller.lxc", package.seeall)
23 function fork_exec(command)
31 -- patch stdin, out, err to /dev/null
32 local null = fs.open("/dev/null", "w+")
34 fs.dup(null, fs.stderr)
35 fs.dup(null, fs.stdout)
36 fs.dup(null, fs.stdin)
37 if null:fileno() > 2 then
42 -- replace with target command
43 fs.exec("/bin/sh", "-c", command)
48 page = node("admin", "services", "lxc")
49 page.target = cbi("lxc")
50 page.title = _("LXC Containers")
53 page = entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil)
56 page = entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil)
59 page = entry({"admin", "services", "lxc_get_downloadable"}, call("lxc_get_downloadable"), nil)
62 page = entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil)
65 page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil)
70 function lxc_get_downloadable()
71 local target = lxc_get_arch_target()
74 local f = io.popen('sh /usr/share/lxc/templates/lxc-download --list --no-validate --server %s 2>/dev/null'
75 % util.shellquote(uci:get("lxc", "lxc", "url")), 'r')
77 for line in f:lines() do
78 local dist, version, dist_target = line:match("^(%S+)%s+(%S+)%s+(%S+)%s+default%s+%S+$")
79 if dist and version and dist_target == target then
80 templates[#templates+1] = "%s:%s" %{ dist, version }
86 luci.http.prepare_content("application/json")
87 luci.http.write_json(templates)
90 function lxc_create(lxc_name, lxc_template)
91 luci.http.prepare_content("text/plain")
93 if not pcall(dofile, "/etc/openwrt_release") then
94 return luci.http.write("1")
97 local lxc_dist, lxc_release = lxc_template:match("^(.+):(.+)$")
99 luci.http.write(util.ubus("lxc", "create", {
101 template = "download",
103 "--server", uci:get("lxc", "lxc", "url"),
106 "--release", lxc_release,
107 "--arch", lxc_get_arch_target()
112 function lxc_action(lxc_action, lxc_name)
113 local data, ec = util.ubus("lxc", lxc_action, lxc_name and { name = lxc_name } or {})
115 luci.http.prepare_content("application/json")
116 luci.http.write_json(ec and {} or data)
119 function lxc_get_config_path()
120 local f = io.open("/etc/lxc/lxc.conf", "r")
121 local content = f:read("*all")
124 local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)')
132 function lxc_configuration_get(lxc_name)
133 luci.http.prepare_content("text/plain")
135 local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r")
136 local content = f:read("*all")
139 luci.http.write(content)
142 function lxc_configuration_set(lxc_name)
143 luci.http.prepare_content("text/plain")
145 local lxc_configuration = luci.http.formvalue("lxc_configuration")
147 if lxc_configuration == nil then
148 return luci.http.write("1")
151 local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+")
153 return luci.http.write("2")
156 f:write(lxc_configuration)
162 function lxc_get_arch_target()
163 local target = fs.uname().machine
173 for k, v in pairs(target_map) do
174 if target:find(k) then