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