From e6dc9acb365bb438e47e1ddd85adadaad1f208ca Mon Sep 17 00:00:00 2001 From: janson Date: Thu, 17 Nov 2022 17:28:48 +0800 Subject: [PATCH] code server ok --- applications/luci-app-codeserver/Makefile | 18 ++++ .../luasrc/controller/codeserver.lua | 7 ++ .../luasrc/model/cbi/codeserver.lua | 64 ++++++++++++++ .../luasrc/model/codeserver.lua | 54 ++++++++++++ .../luasrc/view/codeserver/status.htm | 31 +++++++ .../po/zh-cn/codeserver.po | 39 +++++++++ .../root/etc/config/codeserver | 10 +++ .../root/usr/libexec/istorec/codeserver.sh | 84 +++++++++++++++++++ 8 files changed, 307 insertions(+) create mode 100644 applications/luci-app-codeserver/Makefile create mode 100755 applications/luci-app-codeserver/luasrc/controller/codeserver.lua create mode 100644 applications/luci-app-codeserver/luasrc/model/cbi/codeserver.lua create mode 100644 applications/luci-app-codeserver/luasrc/model/codeserver.lua create mode 100644 applications/luci-app-codeserver/luasrc/view/codeserver/status.htm create mode 100644 applications/luci-app-codeserver/po/zh-cn/codeserver.po create mode 100644 applications/luci-app-codeserver/root/etc/config/codeserver create mode 100755 applications/luci-app-codeserver/root/usr/libexec/istorec/codeserver.sh diff --git a/applications/luci-app-codeserver/Makefile b/applications/luci-app-codeserver/Makefile new file mode 100644 index 0000000..1f1bd00 --- /dev/null +++ b/applications/luci-app-codeserver/Makefile @@ -0,0 +1,18 @@ + + +include $(TOPDIR)/rules.mk + +PKG_VERSION:=1.0.0-20221114 +PKG_RELEASE:= + +LUCI_TITLE:=LuCI support for Emby +LUCI_PKGARCH:=all +LUCI_DEPENDS:=+docker +luci-lib-taskd + +define Package/luci-app-emby/conffiles +/etc/config/emby +endef + +include $(TOPDIR)/feeds/luci/luci.mk + +# call BuildPackage - OpenWrt buildroot signature diff --git a/applications/luci-app-codeserver/luasrc/controller/codeserver.lua b/applications/luci-app-codeserver/luasrc/controller/codeserver.lua new file mode 100755 index 0000000..59be395 --- /dev/null +++ b/applications/luci-app-codeserver/luasrc/controller/codeserver.lua @@ -0,0 +1,7 @@ + +module("luci.controller.codeserver", package.seeall) + +function index() + entry({"admin", "services", "codeserver"}, alias("admin", "services", "codeserver", "config"), _("CodeServer"), 30).dependent = true + entry({"admin", "services", "codeserver", "config"}, cbi("codeserver")) +end diff --git a/applications/luci-app-codeserver/luasrc/model/cbi/codeserver.lua b/applications/luci-app-codeserver/luasrc/model/cbi/codeserver.lua new file mode 100644 index 0000000..d8b5be3 --- /dev/null +++ b/applications/luci-app-codeserver/luasrc/model/cbi/codeserver.lua @@ -0,0 +1,64 @@ +--[[ +LuCI - Lua Configuration Interface +]]-- + +local taskd = require "luci.model.tasks" +local codeserver_model = require "luci.model.codeserver" +local m, s, o + +m = taskd.docker_map("codeserver", "codeserver", "/usr/libexec/istorec/codeserver.sh", + translate("CodeServer"), + translate("CodeServer offloads development from local workstations to your on-prem and public cloud infrastructure.") + .. translate("Official website:") .. ' https://coder.com/') + +s = m:section(SimpleSection, translate("Service Status"), translate("CodeServer status:")) +s:append(Template("codeserver/status")) + +s = m:section(TypedSection, "main", translate("Setup"), translate("The following parameters will only take effect during installation or upgrade:")) +s.addremove=false +s.anonymous=true + +o = s:option(Value, "http_port", translate("HTTP Port").."*") +o.default = "8082" +o.datatype = "string" + +o = s:option(Value, "image_name", translate("Image").."*") +o.rmempty = false +o.datatype = "string" +o:value("lscr.io/linuxserver/code-server:latest", "lscr.io/linuxserver/code-server:latest") +o:value("lscr.io/linuxserver/code-server:4.8.3", "lscr.io/linuxserver/code-server:4.8.3") +o.default = "lscr.io/linuxserver/code-server:latest" + +local blocks = codeserver_model.blocks() +local home = codeserver_model.home() + +o = s:option(Value, "config_path", translate("Config path").."*") +o.rmempty = false +o.datatype = "string" + +local paths, default_path = codeserver_model.find_paths(blocks, home, "Configs") +for _, val in pairs(paths) do + o:value(val, val) +end +o.default = default_path + +o = s:option(Value, "env_password", "PASSWORD") +o.password = true +o.datatype = "string" + +o = s:option(Value, "env_hashed_password", "HASHED_PASSWORD") +o.datatype = "string" +o.password = true + +o = s:option(Value, "env_sudo_password", "SUDO_PASSWORD") +o.password = true +o.datatype = "string" + +o = s:option(Value, "env_sudo_password_hash", "SUDO_PASSWORD_HASH") +o.password = true +o.datatype = "string" + +o = s:option(Value, "env_proxy_domain", "PROXY_DOMAIN") +o.datatype = "string" + +return m diff --git a/applications/luci-app-codeserver/luasrc/model/codeserver.lua b/applications/luci-app-codeserver/luasrc/model/codeserver.lua new file mode 100644 index 0000000..d476ae9 --- /dev/null +++ b/applications/luci-app-codeserver/luasrc/model/codeserver.lua @@ -0,0 +1,54 @@ +local util = require "luci.util" +local jsonc = require "luci.jsonc" + +local codeserver = {} + +codeserver.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 + +codeserver.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", "dl_dir", home_dirs["main_dir"].."/Public") + home_dirs["Caches"] = uci:get_first("quickstart", "main", "tmp_dir", home_dirs["main_dir"].."/Caches") + return home_dirs +end + +codeserver.find_paths = function(blocks, home_dirs, path_name) + local default_path = '' + local configs = {} + + default_path = home_dirs[path_name] .. "/CodeServer" + if #blocks == 0 then + table.insert(configs, default_path) + else + for _, val in pairs(blocks) do + table.insert(configs, val .. "/" .. path_name .. "/CodeServer") + end + local without_conf_dir = "/root/" .. path_name .. "/CodeServer" + if default_path == without_conf_dir then + default_path = configs[1] + end + end + + return configs, default_path +end + +return codeserver diff --git a/applications/luci-app-codeserver/luasrc/view/codeserver/status.htm b/applications/luci-app-codeserver/luasrc/view/codeserver/status.htm new file mode 100644 index 0000000..effa0c1 --- /dev/null +++ b/applications/luci-app-codeserver/luasrc/view/codeserver/status.htm @@ -0,0 +1,31 @@ +<% +local util = require "luci.util" +local container_status = util.trim(util.exec("/usr/libexec/istorec/codeserver.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/istorec/codeserver.sh port")) + if port == "" then + port="8443" + end +-%> +
+ +
+ + +
+
+<% end %> diff --git a/applications/luci-app-codeserver/po/zh-cn/codeserver.po b/applications/luci-app-codeserver/po/zh-cn/codeserver.po new file mode 100644 index 0000000..b170860 --- /dev/null +++ b/applications/luci-app-codeserver/po/zh-cn/codeserver.po @@ -0,0 +1,39 @@ +msgid "" +msgstr "Content-Type: text/plain; charset=UTF-8" + +msgid "Official website:" +msgstr "官方网站:" + +msgid "CodeServer offloads development from local workstations to your on-prem and public cloud infrastructure." +msgstr "CodeServer 是一个在线开发平台。" + +msgid "Config path" +msgstr "配置文件路径" + +msgid "HTTP Port" +msgstr "HTTP 端口" + +msgid "Service Status" +msgstr "服务状态" + +msgid "CodeServer status:" +msgstr "CodeServer 的状态信息如下:" + +msgid "Setup" +msgstr "安装配置" + +msgid "The following parameters will only take effect during installation or upgrade:" +msgstr "以下参数只在安装或者升级时才会生效:" + +msgid "Status" +msgstr "状态" + +msgid "CodeServer is running" +msgstr "CodeServer 运行中" + +msgid "CodeServer is not running" +msgstr "CodeServer 未运行" + +msgid "Open CodeServer" +msgstr "打开 CodeServer" + diff --git a/applications/luci-app-codeserver/root/etc/config/codeserver b/applications/luci-app-codeserver/root/etc/config/codeserver new file mode 100644 index 0000000..9a4c1f1 --- /dev/null +++ b/applications/luci-app-codeserver/root/etc/config/codeserver @@ -0,0 +1,10 @@ +config main + option 'http_port' '8082' + option 'image_name' 'lscr.io/linuxserver/code-server:latest' + option 'config_path' '' + option 'env_password' '' + option 'env_hashed_password' '' + option 'env_sudo_password' '' + option 'env_sudo_password_hash' '' + option 'env_proxy_domain' '' + diff --git a/applications/luci-app-codeserver/root/usr/libexec/istorec/codeserver.sh b/applications/luci-app-codeserver/root/usr/libexec/istorec/codeserver.sh new file mode 100755 index 0000000..c161d2a --- /dev/null +++ b/applications/luci-app-codeserver/root/usr/libexec/istorec/codeserver.sh @@ -0,0 +1,84 @@ +#!/bin/sh +# Author Xiaobao(xiaobao@linkease.com) + +ACTION=${1} +shift 1 + +do_install() { + local http_port=`uci get codeserver.@main[0].http_port 2>/dev/null` + local image_name=`uci get codeserver.@main[0].image_name 2>/dev/null` + local config=`uci get codeserver.@main[0].config_path 2>/dev/null` + local env_password=`uci get codeserver.@main[0].env_password 2>/dev/null` + local env_hashed_password=`uci get codeserver.@main[0].env_hashed_password 2>/dev/null` + local env_sudo_password=`uci get codeserver.@main[0].env_sudo_password 2>/dev/null` + local env_sudo_password_hash=`uci get codeserver.@main[0].env_sudo_password_hash 2>/dev/null` + local env_proxy_domain=`uci get codeserver.@main[0].env_proxy_domain 2>/dev/null` + + [ -z "$image_name" ] && image_name="lscr.io/linuxserver/code-server:latest" + echo "docker pull ${image_name}" + docker pull ${image_name} + docker rm -f codeserver + + if [ -z "$config" ]; then + echo "config path is empty!" + exit 1 + fi + + [ -z "$http_port" ] && http_port=8082 + + local cmd="docker run --restart=unless-stopped -d -v \"$config:/config\" \ + --dns=172.17.0.1 \ + -e DEFAULT_WORKSPACE=/config/workspace \ + -p $http_port:8443 " + + local tz="`cat /tmp/TZ`" + [ -z "$tz" ] || cmd="$cmd -e TZ=$tz" + + [ -z "$env_password" ] || cmd="$cmd -e \"PASSWORD=$env_password\"" + [ -z "$env_hashed_password" ] || cmd="$cmd -e \"HASHED_PASSWORD=$env_hashed_password\"" + [ -z "$env_sudo_password" ] || cmd="$cmd -e \"SUDO_PASSWORD=$env_sudo_password\"" + [ -z "$env_sudo_password_hash" ] || cmd="$cmd -e \"SUDO_PASSWORD_HASH=$env_sudo_password_hash\"" + [ -z "$env_proxy_domain" ] || cmd="$cmd -e \"PROXY_DOMAIN=$env_proxy_domain\"" + + cmd="$cmd -v /mnt:/mnt" + mountpoint -q /mnt && cmd="$cmd:rslave" + cmd="$cmd --name codeserver \"$image_name\"" + + echo "$cmd" + eval "$cmd" +} + +usage() { + echo "usage: $0 sub-command" + echo "where sub-command is one of:" + echo " install Install the codeserver" + echo " upgrade Upgrade the codeserver" + echo " rm/start/stop/restart Remove/Start/Stop/Restart the codeserver" + echo " status CodeServer status" + echo " port CodeServer port" +} + +case ${ACTION} in + "install") + do_install + ;; + "upgrade") + do_install + ;; + "rm") + docker rm -f codeserver + ;; + "start" | "stop" | "restart") + docker ${ACTION} codeserver + ;; + "status") + docker ps --all -f 'name=codeserver' --format '{{.State}}' + ;; + "port") + docker ps --all -f 'name=codeserver' --format '{{.Ports}}' | grep -om1 '0.0.0.0:[0-9]*' | sed 's/0.0.0.0://' + ;; + *) + usage + exit 1 + ;; +esac