Skip to content

Instantly share code, notes, and snippets.

@uubulb
Last active July 28, 2024 16:19
Show Gist options
  • Save uubulb/3be8ad862aac98ef6cdae6d2c60be1e2 to your computer and use it in GitHub Desktop.
Save uubulb/3be8ad862aac98ef6cdae6d2c60be1e2 to your computer and use it in GitHub Desktop.
Nezha Agent update script
#!/bin/sh
# 哪吒 Agent 更新脚本
NZ_BASE_PATH="/opt/nezha"
NZ_AGENT_PATH="${NZ_BASE_PATH}/agent"
sudo() {
myEUID=$(id -ru)
if [ "$myEUID" -ne 0 ]; then
if command -v sudo > /dev/null 2>&1; then
command sudo "$@"
else
err "错误: 您的系统未安装 sudo,因此无法进行该项操作。"
exit 1
fi
else
"$@"
fi
}
err() {
printf "${red}$*${plain}\n" >&2
}
geo_check() {
api_list="https://blog.cloudflare.com/cdn-cgi/trace https://dash.cloudflare.com/cdn-cgi/trace https://cf-ns.com/cdn-cgi/trace"
ua="Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0"
set -- $api_list
for url in $api_list; do
text="$(curl -A "$ua" -m 10 -s $url)"
if echo $text | grep -qw 'CN'; then
isCN=true
break
fi
done
}
pre_check() {
## os_arch
if uname -m | grep -q 'x86_64'; then
os_arch="amd64"
elif uname -m | grep -q 'i386\|i686'; then
os_arch="386"
elif uname -m | grep -q 'aarch64\|armv8b\|armv8l'; then
os_arch="arm64"
elif uname -m | grep -q 'arm'; then
os_arch="arm"
elif uname -m | grep -q 's390x'; then
os_arch="s390x"
elif uname -m | grep -q 'riscv64'; then
os_arch="riscv64"
fi
## China_IP
if [ -z "$CN" ]; then
geo_check
if [ ! -z "$isCN" ]; then
echo "根据geoip api提供的信息,当前IP可能在中国"
printf "使用中国镜像完成安装"
CN=true
fi
fi
if [ -n "$CUSTOM_MIRROR" ]; then
GITHUB_URL=$CUSTOM_MIRROR
else
if [ -z "$CN" ]; then
GITHUB_URL="github.com"
else
GITHUB_URL="gitee.com"
fi
fi
}
restart_and_update() {
echo "> 重启和更新"
local version=$(curl -m 10 -sL "https://api.github.com/repos/nezhahq/agent/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://gitee.com/api/v5/repos/naibahq/agent/releases/latest" | awk -F '"' '{for(i=1;i<=NF;i++){if($i=="tag_name"){print $(i+2)}}}')
fi
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://fastly.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
version=$(curl -m 10 -sL "https://gcore.jsdelivr.net/gh/nezhahq/agent/" | grep "option\.value" | awk -F "'" '{print $2}' | sed 's/nezhahq\/agent@/v/g')
fi
if [ ! -n "$version" ]; then
err "获取版本号失败,请检查本机能否链接 https://api.github.com/repos/nezhahq/agent/releases/latest"
return 1
else
echo "当前最新版本为: ${version}"
fi
sudo ${NZ_AGENT_PATH}/nezha-agent service stop
if [ -z "$CN" ]; then
NZ_AGENT_URL="https://${GITHUB_URL}/nezhahq/agent/releases/download/$version/nezha-agent_linux_$os_arch.zip"
else
NZ_AGENT_URL="https://${GITHUB_URL}/naibahq/agent/releases/download/$version/nezha-agent_linux_$os_arch.zip"
fi
sudo wget -qO $NZ_AGENT_PATH/agent.zip $NZ_AGENT_URL >/dev/null 2>&1 && sudo unzip -qq -o $NZ_AGENT_PATH/agent.zip -d $NZ_AGENT_PATH && sudo rm -r $NZ_AGENT_PATH/agent.zip
sudo ${NZ_AGENT_PATH}/nezha-agent service start
if [ $? = 0 ]; then
printf "${green}哪吒Agent重启成功${plain}\n"
else
err "重启失败,可能是因为启动时间超过了两秒,请稍后查看日志信息"
fi
}
pre_check
restart_and_update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment