mirror of
https://github.com/caiwx86/small-packages.git
synced 2026-10-31 03:55:13 +08:00
update 2026-09-26 11:07:27
This commit is contained in:
1 parent
7b88b273d0
commit
97ae9f68da
4898 files changed
-1712274
No files matched your search
@@ -1,98 +0,0 @@
|
||||
|
||||
module("luci.controller.tasks-lib", package.seeall)
|
||||
|
||||
|
||||
function index()
|
||||
entry({"admin", "system", "tasks"}, call("tasks_ping")).dependent=false -- just for compatible
|
||||
entry({"admin", "system", "tasks", "status"}, call("tasks_status")).dependent=false
|
||||
entry({"admin", "system", "tasks", "log"}, call("tasks_log")).dependent=false
|
||||
entry({"admin", "system", "tasks", "stop"}, post("tasks_stop")).dependent=false
|
||||
end
|
||||
|
||||
local util = require "luci.util"
|
||||
local jsonc = require "luci.jsonc"
|
||||
local ltn12 = require "luci.ltn12"
|
||||
|
||||
local taskd = require "luci.model.tasks"
|
||||
|
||||
function tasks_ping()
|
||||
luci.http.prepare_content("application/json")
|
||||
luci.http.write_json({})
|
||||
end
|
||||
|
||||
function tasks_status()
|
||||
local data = taskd.status(luci.http.formvalue("task_id"))
|
||||
luci.http.prepare_content("application/json")
|
||||
luci.http.write_json(data)
|
||||
end
|
||||
|
||||
function tasks_log()
|
||||
local wait = 107
|
||||
local task_id = luci.http.formvalue("task_id")
|
||||
local offset = luci.http.formvalue("offset")
|
||||
offset = offset and tonumber(offset) or 0
|
||||
local logpath = "/var/log/tasks/"..task_id..".log"
|
||||
local i
|
||||
local logfd = io.open(logpath, "rb")
|
||||
if logfd == nil then
|
||||
luci.http.status(404)
|
||||
luci.http.write("log not found")
|
||||
return
|
||||
end
|
||||
|
||||
local size = logfd:seek("end")
|
||||
|
||||
if size < offset then
|
||||
luci.http.status(205, "Reset Content")
|
||||
luci.http.write("reset offset")
|
||||
return
|
||||
end
|
||||
|
||||
i = 0
|
||||
while (i < wait)
|
||||
do
|
||||
if size > offset then
|
||||
break
|
||||
end
|
||||
nixio.nanosleep(0, 10000000) -- sleep 10ms
|
||||
size = logfd:seek("end")
|
||||
i = i+1
|
||||
end
|
||||
if i == wait then
|
||||
logfd:close()
|
||||
luci.http.status(204)
|
||||
luci.http.prepare_content("application/octet-stream")
|
||||
return
|
||||
end
|
||||
logfd:seek("set", offset)
|
||||
|
||||
local write_log = function()
|
||||
local buffer = logfd:read(4096)
|
||||
if buffer and #buffer > 0 then
|
||||
return buffer
|
||||
else
|
||||
logfd:close()
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
luci.http.prepare_content("application/octet-stream")
|
||||
|
||||
if logfd then
|
||||
ltn12.pump.all(write_log, luci.http.write)
|
||||
end
|
||||
end
|
||||
|
||||
function tasks_stop()
|
||||
local sys = require("luci.sys")
|
||||
local task_id = luci.http.formvalue("task_id") or ""
|
||||
if task_id == "" then
|
||||
luci.http.status(400)
|
||||
luci.http.write("task_id is empty")
|
||||
return
|
||||
end
|
||||
if sys.call("/etc/init.d/tasks task_del "..task_id.." >/dev/null 2>&1") ~= 0 then
|
||||
nixio.nanosleep(2, 10000000)
|
||||
end
|
||||
luci.http.status(204)
|
||||
end
|
||||
@@ -1,100 +0,0 @@
|
||||
local util = require "luci.util"
|
||||
local jsonc = require "luci.jsonc"
|
||||
|
||||
local taskd = {}
|
||||
|
||||
local function output(data)
|
||||
local ret={}
|
||||
ret.running=data.running
|
||||
if not data.running then
|
||||
ret.exit_code=data.exit_code
|
||||
if nil == ret.exit_code then
|
||||
if data["data"] and data["data"]["exit_code"] and data["data"]["exit_code"] ~= "" then
|
||||
ret.exit_code=tonumber(data["data"]["exit_code"])
|
||||
else
|
||||
ret.exit_code=143
|
||||
end
|
||||
end
|
||||
end
|
||||
ret.command=data["command"] and data["command"][4] or '#'
|
||||
if data["data"] then
|
||||
ret.start=tonumber(data["data"]["start"])
|
||||
if not data.running and data["data"]["stop"] then
|
||||
ret.stop=tonumber(data["data"]["stop"])
|
||||
end
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
taskd.status = function (task_id)
|
||||
task_id = task_id or ""
|
||||
local data = util.trim(util.exec("/etc/init.d/tasks task_status "..task_id.." 2>/dev/null")) or ""
|
||||
if data ~= "" then
|
||||
data = jsonc.parse(data)
|
||||
else
|
||||
if task_id == "" then
|
||||
data = {}
|
||||
else
|
||||
data = {running=false, exit_code=404}
|
||||
end
|
||||
end
|
||||
if task_id ~= "" then
|
||||
return output(data)
|
||||
end
|
||||
local ary={}
|
||||
for k, v in pairs(data) do
|
||||
ary[k] = output(v)
|
||||
end
|
||||
return ary
|
||||
end
|
||||
|
||||
taskd.docker_map = function(config, task_id, script_path, title, desc)
|
||||
require("luci.cbi")
|
||||
require("luci.http")
|
||||
require("luci.sys")
|
||||
local translate = require("luci.i18n").translate
|
||||
local m
|
||||
m = luci.cbi.Map(config, title, desc)
|
||||
m.template = "tasks/docker"
|
||||
-- hide default buttons
|
||||
m.pageaction = false
|
||||
-- we want hook 'on_after_apply' works, 'apply_on_parse' can be true (rollback) or false (no rollback),
|
||||
-- but 'apply_on_parse' must be true for luci 17.01 and below
|
||||
m.apply_on_parse = true
|
||||
m.script_path = script_path
|
||||
m.task_id = task_id
|
||||
m.auto_show_task = true
|
||||
m.on_before_apply = function(self)
|
||||
if self.uci.rollback then
|
||||
-- luci 18.06+ has 'rollback' function
|
||||
-- rollback dialog will show because 'apply_on_parse' is true,
|
||||
-- hide rollback dialog by hook 'apply' function
|
||||
local apply = self.uci.apply
|
||||
self.uci.apply = function(uci, rollback)
|
||||
apply(uci, false)
|
||||
end
|
||||
end
|
||||
end
|
||||
m.on_after_apply = function(self)
|
||||
local cmd
|
||||
local action = luci.http.formvalue("cbi.apply") or "null"
|
||||
if "upgrade" == action or "install" == action
|
||||
or "start" == action or "stop" == action or "restart" == action or "rm" == action then
|
||||
cmd = string.format("\"%s\" %s", script_path, action)
|
||||
end
|
||||
if cmd then
|
||||
if luci.sys.call("/etc/init.d/tasks task_add " .. task_id .. " " .. luci.util.shellquote(cmd) .. " >/dev/null 2>&1") ~= 0 then
|
||||
self.task_start_failed = true
|
||||
self.message = translate("Config saved, but apply failed")
|
||||
end
|
||||
else
|
||||
self.message = translate("Unknown command: ") .. action
|
||||
end
|
||||
if self.message then
|
||||
self.auto_show_task = false
|
||||
end
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
return taskd
|
||||
@@ -1,56 +0,0 @@
|
||||
|
||||
<% if self.task_start_failed then %>
|
||||
<div class="alert-message warning"><%:Another task running, try again later.%> <a href="javascript:void(taskd.show_log('<%=self.task_id%>'))"><%:Click here to check running task%></a></div>
|
||||
<% end %>
|
||||
|
||||
<%+cbi/map%>
|
||||
<%
|
||||
local task_running = false
|
||||
local taskd = require "luci.model.tasks"
|
||||
local status = taskd.status(self.task_id)
|
||||
task_running = status.running
|
||||
-%>
|
||||
<div class="cbi-page-actions control-group">
|
||||
<%
|
||||
if not task_running then
|
||||
%>
|
||||
<%
|
||||
local util = require "luci.util"
|
||||
local container_status = util.trim(util.exec(self.script_path.." status"))
|
||||
local container_install = (string.len(container_status) > 0)
|
||||
local container_running = container_status == "running"
|
||||
if container_install then
|
||||
-%>
|
||||
<input class="btn cbi-button cbi-button-apply" type="button" value="<%:Upgrade%>/<%:Apply%>" onclick="cbi_submit(this, 'cbi.apply', 'upgrade')" />
|
||||
<%
|
||||
if container_running then
|
||||
-%>
|
||||
<input class="btn cbi-button cbi-button-remove" type="button" value="<%:Stop%>" onclick="cbi_submit(this, 'cbi.apply', 'stop')" />
|
||||
|
||||
<input class="btn cbi-button cbi-button-reload" type="button" value="<%:Restart%>" onclick="cbi_submit(this, 'cbi.apply', 'restart')" />
|
||||
<% else %>
|
||||
<input class="btn cbi-button cbi-button-apply" type="button" value="<%:Start%>" onclick="cbi_submit(this, 'cbi.apply', 'start')" />
|
||||
|
||||
<input class="btn cbi-button cbi-button-remove" type="button" value="<%:Remove%>" onclick="cbi_submit(this, 'cbi.apply', 'rm')" />
|
||||
<% end
|
||||
else %>
|
||||
<input class="btn cbi-button cbi-button-apply" type="button" value="<%:Install%>" onclick="cbi_submit(this, 'cbi.apply', 'install')" />
|
||||
<% end
|
||||
else
|
||||
%>
|
||||
<input class="btn cbi-button cbi-button-apply" type="button" value="<%:Task Running%>…" onclick="taskd.show_log('<%=self.task_id%>')" />
|
||||
<%
|
||||
end
|
||||
%>
|
||||
</div>
|
||||
|
||||
<%+tasks/embed%>
|
||||
<%
|
||||
if self.auto_show_task and task_running then
|
||||
-%>
|
||||
<script>
|
||||
taskd.show_log("<%=self.task_id%>");
|
||||
</script>
|
||||
<%
|
||||
end
|
||||
%>
|
||||
@@ -1,40 +0,0 @@
|
||||
<%+xterm/embed%>
|
||||
<link rel="stylesheet" href="<%=resource%>/tasks/tasks.css<%# ?v=PKG_VERSION %>">
|
||||
<script src="<%=resource%>/tasks/tasks.js<%# ?v=PKG_VERSION %>"></script>
|
||||
<%
|
||||
local i18n = {}
|
||||
local function tr(str)
|
||||
i18n[str]=translate(str)
|
||||
end
|
||||
tr("Stop running task?")
|
||||
tr("Stopped at:")
|
||||
tr("Finished at:")
|
||||
tr("Failed at:")
|
||||
tr("Lost login status")
|
||||
tr("Fetch log failed, retrying...")
|
||||
tr("Task does not exist or has been deleted")
|
||||
-%>
|
||||
<script>
|
||||
window.taskd.csrfToken="<%=token%>";
|
||||
window.taskd.i18n=<% luci.http.write_json(i18n) %>;
|
||||
window.taskd.dialog_template=`
|
||||
<div id="tasks_dialog">
|
||||
<div class="dialog-title-bar">
|
||||
<span class="dialog-title" id="tasks_id"></span>
|
||||
<span class="dialog-icons">
|
||||
<span class="dialog-icon dialog-icon-close" title="<%:Stop and Remove%>"></span>
|
||||
<span class="dialog-icon dialog-icon-min" title="<%:Hide%>"></span>
|
||||
</span>
|
||||
</div>
|
||||
<div class="dialog-content">
|
||||
<div id="tasks_xterm_log"></div>
|
||||
<div id="tasks_result_mask">
|
||||
<span class="tasks_result_success"><%:Task execution successful !%></span>
|
||||
<span class="tasks_result_failed"><%:Task execution failed !%></span>
|
||||
<br/>
|
||||
<span><%:Click the button in the upper right corner of the dialog box to close it, or click here to view the log%></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
</script>
|
||||
Reference in new issue
Block a user