nextcloud: add config_path option

This commit is contained in:
jjm2473 2022-08-30 13:19:20 +08:00
parent 99ff9d957f
commit d0d7dcd49b
10 changed files with 195 additions and 393 deletions

View File

@ -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

View File

@ -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 = '<a href=\"https://nextcloud.com/\" target=\"_blank\">https://nextcloud.com/</a>'
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

View File

@ -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:") .. ' <a href=\"https://nextcloud.com/\" target=\"_blank\">https://nextcloud.com/</a>')
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").."<b>*</b>")
o.rmempty = false
o.default = "8082"
o.datatype = "port"
o = s:option(Value, "config_path", translate("Config path").."<b>*</b>")
o.rmempty = false
o.default = "/root/nextcloud/config"
o.datatype = "string"
return m

View File

@ -1,34 +0,0 @@
<%+header%>
<script>
(function(){
})();
</script>
<div id="app">
</div>
<script>
window.IstoreosFormConfig = {
getApi:"/cgi-bin/luci/admin/services/nextcloud/form/",
logApi:"/cgi-bin/luci/admin/services/nextcloud/log",
submitApi:"/cgi-bin/luci/admin/services/nextcloud/submit",
getHook:function(resp){
var c1 = resp.result.schema.containers[0];
var idx = 1;
if (resp.result.data.container_install) {
c1.labels[idx].value = '<a href="http://'+location.host+':'+resp.result.data.port+'" target="_blank">Nextcloud 链接</a>';
} else {
c1.labels[idx].value = "安装并运行之后,才能用网页访问";
}
// console.log("resp=", data);
return resp;
},
submitHook:function(params){
return params;
}
}
</script>
<script type="module" crossorigin src="/luci-static/iform/1.0/index.js?v=<%=math.random(1,100000)%>"></script>
<link rel="stylesheet" href="/luci-static/iform/1.0/style.css?v=<%=math.random(1,100000)%>">
<%+footer%>

View File

@ -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"
-%>
<div class="cbi-value">
<label class="cbi-value-title"><%:Status%></label>
<div class="cbi-value-field">
<% if container_running then %>
<button class="cbi-button cbi-button-success" disabled="true"><%:Nextcloud is running%></button>
<% else %>
<button class="cbi-button cbi-button-negative" disabled="true"><%:Nextcloud is not running%></button>
<% end %>
</div>
</div>
<%
if container_running then
local port=util.trim(util.exec("/usr/libexec/istorec/nextcloud.sh port"))
if port == "" then
port="8082"
end
-%>
<div class="cbi-value cbi-value-last">
<label class="cbi-value-title">&nbsp;</label>
<div class="cbi-value-field">
<input type="button" class="btn cbi-button cbi-button-apply" name="start" value="<%:Open Nextcloud%>" onclick="window.open('http://'+location.hostname+':<%=port%>/', '_blank')">
</div>
</div>
<% end %>

View File

@ -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"

View File

@ -1,3 +1,3 @@
config nextcloud
option 'port' '8082'
option 'enabled' '1'
option 'config_path' '/root/nextcloud/config'

View File

@ -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

View File

@ -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

View File

@ -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