add nas-tools

This commit is contained in:
janson 2022-07-01 14:11:15 +08:00
parent a310263007
commit 762c1d880b
8 changed files with 486 additions and 13 deletions

View File

@ -0,0 +1,19 @@
include $(TOPDIR)/rules.mk
PKG_VERSION:=1.0.0
PKG_RELEASE:=20220629
LUCI_TITLE:=LuCI support for nastools
LUCI_PKGARCH:=all
LUCI_DEPENDS:=+docker +luci-lib-iform
define Package/luci-app-nastools/conffiles
/etc/config/nastools
endef
include $(TOPDIR)/feeds/luci/luci.mk
# call BuildPackage - OpenWrt buildroot signature

View File

@ -0,0 +1,290 @@
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_update",
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_update = false,
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_update = req["auto_update"]
local auto_update_num
if auto_update then
auto_update_num = 1
else
auto_update_num = 0
end
-- save config
local uci = require "luci.model.uci".cursor()
uci:tset(appname, "@"..appname.."[0]", {
http_port = http_port or "3003",
auto_update = auto_update_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
end

View File

@ -0,0 +1,33 @@
<%+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.host+':'+resp.result.data.http_port+'" target="_blank">NasTools HTTP 链接</a>';
} 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,9 @@
msgid "NasTools"
msgstr "NasTool 工具集"
msgid "access homepage: "
msgstr "访问主页:"
msgid "NasTools is a tools for resource aggregation running in NAS."
msgstr "NasTools 汇聚了电影搜索,下载,订阅,观看等等 NAS 功能的工具集合。"

View File

@ -0,0 +1,5 @@
config nastools
option 'config_path' ''
option 'http_port' '3003'
option 'auto_upgrade' '0'

View File

@ -0,0 +1,122 @@
#!/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].https_port 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 \
--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

View File

@ -1,10 +1,15 @@
#!/bin/sh
# run in router
APPNAME=$1
mkdir -p /usr/lib/lua/luci/view/ubuntu
cp ./luasrc/controller/ubuntu.lua /usr/lib/lua/luci/controller/
cp ./luasrc/view/ubuntu/* /usr/lib/lua/luci/view/ubuntu/
if [ -z "${APPNAME}" ]; then
APPNAME=ubuntu
fi
mkdir -p /usr/lib/lua/luci/view/${APPNAME}
cp ./luasrc/controller/${APPNAME}.lua /usr/lib/lua/luci/controller/
cp ./luasrc/view/${APPNAME}/* /usr/lib/lua/luci/view/${APPNAME}/
cp -rf ./root/* /
rm -rf /tmp/luci-*

View File

@ -1,10 +0,0 @@
#!/bin/sh
# run in router
mkdir -p /usr/lib/lua/luci/view/wxedge
cp ./luasrc/controller/wxedge.lua /usr/lib/lua/luci/controller/
cp ./luasrc/view/wxedge/* /usr/lib/lua/luci/view/wxedge/
cp -rf ./root/* /
rm -rf /tmp/luci-*