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_configuration_get"}, call("lxc_configuration_get"), nil)
65 page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil)
70 function lxc_create(lxc_name, lxc_template)
71 luci.http.prepare_content("text/plain")
73 local uci = require("uci").cursor()
75 local url = uci:get("lxc", "lxc", "url")
77 if not pcall(dofile, "/etc/openwrt_release") then
78 return luci.http.write("1")
81 local target = _G.DISTRIB_TARGET:match('([^/]+)')
83 local data = conn:call("lxc", "create", { name = lxc_name, template = "download", args = { "--server", url, "--no-validate", "--dist", lxc_template, "--release", "bb", "--arch", target } } )
88 function lxc_action(lxc_action, lxc_name)
89 luci.http.prepare_content("application/json")
91 local data, ec = conn:call("lxc", lxc_action, lxc_name and { name = lxc_name} or {} )
93 luci.http.write_json(ec and {} or data)
96 function lxc_configuration_get(lxc_name)
97 luci.http.prepare_content("text/plain")
99 local f = io.open("/lxc/" .. lxc_name .. "/config", "r")
100 local content = f:read("*all")
103 luci.http.write(content)
106 function lxc_configuration_set(lxc_name)
107 luci.http.prepare_content("text/plain")
109 local lxc_configuration = luci.http.formvalue("lxc_configuration")
111 if lxc_configuration == nil then
112 return luci.http.write("1")
115 local f, err = io.open("/lxc/" .. lxc_name .. "/config","w+")
117 return luci.http.write("2")
120 f:write(lxc_configuration)