-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnzinstall.sh
198 lines (171 loc) · 4.81 KB
/
nzinstall.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/sh
NZ_BASE_PATH="/opt/nezha"
NZ_AGENT_PATH="${NZ_BASE_PATH}/agent"
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
err() {
sudo printf "${red}%s${plain}\n" "$*" >&2
}
success() {
sudo printf "${green}%s${plain}\n" "$*"
}
info() {
sudo printf "${yellow}%s${plain}\n" "$*"
}
sudo() {
myEUID=$(id -ru)
if [ "$myEUID" -ne 0 ]; then
if command -v sudo > /dev/null 2>&1; then
command sudo "$@"
else
err "ERROR: sudo is not installed on the system, the action cannot be proceeded."
exit 1
fi
else
"$@"
fi
}
deps_check() {
deps="wget unzip grep"
set -- "$api_list"
for dep in $deps; do
if ! command -v "$dep" >/dev/null 2>&1; then
err "$dep not found, please install it first."
exit 1
fi
done
}
geo_check() {
api_list="https://blog.cloudflare.com/cdn-cgi/trace https://developers.cloudflare.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="$(sudo wget --header="User-Agent: $ua" -T 10 -qO- "$url")"
endpoint="$(echo "$text" | sed -n 's/.*h=\([^ ]*\).*/\1/p')"
if echo "$text" | grep -qw 'CN'; then
isCN=true
break
elif echo "$url" | grep -q "$endpoint"; then
break
fi
done
}
env_check() {
mach=$(uname -m)
case "$mach" in
amd64|x86_64)
os_arch="amd64"
;;
i386|i686)
os_arch="386"
;;
aarch64|arm64)
os_arch="arm64"
;;
*arm*)
os_arch="arm"
;;
s390x)
os_arch="s390x"
;;
riscv64)
os_arch="riscv64"
;;
mips)
os_arch="mips"
;;
mipsel|mipsle)
os_arch="mipsle"
;;
*)
err "Unknown architecture: $uname"
exit 1
;;
esac
system=$(uname)
case "$system" in
*Linux*)
os="linux"
;;
*Darwin*)
os="darwin"
;;
*FreeBSD*)
os="freebsd"
;;
*)
err "Unknown architecture: $system"
exit 1
;;
esac
}
init() {
deps_check
env_check
## China_IP
if [ -z "$CN" ]; then
geo_check
if [ -n "$isCN" ]; then
CN=true
fi
fi
if [ -z "$CN" ]; then
GITHUB_URL="github.com"
else
GITHUB_URL="gitee.com"
fi
}
install() {
echo "Installing..."
if [ -z "$CN" ]; then
NZ_AGENT_URL="https://${GITHUB_URL}/nezhahq/agent/releases/latest/download/nezha-agent_${os}_${os_arch}.zip"
else
_version=$(sudo wget -T 10 -qO- "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)}}}')
NZ_AGENT_URL="https://${GITHUB_URL}/naibahq/agent/releases/download/${_version}/nezha-agent_${os}_${os_arch}.zip"
fi
_cmd="sudo wget -T 60 -O /tmp/nezha-agent_${os}_${os_arch}.zip $NZ_AGENT_URL >/dev/null 2>&1"
if ! eval "$_cmd"; then
err "Download nezha-agent release failed, check your network connectivity"
exit 1
fi
sudo mkdir -p $NZ_AGENT_PATH
sudo unzip -qo /tmp/nezha-agent_${os}_${os_arch}.zip -d $NZ_AGENT_PATH &&
sudo rm -rf /tmp/nezha-agent_${os}_${os_arch}.zip
path="$NZ_AGENT_PATH/config.yml"
if [ -f "$path" ]; then
random=$(LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 5)
path=$(printf "%s" "$NZ_AGENT_PATH/config-$random.yml")
fi
if [ -z "$NZ_SERVER" ]; then
err "NZ_SERVER should not be empty"
exit 1
fi
if [ -z "$NZ_CLIENT_SECRET" ]; then
err "NZ_CLIENT_SECRET should not be empty"
exit 1
fi
env="NZ_SERVER=$NZ_SERVER NZ_CLIENT_SECRET=$NZ_CLIENT_SECRET NZ_TLS=$NZ_TLS NZ_DISABLE_AUTO_UPDATE=$NZ_DISABLE_AUTO_UPDATE NZ_DISABLE_FORCE_UPDATE=$DISABLE_FORCE_UPDATE NZ_DISABLE_COMMAND_EXECUTE=[...]
sudo "${NZ_AGENT_PATH}"/nezha-agent service -c "$path" uninstall >/dev/null 2>&1
_cmd="sudo env $env $NZ_AGENT_PATH/nezha-agent service -c $path install"
if ! eval "$_cmd"; then
err "Install nezha-agent service failed"
sudo "${NZ_AGENT_PATH}"/nezha-agent service -c "$path" uninstall >/dev/null 2>&1
exit 1
fi
success "nezha-agent successfully installed"
}
uninstall() {
find "$NZ_AGENT_PATH" -type f -name "*config*.yml" | while read -r file; do
sudo "$NZ_AGENT_PATH/nezha-agent" service -c "$file" uninstall
sudo rm "$file"
done
info "Uninstallation completed."
}
if [ "$1" = "uninstall" ]; then
uninstall
exit
fi
init
install