From: Hannu Nyman Date: Tue, 21 Feb 2017 09:00:16 +0000 (+0200) Subject: Merge pull request #1027 from danrl/lxc X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fluci.git;a=commitdiff_plain;h=3df5fd2697199f1cd29f0e4c7e5701d0b39d3a21;hp=80a1946a2e10393e38a716a04a7fdf11fc80f6dc Merge pull request #1027 from danrl/lxc luci-app-lxc: initial commit --- diff --git a/applications/luci-app-lxc/Makefile b/applications/luci-app-lxc/Makefile new file mode 100644 index 000000000..9f313dfb1 --- /dev/null +++ b/applications/luci-app-lxc/Makefile @@ -0,0 +1,17 @@ +# +# Copyright (C) 2017 Dan Luedtke +# +# This is free software, licensed under the Apache License, Version 2.0 . +# + +include $(TOPDIR)/rules.mk + +LUCI_TITLE:=LXC management Web UI +LUCI_DEPENDS:=+luci-mod-admin-full +lxc +lxc-create +liblxc +rpcd-mod-lxc +getopt +xz +LUCI_PKGARCH:=all + +PKG_MAINTAINER:=Petar Koretic + +include ../../luci.mk + +# call BuildPackage - OpenWrt buildroot signature diff --git a/applications/luci-app-lxc/htdocs/luci-static/resources/cbi/green.gif b/applications/luci-app-lxc/htdocs/luci-static/resources/cbi/green.gif new file mode 100644 index 000000000..d09febf12 Binary files /dev/null and b/applications/luci-app-lxc/htdocs/luci-static/resources/cbi/green.gif differ diff --git a/applications/luci-app-lxc/htdocs/luci-static/resources/cbi/purple.gif b/applications/luci-app-lxc/htdocs/luci-static/resources/cbi/purple.gif new file mode 100644 index 000000000..f0d68cc8b Binary files /dev/null and b/applications/luci-app-lxc/htdocs/luci-static/resources/cbi/purple.gif differ diff --git a/applications/luci-app-lxc/htdocs/luci-static/resources/cbi/red.gif b/applications/luci-app-lxc/htdocs/luci-static/resources/cbi/red.gif new file mode 100644 index 000000000..c1b39bbed Binary files /dev/null and b/applications/luci-app-lxc/htdocs/luci-static/resources/cbi/red.gif differ diff --git a/applications/luci-app-lxc/luasrc/controller/lxc.lua b/applications/luci-app-lxc/luasrc/controller/lxc.lua new file mode 100644 index 000000000..ea7adbafb --- /dev/null +++ b/applications/luci-app-lxc/luasrc/controller/lxc.lua @@ -0,0 +1,167 @@ +--[[ + +LuCI LXC module + +Copyright (C) 2014, Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Author: Petar Koretic + +]]-- + +module("luci.controller.lxc", package.seeall) + +require "ubus" +local conn = ubus.connect() +if not conn then + error("Failed to connect to ubus") +end + + +function fork_exec(command) + local pid = nixio.fork() + if pid > 0 then + return + elseif pid == 0 then + -- change to root dir + nixio.chdir("/") + + -- patch stdin, out, err to /dev/null + local null = nixio.open("/dev/null", "w+") + if null then + nixio.dup(null, nixio.stderr) + nixio.dup(null, nixio.stdout) + nixio.dup(null, nixio.stdin) + if null:fileno() > 2 then + null:close() + end + end + + -- replace with target command + nixio.exec("/bin/sh", "-c", command) + end +end + +function index() + page = node("admin", "services", "lxc") + page.target = cbi("lxc") + page.title = _("LXC Containers") + page.order = 70 + + page = entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_get_downloadable"}, call("lxc_get_downloadable"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil) + page.leaf = true + +end + +function lxc_get_downloadable() + luci.http.prepare_content("application/json") + + local f = io.popen('uname -m', 'r') + local target = f:read('*a') + f:close() + target = target:gsub("^%s*(.-)%s*$", "%1") + + local templates = {} + + local f = io.popen('lxc-create -n just_want_to_list_available_lxc_templates -t download -- --list', 'r') + + for line in f:lines() do + local dist,version = line:match("^(%S+)%s+(%S+)%s+" .. target .. "%s+default%s+%S+$") + if dist~=nil and version~=nil then templates[#templates + 1] = dist .. ":" .. version end + end + + f:close() + luci.http.write_json(templates) +end + +function lxc_create(lxc_name, lxc_template) + luci.http.prepare_content("text/plain") + + local uci = require("uci").cursor() + + local url = uci:get("lxc", "lxc", "url") + + if not pcall(dofile, "/etc/openwrt_release") then + return luci.http.write("1") + end + + local f = io.popen('uname -m', 'r') + local target = f:read('*a') + f:close() + target = target:gsub("^%s*(.-)%s*$", "%1") + + local lxc_dist = lxc_template:gsub("(.*):(.*)", '%1') + local lxc_release = lxc_template:gsub("(.*):(.*)", '%2') + + local data = conn:call("lxc", "create", { name = lxc_name, template = "download", args = { "--server", url, "--no-validate", "--dist", lxc_dist, "--release", lxc_release, "--arch", target } } ) + + luci.http.write(data) +end + +function lxc_action(lxc_action, lxc_name) + luci.http.prepare_content("application/json") + + local data, ec = conn:call("lxc", lxc_action, lxc_name and { name = lxc_name} or {} ) + + luci.http.write_json(ec and {} or data) +end + +function lxc_get_config_path() + local f = io.open("/etc/lxc/lxc.conf", "r") + local content = f:read("*all") + f:close() + local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)') + if ret then + return ret .. "/" + else + return "/srv/lxc/" + end +end + +function lxc_configuration_get(lxc_name) + luci.http.prepare_content("text/plain") + + local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r") + local content = f:read("*all") + f:close() + + luci.http.write(content) +end + +function lxc_configuration_set(lxc_name) + luci.http.prepare_content("text/plain") + + local lxc_configuration = luci.http.formvalue("lxc_configuration") + + if lxc_configuration == nil then + return luci.http.write("1") + end + + local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+") + if not f then + return luci.http.write("2") + end + + f:write(lxc_configuration) + f:close() + + luci.http.write("0") +end + diff --git a/applications/luci-app-lxc/luasrc/model/cbi/lxc.lua b/applications/luci-app-lxc/luasrc/model/cbi/lxc.lua new file mode 100644 index 000000000..ac0fdff33 --- /dev/null +++ b/applications/luci-app-lxc/luasrc/model/cbi/lxc.lua @@ -0,0 +1,31 @@ +--[[ + +LuCI LXC module + +Copyright (C) 2014, Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Author: Petar Koretic + +]]-- + +local fs = require "nixio.fs" + +m = Map("lxc", translate("LXC Containers")) + +if fs.access("/etc/config/lxc") then + m:section(SimpleSection).template = "lxc" + + s = m:section(TypedSection, "lxc", translate("Options")) + s.anonymous = true + s.addremove = false + + s:option(Value, "url", translate("Containers URL")) +end + +return m diff --git a/applications/luci-app-lxc/luasrc/view/lxc.htm b/applications/luci-app-lxc/luasrc/view/lxc.htm new file mode 100644 index 000000000..edfff8e06 --- /dev/null +++ b/applications/luci-app-lxc/luasrc/view/lxc.htm @@ -0,0 +1,458 @@ +<%# + +LuCI LXC module + +Copyright (C) 2014, Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Author: Petar Koretic + +-%> + +
+ <%:Available Containers%> +
+ + + + + + +
<%:Name%><%:Status%><%:Actions%>
+
+
+ +
+ +
+ +
+
+ <%:Create New Container%> +
+ + + + + + + + + + + +
<%:Name%><%:Template%><%:Actions%>
+ + + + + + +
+
+
+ +
+ +
+ +
+ + + diff --git a/applications/luci-app-lxc/root/etc/config/lxc b/applications/luci-app-lxc/root/etc/config/lxc new file mode 100644 index 000000000..5572c735f --- /dev/null +++ b/applications/luci-app-lxc/root/etc/config/lxc @@ -0,0 +1,6 @@ +# +# lxc uci configuration +# + +config lxc 'lxc' + option url 'virtualwrt.org/containers/'