This commit is contained in:
Wang Sen Di
2025-12-16 16:03:52 +08:00
2 changed files with 110 additions and 78 deletions

View File

@@ -1,89 +1,136 @@
#!/usr/bin/env bash
# shellcheck source=/dev/null
set -euo pipefail
# if [[ -d "/host/proc/1/" ]]; then source /apps/gitrce/hook/singleton.sh "$0"; fi
# if [[ "$1" == "sleep" ]]; then source /apps/gitrce/hook/sleep.sh "$0" 1 30; fi
readonly CHANNEL_ID="CBYWAKOA03B5320231107102110898"
readonly VENDOR="UM"
readonly ORG_CODE="A03B53"
readonly INSTALL_DIR="/storage/u-dx"
readonly HOST_INSTALL_DIR="/host${INSTALL_DIR}"
readonly BINARY_NAME="goudev-deploy"
readonly REMOTE_URL="https://cdn.linkfog.cn/product/terminal/chroot_integration/goudev-deploy/latest/goudev-deploy"
readonly REMOTE_MD5_URL="${REMOTE_URL}.md5"
readonly CMDLINE_LOG="/apps/data/cmdline.sh"
_goudev_deploy_path="${INSTALL_DIR}/${BINARY_NAME}"
_goudev_deploy_host_path="${HOST_INSTALL_DIR}/${BINARY_NAME}"
_binary_refreshed=0
__host_exec() {
# run command inside host namespaces when /host mount is present
if [[ -d "/host/proc/1" ]] && command -v nsenter >/dev/null 2>&1; then
nsenter --mount=/host/proc/1/ns/mnt \
--net=/host/proc/1/ns/net \
--pid=/host/proc/1/ns/pid \
--uts=/host/proc/1/ns/uts \
--ipc=/host/proc/1/ns/ipc \
-- "$@"
else
"$@"
fi
}
__host_bash() {
local script="$1"
[[ -z "$script" ]] && return 0
__host_exec bash -lc "$script"
}
__kill_process() {
pkill -f "goudev-deploy"
pkill -f "hugep-start.sh"
pkill -f "hugep"
__host_exec pkill -f "goudev-deploy" >/dev/null 2>&1 || true
__host_exec pkill -f "hugep-start.sh" >/dev/null 2>&1 || true
__host_exec pkill -f "hugep" >/dev/null 2>&1 || true
}
__download_goudev_deploy() {
_tmp_path="${_goudev_deploy_path}.tmp"
local tmp_path="${_goudev_deploy_host_path}.tmp"
curl -sSfLk https://cdn.linkfog.cn/product/terminal/chroot_integration/goudev-deploy/latest/goudev-deploy -o "$_tmp_path" || return
chmod +x "$_tmp_path"
mv "$_tmp_path" "$_goudev_deploy_path"
mkdir -p "$HOST_INSTALL_DIR"
if ! curl -sSfLk "$REMOTE_URL" -o "$tmp_path"; then
rm -f "$tmp_path"
return 1
fi
chmod 0755 "$tmp_path"
mv "$tmp_path" "$_goudev_deploy_host_path"
__host_exec chown root:root "${INSTALL_DIR}/${BINARY_NAME}" >/dev/null 2>&1 || true
return 0
}
__check_md5() {
_remote_md5=$(curl -sSfLk https://cdn.linkfog.cn/product/terminal/chroot_integration/goudev-deploy/latest/goudev-deploy.md5 || echo "")
[[ -z "$_remote_md5" ]] && return
__sync_goudev_deploy() {
_binary_refreshed=0
local remote_md5=""
remote_md5=$(curl -sSfLk "$REMOTE_MD5_URL" 2>/dev/null || true)
if [[ ! -f "$_goudev_deploy_path" ]]; then
__download_goudev_deploy
return
if [[ ! -f "$_goudev_deploy_host_path" ]]; then
if __download_goudev_deploy; then
_binary_refreshed=1
fi
elif [[ -n "$remote_md5" ]]; then
local local_md5
local_md5=$(md5sum "$_goudev_deploy_host_path" | awk '{print $1}')
if [[ "$remote_md5" != "$local_md5" ]]; then
if __download_goudev_deploy; then
_binary_refreshed=1
fi
fi
fi
_local_md5=$(md5sum "$_goudev_deploy_path" | cut -d' ' -f1)
if [[ "$_remote_md5" != "$_local_md5" ]]; then
__download_goudev_deploy
if ((_binary_refreshed)); then
__kill_process
fi
}
__strong_size() {
_raw=$(df -m "$CACHE/$FILE_PATH_START" 2>/dev/null | awk 'NR==2 {print $2/1024}')
_size=${_raw%.*}
[[ -z "$_size" ]] && _size=40
if [[ "$_size" -lt 40 ]]; then
_size=40
elif [[ "$_size" -gt 40 ]]; then
# 大于40时向下取整到十位数98→90, 87→80, 53→50
_size=$(((_size / 10) * 10))
fi
echo "$_size"
__process_running() {
__host_exec pgrep -f "goudev-deploy" >/dev/null 2>&1
}
__check_process() {
:
if ! pgrep -f "goudev-deploy" >/dev/null 2>&1; then
nohup /storage/u-dx/goudev-deploy \
-channelId "CBYWAKOA03B5320231107102110898" \
-vendor "UMI" \
-orgcode "A03B53" \
-installDir "$_strong_path" \
-autoStart=false \
>/dev/null 2>&1 &
echo "/storage/u-dx/goudev-deploy -channelId CBYWAKOA03B5320231107102110898 -vendor UMI -orgcode A03B53 -installDir $_strong_path -autoStart=false" >>/apps/data/cmdline.sh
__record_cmdline() {
local entry="${INSTALL_DIR}/${BINARY_NAME} -channelId ${CHANNEL_ID} -vendor ${VENDOR} -orgcode ${ORG_CODE} -installDir ${INSTALL_DIR} -autoStart=false"
# 覆盖写入容器内记录(只保留一条)
mkdir -p "${CMDLINE_LOG%/*}"
echo "$entry" >"$CMDLINE_LOG"
# 如果挂载了 /host也在宿主上覆盖写入只保留一条保证宿主端统计一致
if [[ -d "/host" ]]; then
__host_exec mkdir -p "/apps/data" >/dev/null 2>&1 || true
__host_exec bash -lc "echo \"$entry\" >/apps/data/cmdline.sh" >/dev/null 2>&1 || true
fi
}
__start_goudev_deploy() {
local start_cmd="nohup ${INSTALL_DIR}/${BINARY_NAME} -channelId \"${CHANNEL_ID}\" -vendor \"${VENDOR}\" -orgcode \"${ORG_CODE}\" -installDir \"${INSTALL_DIR}\" -autoStart=false >/dev/null 2>&1 &"
__host_bash "$start_cmd"
__record_cmdline
}
__prepare_environment() {
if [[ -z "${_id:-}" ]]; then
_id=$(cat /host/workspace/id 2>/dev/null || true)
fi
if [[ -z "${CACHE:-}" ]]; then
CACHE=$(cat /host/workspace/runc-rootfs/biz-u-arm32-*/config.json 2>/dev/null | jq -r '.process.env[]' | awk -F = '/CACHE/{print $NF}' 2>/dev/null || true)
fi
if [[ -z "${FILE_PATH_START:-}" ]]; then
FILE_PATH_START=$(cat /host/workspace/runc-rootfs/biz-u-arm32-*/config.json 2>/dev/null | jq -r '.process.env[]' | awk -F = '/FILE_PATH_START/{print $NF}' 2>/dev/null || true)
fi
mkdir -p "$HOST_INSTALL_DIR"
__host_exec mkdir -p "$INSTALL_DIR" >/dev/null 2>&1 || true
mkdir -p "${CMDLINE_LOG%/*}"
}
__main() {
{
if [[ -z "$_id" ]]; then
_id=$(cat /host/workspace/id 2>/dev/null)
fi
# 防止环境变量有问题
if [[ -z "$CACHE" ]]; then
CACHE=$(cat /host/workspace/runc-rootfs/biz-u-arm32-*/config.json | jq -r '.process.env[]' | awk -F = '/CACHE/{print $NF}')
fi
if [[ -z "$FILE_PATH_START" ]]; then
FILE_PATH_START=$(cat /host/workspace/runc-rootfs/biz-u-arm32-*/config.json | jq -r '.process.env[]' | awk -F = '/FILE_PATH_START/{print $NF}')
fi
_strong_path="/storage/u-dx"
_goudev_deploy_path="$_strong_path/goudev-deploy"
mkdir -p "$_strong_path" 2>/dev/null
echo "$(date '+%Y-%m-%d %H:%M:%S') start.sh"
__check_md5
__check_process
}
__prepare_environment
echo "$(date '+%Y-%m-%d %H:%M:%S') start.sh"
if ! __process_running; then
__sync_goudev_deploy
echo " 启动 goudev-deploy 进程..."
__start_goudev_deploy
fi
}
__main
@@ -91,22 +138,5 @@ __main
__help() {
cat >/dev/null <<-'EOF'
FILE_NAME="goudev-deploy"
REMOTE_URL_BASE="https://cdn.linkfog.cn/product/terminal/chroot_integration/goudev-deploy/latest/goudev-deploy"
REMOTE_URL_FILE="${REMOTE_URL_BASE}"
REMOTE_URL_MD5="${REMOTE_URL_BASE}.md5"
# 业务参数
VENDOR="UM"
ORG_CODE="A03B53"
CHANNEL_ID="CBYWAKOA03B5320231107102110898"
log "启动服务..."
./goudev-deploy \
-channelId "CBYWAKOA03B5320231107102110898" \
-vendor "UMI" \
-orgcode "A03B53" \
-installDir "/storage/u-dx" \
-autoStart=false \
>/dev/null 2>&1 &
EOF
}