luci-app-lxc: fix "plain-vanilla" integration
[project/luci.git] / applications / luci-app-lxc / luasrc / controller / lxc.lua
1 --[[
2
3 LuCI LXC module
4
5 Copyright (C) 2014, Cisco Systems, Inc.
6
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
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 Author: Petar Koretic <petar.koretic@sartura.hr>
14
15 ]]--
16
17 local uci  = require "luci.model.uci".cursor()
18 local util = require "luci.util"
19 local fs   = require "nixio"
20
21 module("luci.controller.lxc", package.seeall)
22
23 function fork_exec(command)
24         local pid = fs.fork()
25         if pid > 0 then
26                 return
27         elseif pid == 0 then
28                 -- change to root dir
29                 fs.chdir("/")
30
31                 -- patch stdin, out, err to /dev/null
32                 local null = fs.open("/dev/null", "w+")
33                 if null then
34                         fs.dup(null, fs.stderr)
35                         fs.dup(null, fs.stdout)
36                         fs.dup(null, fs.stdin)
37                         if null:fileno() > 2 then
38                                 null:close()
39                         end
40                 end
41
42                 -- replace with target command
43                 fs.exec("/bin/sh", "-c", command)
44         end
45 end
46
47 function index()
48         page = node("admin", "services", "lxc")
49         page.target = cbi("lxc")
50         page.title = _("LXC Containers")
51         page.order = 70
52
53         page = entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil)
54         page.leaf = true
55
56         page = entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil)
57         page.leaf = true
58
59         page = entry({"admin", "services", "lxc_get_downloadable"}, call("lxc_get_downloadable"), nil)
60         page.leaf = true
61
62         page = entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil)
63         page.leaf = true
64
65         page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil)
66         page.leaf = true
67
68 end
69
70 function lxc_get_downloadable()
71         local target = lxc_get_arch_target()
72         local templates = {}
73
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')
76         local line
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 }
81                 end
82         end
83
84         f:close()
85
86         luci.http.prepare_content("application/json")
87         luci.http.write_json(templates)
88 end
89
90 function lxc_create(lxc_name, lxc_template)
91         luci.http.prepare_content("text/plain")
92
93         if not pcall(dofile, "/etc/openwrt_release") then
94                 return luci.http.write("1")
95         end
96
97         local lxc_dist, lxc_release = lxc_template:match("^(.+):(.+)$")
98
99         luci.http.write(util.ubus("lxc", "create", {
100                 name = lxc_name,
101                 template = "download",
102                 args = {
103                         "--server", uci:get("lxc", "lxc", "url"),
104                         "--no-validate",
105                         "--dist", lxc_dist,
106                         "--release", lxc_release,
107                         "--arch", lxc_get_arch_target()
108                 }
109         }))
110 end
111
112 function lxc_action(lxc_action, lxc_name)
113         local data, ec = util.ubus("lxc", lxc_action, lxc_name and { name = lxc_name } or {})
114
115         luci.http.prepare_content("application/json")
116         luci.http.write_json(ec and {} or data)
117 end
118
119 function lxc_get_config_path()
120         local f = io.open("/etc/lxc/lxc.conf", "r")
121         local content = f:read("*all")
122         f:close()
123
124         local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)')
125         if ret then
126                 return ret .. "/"
127         else
128                 return "/srv/lxc/"
129         end
130 end
131
132 function lxc_configuration_get(lxc_name)
133         luci.http.prepare_content("text/plain")
134
135         local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r")
136         local content = f:read("*all")
137         f:close()
138
139         luci.http.write(content)
140 end
141
142 function lxc_configuration_set(lxc_name)
143         luci.http.prepare_content("text/plain")
144
145         local lxc_configuration = luci.http.formvalue("lxc_configuration")
146
147         if lxc_configuration == nil then
148                 return luci.http.write("1")
149         end
150
151         local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+")
152         if not f then
153                 return luci.http.write("2")
154         end
155
156         f:write(lxc_configuration)
157         f:close()
158
159         luci.http.write("0")
160 end
161
162 function lxc_get_arch_target()
163         local target = fs.uname().machine
164         local target_map = {
165                 armv5  = "armel",
166                 armv6  = "armel",
167                 armv7  = "armhf",
168                 armv8  = "arm64",
169                 x86_64 = "amd64"
170         }
171
172         local k, v
173         for k, v in pairs(target_map) do
174                 if target:find(k) then
175                         return v
176                 end
177         end
178
179         return target
180 end