Merge pull request #840 from cjkoenig/update_liburcu
[feed/packages.git] / utils / luci-app-lxc / files / 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 require "ubus"
20 local conn = ubus.connect()
21 if not conn then
22 error("Failed to connect to ubus")
23 end
24
25
26 function fork_exec(command)
27 local pid = nixio.fork()
28 if pid > 0 then
29 return
30 elseif pid == 0 then
31 -- change to root dir
32 nixio.chdir("/")
33
34 -- patch stdin, out, err to /dev/null
35 local null = nixio.open("/dev/null", "w+")
36 if null then
37 nixio.dup(null, nixio.stderr)
38 nixio.dup(null, nixio.stdout)
39 nixio.dup(null, nixio.stdin)
40 if null:fileno() > 2 then
41 null:close()
42 end
43 end
44
45 -- replace with target command
46 nixio.exec("/bin/sh", "-c", command)
47 end
48 end
49
50 function index()
51 page = node("admin", "services", "lxc")
52 page.target = cbi("lxc")
53 page.title = _("LXC Containers")
54 page.order = 70
55
56 page = entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil)
57 page.leaf = true
58
59 page = entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil)
60 page.leaf = true
61
62 page = entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil)
63 page.leaf = true
64
65 page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil)
66 page.leaf = true
67
68 end
69
70 function lxc_create(lxc_name, lxc_template)
71 luci.http.prepare_content("text/plain")
72
73 local uci = require("uci").cursor()
74
75 local url = uci:get("lxc", "lxc", "url")
76
77 if not pcall(dofile, "/etc/openwrt_release") then
78 return luci.http.write("1")
79 end
80
81 local target = _G.DISTRIB_TARGET:match('([^/]+)')
82
83 local data = conn:call("lxc", "create", { name = lxc_name, template = "download", args = { "--server", url, "--no-validate", "--dist", lxc_template, "--release", "bb", "--arch", target } } )
84
85 luci.http.write(data)
86 end
87
88 function lxc_action(lxc_action, lxc_name)
89 luci.http.prepare_content("application/json")
90
91 local data, ec = conn:call("lxc", lxc_action, lxc_name and { name = lxc_name} or {} )
92
93 luci.http.write_json(ec and {} or data)
94 end
95
96 function lxc_configuration_get(lxc_name)
97 luci.http.prepare_content("text/plain")
98
99 local f = io.open("/lxc/" .. lxc_name .. "/config", "r")
100 local content = f:read("*all")
101 f:close()
102
103 luci.http.write(content)
104 end
105
106 function lxc_configuration_set(lxc_name)
107 luci.http.prepare_content("text/plain")
108
109 local lxc_configuration = luci.http.formvalue("lxc_configuration")
110
111 if lxc_configuration == nil then
112 return luci.http.write("1")
113 end
114
115 local f, err = io.open("/lxc/" .. lxc_name .. "/config","w+")
116 if not f then
117 return luci.http.write("2")
118 end
119
120 f:write(lxc_configuration)
121 f:close()
122
123 luci.http.write("0")
124 end
125