diff --git a/utils/luci-app-lxc/Makefile b/utils/luci-app-lxc/Makefile new file mode 100644 index 000000000..ffdb95843 --- /dev/null +++ b/utils/luci-app-lxc/Makefile @@ -0,0 +1,66 @@ +# +# Copyright (C) 2014 OpenWrt.org +# +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=luci-app-lxc +PKG_RELEASE:=20141012 + +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) + +include $(INCLUDE_DIR)/package.mk + +define Package/luci-app-lxc + SECTION:=luci + CATEGORY:=LuCI + SUBMENU:=3. Applications + TITLE:=LXC management Web UI + DEPENDS:=+luci-mod-admin-full +lxc +liblxc +rpcd-mod-lxc + MAINTAINER:=Petar Koretic +endef + +define Package/luci-app-lxc/description + This package will install LXC management Web UI. +endef + +define Build/Prepare +endef + +define Build/Configure +endef + +define Build/Compile +endef + +define Package/luci-app-lxc/install + $(INSTALL_DIR) $(1)/usr/lib/lua/luci/controller/ + $(INSTALL_BIN) \ + ./files/controller/lxc.lua \ + $(1)/usr/lib/lua/luci/controller/ + + $(INSTALL_DIR) $(1)/usr/lib/lua/luci/view/ + $(INSTALL_DATA) \ + ./files/view/lxc.htm \ + $(1)/usr/lib/lua/luci/view/ + + $(INSTALL_DIR) $(1)/usr/lib/lua/luci/model/cbi/ + $(INSTALL_BIN) \ + ./files/model/cbi/lxc.lua \ + $(1)/usr/lib/lua/luci/model/cbi/ + + $(INSTALL_DIR) $(1)/etc/config/ + $(INSTALL_DATA) \ + ./files/lxc.config \ + $(1)/etc/config/lxc + + $(INSTALL_DIR) $(1)/www + $(CP) -R \ + ./files/www/* \ + $(1)/www +endef + +$(eval $(call BuildPackage,luci-app-lxc)) diff --git a/utils/luci-app-lxc/files/controller/lxc.lua b/utils/luci-app-lxc/files/controller/lxc.lua new file mode 100644 index 000000000..8172fb9c0 --- /dev/null +++ b/utils/luci-app-lxc/files/controller/lxc.lua @@ -0,0 +1,182 @@ +--[[ + +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) + +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_stop"}, call("lxc_stop"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_start"}, call("lxc_start"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_reboot"}, call("lxc_reboot"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_delete"}, call("lxc_delete"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_list"}, call("lxc_list"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_rename"}, call("lxc_rename"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_clone"}, call("lxc_clone"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_freeze"}, call("lxc_freeze"), nil) + page.leaf = true + + page = entry({"admin", "services", "lxc_unfreeze"}, call("lxc_unfreeze"), 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_create(lxc_name, lxc_template) + luci.http.prepare_content("text/plain") + + local uci = require("uci").cursor() + + local url = uci:get("lxc", "lxc", "url") + + local f = io.popen([[grep DISTRIB_TARGET /etc/openwrt_release | awk -F"[/'']" '{ print $2 }']]) + if not f then + return luci.http.write("1") + end + + local target = f:read("*all") + + local res = os.execute("lxc-create -t download -n " .. lxc_name .. " -- --server=" .. url .. " --no-validate --dist openwrt --release bb --arch " .. target) + + luci.http.write(tostring(res)) +end + +function lxc_start(lxc_name) + luci.http.prepare_content("text/plain") + + local res = os.execute("ubus call lxc start '{\"name\" : \"" .. lxc_name .. "\"}' ") + + luci.http.write(tostring(res)) +end + +function lxc_stop(lxc_name) + luci.http.prepare_content("text/plain") + + local res = os.execute("ubus call lxc stop '{\"name\" : \"" .. lxc_name .. "\"}' ") + + luci.http.write(tostring(res)) +end + +function lxc_delete(lxc_name) + luci.http.prepare_content("text/plain") + + os.execute("ubus call lxc stop '{\"name\" : \"" .. lxc_name .. "\"}' ") + local res = os.execute("ubus call lxc destroy '{\"name\" : \"" .. lxc_name .. "\"}' ") + + luci.http.write(tostring(res)) +end + +function lxc_reboot(lxc_name) + luci.http.prepare_content("text/plain") + + local res = os.execute("ubus call lxc reboot '{\"name\" : \"" .. lxc_name .. "\"}' ") + + luci.http.write(tostring(res)) +end + +function lxc_rename(lxc_name_cur, lxc_name_new) + luci.http.prepare_content("text/plain") + + local res = os.execute("ubus call lxc rename '{\"name\" : \"" .. lxc_name_cur .. "\", \"newname\" : \"" .. lxc_name_new .. "\"}' ") + + luci.http.write(tostring(res)) +end + +function lxc_freeze(lxc_name) + luci.http.prepare_content("text/plain") + + local res = os.execute("ubus call lxc freeze '{\"name\" : \"" .. lxc_name .. "\"}' ") + + luci.http.write(tostring(res)) +end + +function lxc_unfreeze(lxc_name) + luci.http.prepare_content("text/plain") + + local res = os.execute("ubus call lxc unfreeze '{\"name\" : \"" .. lxc_name .. "\"}' ") + + luci.http.write(tostring(res)) +end + +function lxc_list() + luci.http.prepare_content("application/json") + + local cmd = io.popen("ubus call lxc list") + if not cmd then + return luci.http.write("{}") + end + + local res = cmd:read("*all") + cmd:close() + + luci.http.write(res) +end + +function lxc_configuration_get(lxc_name) + luci.http.prepare_content("text/plain") + + local f = io.open("/lxc/" .. 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/" .. 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/utils/luci-app-lxc/files/lxc.config b/utils/luci-app-lxc/files/lxc.config new file mode 100644 index 000000000..5572c735f --- /dev/null +++ b/utils/luci-app-lxc/files/lxc.config @@ -0,0 +1,6 @@ +# +# lxc uci configuration +# + +config lxc 'lxc' + option url 'virtualwrt.org/containers/' diff --git a/utils/luci-app-lxc/files/model/cbi/lxc.lua b/utils/luci-app-lxc/files/model/cbi/lxc.lua new file mode 100644 index 000000000..ac0fdff33 --- /dev/null +++ b/utils/luci-app-lxc/files/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/utils/luci-app-lxc/files/view/lxc.htm b/utils/luci-app-lxc/files/view/lxc.htm new file mode 100644 index 000000000..1d38721da --- /dev/null +++ b/utils/luci-app-lxc/files/view/lxc.htm @@ -0,0 +1,474 @@ +<%# + +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/utils/luci-app-lxc/files/www/luci-static/resources/cbi/green.gif b/utils/luci-app-lxc/files/www/luci-static/resources/cbi/green.gif new file mode 100644 index 000000000..d09febf12 Binary files /dev/null and b/utils/luci-app-lxc/files/www/luci-static/resources/cbi/green.gif differ diff --git a/utils/luci-app-lxc/files/www/luci-static/resources/cbi/purple.gif b/utils/luci-app-lxc/files/www/luci-static/resources/cbi/purple.gif new file mode 100644 index 000000000..f0d68cc8b Binary files /dev/null and b/utils/luci-app-lxc/files/www/luci-static/resources/cbi/purple.gif differ diff --git a/utils/luci-app-lxc/files/www/luci-static/resources/cbi/red.gif b/utils/luci-app-lxc/files/www/luci-static/resources/cbi/red.gif new file mode 100644 index 000000000..c1b39bbed Binary files /dev/null and b/utils/luci-app-lxc/files/www/luci-static/resources/cbi/red.gif differ