luci-app-lxc: various changes
[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"
18 local util = require "luci.util"
19 local nixio = require "nixio"
20
21 module("luci.controller.lxc", package.seeall)
22
23 function fork_exec(command)
24 local pid = nixio.fork()
25 if pid > 0 then
26 return
27 elseif pid == 0 then
28 -- change to root dir
29 nixio.chdir("/")
30
31 -- patch stdin, out, err to /dev/null
32 local null = nixio.open("/dev/null", "w+")
33 if null then
34 nixio.dup(null, nixio.stderr)
35 nixio.dup(null, nixio.stdout)
36 nixio.dup(null, nixio.stdin)
37 if null:fileno() > 2 then
38 null:close()
39 end
40 end
41
42 -- replace with target command
43 nixio.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'
75 % util.shellquote(uci.cursor():get("lxc", "lxc", "url")), 'r')
76
77 local line
78 for line in f:lines() do
79 local dist, version, dist_target = line:match("^(%S+)%s+(%S+)%s+(%S+)%s+default%s+%S+$")
80 if dist and version and dist_target == target then
81 templates[#templates+1] = "%s:%s" %{ dist, version }
82 end
83 end
84
85 f:close()
86
87 luci.http.prepare_content("application/json")
88 luci.http.write_json(templates)
89 end
90
91 function lxc_create(lxc_name, lxc_template)
92 luci.http.prepare_content("text/plain")
93
94 if not pcall(dofile, "/etc/openwrt_release") then
95 return luci.http.write("1")
96 end
97
98 local lxc_dist, lxc_release = lxc_template:match("^(.+):(.+)$")
99
100 luci.http.write(util.ubus("lxc", "create", {
101 name = lxc_name,
102 template = "download",
103 args = {
104 "--server", uci.cursor():get("lxc", "lxc", "url"),
105 "--no-validate",
106 "--dist", lxc_dist,
107 "--release", lxc_release,
108 "--arch", lxc_get_arch_target()
109 }
110 }))
111 end
112
113 function lxc_action(lxc_action, lxc_name)
114 local data, ec = util.ubus("lxc", lxc_action, lxc_name and { name = lxc_name } or {})
115
116 luci.http.prepare_content("application/json")
117 luci.http.write_json(ec and {} or data)
118 end
119
120 function lxc_get_config_path()
121 local f = io.open("/etc/lxc/lxc.conf", "r")
122 local content = f:read("*all")
123 f:close()
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 = nixio.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