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)
20 local conn = ubus.connect()
22 error("Failed to connect to ubus")
26 function fork_exec(command)
27 local pid = nixio.fork()
34 -- patch stdin, out, err to /dev/null
35 local null = nixio.open("/dev/null", "w+")
37 nixio.dup(null, nixio.stderr)
38 nixio.dup(null, nixio.stdout)
39 nixio.dup(null, nixio.stdin)
40 if null:fileno() > 2 then
45 -- replace with target command
46 nixio.exec("/bin/sh", "-c", command)
51 page = node("admin", "services", "lxc")
52 page.target = cbi("lxc")
53 page.title = _("LXC Containers")
56 page = entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil)
59 page = entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil)
62 page = entry({"admin", "services", "lxc_get_downloadable"}, call("lxc_get_downloadable"), nil)
65 page = entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil)
68 page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil)
73 function lxc_get_downloadable()
74 luci.http.prepare_content("application/json")
76 local f = io.popen('uname -m', 'r')
77 local target = f:read('*a')
79 target = target:gsub("^%s*(.-)%s*$", "%1")
83 local f = io.popen('lxc-create -n just_want_to_list_available_lxc_templates -t download -- --list', 'r')
85 for line in f:lines() do
86 local dist,version = line:match("^(%S+)%s+(%S+)%s+" .. target .. "%s+default%s+%S+$")
87 if dist~=nil and version~=nil then templates[#templates + 1] = dist .. ":" .. version end
91 luci.http.write_json(templates)
94 function lxc_create(lxc_name, lxc_template)
95 luci.http.prepare_content("text/plain")
97 local uci = require("uci").cursor()
99 local url = uci:get("lxc", "lxc", "url")
101 if not pcall(dofile, "/etc/openwrt_release") then
102 return luci.http.write("1")
105 local f = io.popen('uname -m', 'r')
106 local target = f:read('*a')
108 target = target:gsub("^%s*(.-)%s*$", "%1")
110 local lxc_dist = lxc_template:gsub("(.*):(.*)", '%1')
111 local lxc_release = lxc_template:gsub("(.*):(.*)", '%2')
113 local data = conn:call("lxc", "create", { name = lxc_name, template = "download", args = { "--server", url, "--no-validate", "--dist", lxc_dist, "--release", lxc_release, "--arch", target } } )
115 luci.http.write(data)
118 function lxc_action(lxc_action, lxc_name)
119 luci.http.prepare_content("application/json")
121 local data, ec = conn:call("lxc", lxc_action, lxc_name and { name = lxc_name} or {} )
123 luci.http.write_json(ec and {} or data)
126 function lxc_get_config_path()
127 local f = io.open("/etc/lxc/lxc.conf", "r")
128 local content = f:read("*all")
130 local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)')
138 function lxc_configuration_get(lxc_name)
139 luci.http.prepare_content("text/plain")
141 local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r")
142 local content = f:read("*all")
145 luci.http.write(content)
148 function lxc_configuration_set(lxc_name)
149 luci.http.prepare_content("text/plain")
151 local lxc_configuration = luci.http.formvalue("lxc_configuration")
153 if lxc_configuration == nil then
154 return luci.http.write("1")
157 local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+")
159 return luci.http.write("2")
162 f:write(lxc_configuration)