-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
bili_task_base.sh
470 lines (399 loc) · 13.2 KB
/
bili_task_base.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env bash
# new Env("bili_base")
# cron 0 0 1 1 * bili_base.sh
# Stop script on NZEC
set -e
# Stop script if unbound variable found (use ${var:-} if intentional)
set -u
# By default cmd1 | cmd2 returns exit code of cmd2 regardless of cmd1 success
# This is causing it to fail
set -o pipefail
verbose=false # 开启debug日志
bili_repo="raywangqvq/bilibilitoolpro" # 仓库地址
bili_branch="" # 分支名,空或_develop
prefer_mode=${BILI_MODE:-"dotnet"} # dotnet或bilitool,需要通过环境变量配置
github_proxy=${BILI_GITHUB_PROXY:-""} # 下载github release包时使用的代理,会拼在地址前面,需要通过环境变量配置
# Use in the the functions: eval $invocation
invocation='say_verbose "Calling: ${yellow:-}${FUNCNAME[0]} ${green:-}$*${normal:-}"'
# standard output may be used as a return value in the functions
# we need a way to write text on the screen in the functions so that
# it won't interfere with the return value.
# Exposing stream 3 as a pipe to standard output of the script itself
exec 3>&1
# Setup some colors to use. These need to work in fairly limited shells, like the Ubuntu Docker container where there are only 8 colors.
# See if stdout is a terminal
if [ -t 1 ] && command -v tput >/dev/null; then
# see if it supports colors
ncolors=$(tput colors || echo 0)
if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
bold="$(tput bold || echo)"
normal="$(tput sgr0 || echo)"
black="$(tput setaf 0 || echo)"
red="$(tput setaf 1 || echo)"
green="$(tput setaf 2 || echo)"
yellow="$(tput setaf 3 || echo)"
blue="$(tput setaf 4 || echo)"
magenta="$(tput setaf 5 || echo)"
cyan="$(tput setaf 6 || echo)"
white="$(tput setaf 7 || echo)"
fi
fi
say_warning() {
printf "%b\n" "${yellow:-}bilitool: Warning: $1${normal:-}" >&3
}
say_err() {
printf "%b\n" "${red:-}bilitool: Error: $1${normal:-}" >&2
}
say() {
# using stream 3 (defined in the beginning) to not interfere with stdout of functions
# which may be used as return value
printf "%b\n" "${cyan:-}bilitool:${normal:-} $1" >&3
}
say_verbose() {
if [ "$verbose" = true ]; then
say "$1"
fi
}
QL_DIR=${QL_DIR:-"/ql"}
QL_BRANCH=${QL_BRANCH:-"develop"}
DefaultCronRule=${DefaultCronRule:-""}
CpuWarn=${CpuWarn:-""}
MemoryWarn=${MemoryWarn:-""}
DiskWarn=${DiskWarn:-""}
dir_repo=${dir_repo:-"$QL_DIR/data/repo"}
dir_shell=$QL_DIR/shell
. $dir_shell/env.sh
touch /root/.bashrc && . /root/.bashrc
# 目录
say "青龙repo目录: $dir_repo"
qinglong_bili_repo="$(echo "$bili_repo" | sed 's/\//_/g')${bili_branch}"
qinglong_bili_repo_dir="$(find $dir_repo -type d -iname $qinglong_bili_repo | head -1)"
say "bili仓库目录: $qinglong_bili_repo_dir"
current_linux_os="debian" # 或alpine
current_os="linux" # 或linux-musl
machine_architecture="x64" # 或arm、arm64
bilitool_installed_version=0
# 以下操作仅在bilitool仓库的根bin文件下执行
cd $qinglong_bili_repo_dir
mkdir -p bin && cd $qinglong_bili_repo_dir/bin
# 判断是否存在某指令
machine_has() {
eval $invocation
command -v "$1" >/dev/null 2>&1
return $?
}
# 判断系统架构
# 输出:arm、arm64、x64
get_machine_architecture() {
eval $invocation
if command -v uname >/dev/null; then
CPUName=$(uname -m)
case $CPUName in
armv*l)
echo "arm"
return 0
;;
aarch64 | arm64)
echo "arm64"
return 0
;;
esac
fi
# Always default to 'x64'
echo "x64"
return 0
}
# 获取linux系统名称
# 输出:debian.10、debian.11、debian.12、ubuntu.20.04、ubuntu.22.04、alpine.3.4.3...
get_linux_platform_name() {
eval $invocation
if [ -e /etc/os-release ]; then
. /etc/os-release
echo "$ID${VERSION_ID:+.${VERSION_ID}}"
return 0
elif [ -e /etc/redhat-release ]; then
local redhatRelease=$(</etc/redhat-release)
if [[ $redhatRelease == "CentOS release 6."* || $redhatRelease == "Red Hat Enterprise Linux "*" release 6."* ]]; then
echo "rhel.6"
return 1
fi
fi
echo "Linux specific platform name and version could not be detected: UName = $uname"
return 1
}
# 判断是否为musl(一般指alpine)
is_musl_based_distro() {
eval $invocation
(ldd --version 2>&1 || true) | grep -q musl
}
# 获取当前系统名称
# 输出:linux、linux-musl、osx、freebsd
get_current_os_name() {
eval $invocation
local uname=$(uname)
if [ "$uname" = "Darwin" ]; then
say_warning "当前系统:osx"
echo "osx"
return 1
elif [ "$uname" = "FreeBSD" ]; then
say_warning "当前系统:freebsd"
echo "freebsd"
return 1
elif [ "$uname" = "Linux" ]; then
local linux_platform_name=""
linux_platform_name="$(get_linux_platform_name)" || true
say "当前系统发行版本:$linux_platform_name"
if [ "$linux_platform_name" = "rhel.6" ]; then
echo $linux_platform_name
return 1
elif is_musl_based_distro; then
echo "linux-musl"
return 0
elif [ "$linux_platform_name" = "linux-musl" ]; then
echo "linux-musl"
return 0
else
echo "linux"
return 0
fi
fi
say_err "OS name could not be detected: UName = $uname"
return 1
}
# 检查操作系统
check_os() {
eval $invocation
current_os="$(get_current_os_name)"
say "当前系统:$current_os"
machine_architecture="$(get_machine_architecture)"
say "当前架构:$machine_architecture"
if [ "$current_os" = "linux" ]; then
current_linux_os="debian" # 当前青龙只有debian和aplpine两种
if ! machine_has curl; then
say "curl未安装,开始安装依赖..."
apt-get update
apt-get install -y curl
fi
else
current_linux_os="alpine"
if ! machine_has curl; then
say "curl未安装,开始安装依赖..."
apk update
apk add -y curl
fi
fi
say "当前选择的运行方式:$prefer_mode"
}
# 检查安装jq
check_jq() {
if [ "$current_linux_os" = "debian" ]; then
if ! machine_has jq; then
say "jq未安装,开始安装依赖..."
apt-get update
apt-get install -y jq
fi
else
if ! machine_has jq; then
say "jq未安装,开始安装依赖..."
apk update
apk add -y jq
fi
fi
}
# 检查安装unzip
check_unzip() {
if [ "$current_linux_os" = "debian" ]; then
if ! machine_has unzip; then
say "unzip未安装,开始安装依赖..."
apt-get update
apt-get install -y unzip
fi
else
if ! machine_has unzip; then
say "jq未安装,开始安装依赖..."
apk update
apk add -y unzip
fi
fi
}
# 检查dotnet
check_dotnet() {
eval $invocation
dotnetVersion=$(dotnet --version)
if [[ $dotnetVersion == 6.* ]]; then
say "已安装dotnet,当前版本:$dotnetVersion"
say "which dotnet: $(which dotnet)"
return 0
else
say "未安装"
return 1
fi
}
# 检查bilitool
check_bilitool() {
eval $invocation
TAG_FILE="./tag.txt"
touch $TAG_FILE
local STORED_TAG=$(cat $TAG_FILE 2>/dev/null)
#如果STORED_TAG为空,则返回1
if [[ -z $STORED_TAG ]]; then
say "tag.txt为空,未安装过"
return 1
fi
say "tag.txt记录的版本:$STORED_TAG"
# 查找当前目录下是否有叫Ray.BiliBiliTool.Console的文件
if [ -f "./Ray.BiliBiliTool.Console" ]; then
say "bilitool已安装"
bilitool_installed_version=$STORED_TAG
return 0
else
say "bilitool未安装"
return 1
fi
}
# 检查环境
check_installed() {
eval $invocation
if [ "$prefer_mode" == "dotnet" ]; then
check_dotnet
return $?
fi
if [ "$prefer_mode" == "bilitool" ]; then
check_bilitool
return $?
fi
return 1
}
# 使用官方脚本安装dotnet
install_dotnet_by_script() {
eval $invocation
say "再尝试使用官方脚本安装"
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel 6.0 --no-cdn --verbose
say "添加到PATH"
local exportFile="/root/.bashrc"
touch $exportFile
echo '' >>$exportFile
echo 'export DOTNET_ROOT=$HOME/.dotnet' >>$exportFile
echo 'export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools' >>$exportFile
. $exportFile
}
# 安装dotnet环境
install_dotnet() {
eval $invocation
say "开始安装dotnet"
say "当前系统:$current_linux_os"
if [[ $current_linux_os == "debian" ]]; then
say "使用apt安装"
if ! (curl -s -m 5 www.google.com >/dev/nul); then
say "机器位于墙内,切换为包源为国内镜像源"
cp /etc/apt/sources.list /etc/apt/sources.list.bak
sed -i 's/https:\/\/deb.debian.org/https:\/\/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
sed -i 's/http:\/\/deb.debian.org/https:\/\/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
apt-get update
fi
{
. /etc/os-release
curl -o packages-microsoft-prod.deb https://packages.microsoft.com/config/debian/$VERSION_ID/packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
apt-get update && apt-get install -y dotnet-sdk-6.0
} || {
install_dotnet_by_script
}
else
say "使用apk安装"
if ! (curl -s -m 5 www.google.com >/dev/nul); then
say "机器位于墙内,切换为包源为国内镜像源"
cp /etc/apk/repositories /etc/apk/repositories.bak
sed -i 's/https:\/\/dl-cdn.alpinelinux.org/https:\/\/mirrors.ustc.edu.cn/g' /etc/apk/repositories
sed -i 's/http:\/\/dl-cdn.alpinelinux.org/https:\/\/mirrors.ustc.edu.cn/g' /etc/apk/repositories
apk update
fi
{
apk add dotnet6-sdk
} || {
install_dotnet_by_script
}
fi
dotnet --version && say "which dotnet: $(which dotnet)" && say "安装成功"
return $?
}
# 从github获取bilitool下载地址
get_download_url() {
eval $invocation
tag=$1
url="${github_proxy}https://github.com/RayWangQvQ/BiliBiliToolPro/releases/download/$tag/bilibili-tool-pro-v$tag-$current_os-$machine_architecture.zip"
say "下载地址:$url"
echo $url
return 0
}
# 安装bilitool
install_bilitool() {
eval $invocation
say "开始安装bilitool"
# 获取最新的release信息
LATEST_RELEASE=$(curl -s https://api.github.com/repos/$bili_repo/releases/latest)
# 解析最新的tag名称
check_jq
LATEST_TAG=$(echo $LATEST_RELEASE | jq -r '.tag_name')
say "最新版本:$LATEST_TAG"
# 读取之前存储的tag并比较
if [ "$LATEST_TAG" != "$bilitool_installed_version" ]; then
# 如果不一样,则需要更新安装
ASSET_URL=$(get_download_url $LATEST_TAG)
# 使用curl下载文件到当前目录下的test.zip文件
local zip_file_name="bilitool-$LATEST_TAG.zip"
curl -L -o "$zip_file_name" $ASSET_URL
# 解压
check_unzip
unzip -jo "$zip_file_name" -d ./ &&
rm "$zip_file_name" &&
rm -f appsettings.*
# 更新tag.txt文件
echo $LATEST_TAG >./tag.txt
else
say "已经是最新版本,无需下载。"
fi
}
## 安装dotnet(如果未安装过)
install() {
eval $invocation
if check_installed; then
say "环境正常,本次无需安装"
return 0
else
say "开始安装环境"
if [ "$prefer_mode" == "dotnet" ]; then
install_dotnet || {
say_err "安装失败"
say_err "请根据文档自行在青龙容器中安装dotnet:https://learn.microsoft.com/zh-cn/dotnet/core/install/linux-$current_linux_os"
say_err "或者尝试切换运行模式为bilitool,它不需要安装dotnet:https://github.com/RayWangQvQ/BiliBiliToolPro/blob/develop/qinglong/README.md"
exit 1
}
fi
if [ "$prefer_mode" == "bilitool" ]; then
install_bilitool || {
say_err "安装失败,请检查日志并重试"
say_err "或者尝试切换运行模式为dotnet:https://github.com/RayWangQvQ/BiliBiliToolPro/blob/develop/qinglong/README.md"
exit 1
}
fi
return $?
fi
}
# 运行bilitool任务
run_task() {
eval $invocation
local target_code=$1
export Ray_PlateformType=QingLong
export Ray_RunTasks=$target_code
cd $qinglong_bili_repo_dir/src/Ray.BiliBiliTool.Console
if [ "$prefer_mode" == "dotnet" ]; then
dotnet run --ENVIRONMENT=Production
else
cp -f $qinglong_bili_repo_dir/bin/Ray.BiliBiliTool.Console .
chmod +x ./Ray.BiliBiliTool.Console && ./Ray.BiliBiliTool.Console --ENVIRONMENT=Production
fi
}
check_os
install