-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall-release.sh
407 lines (364 loc) · 10.1 KB
/
install-release.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env bash
# The files installed by the script conform to the Filesystem Hierarchy Standard:
# https://wiki.linuxfoundation.org/lsb/fhs
# The URL of the script project is:
# https://github.com/px-org/PanIndex-install
# The URL of the script is:
# https://raw.githubusercontent.com/px-org/PanIndex-install/main/install.sh
# export WORKING_DIRECTORY='/usr/local/etc/PanIndex'
WORKING_DIRECTORY=${WORKING_DIRECTORY:-/usr/local/etc/PanIndex}
if [[ -f '/etc/systemd/system/PanIndex.service' ]] && [[ -f '/usr/local/bin/PanIndex' ]]; then
PAN_INDEX_IS_INSTALLED_BEFORE_RUNNING_SCRIPT=1
else
PAN_INDEX_IS_INSTALLED_BEFORE_RUNNING_SCRIPT=0
fi
# PanIndex current version
CURRENT_VERSION=''
# PanIndex latest release version
RELEASE_LATEST_VERSION=''
# install
INSTALL='0'
# remove
REMOVE='0'
# help
HELP='0'
# check
CHECK='0'
# color code
RED="31m" # Error message
GREEN="32m" # Success message
YELLOW="33m" # Warning message
BLUE="36m" # Info message
VDIS=""
SYSTEMCTL_CMD=$(command -v systemctl 2>/dev/null)
CURL_CMD=$(command -v curl 2>/dev/null)
TAR_CMD=$(command -v tar 2>/dev/null)
#SERVICE_CMD=$(command -v service 2>/dev/null)
PROXY=""
CHANNEL="/latest"
YES="0"
CONFIRM_MSG=""
CEcho(){
# shellcheck disable=SC2145
echo -e "\033[${1}${@:2}\033[0m" 1>& 2
}
show_help(){
cat - 1>& 2 << EOF
./install-release.sh [-h] [-i] [-r] [-c] [-p] [-a] [-pre]
-h, --help Show help.
-i, --install Install or update.
-r, --remove Remove installed PanIndex.
-c, --check Check for update.
-p, --proxy To download through a proxy server, use -p socks5://127.0.0.1:1080 or -p http://127.0.0.1:3128 etc.
-a, --auto Auto install or update without confirm.
-pre Change channel to pre-release.
EOF
}
download_PanIndex(){
mkdir -p "$WORKING_DIRECTORY"
local TARGET_FILE="${WORKING_DIRECTORY}/PanIndex.tar.gz"
DOWNLOAD_LINK="https://github.com/px-org/PanIndex/releases/download/${RELEASE_LATEST_VERSION}/PanIndex-linux-${VDIS}.tar.gz"
CEcho ${BLUE} "info: Downloading PanIndex: ${DOWNLOAD_LINK}"
curl ${PROXY} -L -H "Cache-Control: no-cache" -o "${TARGET_FILE}" ${DOWNLOAD_LINK}
if [ $? != 0 ];then
CEcho ${RED} "error: Failed to download! Please check your network or try again."
return 3
fi
return 0
}
archAffix(){
case "${1:-"$(uname -m)"}" in
i686|i386)
echo '386'
;;
x86_64|amd64)
echo 'amd64'
;;
armv5tel)
echo 'arm32-v5'
;;
armv6l)
echo 'arm32-v6'
;;
*armv7*|armv7l)
echo 'arm32-v7'
;;
*armv8*|aarch64)
echo 'arm64'
;;
*)
return 1
;;
esac
return 0
}
normalizeVersion() {
if [ -n "$1" ]; then
case "$1" in
v*)
echo "$1"
;;
*)
echo "v$1"
;;
esac
else
echo ""
fi
}
# 1: new version. 0: no. 2: not installed. 3: check failed. 4: don't check.
get_version(){
if [[ -n "$VERSION" ]]; then
RELEASE_LATEST_VERSION="$(normalizeVersion "$VERSION")"
return 4
else
#get the latest release
TAG_URL="https://api.github.com/repos/px-org/PanIndex/releases${CHANNEL}"
RELEASE_LATEST_VERSION="$(normalizeVersion "$(curl ${PROXY} \
-H "Accept: application/json" \
-H "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0" \
-s "${TAG_URL}" --connect-timeout 20 | awk -F'[ "]+' '$0~"tag_name"{print $4;exit}' )")"
[ ! -f /usr/local/bin/PanIndex ] && return 2
VER="$(/usr/local/bin/PanIndex -config_query=version)"
RETVAL=$?
CURRENT_VERSION="$(normalizeVersion "$VER")"
if [[ $? -ne 0 ]] || [[ $RELEASE_LATEST_VERSION == "" ]]; then
CEcho ${RED} "error: Failed to fetch release information. Please check your network or try again."
return 3
elif [[ $RETVAL -ne 0 ]];then
return 2
elif [[ $RELEASE_LATEST_VERSION != $CURRENT_VERSION ]];then
return 1
fi
return 0
fi
}
check_for_update(){
CEcho ${BLUE} "info: Checking for update."
get_version
RETVAL="$?"
if [[ $RETVAL -eq 1 ]]; then
CEcho ${BLUE} "info: Found new version ${RELEASE_LATEST_VERSION} for PanIndex.(Current version:$CURRENT_VERSION)"
return 1
elif [[ $RETVAL -eq 0 ]]; then
CEcho ${BLUE} "info: No new version. Current version is ${RELEASE_LATEST_VERSION}."
return 0
elif [[ $RETVAL -eq 2 ]]; then
CEcho ${YELLOW} "warn: No PanIndex installed."
CEcho ${BLUE} "info: The newest version for PanIndex is ${RELEASE_LATEST_VERSION}."
return 1
fi
return 0
}
judgment_parameters() {
while [[ "$#" -gt '0' ]]; do
case "$1" in
-p|--proxy)
PROXY="-x ${2}"
shift
;;
-pre)
CHANNEL=""
shift
;;
-a|--auto)
YES="1"
;;
-i|--install)
INSTALL="1"
;;
-h|--help)
HELP="1"
;;
-c|--check)
CHECK="1"
;;
-r|--remove)
REMOVE="1"
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
}
confirm(){
if [[ $YES -eq 1 ]]; then
return 1
fi
read -r -p "$CONFIRM_MSG" input
case $input in
[yY][eE][sS]|[yY])
return 1
;;
[nN][oO]|[nN])
return 0
;;
*)
echo "Invalid input..."
exit 1
;;
esac
return 0
}
start_install_pan_index(){
local ARCH=$(uname -m)
if [[ $UID -ne 0 ]]; then
CEcho ${YELLOW} "warn: You must run as root user."
exit 0
fi
VDIS="$(archAffix)"
if [[ -n "$VDIS" ]]; then
check_for_update
CU="$?"
if [[ $CU -ne 0 ]]; then
CONFIRM_MSG="Are you sure to install it ? [Y/n] "
confirm
CI="$?"
if [[ $CI -eq 1 ]]; then
install_pan_index
RETVAL="$?"
if [[ $RETVAL -eq 1 ]]; then
install_pan_index_service
if [[ $? -eq 1 ]]; then
start_pan_index
if [[ $? -eq 1 ]]; then
CEcho ${GREEN} "info: Congratulations! PanIndex $RELEASE_LATEST_VERSION is installed successfully."
CEcho ${GREEN} "info: Now you can access the address http://127.0.0.1:5238 to configure your PanIndex. The default password is PanIndex."
fi
fi
fi
fi
fi
else
CEcho ${RED} "error: The architecture $ARCH is not supported."
fi
}
install_pan_index(){
download_PanIndex
RETVAL="$?"
if [[ $RETVAL -eq 0 ]] ; then
CEcho ${BLUE} "info: Extracting PanIndex package to ${WORKING_DIRECTORY}."
mkdir -p "${WORKING_DIRECTORY}"
tar --no-same-owner -zxf "${WORKING_DIRECTORY}/PanIndex.tar.gz" -C ${WORKING_DIRECTORY}
rm -rf "${WORKING_DIRECTORY}/README.md" "${WORKING_DIRECTORY}/LICENSE" "${WORKING_DIRECTORY}/PanIndex.tar.gz"
cat > "${WORKING_DIRECTORY}/config.json" << EOF
{
"host": "0.0.0.0",
"port": 5238,
"log_level": "info",
"data_path": "${WORKING_DIRECTORY}/data",
"cert_file": "",
"key_file": "",
"config_query": "",
"db_type": "sqlite",
"dsn": "${WORKING_DIRECTORY}/data/data.db"
}
EOF
mv "${WORKING_DIRECTORY}/PanIndex-linux-${VDIS}" /usr/local/bin/PanIndex
chmod +x '/usr/local/bin/PanIndex'
CEcho ${BLUE} "info: Move PanIndex to /usr/local/bin."
return 1
fi
return 0
}
install_pan_index_service(){
if [[ ! -f "/etc/systemd/system/PanIndex.service" ]]; then
cat > /etc/systemd/system/PanIndex.service << EOF
[Unit]
Description=PanIndex Service
Documentation=https://pan-index.pages.dev
After=network.target
[Service]
User=root
WorkingDirectory=${WORKING_DIRECTORY}
ExecStart=/usr/local/bin/PanIndex -c=${WORKING_DIRECTORY}/config.json
Restart=on-failure
RestartPreventExitStatus=23
LimitNPROC=10000
LimitNOFILE=1000000
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable PanIndex.service
CEcho ${GREEN} "info: PanIndex.service installed successfully."
else
CEcho ${BLUE} "info: PanIndex.service already exist."
fi
return 1
}
start_pan_index(){
if [[ -f "/etc/systemd/system/PanIndex.service" ]]; then
systemctl restart PanIndex.service
return 1
else
CEcho ${GREEN} "warn: /etc/systemd/system/PanIndex.service does not exist."
fi
return 0
}
stop_pan_index(){
if [[ -f "/etc/systemd/system/PanIndex.service" ]]; then
systemctl stop PanIndex.service
systemctl disable PanIndex.service
systemctl daemon-reload
return 1
else
CEcho ${YELLOW} "warn: /etc/systemd/system/PanIndex.service does not exist."
fi
return 0
}
remove_pan_index(){
stop_pan_index
if [[ $? -eq 1 ]]; then
rm -rf /etc/systemd/system/PanIndex.service
CEcho ${GREEN} "info: /etc/systemd/system/PanIndex.service already removed."
fi
if [[ -f "/usr/local/bin/PanIndex" ]]; then
rm -rf /usr/local/bin/PanIndex
CEcho ${GREEN} "info: /usr/local/bin/PanIndex already removed."
else
CEcho ${YELLOW} "warn: /usr/local/bin/PanIndex does not exist."
fi
if [[ -d "$WORKING_DIRECTORY" ]]; then
YES="0"
CONFIRM_MSG="Are you sure to remove working directory? You will lose PanIndex data .Input N to skip this operation. [Y/n] "
confirm
if [[ $? -eq 1 ]]; then
rm -rf "$WORKING_DIRECTORY"
CEcho ${GREEN} "info: $WORKING_DIRECTORY already removed."
fi
else
CEcho ${YELLOW} "warn: $WORKING_DIRECTORY does not exist."
fi
CEcho ${GREEN} "info: All installed files have been removed."
}
check_environment(){
if [[ ! -n "$TAR_CMD" ]]; then
CEcho ${RED} "error: tar command not found."
return 0
elif [[ ! -n "$CURL_CMD" ]]; then
CEcho ${RED} "error: curl command not found."
return 0
elif [[ ! -n "$SYSTEMCTL_CMD" ]]; then
CEcho ${RED} "error: systemctl command not found."
return 0
else
CEcho ${BLUE} "info: The environment is ok."
return 1
fi
return 1
}
main(){
judgment_parameters "$@"
# Parameter information
[[ "$HELP" -eq '1' ]] && show_help && return
check_environment
[[ $? -eq 0 ]] && return
[[ "$CHECK" -eq '1' ]] && check_for_update && return
[[ "$INSTALL" -eq '1' ]] && start_install_pan_index && return
[[ "$YES" -eq '1' ]] && start_install_pan_index && return
[[ "$REMOVE" -eq '1' ]] && remove_pan_index && return
}
main "$@"