add luci_app_wxedge

This commit is contained in:
YGAS 2021-11-01 14:27:46 +08:00
parent 93e66347a5
commit 37007b7466
7 changed files with 376 additions and 0 deletions

View File

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

View File

@ -0,0 +1,140 @@
module("luci.controller.wxedge", package.seeall)
function index()
entry({'admin', 'services', 'wxedge'}, alias('admin', 'services', 'wxedge', 'client'), _('wxedge'), 10)
entry({"admin", "services", "wxedge",'client'}, cbi("wxedge/status"), nil).leaf = true
entry({"admin", "services", "wxedge","status"}, call("get_container_status"))
entry({"admin", "services", "wxedge","stop"}, post("stop_container"))
entry({"admin", "services", "wxedge","start"}, post("start_container"))
entry({"admin", "services", "wxedge","install"}, post("install_container"))
entry({"admin", "services", "wxedge","uninstall"}, post("uninstall_container"))
end
local sys = require "luci.sys"
local uci = require "luci.model.uci".cursor()
local keyword = "wxedge"
local util = require("luci.util")
local docker = require "luci.model.docker"
function container_status()
local docker_path = util.exec("which docker")
local docker_install = (string.len(docker_path) > 0)
local docker_running = util.exec("ps | grep dockerd | grep -v 'grep' | wc -l")
local container_id = util.trim(util.exec("docker ps -aqf 'name="..keyword.."'"))
local container_install = (string.len(container_id) > 0)
local container_running = container_install and (string.len(util.trim(util.exec("docker ps -qf 'id="..container_id.."'"))) > 0)
local status = {
docker_install = docker_install,
docker_start = docker_running,
container_id = container_id,
container_port = (18888),
container_install = container_install,
container_running = container_running,
cache_path = uci:get_first(keyword, keyword, "cache_path", "/wxedge"),
}
return status
end
function get_container_status()
local status = container_status()
luci.http.prepare_content("application/json")
luci.http.write_json(status)
end
function stop_container()
local status = container_status()
local container_id = status.container_id
util.exec("docker stop '"..container_id.."'")
end
function start_container()
local status = container_status()
local container_id = status.container_id
util.exec("docker start '"..container_id.."'")
end
function install_container()
local image = util.exec("sh /usr/share/wxedge/install.sh -l")
local cache_path = luci.http.formvalue("cache")
uci:tset(keyword, "@"..keyword.."[0]", {
cache_path = cache_path or "/wxedge",
})
uci:save(keyword)
uci:commit(keyword)
local pull_image = function(image)
docker:append_status("Images: " .. "pulling" .. " " .. image .. "...\n")
local dk = docker.new()
local res = dk.images:create({query = {fromImage=image}}, docker.pull_image_show_status_cb)
if res and res.code and res.code == 200 and (res.body[#res.body] and not res.body[#res.body].error and res.body[#res.body].status and (res.body[#res.body].status == "Status: Downloaded newer image for ".. image or res.body[#res.body].status == "Status: Image is up to date for ".. image)) then
docker:append_status("done\n")
else
res.code = (res.code == 200) and 500 or res.code
docker:append_status("code:" .. res.code.." ".. (res.body[#res.body] and res.body[#res.body].error or (res.body.message or res.message)).. "\n")
end
end
local install_wxedge = function()
local os = require "os"
local fs = require "nixio.fs"
local c = ("sh /usr/share/wxedge/install.sh -i >/tmp/log/wxedge.stdout 2>/tmp/log/wxedge.stderr")
-- docker:append_status(c)
local r = os.execute(c)
local e = fs.readfile("/tmp/log/wxedge.stderr")
local o = fs.readfile("/tmp/log/wxedge.stdout")
fs.unlink("/tmp/log/wxedge.stderr")
fs.unlink("/tmp/log/wxedge.stdout")
if r == 0 then
docker:append_status(o)
else
docker:append_status( e )
end
end
-- local status = {
-- shell = shell,
-- image_name = image,
-- }
-- luci.http.prepare_content("application/json")
-- luci.http.write_json(status)
if image then
docker:write_status("wxedge installing\n")
pull_image(image)
install_wxedge()
else
docker:write_status("wxedge image not defined!\n")
end
end
function uninstall_container()
local status = container_status()
local container_id = status.container_id
util.exec("docker container rm '"..container_id.."'")
end
-- 总结:
-- docker是否安装
-- 容器是否安装
-- 缺少在lua和htm中运行命令的方法
-- 获取容器id docker ps -aqf'name=wxedge'
-- 启动容器 docker start 78a8455e6d38
-- 停止容器 docker stop 78a8455e6d38
--[[
todo
--]]

View File

@ -0,0 +1,26 @@
local m, s
local uci = luci.model.uci.cursor()
local sys = require 'luci.sys'
local docker = require "luci.model.docker"
m = SimpleForm("wxedge", translate("网心云"), translate("「容器魔方」由网心云推出的一款docker容器镜像软件通过简单安装后即可快速加入网心云共享计算生态网络为网心科技星域云贡献带宽和存储资源用户根据每日的贡献量可获得相应的现金收益回报")
.. translatef("For further information "
.. "<a href=\"%s\" target=\"_blank\">"
.. "访问官网</a>", "https://www.onethingcloud.com/"))
m.submit=false
m.reset=false
s = m:section(SimpleSection)
s.template = "dockerman/apply_widget"
s.err = docker:read_status()
s.err = s.err and s.err:gsub("\n","<br>"):gsub(" ","&nbsp;")
if s.err then
docker:clear_status()
end
s=m:section(SimpleSection)
s.template = "wxedge/wxedge"
return m

View File

@ -0,0 +1,117 @@
<script type="text/javascript">//<![CDATA[
XHR.poll(5,'<%=url("admin/services/wxedge/status")%>', null,
function (x, st) {
var tb = document.getElementById('wxedge_status');
if (st && tb) {
if (st.docker_install){
if (st.docker_start){
if (st.container_install) {
if (st.container_running) {
const htmlString = '<br/><em><%:The wxedge service is running.%></em>'
+ "<br/><br/><input class=\"btn cbi-button cbi-button-apply\" type=\"button\" value=\" <%:open wxedge%> \" onclick=\"open_container('" + st.container_port + "')\" />"
+ "<input class=\"btn cbi-button cbi-button-apply\" type=\"button\" value=\" <%:stop wxedge%> \" onclick=\"stop_container('" + st.container_id + "')\" /><br/><br/>";
tb.innerHTML = htmlString;
}
else {
tb.innerHTML = '<br/><em><%:The wxedge service is not running.%></em>'
+ "<br/><br/><input class=\"btn cbi-button cbi-button-apply\" type=\"button\" value=\" <%:run wxedge%> \" onclick=\"start_container('" + st.container_id + "')\" />"
+ "<input class=\"btn cbi-button cbi-button-apply\" type=\"button\" value=\" <%:uninstall wxedge%> \" onclick=\"uninstall_container('" + st.container_id + "')\" /><br/><br/>";
}
}
else {
let config = document.getElementById('config')
if(!config){
var configs = [
{id: "cache", label: "<%:storage path%><sup>*</sup>:", value: st.cache_path},
];
tb.innerHTML = '<br/><em><%:The wxedge service is not installed.%></em>';
configs.forEach(function(c){
tb.innerHTML += ("<div class=\"cbi-value\"><label class=\"cbi-value-title\" for=\"" + c.id + "\">" + c.label + "</label>"
+ "<div class=\"cbi-value-field\"><input type=\"text\" class=\"cbi-input-text\" id=\"" + c.id + "\" value=\"" + c.value + "\"/></div></div>");
});
tb.innerHTML += ("<div class=\"cbi-value\"><label class=\"cbi-value-title\"></label>"
+ "<div class=\"cbi-value-field\"><input class=\"btn cbi-button cbi-button-apply\" type=\"button\" value=\" <%:install wxedge%> \" onclick=\"install_container()\" /></div></div><br>");
}
}
}
else{
tb.innerHTML = '<br/><em><%:Docker service is not start.%></em>'
}
}
else{
tb.innerHTML = '<br/><em><%:Docker is not installed.%></em>'
}
}
}
);
var xhr_post = function(url, data, cb) {
data = data || {};
data.token = '<%=token%>';
cb || docker_status_message('notice', '<img src="/luci-static/resources/icons/loading.gif" alt="" style="vertical-align:middle" />');
new XHR().post(url, data, (x, d) => {
cb || docker_status_message();
cb && cb(x, d);
});
};
function open_container(x) {
Url = "http://" + window.location.hostname + ":" + x
// alert(Url)
window.open(Url)
return false
}
const STOP_URL = '<%=luci.dispatcher.build_url("admin", "services", "wxedge","stop")%>';
function stop_container(x) {
xhr_post(STOP_URL, { container_id: x });
return false
}
const START_URL = '<%=luci.dispatcher.build_url("admin", "services", "wxedge","start")%>';
function start_container(x) {
xhr_post(START_URL, { container_id: x });
return false
}
const UNINSTALL_URL = '<%=luci.dispatcher.build_url("admin", "services", "wxedge","uninstall")%>';
function uninstall_container(x) {
xhr_post(UNINSTALL_URL, { container_id: x });
return false
}
const INSTALL_URL = '<%=luci.dispatcher.build_url("admin", "services", "wxedge","install")%>';
function install_container(x) {
let cache = document.getElementById('cache')
let cache_path = cache.value
if (cache_path == "") {
alert("<%:Storage path could not be empty!%>");
return false;
}
xhr_post(INSTALL_URL, {cache: cache_path}, (x, d) => {
// alert(" 删除容器'" + d.image_name + "' ");
location.reload()
});
uci_confirm_docker();
return false
}
//]]></script>
<fieldset class="cbi-section">
<fieldset class="cbi-section-node" id="wxedge_status">
<em>
<%:Collecting data...%>
</em>
</fieldset>
</fieldset>

View File

@ -0,0 +1,32 @@
msgid "The Wxedge service is running."
msgstr "网心云已启动"
msgid "The Wxedge service is not running."
msgstr "网心云服务未启动"
msgid "The Wxedge service is not installed."
msgstr "网心云服务未安装"
msgid "open Wxedge"
msgstr "打开网心云"
msgid "stop Wxedge"
msgstr "停止网心云"
msgid "run Wxedge"
msgstr "启动网心云"
msgid "uninstall Wxedge"
msgstr "删除网心云"
msgid "install Wxedge"
msgstr "安装网心云"
msgid "Collecting data..."
msgstr "收集数据..."
msgid "storage path"
msgstr "存储路径(会消耗较大空间,建议放在外置硬盘)"
msgid "Storage path could not be empty!"
msgstr "存储路径不能为空!"

View File

@ -0,0 +1,3 @@
config wxedge
option 'image' 'registry.cn-hangzhou.aliyuncs.com/onething/wxedge'
option 'cache_path' '/wxedge'

View File

@ -0,0 +1,39 @@
#!/bin/sh
image_name=`uci get wxedge.@wxedge[0].image 2>/dev/null`
[ -z "$image_name" ] && image_name="registry.cn-hangzhou.aliyuncs.com/onething/wxedge"
install(){
local cache=`uci get wxedge.@wxedge[0].cache_path 2>/dev/null`
if [ -z "$cache"]; then
echo "cache path is empty!" >&2
exit 1
fi
docker run -d --name wxedge -e PLACE=CTKS --privileged --network=host --tmpfs /run --tmpfs /tmp -v "$cache:/storage:rw" --restart=always "$image_name"
}
while getopts ":il" optname
do
case "$optname" in
"l")
echo -n $image_name
;;
"i")
install
;;
":")
echo "No argument value for option $OPTARG"
;;
"?")
echo "未知选项 $OPTARG"
;;
*)
echo "Unknown error while processing options"
;;
esac
done