nastools: update ui

This commit is contained in:
jjm2473 2022-08-30 15:03:50 +08:00
parent 2688a307ae
commit 2a875d4f58
9 changed files with 195 additions and 450 deletions

View File

@ -2,12 +2,12 @@
include $(TOPDIR)/rules.mk
PKG_VERSION:=1.0.1-20220715
PKG_VERSION:=1.1.0-20220830
PKG_RELEASE:=
LUCI_TITLE:=LuCI support for nastools
LUCI_PKGARCH:=all
LUCI_DEPENDS:=+docker +luci-lib-iform
LUCI_DEPENDS:=+docker +luci-lib-taskd
define Package/luci-app-nastools/conffiles
/etc/config/nastools
@ -16,4 +16,3 @@ endef
include $(TOPDIR)/feeds/luci/luci.mk
# call BuildPackage - OpenWrt buildroot signature

View File

@ -1,290 +1,7 @@
local util = require "luci.util"
local http = require "luci.http"
local iform = require "luci.iform"
local jsonc = require "luci.jsonc"
module("luci.controller.nastools", package.seeall)
function index()
entry({"admin", "services", "nastools"}, call("redirect_index"), _("NasTools"), 30).dependent = true
entry({"admin", "services", "nastools", "pages"}, call("nastools_index")).leaf = true
entry({"admin", "services", "nastools", "form"}, call("nastools_form"))
entry({"admin", "services", "nastools", "submit"}, call("nastools_submit"))
entry({"admin", "services", "nastools", "log"}, call("nastools_log"))
end
local const_log_end = "XU6J03M6"
local appname = "nastools"
local page_index = {"admin", "services", "nastools", "pages"}
function redirect_index()
http.redirect(luci.dispatcher.build_url(unpack(page_index)))
end
function nastools_index()
luci.template.render("nastools/main", {prefix=luci.dispatcher.build_url(unpack(page_index))})
end
function nastools_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://github.com/jxxghp/nas-tools\" target=\"_blank\">NasTools</a>'
local schema = {
actions = actions,
containers = get_containers(data),
description = _("NasTools is a tools for resource aggregation running in NAS.")..access.." "..homepage,
title = _("NasTools")
}
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 = "NasTools 运行中"
else
status_value = "NasTools 未运行"
end
local status_c1 = {
labels = {
{
key = "状态:",
value = status_value
},
{
key = "访问:",
value = ""
}
},
description = "NasTools 的状态信息如下:",
title = "服务状态"
}
return status_c1
end
function main_container(data)
local main_c2 = {
properties = {
{
name = "http_port",
required = true,
title = "HTTP 端口",
type = "string"
},
{
name = "auto_upgrade",
required = true,
title = "自动更新",
type = "boolean"
},
{
name = "config_path",
required = true,
title = "配置路径:",
type = "string",
enum = dup_to_enums(data.blocks),
enumNames = dup_to_enums(data.blocks)
},
},
description = "请选择合适的配置路径进行安装:",
title = "服务操作"
}
return main_c2
end
function get_data()
local uci = require "luci.model.uci".cursor()
local default_path = ""
local blks = blocks()
if #blks > 0 then
default_path = blks[1] .. "/nastools"
end
local blk1 = {}
for _, val in pairs(blks) do
table.insert(blk1, val .. "/nastools")
end
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 http_port = tonumber(uci:get_first(appname, appname, "http_port", "3003"))
local data = {
http_port = http_port,
auto_upgrade = uci:get_first(appname, appname, "auto_upgrade", default_path) == "1",
config_path = uci:get_first(appname, appname, "config_path", default_path),
blocks = blk1,
container_install = container_install
}
return data
end
function nastools_submit()
local error = ""
local scope = ""
local success = 0
local result
local json_parse = jsonc.parse
local content = http.content()
local req = json_parse(content)
if req["$apply"] == "upgrade" then
result = install_upgrade_nastools(req)
elseif req["$apply"] == "install" then
result = install_upgrade_nastools(req)
elseif req["$apply"] == "restart" then
result = restart_nastools(req)
else
result = delete_nastools()
end
http.prepare_content("application/json")
local resp = {
error = error,
scope = scope,
success = success,
result = result,
}
http.write_json(resp)
end
function nastools_log()
iform.response_log("/var/log/"..appname..".log")
end
function install_upgrade_nastools(req)
local http_port = req["http_port"]
local auto_upgrade = req["auto_upgrade"]
local auto_upgrade_num
if auto_upgrade then
auto_upgrade_num = 1
else
auto_upgrade_num = 0
end
-- save config
local uci = require "luci.model.uci".cursor()
uci:tset(appname, "@"..appname.."[0]", {
http_port = http_port or "3003",
auto_upgrade = auto_upgrade_num,
config_path = req["config_path"],
})
uci:save(appname)
uci:commit(appname)
local exec_cmd = string.format("/usr/share/nastools/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_nastools()
local log = iform.exec_to_log("docker rm -f nastools")
local result = {
async = false,
log = log
}
return result
end
function restart_nastools()
local log = iform.exec_to_log("docker restart nastools")
local result = {
async = false,
log = log
}
return result
end
function blocks()
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
function dup_to_enums(a)
if #a == 0 then
return nil
end
local a2 = {}
for _, val in pairs(a) do
table.insert(a2, val)
end
return a2
entry({"admin", "services", "nastools"}, alias("admin", "services", "nastools", "config"), _("NasTools"), 30).dependent = true
entry({"admin", "services", "nastools", "config"}, cbi("nastools"))
end

View File

@ -0,0 +1,33 @@
--[[
LuCI - Lua Configuration Interface
]]--
local taskd = require "luci.model.tasks"
local m, s, o
m = taskd.docker_map("nastools", "nastools", "/usr/libexec/istorec/nastools.sh",
translate("NasTools"),
translate("NasTools is a tools for resource aggregation running in NAS.")
.. translate("Official website:") .. ' <a href=\"https://github.com/jxxghp/nas-tools\" target=\"_blank\">https://github.com/jxxghp/nas-tools</a>')
s = m:section(SimpleSection, translate("Service Status"), translate("NasTools status:"))
s:append(Template("nastools/status"))
s = m:section(TypedSection, "nastools", 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("Port").."<b>*</b>")
o.rmempty = false
o.default = "3003"
o.datatype = "port"
o = s:option(Value, "config_path", translate("Config path").."<b>*</b>")
o.rmempty = false
o.datatype = "string"
o = s:option(Flag, "auto_upgrade", translate("Auto update"))
o.default = 1
o.rmempty = false
return m

View File

@ -1,33 +0,0 @@
<%+header%>
<script>
(function(){
})();
</script>
<div id="app">
</div>
<script>
window.IstoreosFormConfig = {
getApi:"/cgi-bin/luci/admin/services/nastools/form/",
logApi:"/cgi-bin/luci/admin/services/nastools/log",
submitApi:"/cgi-bin/luci/admin/services/nastools/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.hostname+':'+resp.result.data.http_port+'" target="_blank">打开 NasTools</a>(默认用户名/密码admin/password';
} else {
c1.labels[idx].value = "安装并运行之后,才能用网页访问";
}
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/nastools.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"><%:NasTools is running%></button>
<% else %>
<button class="cbi-button cbi-button-negative" disabled="true"><%:NasTools is not running%></button>
<% end %>
</div>
</div>
<%
if container_running then
local port=util.trim(util.exec("/usr/libexec/istorec/nastools.sh port"))
if port == "" then
port="3003"
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 NasTools%>" onclick="window.open('http://'+location.hostname+':<%=port%>/', '_blank')">
</div>
</div>
<% end %>

View File

@ -1,9 +1,41 @@
msgid "NasTools"
msgstr "NasTool 工具集"
msgid ""
msgstr "Content-Type: text/plain; charset=UTF-8"
msgid "Access homepage:"
msgstr "访问主页"
msgid "Official website:"
msgstr "官方网站"
msgid "NasTools is a tools for resource aggregation running in NAS."
msgstr "NasTools 汇聚了电影搜索,下载,订阅,观看等等 NAS 功能的工具集合。"
msgstr "NasTools 汇聚了电影搜索,下载,订阅,观看等等 NAS 功能的工具集合。"
msgid "Config path"
msgstr "配置文件路径"
msgid "Port"
msgstr "端口"
msgid "Auto update"
msgstr "自动更新"
msgid "Service Status"
msgstr "服务状态"
msgid "NasTools status:"
msgstr "NasTools 的状态信息如下:"
msgid "Setup"
msgstr "安装配置"
msgid "The following parameters will only take effect during installation or upgrade:"
msgstr "以下参数只在安装或者升级时才会生效:"
msgid "Status"
msgstr "状态"
msgid "NasTools is running"
msgstr "NasTools 运行中"
msgid "NasTools is not running"
msgstr "NasTools 未运行"
msgid "Open NasTools"
msgstr "打开 NasTools"

View File

@ -1,4 +1,4 @@
config nastools
option 'config_path' ''
option 'config_path' '/root/nastools/config'
option 'http_port' '3003'
option 'auto_upgrade' '0'

View File

@ -0,0 +1,89 @@
#!/bin/sh
ACTION=${1}
shift 1
get_image() {
IMAGE_NAME="jxxghp/nas-tools"
}
do_install() {
get_image
echo "docker pull ${IMAGE_NAME}"
docker pull ${IMAGE_NAME}
docker rm -f nastools
do_install_detail
}
do_install_detail() {
local config=`uci get nastools.@nastools[0].config_path 2>/dev/null`
local port=`uci get nastools.@nastools[0].http_port 2>/dev/null`
local auto_update=`uci get nastools.@nastools[0].auto_upgrade 2>/dev/null`
if [ -z "$config" ]; then
echo "config path is empty!"
exit 1
fi
[ -z "$port" ] && port=3003
local cmd="docker run --restart=unless-stopped -d \
--hostname nastools \
-v \"$config:/config\" \
--dns=172.17.0.1 \
-p $port:3000 \
-e UMASK=000"
local tz="`cat /tmp/TZ`"
[ -z "$tz" ] || cmd="$cmd -e TZ=$tz"
if [ -n "$auto_update" ]; then
if [ "$auto_update" = 1 ]; then
cmd="$cmd -e NASTOOL_AUTO_UPDATE=true"
else
cmd="$cmd -e NASTOOL_AUTO_UPDATE=false"
fi
fi
cmd="$cmd --name nastools \"$IMAGE_NAME\""
echo "$cmd"
eval "$cmd"
}
usage() {
echo "usage: $0 sub-command"
echo "where sub-command is one of:"
echo " install Install the nastools"
echo " upgrade Upgrade the nastools"
echo " rm/start/stop/restart Remove/Start/Stop/Restart the nastools"
echo " status NasTools status"
echo " port NasTools port"
}
case ${ACTION} in
"install")
do_install
;;
"upgrade")
do_install
;;
"rm")
docker rm -f nastools
;;
"start" | "stop" | "restart")
docker ${ACTION} nastools
;;
"status")
docker ps --all -f 'name=nastools' --format '{{.State}}'
;;
"port")
docker ps --all -f 'name=nastools' --format '{{.Ports}}' | grep -om1 '0.0.0.0:[0-9]*' | sed 's/0.0.0.0://'
;;
*)
usage
exit 1
;;
esac

View File

@ -1,123 +0,0 @@
#!/bin/sh
# Author Xiaobao(xiaobao@linkease.com)
ACTION=${1}
WRLOCK=/var/lock/nastools.lock
LOGFILE=/var/log/nastools.log
LOGEND="XU6J03M6"
shift 1
IMAGE_NAME='jxxghp/nas-tools'
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() {
local CONFIG_PATH=`uci get nastools.@nastools[0].config_path 2>/dev/null`
local HTTP_PORT=`uci get nastools.@nastools[0].http_port 2>/dev/null`
local AUTO_UPDATE=`uci get nastools.@nastools[0].auto_upgrade 2>/dev/null`
if [ -z "${CONFIG_PATH}" ]; then
echo "config path is empty!" >${LOGFILE}
exit 1
fi
if [ -z "${HTTP_PORT}" ]; then
HTTP_PORT=3003
fi
if [ -z "${AUTO_UPDATE}" ]; then
AUTO_UPDATE=0
fi
UPDATE_BOOL=false
if [ "${AUTO_UPDATE}" = "1" ]; then
UPDATE_BOOL=true
fi
echo "docker pull ${IMAGE_NAME}" >${LOGFILE}
docker pull ${IMAGE_NAME} >>${LOGFILE} 2>&1
docker rm -f nastools
local mntv="/mnt:/mnt"
mountpoint -q /mnt && mntv="$mntv:rslave"
docker run -d \
--name=nastools \
--dns=172.17.0.1 \
--hostname nastools \
-p ${HTTP_PORT}:3000 \
-v ${CONFIG_PATH}:/config -v ${mntv} \
-e UMASK=000 \
-e NASTOOL_AUTO_UPDATE=${UPDATE_BOOL} \
--restart unless-stopped \
$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: wxedge sub-command"
echo "where sub-command is one of:"
echo " install Install the nastools"
echo " upgrade Upgrade the nastools"
echo " remove Remove the nastools"
}
case ${ACTION} in
"install")
run_action
;;
"upgrade")
run_action
;;
"remove")
docker rm -f nastools
;;
*)
usage
;;
esac