From d0d7dcd49b10ee74eae28632db8ee6ca944754dc Mon Sep 17 00:00:00 2001 From: jjm2473 <1129525450@qq.com> Date: Tue, 30 Aug 2022 13:19:20 +0800 Subject: [PATCH] nextcloud: add config_path option --- applications/luci-app-nextcloud/Makefile | 13 +- .../luasrc/controller/nextcloud.lua | 225 +----------------- .../luasrc/model/cbi/nextcloud.lua | 30 +++ .../luasrc/view/nextcloud/main.htm | 34 --- .../luasrc/view/nextcloud/status.htm | 31 +++ .../luci-app-nextcloud/po/zh-cn/nextcloud.po | 47 ++-- .../root/etc/config/nextcloud | 2 +- .../root/etc/uci-defaults/luci-app-nextcloud | 14 ++ .../root/usr/libexec/istorec/nextcloud.sh | 81 +++++++ .../root/usr/share/nextcloud/install.sh | 111 --------- 10 files changed, 195 insertions(+), 393 deletions(-) create mode 100644 applications/luci-app-nextcloud/luasrc/model/cbi/nextcloud.lua delete mode 100644 applications/luci-app-nextcloud/luasrc/view/nextcloud/main.htm create mode 100644 applications/luci-app-nextcloud/luasrc/view/nextcloud/status.htm create mode 100644 applications/luci-app-nextcloud/root/etc/uci-defaults/luci-app-nextcloud create mode 100755 applications/luci-app-nextcloud/root/usr/libexec/istorec/nextcloud.sh delete mode 100755 applications/luci-app-nextcloud/root/usr/share/nextcloud/install.sh diff --git a/applications/luci-app-nextcloud/Makefile b/applications/luci-app-nextcloud/Makefile index 6878293..808e904 100644 --- a/applications/luci-app-nextcloud/Makefile +++ b/applications/luci-app-nextcloud/Makefile @@ -2,16 +2,17 @@ include $(TOPDIR)/rules.mk -PKG_NAME:=luci-app-nextcloud -PKG_VERSION:=1.0.2 -PKG_RELEASE:=20210923 +PKG_VERSION:=1.1.0-20220830 +PKG_RELEASE:= LUCI_TITLE:=LuCI support for nextcloud LUCI_PKGARCH:=all -LUCI_DEPENDS:=+docker +luci-lib-iform +LUCI_DEPENDS:=+docker +luci-lib-taskd + +define Package/luci-app-nextcloud/conffiles +/etc/config/nextcloud +endef include $(TOPDIR)/feeds/luci/luci.mk # call BuildPackage - OpenWrt buildroot signature - - diff --git a/applications/luci-app-nextcloud/luasrc/controller/nextcloud.lua b/applications/luci-app-nextcloud/luasrc/controller/nextcloud.lua index be80c58..33d8078 100755 --- a/applications/luci-app-nextcloud/luasrc/controller/nextcloud.lua +++ b/applications/luci-app-nextcloud/luasrc/controller/nextcloud.lua @@ -1,228 +1,7 @@ -local util = require "luci.util" -local http = require "luci.http" -local docker = require "luci.model.docker" -local iform = require "luci.iform" module("luci.controller.nextcloud", package.seeall) function index() - - entry({"admin", "services", "nextcloud"}, call("redirect_index"), _("NextCloud"), 30).dependent = true - entry({"admin", "services", "nextcloud", "pages"}, call("nextcloud_index")).leaf = true - entry({"admin", "services", "nextcloud", "form"}, call("nextcloud_form")) - entry({"admin", "services", "nextcloud", "submit"}, call("nextcloud_submit")) - entry({"admin", "services", "nextcloud", "log"}, call("nextcloud_log")) - + entry({"admin", "services", "nextcloud"}, alias("admin", "services", "nextcloud", "config"), _("Nextcloud"), 30).dependent = true + entry({"admin", "services", "nextcloud", "config"}, cbi("nextcloud")) end - -local const_log_end = "XU6J03M6" -local appname = "nextcloud" -local page_index = {"admin", "services", "nextcloud", "pages"} - -function redirect_index() - http.redirect(luci.dispatcher.build_url(unpack(page_index))) -end - -function nextcloud_index() - luci.template.render("nextcloud/main", {prefix=luci.dispatcher.build_url(unpack(page_index))}) -end - -function nextcloud_form() - local error = "" - local scope = "" - local success = 0 - - local data = get_data() - local result = { - data = data, - schema = get_schema(data) - } - local response = { - error = error, - scope = scope, - success = success, - result = result, - } - http.prepare_content("application/json") - http.write_json(response) -end - -function get_schema(data) - local actions - if data.container_install then - actions = { - { - name = "restart", - text = "重启", - type = "apply", - }, - { - name = "upgrade", - text = "更新", - type = "apply", - }, - { - name = "remove", - text = "删除", - type = "apply", - }, - } - else - actions = { - { - name = "install", - text = "安装", - type = "apply", - }, - } - end - local _ = luci.i18n.translate - local access = _('access homepage: ') - local homepage = 'https://nextcloud.com/' - local schema = { - actions = actions, - containers = get_containers(data), - description = _("A safe home for all your data. Access & share your files, calendars, contacts, mail & more from any device, on your terms.")..access..homepage, - title = _("NextCloud") - } - return schema -end - -function get_containers(data) - local containers = { - status_container(data), - main_container(data) - } - return containers -end - -function status_container(data) - local status_value - - if data.container_install then - status_value = "NextCloud 运行中" - else - status_value = "NextCloud 未运行" - end - - local status_c1 = { - labels = { - { - key = "状态:", - value = status_value - }, - { - key = "访问:", - value = "" - } - - }, - description = "NextCloud 的状态信息如下:", - title = "服务状态" - } - return status_c1 -end - -function main_container(data) - local main_c2 = { - properties = { - { - name = "port", - required = true, - title = "端口", - type = "string" - }, - }, - description = "请设置端口:", - title = "服务操作" - } - return main_c2 -end - -function get_data() - local uci = require "luci.model.uci".cursor() - local docker_path = util.exec("which docker") - local docker_install = (string.len(docker_path) > 0) - local container_id = util.trim(util.exec("docker ps -qf 'name="..appname.."'")) - local container_install = (string.len(container_id) > 0) - local port = tonumber(uci:get_first(appname, appname, "port", "8082")) - local data = { - port = port, - container_install = container_install - } - return data -end - -function nextcloud_submit() - local error = "" - local scope = "" - local success = 0 - local result - - local jsonc = require "luci.jsonc" - local json_parse = jsonc.parse - local content = http.content() - local req = json_parse(content) - if req["$apply"] == "upgrade" then - result = install_upgrade_nextcloud(req) - elseif req["$apply"] == "install" then - result = install_upgrade_nextcloud(req) - elseif req["$apply"] == "restart" then - result = restart_nextcloud(req) - else - result = delete_nextcloud() - end - http.prepare_content("application/json") - local resp = { - error = error, - scope = scope, - success = success, - result = result, - } - http.write_json(resp) -end - -function nextcloud_log() - iform.response_log("/var/log/"..appname..".log") -end - -function install_upgrade_nextcloud(req) - local port = req["port"] - - -- save config - local uci = require "luci.model.uci".cursor() - uci:tset(appname, "@"..appname.."[0]", { - port = port or "8082", - }) - uci:save(appname) - uci:commit(appname) - - local exec_cmd = string.format("/usr/share/nextcloud/install.sh %s", req["$apply"]) - iform.fork_exec(exec_cmd) - - local result = { - async = true, - exec = exec_cmd, - async_state = req["$apply"] - } - return result -end - -function delete_nextcloud() - local log = iform.exec_to_log("docker rm -f nextcloud") - local result = { - async = false, - log = log - } - return result -end - -function restart_nextcloud() - local log = iform.exec_to_log("docker restart nextcloud") - local result = { - async = false, - log = log - } - return result -end - diff --git a/applications/luci-app-nextcloud/luasrc/model/cbi/nextcloud.lua b/applications/luci-app-nextcloud/luasrc/model/cbi/nextcloud.lua new file mode 100644 index 0000000..9205520 --- /dev/null +++ b/applications/luci-app-nextcloud/luasrc/model/cbi/nextcloud.lua @@ -0,0 +1,30 @@ +--[[ +LuCI - Lua Configuration Interface +]]-- + +local taskd = require "luci.model.tasks" +local m, s, o + +m = taskd.docker_map("nextcloud", "nextcloud", "/usr/libexec/istorec/nextcloud.sh", + translate("Nextcloud"), + translate("A safe home for all your data. Access & share your files, calendars, contacts, mail & more from any device, on your terms.") + .. translate("Official website:") .. ' https://nextcloud.com/') + +s = m:section(SimpleSection, translate("Service Status"), translate("Nextcloud status:")) +s:append(Template("nextcloud/status")) + +s = m:section(TypedSection, "nextcloud", translate("Setup"), translate("The following parameters will only take effect during installation or upgrade:")) +s.addremove=false +s.anonymous=true + +o = s:option(Value, "port", translate("Port").."*") +o.rmempty = false +o.default = "8082" +o.datatype = "port" + +o = s:option(Value, "config_path", translate("Config path").."*") +o.rmempty = false +o.default = "/root/nextcloud/config" +o.datatype = "string" + +return m diff --git a/applications/luci-app-nextcloud/luasrc/view/nextcloud/main.htm b/applications/luci-app-nextcloud/luasrc/view/nextcloud/main.htm deleted file mode 100644 index 1109306..0000000 --- a/applications/luci-app-nextcloud/luasrc/view/nextcloud/main.htm +++ /dev/null @@ -1,34 +0,0 @@ -<%+header%> - - -
-
- - - - - -<%+footer%> diff --git a/applications/luci-app-nextcloud/luasrc/view/nextcloud/status.htm b/applications/luci-app-nextcloud/luasrc/view/nextcloud/status.htm new file mode 100644 index 0000000..d3f3afa --- /dev/null +++ b/applications/luci-app-nextcloud/luasrc/view/nextcloud/status.htm @@ -0,0 +1,31 @@ +<% +local util = require "luci.util" +local container_status = util.trim(util.exec("/usr/libexec/istorec/nextcloud.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/nextcloud.sh port")) + if port == "" then + port="8082" + end +-%> +
+ +
+ + +
+
+<% end %> \ No newline at end of file diff --git a/applications/luci-app-nextcloud/po/zh-cn/nextcloud.po b/applications/luci-app-nextcloud/po/zh-cn/nextcloud.po index 0330ea7..13fd28f 100644 --- a/applications/luci-app-nextcloud/po/zh-cn/nextcloud.po +++ b/applications/luci-app-nextcloud/po/zh-cn/nextcloud.po @@ -1,27 +1,38 @@ -msgid "The nextcloud service is running." -msgstr "nextcloud已启动" +msgid "" +msgstr "Content-Type: text/plain; charset=UTF-8" -msgid "The nextcloud service is not running." -msgstr "nextcloud服务未启动" +msgid "Official website:" +msgstr "官方网站:" -msgid "The nextcloud service is not installed." -msgstr "nextcloud服务未安装" +msgid "A safe home for all your data. Access & share your files, calendars, contacts, mail & more from any device, on your terms." +msgstr "所有数据的安全之家。 根据您的条件,从任何设备访问和共享您的文件、日历、联系人、邮件等。" -msgid "open nextcloud" -msgstr "打开nextcloud" +msgid "Config path" +msgstr "配置文件路径" -msgid "stop nextcloud" -msgstr "停止nextcloud" +msgid "Port" +msgstr "端口" -msgid "run nextcloud" -msgstr "启动nextcloud" +msgid "Service Status" +msgstr "服务状态" -msgid "uninstall nextcloud" -msgstr "删除nextcloud" +msgid "Nextcloud status:" +msgstr "Nextcloud 的状态信息如下:" -msgid "install nextcloud" -msgstr "安装nextcloud" +msgid "Setup" +msgstr "安装配置" -msgid "Collecting data..." -msgstr "收集数据..." +msgid "The following parameters will only take effect during installation or upgrade:" +msgstr "以下参数只在安装或者升级时才会生效:" +msgid "Status" +msgstr "状态" + +msgid "Nextcloud is running" +msgstr "Nextcloud 运行中" + +msgid "Nextcloud is not running" +msgstr "Nextcloud 未运行" + +msgid "Open Nextcloud" +msgstr "打开 Nextcloud" diff --git a/applications/luci-app-nextcloud/root/etc/config/nextcloud b/applications/luci-app-nextcloud/root/etc/config/nextcloud index 501747e..d15234a 100644 --- a/applications/luci-app-nextcloud/root/etc/config/nextcloud +++ b/applications/luci-app-nextcloud/root/etc/config/nextcloud @@ -1,3 +1,3 @@ config nextcloud option 'port' '8082' - option 'enabled' '1' + option 'config_path' '/root/nextcloud/config' diff --git a/applications/luci-app-nextcloud/root/etc/uci-defaults/luci-app-nextcloud b/applications/luci-app-nextcloud/root/etc/uci-defaults/luci-app-nextcloud new file mode 100644 index 0000000..75bf83d --- /dev/null +++ b/applications/luci-app-nextcloud/root/etc/uci-defaults/luci-app-nextcloud @@ -0,0 +1,14 @@ +#!/bin/sh + +config_dir=`uci -q get nextcloud.@nextcloud[0].config_path` + +data_dir=`docker inspect --format '{{.Mounts}}' nextcloud | 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 nextcloud.@nextcloud[0].config_path="$data_dir" + commit nextcloud +EOF +fi + +exit 0 diff --git a/applications/luci-app-nextcloud/root/usr/libexec/istorec/nextcloud.sh b/applications/luci-app-nextcloud/root/usr/libexec/istorec/nextcloud.sh new file mode 100755 index 0000000..5f5e149 --- /dev/null +++ b/applications/luci-app-nextcloud/root/usr/libexec/istorec/nextcloud.sh @@ -0,0 +1,81 @@ +#!/bin/sh + +ACTION=${1} +shift 1 + +get_image() { + IMAGE_NAME="nextcloud" +} + +do_install() { + get_image + echo "docker pull ${IMAGE_NAME}" + docker pull ${IMAGE_NAME} + docker rm -f nextcloud + + do_install_detail +} + +do_install_detail() { + local config=`uci get nextcloud.@nextcloud[0].config_path 2>/dev/null` + local port=`uci get nextcloud.@nextcloud[0].port 2>/dev/null` + + if [ -z "$config" ]; then + echo "config path is empty!" + exit 1 + fi + + [ -z "$port" ] && port=8082 + + local cmd="docker run --restart=unless-stopped -d \ + -v \"$config:/var/www/html\" \ + --dns=172.17.0.1 \ + -p $port:80 " + + cmd="$cmd -v /mnt:/mnt" + mountpoint -q /mnt && cmd="$cmd:rslave" + + local tz="`cat /tmp/TZ`" + [ -z "$tz" ] || cmd="$cmd -e TZ=$tz" + + cmd="$cmd --name nextcloud \"$IMAGE_NAME\"" + + echo "$cmd" + eval "$cmd" + +} + +usage() { + echo "usage: $0 sub-command" + echo "where sub-command is one of:" + echo " install Install the nextcloud" + echo " upgrade Upgrade the nextcloud" + echo " rm/start/stop/restart Remove/Start/Stop/Restart the nextcloud" + echo " status Nextcloud status" + echo " port Nextcloud port" +} + +case ${ACTION} in + "install") + do_install + ;; + "upgrade") + do_install + ;; + "rm") + docker rm -f nextcloud + ;; + "start" | "stop" | "restart") + docker ${ACTION} nextcloud + ;; + "status") + docker ps --all -f 'name=nextcloud' --format '{{.State}}' + ;; + "port") + docker ps --all -f 'name=nextcloud' --format '{{.Ports}}' | grep -om1 '0.0.0.0:[0-9]*' | sed 's/0.0.0.0://' + ;; + *) + usage + exit 1 + ;; +esac diff --git a/applications/luci-app-nextcloud/root/usr/share/nextcloud/install.sh b/applications/luci-app-nextcloud/root/usr/share/nextcloud/install.sh deleted file mode 100755 index 55e37a1..0000000 --- a/applications/luci-app-nextcloud/root/usr/share/nextcloud/install.sh +++ /dev/null @@ -1,111 +0,0 @@ -#!/bin/sh -# Author Xiaobao(xiaobao@linkease.com) - -ACTION=${1} -WRLOCK=/var/lock/nextcloud.lock -LOGFILE=/var/log/nextcloud.log -LOGEND="XU6J03M6" -shift 1 - -IMAGE_NAME='nextcloud' - -check_params() { - - if [ -z "${WRLOCK}" ]; then - echo "lock file not found" - exit 1 - fi - - if [ -z "${LOGFILE}" ]; then - echo "logger file not found" - exit 1 - fi - -} - -lock_run() { - local lock="$WRLOCK" - exec 300>$lock - flock -n 300 || return - do_run - flock -u 300 - return -} - -run_action() { - if check_params; then - lock_run - fi -} - -do_install() { - echo "docker pull ${IMAGE_NAME}" >${LOGFILE} - docker pull ${IMAGE_NAME} >>${LOGFILE} 2>&1 - docker rm -f nextcloud - - local port=`uci get nextcloud.@nextcloud[0].port 2>/dev/null` - if [ -z "${port}" ]; then - port=8082 - fi - - local mntv="/mnt:/mnt" - mountpoint -q /mnt && mntv="$mntv:rslave" - - docker run -d \ - --name nextcloud \ - --dns=172.17.0.1 \ - --restart=unless-stopped \ - -p ${port}:80 \ - -v /mnt:/mnt:rslave \ - -e TZ="Asia/Shanghai" \ - ${IMAGE_NAME} >>${LOGFILE} 2>&1 - - RET=$? - if [ "${RET}" = "0" ]; then - # mark END, remove the log file - echo ${LOGEND} >> ${LOGFILE} - sleep 5 - rm -f ${LOGFILE} - else - # reserve the log - echo "docker run ${IMAGE_NAME} failed" >>${LOGFILE} - echo ${LOGEND} >> ${LOGFILE} - fi - exit ${RET} -} - -# run in lock -do_run() { - case ${ACTION} in - "install") - do_install - ;; - "upgrade") - do_install - ;; - esac -} - -usage() { - echo "usage: nextcloud sub-command" - echo "where sub-command is one of:" - echo " install Install the nextcloud" - echo " upgrade Upgrade the nextcloud" - echo " remove Remove the nextcloud" -} - -case ${ACTION} in - "install") - run_action - ;; - "upgrade") - run_action - ;; - "remove") - docker rm -f nextcloud - ;; - *) - usage - ;; -esac -