bmtedge add

This commit is contained in:
janson 2023-06-14 20:22:03 +08:00
parent f6d36be451
commit 1cb903957b
10 changed files with 344 additions and 0 deletions

View File

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

View File

@ -0,0 +1,7 @@
module("luci.controller.bmtedge", package.seeall)
function index()
entry({"admin", "services", "bmtedge"}, alias("admin", "services", "bmtedge", "config"), _("BlueMountain Edge"), 30).dependent = true
entry({"admin", "services", "bmtedge", "config"}, cbi("bmtedge"))
end

View File

@ -0,0 +1,58 @@
local util = require "luci.util"
local jsonc = require "luci.jsonc"
local nixio = require "nixio"
local bmtedge = {}
bmtedge.blocks = function()
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
bmtedge.default_image = function()
if string.find(nixio.uname().machine, "x86_64") then
return "jinshanyun/jinshan-x86_64"
else
return "jinshanyun/jinshan-arm64"
end
end
local random_str = function(t)
math.randomseed(os.time())
local s = "0123456789abcdefghijklmnopqrstuvwsyz"
local value = ""
for x = 1,t do
local rand = math.random(#s)
value = value .. string.sub(s, rand, rand)
end
return value
end
bmtedge.default_uid = function()
local f = io.open("/sys/class/net/eth0/address", "r")
if not f then
f = io.open("/sys/class/net/br-lan/address", "r")
end
if not f then
return random_str(16)
end
local ret = f:read("*all")
f:close()
return string.gsub(ret, "[ \r\n:]+", "") .. random_str(8)
end
return bmtedge

View File

@ -0,0 +1,48 @@
--[[
LuCI - Lua Configuration Interface
]]--
local taskd = require "luci.model.tasks"
local bmtedge_model = require "luci.model.bmtedge"
local m, s, o
m = taskd.docker_map("bmtedge", "bmtedge", "/usr/libexec/istorec/bmtedge.sh",
translate("BlueMountain Edge"),
"蓝山云-流量宝由蓝山联合金山云推出的一款镜像软件,通过简单安装后可快速加入蓝山的边缘计算生态,在线共享带宽即可赚钱,每月可获取一定的现金汇报!了解更多,请登录「<a href=\"www.bmtcloud.com.cn\" target=\"_blank\" >蓝山云官网</a>」")
s = m:section(SimpleSection, translate("Service Status"), translate("BlueMountain Edge status:"), "注意网心云会以超级权限运行!")
s:append(Template("bmtedge/status"))
s = m:section(TypedSection, "bmtedge", translate("Setup"), translate("The following parameters will only take effect during installation or upgrade:"))
s.addremove=false
s.anonymous=true
local default_image = bmtedge_model.default_image()
o = s:option(Value, "image_name", translate("Image").."<b>*</b>")
o.rmempty = false
o.datatype = "string"
o:value("jinshanyun/jinshan-x86_64", "jinshanyun/jinshan-x86_64")
o:value("jinshanyun/jinshan-arm64", "jinshanyun/jinshan-arm64")
o.default = default_image
local default_uid = bmtedge_model.default_uid()
o = s:option(Value, "uid", translate("UID").."<b>*</b>")
o.rmempty = false
o.datatype = "string"
o:value(default_uid, default_uid)
o.default = default_uid
local blks = bmtedge_model.blocks()
local dir
o = s:option(Value, "cache_path", translate("Cache path").."<b>*</b>", "请选择合适的存储位置进行安装,安装位置容量越大,收益越高。安装后请勿轻易改动")
o.rmempty = false
o.datatype = "string"
for _, dir in pairs(blks) do
dir = dir .. "/bmtedge1"
o:value(dir, dir)
end
if #blks > 0 then
o.default = blks[1] .. "/bmtedge1"
end
return m

View File

@ -0,0 +1,61 @@
<%
local util = require "luci.util"
local container_status = util.trim(util.exec("/usr/libexec/istorec/bmtedge.sh status"))
local container_install = (string.len(container_status) > 0)
local container_running = container_status == "running"
local uci = require "luci.model.uci".cursor()
local uid = uci:get_first("bmtedge", "bmtedge", "uid", ""),
-%>
<script src="/luci-static/bmtedge/qrcode.min.js"></script>
<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"><%:BlueMountain Edge is running%></button>
<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" id="btnShowQr" name="start" value="显示二维码" />
</div>
</div>
<% else %>
<button class="cbi-button cbi-button-negative" disabled="true"><%:BlueMountain Edge is not running%></button>
<% end %>
</div>
</div>
<div id="winContainer" style="display: none">
<div id="qrimage" style="
width: 256px;
height: 256px;
">
</div>
<h6>用“蓝山云”小程序扫码,请查看:<a href="https://doc.linkease.com" target="_blank">教程</a></h6>
</div>
<script>
(function() {
'use strict';
var shown = false;
var tryShowTips = function() {
if (shown) {
return;
}
shown = true;
$("#winContainer").show();
$("#winContainer").find(".button").click(function(){
$("#winContainer").hide();
show = false;
});
};
$('#btnShowQr').click(function(){
new QRCode(document.getElementById("qrimage"), "lsyK17032_"+"<%=uid%>");
tryShowTips();
});
})();
</script>

View File

@ -0,0 +1,36 @@
msgid ""
msgstr "Content-Type: text/plain; charset=UTF-8"
msgid "BlueMountain Edge"
msgstr "蓝山云-流量宝"
msgid "Cache path"
msgstr "缓存文件路径"
msgid "Service Status"
msgstr "服务状态"
msgid "BlueMountain Edge status:"
msgstr "蓝山云的状态信息如下:"
msgid "Setup"
msgstr "安装配置"
msgid "The following parameters will only take effect during installation or upgrade:"
msgstr "以下参数只在安装或者升级时才会生效:"
msgid "Status"
msgstr "状态"
msgid "BlueMountain Edge is running"
msgstr "蓝山云运行中"
msgid "BlueMountain Edge is not running"
msgstr "蓝山云未运行"
msgid "Open the BlueMountain Edge"
msgstr "打开蓝山云"
msgid "UID"
msgstr "唯一标识"

View File

@ -0,0 +1,4 @@
config bmtedge
option 'cache_path' ''
option 'image_name' ''
option 'uid' ''

View File

@ -0,0 +1,14 @@
#!/bin/sh
uci -q batch <<-EOF >/dev/null
delete firewall.bmtedge
set firewall.bmtedge=rule
set firewall.bmtedge.name="bmtedge"
set firewall.bmtedge.target="ACCEPT"
set firewall.bmtedge.src="wan"
set firewall.bmtedge.dest_port="1024-65535"
set firewall.bmtedge.enabled="0"
commit firewall
EOF
exit 0

View File

@ -0,0 +1,97 @@
#!/bin/sh
ACTION=${1}
shift 1
do_install() {
local path=`uci get bmtedge.@bmtedge[0].cache_path 2>/dev/null`
local uid=`uci get bmtedge.@bmtedge[0].uid 2>/dev/null`
local image_name=`uci get bmtedge.@bmtedge[0].image_name 2>/dev/null`
if [ -z "$path" ]; then
echo "path is empty!"
exit 1
fi
[ -z "$image_name" ] && image_name="jinshanyun/jinshan-x86_64:latest"
echo "docker pull ${image_name}"
docker pull ${image_name}
docker rm -f bmtedge
local cmd="docker run --restart=unless-stopped -d \
--privileged \
--network=host \
--dns=127.0.0.1 \
--tmpfs /run \
--tmpfs /tmp \
-v \"$path:/data/ksc1\" \
-v \"$path/containerd:/var/lib/containerd\" \
-e ksc_supplier_code=\"92101\" \
-e ksc_datadir=\"/data/ksc1\" \
-e ksc_machine_code=\"lsyK17032_$uid\" \
-e ksc_refer=\"ruiyun_node\""
local tz="`uci get system.@system[0].zonename`"
[ -z "$tz" ] || cmd="$cmd -e TZ=$tz"
cmd="$cmd --name bmtedge \"$image_name\""
echo "$cmd"
eval "$cmd"
if [ "$?" = "0" ]; then
if [ "`uci -q get firewall.bmtedge.enabled`" = 0 ]; then
uci -q batch <<-EOF >/dev/null
set firewall.bmtedge.enabled="1"
commit firewall
EOF
/etc/init.d/firewall reload
fi
fi
echo "Install OK!"
}
usage() {
echo "usage: $0 sub-command"
echo "where sub-command is one of:"
echo " install Install the bmtedge"
echo " upgrade Upgrade the bmtedge"
echo " rm/start/stop/restart Remove/Start/Stop/Restart the bmtedge"
echo " status Onething Edge status"
echo " port Onething Edge port"
}
case ${ACTION} in
"install")
do_install
;;
"upgrade")
do_install
;;
"rm")
docker rm -f bmtedge
if [ "`uci -q get firewall.bmtedge.enabled`" = 1 ]; then
uci -q batch <<-EOF >/dev/null
set firewall.bmtedge.enabled="0"
commit firewall
EOF
/etc/init.d/firewall reload
fi
;;
"start" | "stop" | "restart")
docker ${ACTION} bmtedge
;;
"status")
docker ps --all -f 'name=bmtedge' --format '{{.State}}'
;;
"port")
docker ps --all -f 'name=bmtedge' --format '{{.Ports}}' | grep -om1 '0.0.0.0:[0-9]*' | sed 's/0.0.0.0://'
;;
*)
usage
exit 1
;;
esac

File diff suppressed because one or more lines are too long