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 nx = require "nixio"
22 local url = util.shellquote(uci:get("lxc", "lxc", "url"))
25 if not nixio.fs.access("/etc/config/lxc") then
29 page = node("admin", "services", "lxc")
30 page.target = cbi("lxc")
31 page.title = _("LXC Containers")
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
41 function lxc_get_downloadable()
42 local target = lxc_get_arch_target(url)
43 local ssl_status = lxc_get_ssl_status()
46 local f = io.popen('sh /usr/share/lxc/templates/lxc-download --list %s --server %s 2>/dev/null'
47 %{ ssl_status, url }, 'r')
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 }
57 luci.http.prepare_content("application/json")
58 luci.http.write_json(templates)
61 function lxc_create(lxc_name, lxc_template)
62 luci.http.prepare_content("text/plain")
63 local path = lxc_get_config_path()
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 })
73 while (nx.fs.access(path .. lxc_name .. "/partial")) do
80 function lxc_action(lxc_action, lxc_name)
81 local data, ec = util.ubus("lxc", lxc_action, lxc_name and { name = lxc_name } or {})
83 luci.http.prepare_content("application/json")
84 luci.http.write_json(ec and {} or data)
87 function lxc_get_config_path()
88 local f = io.open("/etc/lxc/lxc.conf", "r")
89 local content = f:read("*all")
92 local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)')
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
103 util.perror("lxc error: not enough temporary space (< " ..min_temp.. " KB)")
106 util.perror("lxc error: not enough space (< " ..min_space.. " KB)")
109 util.perror("lxc error: directory not found")
112 util.perror("lxc error: config path is empty")
116 function lxc_configuration_get(lxc_name)
117 luci.http.prepare_content("text/plain")
119 local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r")
120 local content = f:read("*all")
123 luci.http.write(content)
126 function lxc_configuration_set(lxc_name)
127 luci.http.prepare_content("text/plain")
129 local lxc_configuration = luci.http.formvalue("lxc_conf")
130 if lxc_configuration == nil then
131 util.perror("lxc error: config formvalue is empty")
135 local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+")
137 util.perror("lxc error: config file not found")
141 f:write(lxc_configuration)
147 function lxc_get_arch_target(url)
148 local target = nx.uname().machine
149 if url and url:match("images.linuxcontainers.org") then
158 for k, v in pairs(target_map) do
159 if target:find(k) then
167 function lxc_get_ssl_status()
168 local ssl_enabled = uci:get("lxc", "lxc", "ssl_enabled")
169 local ssl_status = "--no-validate"
171 if ssl_enabled and ssl_enabled == "1" then