small-package/luci-app-store/luasrc/controller/store.lua

741 lines
23 KiB
Lua
Raw Normal View History

2021-10-05 23:44:29 +08:00
module("luci.controller.store", package.seeall)
local myopkg = "is-opkg"
2022-07-18 20:24:49 +08:00
local is_backup = "/usr/libexec/istore/backup"
2021-10-05 23:44:29 +08:00
local page_index = {"admin", "store", "pages"}
function index()
local function store_api(action, onlypost)
local e = entry({"admin", "store", action}, onlypost and post("store_action", {action = action}) or call("store_action", {action = action}))
e.dependent = false -- 父节点不是必须的
e.leaf = true -- 没有子节点
end
local action
entry({"admin", "store"}, call("redirect_index"), _("iStore"), 31)
entry({"admin", "store", "pages"}, call("store_index")).leaf = true
2021-12-01 20:32:24 +08:00
if nixio.fs.access("/usr/lib/lua/luci/view/store/main_dev.htm") then
entry({"admin", "store", "dev"}, call("store_dev")).leaf = true
end
2021-10-05 23:44:29 +08:00
entry({"admin", "store", "token"}, call("store_token"))
entry({"admin", "store", "log"}, call("store_log"))
entry({"admin", "store", "uid"}, call("action_user_id"))
entry({"admin", "store", "upload"}, post("store_upload"))
2021-11-30 20:26:09 +08:00
entry({"admin", "store", "check_self_upgrade"}, call("check_self_upgrade"))
entry({"admin", "store", "do_self_upgrade"}, post("do_self_upgrade"))
2023-07-31 23:36:39 +08:00
entry({"admin", "store", "toggle_docker"}, post("toggle_docker"))
2022-03-10 17:30:47 +08:00
2021-10-05 23:44:29 +08:00
for _, action in ipairs({"update", "install", "upgrade", "remove"}) do
store_api(action, true)
end
for _, action in ipairs({"status", "installed"}) do
store_api(action, false)
end
2022-07-18 20:24:49 +08:00
if nixio.fs.access("/usr/libexec/istore/backup") then
entry({"admin", "store", "get_support_backup_features"}, call("get_support_backup_features"))
entry({"admin", "store", "light_backup"}, post("light_backup"))
entry({"admin", "store", "get_light_backup_file"}, call("get_light_backup_file"))
entry({"admin", "store", "local_backup"}, post("local_backup"))
entry({"admin", "store", "light_restore"}, post("light_restore"))
entry({"admin", "store", "local_restore"}, post("local_restore"))
entry({"admin", "store", "get_backup_app_list_file_path"}, call("get_backup_app_list_file_path"))
entry({"admin", "store", "get_backup_app_list"}, call("get_backup_app_list"))
entry({"admin", "store", "get_available_backup_file_list"}, call("get_available_backup_file_list"))
entry({"admin", "store", "set_local_backup_dir_path"}, post("set_local_backup_dir_path"))
entry({"admin", "store", "get_local_backup_dir_path"}, call("get_local_backup_dir_path"))
entry({"admin", "store", "get_block_devices"}, call("get_block_devices"))
end
2021-10-05 23:44:29 +08:00
end
local function user_id()
local jsonc = require "luci.jsonc"
local json_parse = jsonc.parse
local fs = require "nixio.fs"
local data = fs.readfile("/etc/.app_store.id")
local id
if data ~= nil then
id = json_parse(data)
2021-10-08 20:29:31 +08:00
end
if id == nil then
fs.unlink("/etc/.app_store.id")
2021-10-05 23:44:29 +08:00
id = {arch="",uid=""}
end
id.version = (fs.readfile("/etc/.app_store.version") or "?"):gsub("[\r\n]", "")
return id
end
2023-07-31 23:36:39 +08:00
local function user_config()
local uci = require "luci.model.uci".cursor()
local data = {
hide_docker = uci:get("istore", "istore", "hide_docker") == "1",
channel = uci:get("istore", "istore", "channel")
}
return data
end
2022-06-02 20:21:36 +08:00
local function vue_lang()
local i18n = require("luci.i18n")
local lang = i18n.translate("istore_vue_lang")
if lang == "istore_vue_lang" or lang == "" then
lang = "en"
end
return lang
end
2022-07-18 20:24:49 +08:00
local function is_exec(cmd, async)
2021-11-30 20:26:09 +08:00
local nixio = require "nixio"
local os = require "os"
local fs = require "nixio.fs"
2021-12-21 20:33:16 +08:00
local rshift = nixio.bit.rshift
2021-11-30 20:26:09 +08:00
2021-12-01 20:32:24 +08:00
local oflags = nixio.open_flags("wronly", "creat")
2021-12-10 20:31:35 +08:00
local lock, code, msg = nixio.open("/var/lock/istore.lock", oflags)
if not lock then
return 255, "", "Open lock failed: " .. msg
end
2021-11-30 20:26:09 +08:00
-- Acquire lock
2021-12-10 20:31:35 +08:00
local stat, code, msg = lock:lock("tlock")
if not stat then
2021-11-30 20:26:09 +08:00
lock:close()
2021-12-10 20:31:35 +08:00
return 255, "", "Lock failed: " .. msg
end
2022-07-18 20:24:49 +08:00
if async then
cmd = "/etc/init.d/tasks task_add istore " .. luci.util.shellquote(cmd)
end
2021-12-10 20:31:35 +08:00
local r = os.execute(cmd .. " >/var/log/istore.stdout 2>/var/log/istore.stderr")
local e = fs.readfile("/var/log/istore.stderr")
local o = fs.readfile("/var/log/istore.stdout")
2021-11-30 20:26:09 +08:00
2021-12-10 20:31:35 +08:00
fs.unlink("/var/log/istore.stderr")
fs.unlink("/var/log/istore.stdout")
2021-11-30 20:26:09 +08:00
lock:lock("ulock")
lock:close()
2021-12-10 20:31:35 +08:00
e = e or ""
if r == 256 and e == "" then
2022-03-18 09:20:32 +08:00
e = "os.execute exit code 1"
2021-12-10 20:31:35 +08:00
end
2021-12-21 20:33:16 +08:00
return rshift(r,8), o or "", e or ""
2021-11-30 20:26:09 +08:00
end
2021-10-05 23:44:29 +08:00
function redirect_index()
luci.http.redirect(luci.dispatcher.build_url(unpack(page_index)))
end
function store_index()
2022-07-18 20:24:49 +08:00
local fs = require "nixio.fs"
local features = { "_lua_force_array_" }
if fs.access("/usr/libexec/istore/backup") then
features[#features+1] = "backup"
end
2022-08-03 20:23:12 +08:00
if luci.sys.call("which docker >/dev/null 2>&1") == 0 then
features[#features+1] = "docker"
end
2022-09-07 20:24:04 +08:00
if luci.sys.call("[ -d /ext_overlay ] >/dev/null 2>&1") == 0 then
features[#features+1] = "sandbox"
end
2023-07-31 23:36:39 +08:00
if luci.sys.call("[ -f /www/luci-static/resources/luci.js ] >/dev/null 2>&1") == 0 then
features[#features+1] = "luci-js"
end
luci.template.render("store/main", {prefix=luci.dispatcher.build_url(unpack(page_index)),id=user_id(),lang=vue_lang(),user_config=user_config(),features=features})
2021-10-05 23:44:29 +08:00
end
2021-12-01 20:32:24 +08:00
function store_dev()
2023-07-31 23:36:39 +08:00
luci.template.render("store/main_dev", {prefix=luci.dispatcher.build_url(unpack({"admin", "store", "dev"})),id=user_id(),lang=vue_lang(),user_config=user_config()})
2021-12-01 20:32:24 +08:00
end
2021-10-05 23:44:29 +08:00
function store_log()
local fs = require "nixio.fs"
local code = 0
2021-12-10 20:31:35 +08:00
local e = fs.readfile("/var/log/istore.stderr")
local o = fs.readfile("/var/log/istore.stdout")
2021-10-05 23:44:29 +08:00
if o ~= nil then
code = 206
end
luci.http.prepare_content("application/json")
luci.http.write_json({code=code,stdout=o or "",stderr=e or ""})
end
function action_user_id()
luci.http.prepare_content("application/json")
luci.http.write_json(user_id())
end
2021-11-30 20:26:09 +08:00
function check_self_upgrade()
local ret = {
code = 500,
msg = "Unknown"
}
local r,o,e = is_exec(myopkg .. " check_self_upgrade")
if r ~= 0 then
ret.msg = e
else
ret.code = o == "" and 304 or 200
2021-12-01 20:32:24 +08:00
ret.msg = o:gsub("[\r\n]", "")
2021-11-30 20:26:09 +08:00
end
luci.http.prepare_content("application/json")
luci.http.write_json(ret)
end
function do_self_upgrade()
local code, out, err, ret
code,out,err = is_exec(myopkg .. " do_self_upgrade")
ret = {
code = code,
stdout = out,
stderr = err
}
luci.http.prepare_content("application/json")
luci.http.write_json(ret)
end
2021-10-05 23:44:29 +08:00
-- Internal action function
local function _action(exe, cmd, ...)
2021-11-30 20:26:09 +08:00
2021-12-10 20:31:35 +08:00
local pkg = ""
for k, v in pairs({...}) do
2022-07-18 20:24:49 +08:00
pkg = pkg .. " " .. luci.util.shellquote(v)
2021-12-10 20:31:35 +08:00
end
2021-10-05 23:44:29 +08:00
2021-12-10 20:31:35 +08:00
local c = "%s %s %s" %{ exe, cmd, pkg }
2021-10-05 23:44:29 +08:00
2022-07-18 20:24:49 +08:00
return is_exec(c, true)
2021-10-05 23:44:29 +08:00
end
function store_action(param)
local metadir = "/usr/lib/opkg/meta"
local metapkgpre = "app-meta-"
2023-03-05 22:54:40 +08:00
local code, out, err, ret
2021-10-05 23:44:29 +08:00
local fs = require "nixio.fs"
local ipkg = require "luci.model.ipkg"
local jsonc = require "luci.jsonc"
local json_parse = jsonc.parse
local action = param.action or ""
if action == "status" then
local pkg = luci.http.formvalue("package")
local metapkg = metapkgpre .. pkg
local meta = {}
local metadata = fs.readfile(metadir .. "/" .. pkg .. ".json")
if metadata ~= nil then
meta = json_parse(metadata)
end
meta.installed = false
local status = ipkg.status(metapkg)
if next(status) ~= nil then
meta.installed=true
meta.time=tonumber(status[metapkg]["Installed-Time"])
end
ret = meta
elseif action == "installed" then
local itr = fs.dir(metadir)
local data = {}
if itr then
local pkg
for pkg in itr do
if pkg:match("^.*%.json$") then
2023-02-09 19:06:24 +08:00
local metadata = fs.readfile(metadir .. "/" .. pkg)
if metadata ~= nil then
local meta = json_parse(metadata)
local metapkg = metapkgpre .. meta.name
local status = ipkg.status(metapkg)
if next(status) ~= nil then
meta.time = tonumber(status[metapkg]["Installed-Time"])
data[#data+1] = meta
end
2021-10-25 18:05:09 +08:00
end
2021-10-05 23:44:29 +08:00
end
end
end
ret = data
else
local pkg = luci.http.formvalue("package")
2022-08-03 20:23:12 +08:00
local metapkg = pkg and (metapkgpre .. pkg) or ""
2021-10-05 23:44:29 +08:00
if action == "update" or pkg then
if action == "update" or action == "install" then
code, out, err = _action(myopkg, action, metapkg)
else
local meta = json_parse(fs.readfile(metadir .. "/" .. pkg .. ".json"))
2022-08-03 20:23:12 +08:00
local pkgs = {}
2021-10-05 23:44:29 +08:00
if action == "upgrade" then
2022-08-03 20:23:12 +08:00
pkgs = meta.depends
table.insert(pkgs, metapkg)
2021-12-03 20:31:31 +08:00
code, out, err = _action(myopkg, action, unpack(pkgs))
2021-10-05 23:44:29 +08:00
else -- remove
2022-08-03 20:23:12 +08:00
for _, dep in ipairs(meta.depends) do
2023-07-31 23:36:39 +08:00
if dep ~= "docker-deps" and dep ~= "luci-js-deps" then
2022-08-03 20:23:12 +08:00
pkgs[#pkgs+1] = dep
end
end
table.insert(pkgs, metapkg)
2021-12-03 20:31:31 +08:00
code, out, err = _action(myopkg, action, unpack(pkgs))
2021-10-05 23:44:29 +08:00
fs.unlink("/tmp/luci-indexcache")
end
end
else
code = 400
err = "package is null"
end
ret = {
code = code,
stdout = out,
stderr = err
}
end
luci.http.prepare_content("application/json")
luci.http.write_json(ret)
end
function store_token()
luci.http.prepare_content("application/json")
require "luci.template".render_string("{\"token\":\"<%=token%>\"}")
end
function store_upload()
local fd
local path
local finished = false
local tmpdir = "/tmp/is-root/tmp"
luci.http.setfilehandler(
function(meta, chunk, eof)
if not fd then
path = tmpdir .. "/" .. meta.file
nixio.fs.mkdirr(tmpdir)
fd = io.open(path, "w")
end
if chunk then
fd:write(chunk)
end
if eof then
fd:close()
finished = true
end
end
)
local code, out, err
out = ""
if finished then
if string.lower(string.sub(path, -4, -1)) == ".run" then
2022-07-18 20:24:49 +08:00
code, out, err = _action("sh", "-c", "ls -l \"%s\"; md5sum \"%s\" 2>/dev/null; chmod 755 \"%s\" && \"%s\"; RET=$?; rm -f \"%s\"; exit $RET" %{ path, path, path, path, path })
2021-10-05 23:44:29 +08:00
else
2022-07-18 20:24:49 +08:00
code, out, err = _action("sh", "-c", "opkg install \"%s\"; RET=$?; rm -f \"%s\"; exit $RET" %{ path, path })
2021-10-05 23:44:29 +08:00
end
else
code = 500
err = "upload failed!"
end
2022-07-18 20:24:49 +08:00
--nixio.fs.unlink(path)
2021-10-05 23:44:29 +08:00
local ret = {
code = code,
stdout = out,
stderr = err
}
luci.http.prepare_content("application/json")
luci.http.write_json(ret)
end
2022-03-10 17:30:47 +08:00
local function split(str,reps)
local resultStrList = {}
string.gsub(str,'[^'..reps..']+',function (w)
table.insert(resultStrList,w)
end)
return resultStrList
end
local function ltn12_popen(command)
local fdi, fdo = nixio.pipe()
local pid = nixio.fork()
if pid > 0 then
fdo:close()
local close
return function()
local buffer = fdi:read(2048)
local wpid, stat = nixio.waitpid(pid, "nohang")
if not close and wpid and stat == "exited" then
close = true
end
if buffer and #buffer > 0 then
return buffer
elseif close then
fdi:close()
return nil
end
end
elseif pid == 0 then
nixio.dup(fdo, nixio.stdout)
fdi:close()
fdo:close()
nixio.exec("/bin/sh", "-c", command)
end
end
-- call get_support_backup_features
function get_support_backup_features()
local jsonc = require "luci.jsonc"
local error_ret = {code = 500, msg = "Unknown"}
2022-07-18 20:24:49 +08:00
local success_ret = {code = 200, msg = "Unknown"}
local r,o,e = is_exec(is_backup .. " get_support_backup_features")
2022-03-10 17:30:47 +08:00
if r ~= 0 then
error_ret.msg = e
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
else
success_ret.code = 200
success_ret.msg = jsonc.stringify(split(o,'\n'))
luci.http.prepare_content("application/json")
luci.http.write_json(success_ret)
end
end
-- post light_backup
function light_backup()
local jsonc = require "luci.jsonc"
local error_ret = {code = 500, msg = "Unknown"}
local success_ret = {code = 200,msg = "Unknown"}
2022-07-18 20:24:49 +08:00
local r,o,e = is_exec(is_backup .. " backup")
2022-03-10 17:30:47 +08:00
if r ~= 0 then
error_ret.msg = e
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
else
success_ret.code = 200
success_ret.msg = o:gsub("[\r\n]", "")
luci.http.prepare_content("application/json")
luci.http.write_json(success_ret)
end
end
-- call get_light_backup_file
function get_light_backup_file()
local light_backup_cmd = "tar -c %s | gzip 2>/dev/null"
local loght_backup_filelist = "/etc/istore/app.list"
local reader = ltn12_popen(light_backup_cmd:format(loght_backup_filelist))
luci.http.header('Content-Disposition', 'attachment; filename="light-backup-%s-%s.tar.gz"' % {
luci.sys.hostname(), os.date("%Y-%m-%d")})
luci.http.prepare_content("application/x-targz")
luci.ltn12.pump.all(reader, luci.http.write)
end
local function update_local_backup_path(path)
local uci = require "uci"
local fs = require "nixio.fs"
local x = uci.cursor()
local local_backup_path
if fs.access("/etc/config/istore") then
local_backup_path = x:get("istore","istore","local_backup_path")
else
--create config file
local f=io.open("/etc/config/istore","a+")
f:write("config istore \'istore\'\n\toption local_backup_path \'\'")
f:flush()
f:close()
end
if path ~= local_backup_path then
-- set uci config
x:set("istore","istore","local_backup_path",path)
2023-07-31 23:36:39 +08:00
x:commit("istore")
2022-03-10 17:30:47 +08:00
end
end
-- post local_backup
function local_backup()
local code, out, err, ret
local error_ret
local path = luci.http.formvalue("path")
if path ~= "" then
-- judge path
code,out,err = is_exec("findmnt -T " .. path .. " -o TARGET|sed -n 2p")
if out:gsub("[\r\n]", "") == "/" or out:gsub("[\r\n]", "") == "/tmp" then
-- error
2022-07-18 20:24:49 +08:00
error_ret = {code = 500, stderr = "Path Error,Can not be / or tmp."}
2022-03-10 17:30:47 +08:00
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
else
-- update local backup path
update_local_backup_path(path)
2022-07-18 20:24:49 +08:00
code,out,err = _action(is_backup, "backup", path)
2022-03-10 17:30:47 +08:00
ret = {
code = code,
stdout = out,
stderr = err
}
luci.http.prepare_content("application/json")
luci.http.write_json(ret)
end
else
-- error
2022-07-18 20:24:49 +08:00
error_ret = {code = 500, stderr = "Path Unknown"}
2022-03-10 17:30:47 +08:00
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
end
end
-- post light_restore
function light_restore()
local fd
local path
local finished = false
local tmpdir = "/tmp/"
luci.http.setfilehandler(
function(meta, chunk, eof)
if not fd then
path = tmpdir .. "/" .. meta.file
fd = io.open(path, "w")
end
if chunk then
fd:write(chunk)
end
if eof then
fd:close()
finished = true
end
end
)
local code, out, err, ret
if finished then
is_exec("rm /etc/istore/app.list;tar -xzf " .. path .. " -C /")
2022-07-18 20:24:49 +08:00
nixio.fs.unlink(path)
2022-03-10 17:30:47 +08:00
if nixio.fs.access("/etc/istore/app.list") then
2022-07-18 20:24:49 +08:00
code,out,err = _action(is_backup, "restore")
2022-03-10 17:30:47 +08:00
ret = {
code = code,
stdout = out,
stderr = err
}
luci.http.prepare_content("application/json")
luci.http.write_json(ret)
else
2022-07-18 20:24:49 +08:00
local error_ret = {code = 500, stderr = "File is error!"}
2022-03-10 17:30:47 +08:00
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
end
else
2022-07-18 20:24:49 +08:00
ret = {code = 500, stderr = "upload failed!"}
2022-03-10 17:30:47 +08:00
luci.http.prepare_content("application/json")
luci.http.write_json(ret)
end
end
-- post local_restore
function local_restore()
local path = luci.http.formvalue("path")
local code, out, err, ret
if path ~= "" then
2022-07-18 20:24:49 +08:00
code,out,err = _action(is_backup, "restore", path)
2022-03-10 17:30:47 +08:00
ret = {
code = code,
stdout = out,
stderr = err
}
luci.http.prepare_content("application/json")
luci.http.write_json(ret)
else
-- error
2022-07-18 20:24:49 +08:00
error_ret = {code = 500, stderr = "Path Unknown"}
2022-03-10 17:30:47 +08:00
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
end
end
-- call get_backup_app_list_file_path
function get_backup_app_list_file_path()
local jsonc = require "luci.jsonc"
local error_ret = {code = 500, msg = "Unknown"}
local success_ret = {code = 200,msg = "Unknown"}
2022-07-18 20:24:49 +08:00
local r,o,e = is_exec(is_backup .. " get_backup_app_list_file_path")
2022-03-10 17:30:47 +08:00
if r ~= 0 then
error_ret.msg = e
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
else
success_ret.code = 200
success_ret.msg = o:gsub("[\r\n]", "")
luci.http.prepare_content("application/json")
luci.http.write_json(success_ret)
end
end
-- call get_backup_app_list
function get_backup_app_list()
local jsonc = require "luci.jsonc"
local error_ret = {code = 500, msg = "Unknown"}
local success_ret = {code = 200,msg = "Unknown"}
2022-07-18 20:24:49 +08:00
local r,o,e = is_exec(is_backup .. " get_backup_app_list")
2022-03-10 17:30:47 +08:00
if r ~= 0 then
error_ret.msg = e
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
else
success_ret.code = 200
success_ret.msg = jsonc.stringify(split(o,'\n'))
luci.http.prepare_content("application/json")
luci.http.write_json(success_ret)
end
end
-- call get_available_backup_file_list
function get_available_backup_file_list()
local jsonc = require "luci.jsonc"
local error_ret = {code = 500, msg = "Unknown"}
local success_ret = {code = 200,msg = "Unknown"}
local path = luci.http.formvalue("path")
local r,o,e
if path ~= "" then
-- update local backup path
update_local_backup_path(path)
2022-07-18 20:24:49 +08:00
r,o,e = is_exec(is_backup .. " get_available_backup_file_list " .. path)
2022-03-10 17:30:47 +08:00
if r ~= 0 then
error_ret.msg = e
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
else
success_ret.code = 200
success_ret.msg = jsonc.stringify(split(o,'\n'))
luci.http.prepare_content("application/json")
luci.http.write_json(success_ret)
end
else
-- set error code
error_ret.msg = "Path Unknown"
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
end
end
-- post set_local_backup_dir_path
function set_local_backup_dir_path()
local path = luci.http.formvalue("path")
2023-07-31 23:36:39 +08:00
local success_ret = {code = 200, msg = "Success"}
2022-03-10 17:30:47 +08:00
local error_ret = {code = 500, msg = "Unknown"}
if path ~= "" then
-- update local backup path
update_local_backup_path(path)
luci.http.prepare_content("application/json")
luci.http.write_json(success_ret)
else
-- set error code
error_ret.msg = "Path Unknown"
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
end
end
-- call get_local_backup_dir_path
function get_local_backup_dir_path()
local uci = require "uci"
local fs = require "nixio.fs"
local x = uci.cursor()
local local_backup_path = nil
local success_ret = {code = 200,msg = "Unknown"}
local error_ret = {code = 500, msg = "Path Unknown"}
if fs.access("/etc/config/istore") then
local_backup_path = x:get("istore","istore","local_backup_path")
if local_backup_path == nil then
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
else
success_ret.msg = local_backup_path:gsub("[\r\n]", "")
luci.http.prepare_content("application/json")
luci.http.write_json(success_ret)
end
else
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
end
end
2022-04-25 16:51:47 +08:00
-- copy from /usr/lib/lua/luci/model/diskman.lua
local function byte_format(byte)
local suff = {"B", "KB", "MB", "GB", "TB"}
for i=1, 5 do
if byte > 1024 and i < 5 then
byte = byte / 1024
else
return string.format("%.2f %s", byte, suff[i])
end
end
end
-- copy from /usr/libexec/rpcd/luci
local function getBlockDevices()
local fs = require "nixio.fs"
local block = io.popen("/sbin/block info", "r")
if block then
local rv = {}
while true do
local ln = block:read("*l")
if not ln then
break
end
local dev = ln:match("^/dev/(.-):")
if dev then
local s = tonumber((fs.readfile("/sys/class/block/" .. dev .."/size")))
local e = {
dev = "/dev/" .. dev,
size = s and byte_format(s * 512)
}
local key, val = { }
for key, val in ln:gmatch([[(%w+)="(.-)"]]) do
e[key:lower()] = val
end
rv[dev] = e
end
end
block:close()
return rv
else
return
end
end
function get_block_devices()
local error_ret = {code = 500, msg = "Unable to execute block utility"}
local devices = getBlockDevices()
if devices ~= nil then
luci.http.prepare_content("application/json")
luci.http.write_json({code = 200, data = devices})
else
luci.http.prepare_content("application/json")
luci.http.write_json(error_ret)
end
end
2023-07-31 23:36:39 +08:00
function toggle_docker()
local uci = require "luci.model.uci".cursor()
local hide = luci.http.formvalue("hide")
uci:set("istore", "istore", "hide_docker", hide == "true" and "1" or "0")
uci:commit("istore")
luci.http.prepare_content("application/json")
luci.http.write_json({code = 200, msg = "Success"})
end