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 module("luci.controller.lxc", package.seeall)
19 local uci = require "luci.model.uci".cursor()
20 local util = require "luci.util"
21 local fs = require "nixio"
24 if not nixio.fs.access("/etc/config/lxc") then
28 page = node("admin", "services", "lxc")
29 page.target = cbi("lxc")
30 page.title = _("LXC Containers")
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
40 function lxc_get_downloadable()
41 local target = lxc_get_arch_target()
43 local ssl_status = lxc_get_ssl_status()
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')
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 }
56 luci.http.prepare_content("application/json")
57 luci.http.write_json(templates)
60 function lxc_create(lxc_name, lxc_template)
61 luci.http.prepare_content("text/plain")
63 local check = lxc_get_config_path()
68 local ssl_status = lxc_get_ssl_status()
71 local lxc_dist, lxc_release = lxc_template:match("^(.+):(.+)$")
72 luci.http.write(util.ubus("lxc", "create", {
74 template = "download",
76 "--server", uci:get("lxc", "lxc", "url"),
78 "--release", lxc_release,
79 "--arch", lxc_get_arch_target(),
85 function lxc_action(lxc_action, lxc_name)
86 local data, ec = util.ubus("lxc", lxc_action, lxc_name and { name = lxc_name } or {})
88 luci.http.prepare_content("application/json")
89 luci.http.write_json(ec and {} or data)
92 function lxc_get_config_path()
93 local f = io.open("/etc/lxc/lxc.conf", "r")
94 local content = f:read("*all")
97 local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)')
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
108 util.perror("lxc error: not enough temporary space (< " ..min_temp.. " KB)")
111 util.perror("lxc error: not enough space (< " ..min_space.. " KB)")
114 util.perror("lxc error: directory not found")
117 util.perror("lxc error: config path is empty")
121 function lxc_configuration_get(lxc_name)
122 luci.http.prepare_content("text/plain")
124 local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r")
125 local content = f:read("*all")
128 luci.http.write(content)
131 function lxc_configuration_set(lxc_name)
132 luci.http.prepare_content("text/plain")
134 local lxc_configuration = luci.http.formvalue("lxc_configuration")
135 if lxc_configuration == nil then
136 util.perror("lxc error: config formvalue is empty")
140 local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+")
142 util.perror("lxc error: config file not found")
146 f:write(lxc_configuration)
152 function lxc_get_arch_target()
153 local target = fs.uname().machine
162 for k, v in pairs(target_map) do
163 if target:find("^" ..k.. "$") then
170 function lxc_get_ssl_status()
171 local ssl_enabled = uci:get("lxc", "lxc", "ssl_enabled")
172 local ssl_status = "--no-validate"
174 if ssl_enabled and ssl_enabled == "1" then