commit 63404899833fbc098c259a6bda24d642fc81b789 Author: ben Date: Sun Sep 3 01:16:20 2023 +0000 first commit diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..605736f Binary files /dev/null and b/.DS_Store differ diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..5e839b3 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ + + +include $(TOPDIR)/rules.mk + + +LUCI_TITLE:=LuCI support for lxd +LUCI_PKGARCH:=all +LUCI_DEPENDS:=+lsblk +lxdc +luci-lib-taskd +docker + +define Package/luci-app-lxdterm/conffiles +/etc/config/lxdterm +endef + +include $(TOPDIR)/feeds/luci/luci.mk + +# call BuildPackage - OpenWrt buildroot signature diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/luasrc/controller/lxdterm.lua b/luasrc/controller/lxdterm.lua new file mode 100755 index 0000000..b9f3aac --- /dev/null +++ b/luasrc/controller/lxdterm.lua @@ -0,0 +1,15 @@ + +module("luci.controller.lxdterm", package.seeall) + +function index() + entry({"admin", "apps"}, firstchild(), _("apps") , 45).dependent = false + if not nixio.fs.access("/etc/config/lxdterm") then + return + end + + local page = entry({"admin", "apps", "lxdterm"}, cbi("lxdterm"), _("lxdterm")) + page.order = 10 + page.dependent = true + page.acl_depends = { "luci-app-lxdterm" } + entry({"admin","lxc","lxdterm","status"},call("act_status")).leaf=true +end diff --git a/luasrc/model/cbi/lxdterm.lua b/luasrc/model/cbi/lxdterm.lua new file mode 100755 index 0000000..22f6835 --- /dev/null +++ b/luasrc/model/cbi/lxdterm.lua @@ -0,0 +1,29 @@ +--[[ +LuCI - Lua Configuration Interface +]]-- + +local taskd = require "luci.model.tasks" +local lxdterm_model = require "luci.model.lxdterm" +local m, s, o + +m = taskd.docker_map("lxdterm", "lxdterm", "/usr/libexec/apps/lxdterm/lxdterm.sh", + translate("lxdterm"), + translate("Access your LXD containers in Luci Terminal") + .. translate("Official website:") .. ' https://lxd.com/') + +s = m:section(SimpleSection, translate("Service Status"), translate("lxdterm status:")) +s:append(Template("lxdterm/status")) + +s = m:section(TypedSection, "lxdterm", translate("Container"), translate("Enter the LXC container you want to access then click save.")) +s.addremove=false +s.anonymous=true + +o = s:option(Value, "container", translate("container").."*") +o.rmempty = false +o.default = "ubuntu" -- Changed default to "ubuntu" +o.datatype = "string" -- Changed datatype to "string" + +local blocks = lxdterm_model.blocks() +local home = lxdterm_model.home() + +return m \ No newline at end of file diff --git a/luasrc/model/lxdterm.lua b/luasrc/model/lxdterm.lua new file mode 100755 index 0000000..63c2214 --- /dev/null +++ b/luasrc/model/lxdterm.lua @@ -0,0 +1,55 @@ +local util = require "luci.util" +local jsonc = require "luci.jsonc" + +local lxdterm = {} + +lxdterm.blocks = function() + local f = io.popen("lsblk -s -f -b -o NAME,FSSIZE,MOUNTPOINT --json", "r") + local vals = {} + if f then + local ret = f:read("*all") + f:close() + local obj = jsonc.parse(ret) + for _, val in pairs(obj["blockdevices"]) do + local fsize = val["fssize"] + if fsize ~= nil and string.len(fsize) > 10 and val["mountpoint"] then + -- fsize > 1G + vals[#vals+1] = val["mountpoint"] + end + end + end + return vals +end + +lxdterm.home = function() + local uci = require "luci.model.uci".cursor() + local home_dirs = {} + home_dirs["main_dir"] = uci:get_first("quickstart", "main", "main_dir", "/root") + home_dirs["Configs"] = uci:get_first("quickstart", "main", "conf_dir", home_dirs["main_dir"].."/Configs") + home_dirs["Public"] = uci:get_first("quickstart", "main", "pub_dir", home_dirs["main_dir"].."/Public") + home_dirs["Downloads"] = uci:get_first("quickstart", "main", "dl_dir", home_dirs["Public"].."/Downloads") + home_dirs["Caches"] = uci:get_first("quickstart", "main", "tmp_dir", home_dirs["main_dir"].."/Caches") + return home_dirs +end + +lxdterm.find_paths = function(blocks, home_dirs, path_name) + local default_path = '' + local configs = {} + + default_path = home_dirs[path_name] .. "/lxdterm" + if #blocks == 0 then + table.insert(configs, default_path) + else + for _, val in pairs(blocks) do + table.insert(configs, val .. "/" .. path_name .. "/lxdterm") + end + local without_conf_dir = "/root/" .. path_name .. "/lxdterm" + if default_path == without_conf_dir then + default_path = configs[1] + end + end + + return configs, default_path +end + +return simplex diff --git a/luasrc/view/simplex/status.htm b/luasrc/view/simplex/status.htm new file mode 100755 index 0000000..b6cfdbd --- /dev/null +++ b/luasrc/view/simplex/status.htm @@ -0,0 +1,31 @@ +<% +local util = require "luci.util" +local container_status = util.trim(util.exec("/usr/libexec/apps/simplex/simplex.sh status")) +local container_install = (string.len(container_status) > 0) +local container_running = container_status == "running" +-%> +
+ +
+ <% if container_running then %> + + <% else %> + + <% end %> +
+
+<% +if container_running then + local port=util.trim(util.exec("/usr/libexec/apps/simplex/simplex.sh port")) + if port == "" then + port="5223" + end +-%> +
+ +
+ + +
+
+<% end %> diff --git a/root/etc/config/lxdterm b/root/etc/config/lxdterm new file mode 100755 index 0000000..ece250b --- /dev/null +++ b/root/etc/config/lxdterm @@ -0,0 +1,4 @@ +config lxdterm + option 'container' 'ubuntu' + option 'config_path' '/mnt/sda1/lxc + diff --git a/root/etc/uci-defaults/luci-app-simplex b/root/etc/uci-defaults/luci-app-simplex new file mode 100755 index 0000000..22868dd --- /dev/null +++ b/root/etc/uci-defaults/luci-app-simplex @@ -0,0 +1,14 @@ +#!/bin/sh + +config_dir=`uci -q get simplex.@simplex[0].config_path` + +data_dir=`docker inspect --format '{{.Mounts}}' simplex | grep -Eom1 '[^ ]+/_data /var/www/html local true ' | cut -d' ' -f1` + +if [ -n "$data_dir" -a "$data_dir" != "$config_dir" ]; then + uci -q batch <<-EOF >/dev/null + set simplex.@simplex[0].config_path="$data_dir" + commit simplex +EOF +fi + +exit 0 diff --git a/root/usr/libexec/apps/lxdterm/lxdterm.sh b/root/usr/libexec/apps/lxdterm/lxdterm.sh new file mode 100755 index 0000000..69ff4e9 --- /dev/null +++ b/root/usr/libexec/apps/lxdterm/lxdterm.sh @@ -0,0 +1,64 @@ +#!/bin/sh + +ACTION=${1} +shift 1 + +get_image() { + GEN_PASS=$(< /dev/urandom tr -dc A-Za-z0-9 2>/dev/null | head -c14; echo) +} + +do_install_detail() { + local config=`uci get lxdterm.@lxdterm[0].container 2>/dev/null` + + #Generate the generic environment variables for the docker-compose + + + + # Get our local LAN IP Address + LAN_IP=$(uci get network.lan.ipaddr) + # Strip trailing network mask + LAN_IP="${LAN_IP%/*}" + + +echo "Entering LXC container" + +lxc-attach -n "$config" + +} + +usage() { + echo "usage: $0 sub-command" + echo "where sub-command is one of:" + echo " install Install lxdterm" + echo " upgrade Upgrade lxdterm" + echo " rm/start/stop/restart Remove/Start/Stop/Restart lxdterm" + echo " status lxdterm status" + echo " port lxdterm port" +} + +case ${ACTION} in + "install") + get_image + do_install_detail + ;; + "upgrade") + get_image + do_install_detail + ;; + "rm") + docker rm -f lxdterm + ;; + "start" | "stop" | "restart") + docker ${ACTION} lxdterm + ;; + "status") + docker ps --all -f 'name=lxdterm' --format '{{.State}}' + ;; + "port") + docker ps --all -f 'name=lxdterm' --format '{{.Ports}}' | grep -om1 '0.0.0.0:[0-9]*' | sed 's/0.0.0.0://' + ;; + *) + usage + exit 1 + ;; +esac diff --git a/root/usr/share/rpcd/acl.d/luci-app-lxdterm.json b/root/usr/share/rpcd/acl.d/luci-app-lxdterm.json new file mode 100755 index 0000000..88620e2 --- /dev/null +++ b/root/usr/share/rpcd/acl.d/luci-app-lxdterm.json @@ -0,0 +1,11 @@ +{ + "luci-app-lxdterm": { + "description": "Grant UCI access for luci-app-lxdterm", + "read": { + "uci": [ "lxdterm" ] + }, + "write": { + "uci": [ "lxdterm" ] + } + } +}