luci-app-lxc: add backingstore support
[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 module("luci.controller.lxc", package.seeall)
18
19 local uci = require "luci.model.uci".cursor()
20 local util = require "luci.util"
21 local nx = require "nixio"
22 local url = util.shellquote(uci:get("lxc", "lxc", "url"))
23
24 function index()
25 if not nixio.fs.access("/etc/config/lxc") then
26 return
27 end
28
29 page = node("admin", "services", "lxc")
30 page.target = cbi("lxc")
31 page.title = _("LXC Containers")
32 page.order = 70
33
34 entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil).leaf = true
35 entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil).leaf = true
36 entry({"admin", "services", "lxc_get_downloadable"}, call("lxc_get_downloadable"), nil).leaf = true
37 entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil).leaf = true
38 entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil).leaf = true
39 end
40
41 function lxc_get_downloadable()
42 local target = lxc_get_arch_target(url)
43 local ssl_status = lxc_get_ssl_status()
44 local templates = {}
45
46 local f = io.popen('sh /usr/share/lxc/templates/lxc-download --list %s --server %s 2>/dev/null'
47 %{ ssl_status, url }, 'r')
48 local line
49 for line in f:lines() do
50 local dist, version, dist_target = line:match("^(%S+)%s+(%S+)%s+(%S+)%s+default%s+%S+$")
51 if dist and version and dist_target and dist_target == target then
52 templates[#templates+1] = "%s:%s" %{ dist, version }
53 end
54 end
55 f:close()
56
57 luci.http.prepare_content("application/json")
58 luci.http.write_json(templates)
59 end
60
61 function lxc_create(lxc_name, lxc_template)
62 luci.http.prepare_content("text/plain")
63 local path = lxc_get_config_path()
64 if not path then
65 return
66 end
67
68 local ssl_status = lxc_get_ssl_status()
69 local lxc_dist, lxc_release = lxc_template:match("^(.+):(.+)$")
70 luci.sys.call('/usr/bin/lxc-create --quiet --name %s --bdev best --template download -- --dist %s --release %s --arch %s --server %s %s'
71 %{ lxc_name, lxc_dist, lxc_release, lxc_get_arch_target(url), url, ssl_status })
72
73 while (nx.fs.access(path .. lxc_name .. "/partial")) do
74 nx.nanosleep(1)
75 end
76
77 luci.http.write("0")
78 end
79
80 function lxc_action(lxc_action, lxc_name)
81 local data, ec = util.ubus("lxc", lxc_action, lxc_name and { name = lxc_name } or {})
82
83 luci.http.prepare_content("application/json")
84 luci.http.write_json(ec and {} or data)
85 end
86
87 function lxc_get_config_path()
88 local f = io.open("/etc/lxc/lxc.conf", "r")
89 local content = f:read("*all")
90 f:close()
91
92 local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)')
93 if ret then
94 if nx.fs.access(ret) then
95 local min_space = tonumber(uci:get("lxc", "lxc", "min_space")) or 100000
96 local free_space = tonumber(util.exec("df " ..ret.. " | awk '{if(NR==2)print $4}'"))
97 if free_space and free_space >= min_space then
98 local min_temp = tonumber(uci:get("lxc", "lxc", "min_temp")) or 100000
99 local free_temp = tonumber(util.exec("df /tmp | awk '{if(NR==2)print $4}'"))
100 if free_temp and free_temp >= min_temp then
101 return ret .. "/"
102 else
103 util.perror("lxc error: not enough temporary space (< " ..min_temp.. " KB)")
104 end
105 else
106 util.perror("lxc error: not enough space (< " ..min_space.. " KB)")
107 end
108 else
109 util.perror("lxc error: directory not found")
110 end
111 else
112 util.perror("lxc error: config path is empty")
113 end
114 end
115
116 function lxc_configuration_get(lxc_name)
117 luci.http.prepare_content("text/plain")
118
119 local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r")
120 local content = f:read("*all")
121 f:close()
122
123 luci.http.write(content)
124 end
125
126 function lxc_configuration_set(lxc_name)
127 luci.http.prepare_content("text/plain")
128
129 local lxc_configuration = luci.http.formvalue("lxc_conf")
130 if lxc_configuration == nil then
131 util.perror("lxc error: config formvalue is empty")
132 return
133 end
134
135 local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+")
136 if not f then
137 util.perror("lxc error: config file not found")
138 return
139 end
140
141 f:write(lxc_configuration)
142 f:close()
143
144 luci.http.write("0")
145 end
146
147 function lxc_get_arch_target(url)
148 local target = nx.uname().machine
149 if url and url:match("images.linuxcontainers.org") then
150 local target_map = {
151 armv5 = "armel",
152 armv6 = "armel",
153 armv7 = "armhf",
154 armv8 = "arm64",
155 x86_64 = "amd64"
156 }
157 local k, v
158 for k, v in pairs(target_map) do
159 if target:find(k) then
160 return v
161 end
162 end
163 end
164 return target
165 end
166
167 function lxc_get_ssl_status()
168 local ssl_enabled = uci:get("lxc", "lxc", "ssl_enabled")
169 local ssl_status = "--no-validate"
170
171 if ssl_enabled and ssl_enabled == "1" then
172 ssl_status = ""
173 end
174 return ssl_status
175 end