测试shell

This commit is contained in:
whyour 2021-04-30 14:05:04 +08:00
parent e5e8c45e8b
commit 7c15a074e0
16 changed files with 191 additions and 1078 deletions

View File

@ -31,7 +31,6 @@ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
&& cd ${QL_DIR} \ && cd ${QL_DIR} \
&& cp -f .env.example .env \ && cp -f .env.example .env \
&& ln -sf ${QL_DIR}/shell/task.sh /usr/local/bin/task \ && ln -sf ${QL_DIR}/shell/task.sh /usr/local/bin/task \
&& ln -sf ${QL_DIR}/shell/task.sh /usr/local/bin/mytask \
&& ln -sf ${QL_DIR}/shell/update.sh /usr/local/bin/update \ && ln -sf ${QL_DIR}/shell/update.sh /usr/local/bin/update \
&& ln -sf ${QL_DIR}/shell/rmlog.sh /usr/local/bin/rmlog \ && ln -sf ${QL_DIR}/shell/rmlog.sh /usr/local/bin/rmlog \
&& ln -sf ${QL_DIR}/shell/notify.sh /usr/local/bin/notify \ && ln -sf ${QL_DIR}/shell/notify.sh /usr/local/bin/notify \

View File

@ -11,15 +11,6 @@ echo -e "======================2. 检测配置文件========================\n"
[ ! -d ${QL_DIR}/db ] && mkdir -p ${QL_DIR}/db [ ! -d ${QL_DIR}/db ] && mkdir -p ${QL_DIR}/db
[ ! -d ${QL_DIR}/manual_log ] && mkdir -p ${QL_DIR}/manual_log [ ! -d ${QL_DIR}/manual_log ] && mkdir -p ${QL_DIR}/manual_log
if [ ! -s ${QL_DIR}/config/crontab.list ]
then
echo -e "检测到config配置目录下不存在crontab.list或存在但文件为空从示例文件复制一份用于初始化...\n"
cp -fv ${QL_DIR}/sample/crontab.sample.list ${QL_DIR}/config/crontab.list
perl -i -pe "{s|CMD_UPDATE|update|g; s|CMD_REBUILD|rebuild|g; s|CMD_RMLOG|rmlog|g; s|CMD_TASK|task|g; s|CMD_MYTASK|mytask|g}" ${QL_DIR}/config/crontab.list
fi
crontab ${QL_DIR}/config/crontab.list
echo -e "成功添加定时任务...\n"
if [ ! -s ${QL_DIR}/config/cookie.sh ]; then if [ ! -s ${QL_DIR}/config/cookie.sh ]; then
echo -e "检测到config配置目录下不存在cookie.sh从示例文件复制一份用于初始化...\n" echo -e "检测到config配置目录下不存在cookie.sh从示例文件复制一份用于初始化...\n"
touch ${QL_DIR}/config/cookie.sh touch ${QL_DIR}/config/cookie.sh

1
gossh520_jd_scripts Submodule

@ -0,0 +1 @@
Subproject commit 85da52dc3fb5ede1c7cd70a2d3af62e37c18e341

View File

@ -37,7 +37,7 @@ AutoAddCron="true"
RmLogDaysAgo="7" RmLogDaysAgo="7"
## 在运行 task 或 mytask 命令时,随机延迟启动任务的最大延迟时间 ## 在运行 task 命令时,随机延迟启动任务的最大延迟时间
## 如果任务不是必须准点运行的任务,那么给它增加一个随机延迟,由你定义最大延迟时间,单位为秒,如 RandomDelay="300" ,表示任务将在 1-300 秒内随机延迟一个秒数,然后再运行 ## 如果任务不是必须准点运行的任务,那么给它增加一个随机延迟,由你定义最大延迟时间,单位为秒,如 RandomDelay="300" ,表示任务将在 1-300 秒内随机延迟一个秒数,然后再运行
## 在crontab.list中在每小时第0-2分、第30-31分、第59分这几个时间内启动的任务均算作必须准点运行的任务在启动这些任务时即使你定义了RandomDelay也将准点运行不启用随机延迟 ## 在crontab.list中在每小时第0-2分、第30-31分、第59分这几个时间内启动的任务均算作必须准点运行的任务在启动这些任务时即使你定义了RandomDelay也将准点运行不启用随机延迟
## 在crontab.list中除掉每小时上述时间启动的任务外其他任务在你定义了 RandomDelay 的情况下,一律启用随机延迟,但如果你给某些任务添加了 "now" 或者 "conc",那么这些任务也将无视随机延迟直接启动 ## 在crontab.list中除掉每小时上述时间启动的任务外其他任务在你定义了 RandomDelay 的情况下,一律启用随机延迟,但如果你给某些任务添加了 "now" 或者 "conc",那么这些任务也将无视随机延迟直接启动

View File

@ -1,11 +0,0 @@
# 更新所有脚本
55 2,13 * * * CMD_UPDATE all
# 重构面板
# 30 7 */7 * * CMD_REBUILD >> ${QL_DIR}/log/rebuild.log 2>&1
# 删除 RmLogDaysAgo 指定天数以前的旧日志,不记录日志
57 13 * * * CMD_RMLOG >/dev/null 2>&1
# 其他定时任务,由本程序自动添加的任务使用 CMD_TASK 命令,由你自行添加的任务请使用 CMD_MYTASK 命令
# 如果由本程序自动添加的任务你不想运行,注释即可,不要删除,请在最后保留一个空行

View File

@ -1,34 +0,0 @@
/*
此文件为Node.js专用其他用户请忽略
*/
//此处填写京东账号cookie。
let CookieJDs = [
'',//账号一ck,例:pt_key=XXX;pt_pin=XXX;
'',//账号二ck,例:pt_key=XXX;pt_pin=XXX;如有更多,依次类推
]
// 判断环境变量里面是否有京东ck
if (process.env.JD_COOKIE) {
if (process.env.JD_COOKIE.indexOf('&') > -1) {
CookieJDs = process.env.JD_COOKIE.split('&');
} else if (process.env.JD_COOKIE.indexOf('\n') > -1) {
CookieJDs = process.env.JD_COOKIE.split('\n');
} else {
CookieJDs = [process.env.JD_COOKIE];
}
}
if (JSON.stringify(process.env).indexOf('GITHUB')>-1) {
console.log(`请勿使用github action运行此脚本,无论你是从你自己的私库还是其他哪里拉取的源代码,都会导致我被封号\n`);
!(async () => {
await require('./sendNotify').sendNotify('提醒', `请勿使用github action、滥用github资源会封我仓库以及账号`)
await process.exit(0);
})()
}
CookieJDs = [...new Set(CookieJDs.filter(item => !!item))]
console.log(`\n====================共${CookieJDs.length}个京东账号Cookie=========\n`);
console.log(`==================脚本执行- 北京时间(UTC+8)${new Date(new Date().getTime() + new Date().getTimezoneOffset()*60*1000 + 8*60*60*1000).toLocaleString()}=====================\n`)
if (process.env.JD_DEBUG && process.env.JD_DEBUG === 'false') console.log = () => {};
for (let i = 0; i < CookieJDs.length; i++) {
if (!CookieJDs[i].match(/pt_pin=(.+?);/) || !CookieJDs[i].match(/pt_key=(.+?);/)) console.log(`\n提示:京东cookie 【${CookieJDs[i]}】填写不规范,可能会影响部分脚本正常使用。正确格式为: pt_key=xxx;pt_pin=xxx;(分号;不可少)\n`);
const index = (i + 1 === 1) ? '' : (i + 1);
exports['CookieJD' + index] = CookieJDs[i].trim();
}

View File

@ -1,29 +0,0 @@
{
"name": "lxk0301",
"version": "1.0.0",
"description": "{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**}",
"main": "AlipayManor.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/lxk0301/jd_scripts.git"
},
"keywords": [
""
],
"author": "LXK9301",
"license": "ISC",
"dependencies": {
"crypto-js": "^4.0.0",
"download": "^8.0.0",
"got": "^11.5.1",
"http-server": "^0.12.3",
"qrcode-terminal": "^0.12.0",
"request": "^2.88.2",
"tough-cookie": "^4.0.0",
"tunnel": "0.0.6",
"ws": "^7.4.3"
}
}

File diff suppressed because one or more lines are too long

0
shell/api.sh Normal file → Executable file
View File

View File

@ -1 +0,0 @@
task.sh

View File

@ -1,4 +1,4 @@
const notify = require('../scripts/sendNotify.js'); const notify = require('/ql/scripts/sendNotify.js');
const title = process.argv[2]; const title = process.argv[2];
const content = process.argv[3]; const content = process.argv[3];

View File

@ -1,23 +0,0 @@
#!/usr/bin/env bash
dir_shell=$(dirname $(readlink -f "$0"))
dir_root=$(cd $dir_shell; cd ..; pwd)
echo -e "更新qinglong...\n"
cd $dir_root
git fetch --all
git pull
echo -e "更新更新qinglong完成...\n"
echo -e "重新build...\n"
yarn install --network-timeout 1000000000 || yarn install --registry=https://registry.npm.taobao.org --network-timeout 1000000000
yarn build
yarn build-back
yarn cache clean
echo -e "重新build完成...\n"
echo -e "重启服务...\n"
pm2 restart panel 2>/dev/null || pm2 start $dir_root/build/app.js -n panel
nginx -s reload
echo -e "重启服务完成...\n"

View File

@ -2,7 +2,7 @@
## 判断环境 ## 判断环境
dir_shell=$(dirname $(readlink -f "$0")) dir_shell=$(dirname $(readlink -f "$0"))
dir_root=$(cd $dir_shell; cd ..; pwd) dir_root=$(cd $dir_shell; pwd)
## 导入通用变量与函数 ## 导入通用变量与函数
. $dir_shell/share.sh . $dir_shell/share.sh

67
shell/share.sh Normal file → Executable file
View File

@ -2,6 +2,7 @@
dir_sample=$dir_root/sample dir_sample=$dir_root/sample
dir_config=$dir_root/config dir_config=$dir_root/config
dir_scripts=$dir_root/scripts dir_scripts=$dir_root/scripts
dir_repo=$dir_root/repo
dir_raw=$dir_scripts/raw dir_raw=$dir_scripts/raw
dir_log=$dir_root/log dir_log=$dir_root/log
dir_list_tmp=$dir_log/.tmp dir_list_tmp=$dir_log/.tmp
@ -34,13 +35,11 @@ var_name=(
## 软连接及其原始文件对应关系 ## 软连接及其原始文件对应关系
link_name=( link_name=(
task task
mytask
rmlog rmlog
update update
rebuild rebuild
) )
original_name=( original_name=(
task.sh
task.sh task.sh
rmlog.sh rmlog.sh
update.sh update.sh
@ -69,15 +68,6 @@ import_config_and_check () {
fi fi
} }
## 发送通知依赖于import_config_and_check或import_config_no_check$1标题$2内容
notify () {
local title=$(echo $1 | perl -pe 's|-|_|g')
local msg="$(echo -e $2)"
if [ -d $dir_scripts_node_modules ]; then
node $dir_shell/notify.js "$title" "$msg"
fi
}
## 创建目录,$1目录的绝对路径 ## 创建目录,$1目录的绝对路径
make_dir () { make_dir () {
local dir=$1 local dir=$1
@ -154,52 +144,6 @@ define_cmd () {
done done
} }
## 统计 own 仓库数量
count_own_repo_sum () {
own_repo_sum=0
for ((i=1; i<=1000; i++)); do
local tmp1=RepoUrl$i
local tmp2=${!tmp1}
[[ $tmp2 ]] && own_repo_sum=$i || break
done
}
## 形成 own 仓库的文件夹名清单依赖于import_config_and_check或import_config_no_check
## array_own_repo_pathrepo存放的绝对路径组成的数组array_own_scripts_path所有要使用的脚本所在的绝对路径组成的数组
gen_own_dir_and_path () {
local scripts_path_num="-1"
local repo_num tmp1 tmp2 tmp3 tmp4 tmp5 dir
if [[ $own_repo_sum -ge 1 ]]; then
for ((i=1; i<=$own_repo_sum; i++)); do
repo_num=$((i - 1))
tmp1=RepoUrl$i
array_own_repo_url[$repo_num]=${!tmp1}
tmp2=RepoBranch$i
array_own_repo_branch[$repo_num]=${!tmp2}
array_own_repo_dir[$repo_num]=$(echo ${array_own_repo_url[$repo_num]} | perl -pe "s|\.git||" | awk -F "/|:" '{print $((NF - 1)) "_" $NF}')
array_own_repo_path[$repo_num]=$dir_scripts/${array_own_repo_dir[$repo_num]}
tmp3=RepoPath$i
if [[ ${!tmp3} ]]; then
for dir in ${!tmp3}; do
let scripts_path_num++
tmp4="${array_own_repo_dir[repo_num]}/$dir"
tmp5=$(echo $tmp4 | perl -pe "{s|//|/|g; s|/$||}") # 去掉多余的/
array_own_scripts_path[$scripts_path_num]="$dir_scripts/$tmp5"
done
else
let scripts_path_num++
array_own_scripts_path[$scripts_path_num]="${array_own_repo_path[$repo_num]}"
fi
done
fi
if [[ ${#RawUrl[*]} -ge 1 ]]; then
let scripts_path_num++
array_own_scripts_path[$scripts_path_num]=$dir_raw # 只有own脚本所在绝对路径附加了raw文件夹其他数组均不附加
fi
}
## 修复配置文件 ## 修复配置文件
fix_config () { fix_config () {
make_dir $dir_config make_dir $dir_config
@ -208,16 +152,9 @@ fix_config () {
cp -fv $file_config_sample $file_config_user cp -fv $file_config_sample $file_config_user
echo echo
fi fi
if [ ! -s $list_crontab_user ]; then
echo -e "复制一份 $list_crontab_sample$list_crontab_user,这是你自己的 crontab.list\n"
cp -fv $list_crontab_sample $list_crontab_user
echo
fi
perl -i -pe "{ perl -i -pe "{
s|CMD_UPDATE|$cmd_update|g; s|CMD_UPDATE|$cmd_update|g;
s|CMD_REBUILD|$cmd_rebuild|g;
s|CMD_RMLOG|$cmd_rmlog|g; s|CMD_RMLOG|$cmd_rmlog|g;
s|CMD_TASK|$cmd_task|g; s|CMD_TASK|$cmd_task|g
s|CMD_MYTASK|$cmd_mytask|g
}" $list_crontab_user }" $list_crontab_user
} }

33
shell/task.sh Normal file → Executable file
View File

@ -2,18 +2,11 @@
## 路径 ## 路径
dir_shell=$(dirname $(readlink -f "$0")) dir_shell=$(dirname $(readlink -f "$0"))
dir_root=$(cd $dir_shell; cd ..; pwd) dir_root=$(cd $dir_shell; pwd)
## 导入通用变量与函数 ## 导入通用变量与函数
. $dir_shell/share.sh . $dir_shell/share.sh
## 更新crontab
update_crontab () {
if [[ $(cat $list_crontab_user) != $(crontab -l) ]]; then
crontab $list_crontab_user
fi
}
## 组合Cookie和互助码子程序$1要组合的内容 ## 组合Cookie和互助码子程序$1要组合的内容
combine_sub () { combine_sub () {
local what_combine=$1 local what_combine=$1
@ -68,20 +61,16 @@ random_delay () {
## scripts目录下所有可运行脚本数组 ## scripts目录下所有可运行脚本数组
gen_array_scripts () { gen_array_scripts () {
import_config_no_check import_config_no_check
count_own_repo_sum
gen_own_dir_and_path
local dir_current=$(pwd) local dir_current=$(pwd)
local i="-1" local i="-1"
for ((scripts_path_num=0; scripts_path_num<${#array_own_scripts_path[*]}; scripts_path_num++)); do cd $dir_scripts
cd ${array_own_scripts_path[$scripts_path_num]} for file in $(ls); do
for file in $(ls); do if [ -f $file ] && [[ $file == *.js && $file != sendNotify.js && $file != JD_extra_cookie.js ]]; then
if [ -f $file ] && [[ $(grep "new Env" $file) ]] && [[ $file == *.js && $file != sendNotify.js && $file != JD_extra_cookie.js ]]; then let i++
let i++ array_scripts[i]=$(echo "$file" | perl -pe "s|$dir_scripts/||g")
array_scripts[i]=$(echo "${array_own_scripts_path[$scripts_path_num]}/$file" | perl -pe "s|$dir_scripts/||g") array_scripts_name[i]=$(grep "new Env" $file | awk -F "'|\"" '{print $2}' | head -1)
array_scripts_name[i]=$(grep "new Env" $file | awk -F "'|\"" '{print $2}' | head -1) [[ -z ${array_scripts_name[i]} ]] && array_scripts_name[i]="<未识别出活动名称>"
[[ -z ${array_scripts_name[i]} ]] && array_scripts_name[i]="<未识别出活动名称>" fi
fi
done
done done
cd $dir_current cd $dir_current
} }
@ -113,7 +102,6 @@ run_normal () {
cd $dir_scripts cd $dir_scripts
if [ -f $p1 ]; then if [ -f $p1 ]; then
import_config_and_check "$p1" import_config_and_check "$p1"
update_crontab
define_program "$p1" define_program "$p1"
combine_all combine_all
[[ $# -eq 1 ]] && random_delay [[ $# -eq 1 ]] && random_delay
@ -122,7 +110,6 @@ run_normal () {
make_dir "$dir_log/$p1" make_dir "$dir_log/$p1"
$which_program $p1 2>&1 | tee $log_path $which_program $p1 2>&1 | tee $log_path
else else
update_crontab
echo -e "\n $p1 脚本不存在,请确认...\n" echo -e "\n $p1 脚本不存在,请确认...\n"
usage usage
fi fi
@ -135,7 +122,6 @@ run_concurrent () {
cd $dir_scripts cd $dir_scripts
if [ -f $p1 ]; then if [ -f $p1 ]; then
import_config_and_check "$p1" import_config_and_check "$p1"
update_crontab
define_program define_program
make_dir $dir_log/$p1 make_dir $dir_log/$p1
log_time=$(date "+%Y-%m-%d-%H-%M-%S.%N") log_time=$(date "+%Y-%m-%d-%H-%M-%S.%N")
@ -149,7 +135,6 @@ run_concurrent () {
$which_program $p1 &>$log_path & $which_program $p1 &>$log_path &
done done
else else
update_crontab
echo -e "\n $p1 脚本不存在,请确认...\n" echo -e "\n $p1 脚本不存在,请确认...\n"
usage usage
fi fi

339
shell/update.sh Normal file → Executable file
View File

@ -1,13 +1,24 @@
#!/usr/bin/env bash #!/usr/bin/env bash
## 文件路径、脚本网址 ## 文件路径、脚本网址
dir_shell=$(dirname $(readlink -f "$0")) # dir_shell=$(dirname $(readlink -f "$0"))
dir_root=$(cd $dir_shell; cd ..; pwd) # dir_root=$(
# cd $dir_shell
# cd ..
# pwd
# )
dir_shell=$(pwd)
dir_root=$(
cd $dir_shell
pwd
)
send_mark=$dir_shell/send_mark send_mark=$dir_shell/send_mark
## 导入通用变量与函数 # 导入通用变量与函数
. $dir_shell/share.sh # . $dir_shell/share.sh
. $dir_shell/api.sh # . $dir_shell/api.sh
. ./shell/share.sh
. ./shell/api.sh
## 导入配置文件,检测平台,创建软连接,识别命令,修复配置文件 ## 导入配置文件,检测平台,创建软连接,识别命令,修复配置文件
detect_termux detect_termux
@ -19,7 +30,7 @@ import_config_no_check "update"
get_token get_token
## 重置仓库remote urldocker专用$1要重置的目录$2要重置为的网址 ## 重置仓库remote urldocker专用$1要重置的目录$2要重置为的网址
reset_romote_url () { reset_romote_url() {
local dir_current=$(pwd) local dir_current=$(pwd)
local dir_work=$1 local dir_work=$1
local url=$2 local url=$2
@ -33,7 +44,7 @@ reset_romote_url () {
} }
## 克隆脚本,$1仓库地址$2仓库保存路径$3分支可省略 ## 克隆脚本,$1仓库地址$2仓库保存路径$3分支可省略
git_clone_scripts () { git_clone_scripts() {
local url=$1 local url=$1
local dir=$2 local dir=$2
local branch=$3 local branch=$3
@ -44,7 +55,7 @@ git_clone_scripts () {
} }
## 更新脚本,$1仓库保存路径 ## 更新脚本,$1仓库保存路径
git_pull_scripts () { git_pull_scripts() {
local dir_current=$(pwd) local dir_current=$(pwd)
local dir_work=$1 local dir_work=$1
cd $dir_work cd $dir_work
@ -56,51 +67,27 @@ git_pull_scripts () {
cd $dir_current cd $dir_current
} }
## 生成脚本的路径清单文件
gen_list_own () {
local dir_current=$(pwd)
rm -f $dir_list_tmp/own*.list >/dev/null 2>&1
for ((i=0; i<${#array_own_scripts_path[*]}; i++)); do
cd ${array_own_scripts_path[i]}
if [[ $(ls *.js 2>/dev/null) ]]; then
for file in $(ls *.js); do
if [ -f $file ]; then
perl -ne "{
print if /.*([\d\*]*[\*-\/,\d]*[\d\*] ){4}[\d\*]*[\*-\/,\d]*[\d\*]( |,|\").*\/?$file/
}" $file | \
perl -pe "{
s|.*(([\d\*]*[\*-\/,\d]*[\d\*] ){4}[\d\*]*[\*-\/,\d]*[\d\*])( \|,\|\").*/?$file.*|${array_own_scripts_path[i]}/$file|g;
s|$dir_scripts/||
}" | head -1 >> $list_own_scripts
fi
done
fi
done
grep -E "$cmd_task " $list_crontab_user | perl -pe "s|.* $cmd_task ([^\s]+)( .+\|$)|\1|" | sort -u > $list_own_user
cd $dir_current
}
## 检测cron的差异$1脚本清单文件路径$2cron任务清单文件路径$3增加任务清单文件路径$4删除任务清单文件路径 ## 检测cron的差异$1脚本清单文件路径$2cron任务清单文件路径$3增加任务清单文件路径$4删除任务清单文件路径
diff_cron () { diff_cron() {
make_dir $dir_list_tmp make_dir $dir_list_tmp
local list_scripts="$1" local list_scripts="$1"
local list_task="$2" local list_task="$2"
local list_add="$3" local list_add="$3"
local list_drop="$4" local list_drop="$4"
if [ -s $list_task ]; then if [ -s $list_task ]; then
grep -vwf $list_task $list_scripts > $list_add grep -vwf $list_task $list_scripts >$list_add
elif [ ! -s $list_task ] && [ -s $list_scripts ]; then elif [ ! -s $list_task ] && [ -s $list_scripts ]; then
cp -f $list_scripts $list_add cp -f $list_scripts $list_add
fi fi
if [ -s $list_scripts ]; then if [ -s $list_scripts ]; then
grep -vwf $list_scripts $list_task > $list_drop grep -vwf $list_scripts $list_task >$list_drop
else else
cp -f $list_task $list_drop cp -f $list_task $list_drop
fi fi
} }
## 更新docker-entrypointdocker专用 ## 更新docker-entrypointdocker专用
update_docker_entrypoint () { update_docker_entrypoint() {
if [[ $QL_DIR ]] && [[ $(diff $dir_root/docker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh) ]]; then if [[ $QL_DIR ]] && [[ $(diff $dir_root/docker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh) ]]; then
cp -f $dir_root/docker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh cp -f $dir_root/docker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
chmod 777 /usr/local/bin/docker-entrypoint.sh chmod 777 /usr/local/bin/docker-entrypoint.sh
@ -108,7 +95,7 @@ update_docker_entrypoint () {
} }
## 检测配置文件版本 ## 检测配置文件版本
detect_config_version () { detect_config_version() {
## 识别出两个文件的版本号 ## 识别出两个文件的版本号
ver_config_sample=$(grep " Version: " $file_config_sample | perl -pe "s|.+v((\d+\.?){3})|\1|") ver_config_sample=$(grep " Version: " $file_config_sample | perl -pe "s|.+v((\d+\.?){3})|\1|")
[ -f $file_config_user ] && ver_config_user=$(grep " Version: " $file_config_user | perl -pe "s|.+v((\d+\.?){3})|\1|") [ -f $file_config_user ] && ver_config_user=$(grep " Version: " $file_config_user | perl -pe "s|.+v((\d+\.?){3})|\1|")
@ -127,7 +114,7 @@ detect_config_version () {
local notify_content="更新日期: $update_date\n用户版本: $ver_config_user\n新的版本: $ver_config_sample\n更新内容: $update_content\n更新说明: 如需使用新功能请对照config.sample.sh将相关新参数手动增加到你自己的config.sh中否则请无视本消息。本消息只在该新版本配置文件更新当天发送一次。\n" local notify_content="更新日期: $update_date\n用户版本: $ver_config_user\n新的版本: $ver_config_sample\n更新内容: $update_content\n更新说明: 如需使用新功能请对照config.sample.sh将相关新参数手动增加到你自己的config.sh中否则请无视本消息。本消息只在该新版本配置文件更新当天发送一次。\n"
echo -e $notify_content echo -e $notify_content
notify "$notify_title" "$notify_content" notify "$notify_title" "$notify_content"
[[ $? -eq 0 ]] && echo $ver_config_sample > $send_mark [[ $? -eq 0 ]] && echo $ver_config_sample >$send_mark
fi fi
else else
[ -f $send_mark ] && rm -f $send_mark [ -f $send_mark ] && rm -f $send_mark
@ -135,7 +122,7 @@ detect_config_version () {
} }
## npm install 子程序判断是否为安卓判断是否安装有yarn ## npm install 子程序判断是否为安卓判断是否安装有yarn
npm_install_sub () { npm_install_sub() {
if [ $is_termux -eq 1 ]; then if [ $is_termux -eq 1 ]; then
npm install --production --no-save --no-bin-links --registry=https://registry.npm.taobao.org || npm install --production --no-bin-links --no-save npm install --production --no-save --no-bin-links --registry=https://registry.npm.taobao.org || npm install --production --no-bin-links --no-save
elif ! type yarn >/dev/null 2>&1; then elif ! type yarn >/dev/null 2>&1; then
@ -147,7 +134,7 @@ npm_install_sub () {
} }
## npm install$1package.json文件所在路径 ## npm install$1package.json文件所在路径
npm_install_1 () { npm_install_1() {
local dir_current=$(pwd) local dir_current=$(pwd)
local dir_work=$1 local dir_work=$1
@ -158,7 +145,7 @@ npm_install_1 () {
cd $dir_current cd $dir_current
} }
npm_install_2 () { npm_install_2() {
local dir_current=$(pwd) local dir_current=$(pwd)
local dir_work=$1 local dir_work=$1
@ -171,7 +158,7 @@ npm_install_2 () {
} }
## 比对两个文件,$1比$2新时将$1复制为$2 ## 比对两个文件,$1比$2新时将$1复制为$2
diff_and_copy () { diff_and_copy() {
local copy_source=$1 local copy_source=$1
local copy_to=$2 local copy_to=$2
if [ ! -s $copy_to ] || [[ $(diff $copy_source $copy_to) ]]; then if [ ! -s $copy_to ] || [[ $(diff $copy_source $copy_to) ]]; then
@ -180,20 +167,17 @@ diff_and_copy () {
} }
## 更新依赖 ## 更新依赖
update_depend () { update_depend() {
if [ ! -s $dir_scripts/package.json ] || [[ $(diff $dir_sample/package.json $dir_scripts/package.json) ]]; then if [ ! -s $dir_scripts/package.json ] || [[ $(diff $dir_sample/package.json $dir_scripts/package.json) ]]; then
cp -f $dir_sample/package.json $dir_scripts/package.json cp -f $dir_sample/package.json $dir_scripts/package.json
npm_install_2 $dir_scripts npm_install_2 $dir_scripts
fi fi
[ ! -d $dir_scripts/node_modules ] && npm_install_2 $dir_scripts [ ! -d $dir_scripts/node_modules ] && npm_install_2 $dir_scripts
diff_and_copy "$dir_sample/sendNotify.js" "$dir_scripts/sendNotify.js"
diff_and_copy "$dir_sample/jdCookie.js" "$dir_scripts/jdCookie.js"
} }
## 输出是否有新的或失效的定时任务,$1新的或失效的任务清单文件路径$2新/失效 ## 输出是否有新的或失效的定时任务,$1新的或失效的任务清单文件路径$2新/失效
output_list_add_drop () { output_list_add_drop() {
local list=$1 local list=$1
local type=$2 local type=$2
if [ -s $list ]; then if [ -s $list ]; then
@ -205,14 +189,14 @@ output_list_add_drop () {
## 自动删除失效的脚本与定时任务需要1.AutoDelCron/AutoDelCron 设置为 true2.正常更新js脚本没有报错3.存在失效任务4.crontab.list存在并且不为空 ## 自动删除失效的脚本与定时任务需要1.AutoDelCron/AutoDelCron 设置为 true2.正常更新js脚本没有报错3.存在失效任务4.crontab.list存在并且不为空
## $1失效任务清单文件路径 ## $1失效任务清单文件路径
del_cron () { del_cron() {
local list_drop=$1 local list_drop=$1
local detail detail2 local detail detail2
if [ -s $list_drop ] && [ -s $list_crontab_user ]; then if [ -s $list_drop ] && [ -s $list_crontab_user ]; then
detail=$(cat $list_drop) detail=$(cat $list_drop)
echo -e "开始尝试自动删除失效的定时任务...\n" echo -e "开始尝试自动删除失效的定时任务...\n"
for cron in $detail; do for cron in $detail; do
local id=$(cat $list_crontab_user | grep -E "$cmd_task $cron" | perl -pe "s|.*ID=(.*) $cmd_task $cron|\1|") local id=$(cat $list_crontab_user | grep -E "$cmd_task $cron$" | perl -pe "s|.*ID=(.*) $cmd_task $cron$|\1|")
del_cron_api "$id" del_cron_api "$id"
done done
detail2=$(echo $detail | perl -pe "s| |\\\n|g") detail2=$(echo $detail | perl -pe "s| |\\\n|g")
@ -223,74 +207,75 @@ del_cron () {
## 自动增加定时任务需要1.AutoAddCron 设置为 true2.正常更新js脚本没有报错3.存在新任务4.crontab.list存在并且不为空 ## 自动增加定时任务需要1.AutoAddCron 设置为 true2.正常更新js脚本没有报错3.存在新任务4.crontab.list存在并且不为空
## $1新任务清单文件路径 ## $1新任务清单文件路径
add_cron () { add_cron() {
local list_add=$1 local list_add=$1
if [ -s $list_crontab_user ]; then echo -e "开始尝试自动添加定时任务...\n"
echo -e "开始尝试自动添加定时任务...\n" local detail=$(cat $list_add)
local detail=$(cat $list_add) cd $dir_scripts
cd $dir_scripts for file_relative_path in $detail; do
for file_relative_path in $detail; do local file_name=$(echo $file_relative_path | awk -F "/" '{print $NF}')
local file_name=$(echo $file_relative_path | awk -F "/" '{print $NF}') if [ -f $file_relative_path ]; then
if [ -f $file_relative_path ]; then cron_line=$(
cron_line=$( perl -ne "{
perl -ne "{
print if /.*([\d\*]*[\*-\/,\d]*[\d\*] ){4}[\d\*]*[\*-\/,\d]*[\d\*]( |,|\").*$file_name/ print if /.*([\d\*]*[\*-\/,\d]*[\d\*] ){4}[\d\*]*[\*-\/,\d]*[\d\*]( |,|\").*$file_name/
}" $file_relative_path | \ }" $file_relative_path |
perl -pe "{ perl -pe "{
s|[^\d\*]*(([\d\*]*[\*-\/,\d]*[\d\*] ){4}[\d\*]*[\*-\/,\d]*[\d\*])( \|,\|\").*/?$file_name.*|\1:$cmd_task $file_relative_path|g; s|[^\d\*]*(([\d\*]*[\*-\/,\d]*[\d\*] ){4}[\d\*]*[\*-\/,\d]*[\d\*])( \|,\|\").*/?$file_name.*|\1:$cmd_task $file_relative_path|g;
s| | |g s| | |g
}" | sort -u | head -1 }" | sort -u | head -1
) )
cron_name=$(grep "new Env" $file_relative_path | awk -F "'|\"" '{print $2}' | head -1) cron_name=$(grep "new Env" $file_relative_path | awk -F "'|\"" '{print $2}' | head -1)
[[ -z $cron_name ]] && cron_name="$file_name" [[ -z $cron_name ]] && cron_name="$file_name"
add_cron_api "$cron_line:$cron_name" add_cron_api "$cron_line:$cron_name"
fi
done
exit_status=$?
local detail2=$(echo $detail | perl -pe "s| |\\\n|g")
if [[ $exit_status -eq 0 ]]; then
crontab $list_crontab_user
echo -e "成功添加新的定时任务...\n"
notify "新增任务通知" "成功添加新的定时任务:\n$detail2"
else
echo -e "添加新的定时任务出错,请手动添加...\n"
notify "新任务添加失败通知" "尝试自动添加以下新的定时任务出错,请手动添加:\n$detail2"
fi fi
done
exit_status=$?
local detail2=$(echo $detail | perl -pe "s| |\\\n|g")
if [[ $exit_status -eq 0 ]]; then
echo -e "成功添加新的定时任务...\n"
notify "新增任务通知" "成功添加新的定时任务:\n$detail2"
else
echo -e "添加新的定时任务出错,请手动添加...\n"
notify "新任务添加失败通知" "尝试自动添加以下新的定时任务出错,请手动添加:\n$detail2"
fi fi
} }
## 更新所有仓库 ## 更新仓库
update_own_repo () { update_repo() {
[[ ${#array_own_repo_url[*]} -gt 0 ]] && echo -e "--------------------------------------------------------------\n" echo -e "--------------------------------------------------------------\n"
for ((i=0; i<${#array_own_repo_url[*]}; i++)); do local url="$1"
if [ -d ${array_own_repo_path[i]}/.git ]; then local path="$2"
reset_romote_url ${array_own_repo_path[i]} ${array_own_repo_url[i]} local blackword="$3"
git_pull_scripts ${array_own_repo_path[i]} local urlTmp="${url%*/}"
else local repoTmp="${urlTmp##*/}"
git_clone_scripts ${array_own_repo_url[i]} ${array_own_repo_path[i]} ${array_own_repo_branch[i]} local repo="${repoTmp%.*}"
fi local tmp="${url%/*}"
if [[ $exit_status -eq 0 ]]; then local authorTmp1="${tmp##*/}"
echo -e "\n更新${array_own_repo_path[i]}成功...\n" local authorTmp2="${authorTmp1##*:}"
diff_and_copy "$dir_sample/sendNotify.js" "${array_own_repo_path[i]}/sendNotify.js" local author="${authorTmp2##*.}"
diff_and_copy "$dir_sample/jdCookie.js" "${array_own_repo_path[i]}/jdCookie.js"
else local repo_path="${author}_${repo}"
echo -e "\n更新${array_own_repo_path[i]}失败,请检查原因...\n" if [ -d ${repo_path}/.git ]; then
fi reset_romote_url ${repo_path} ${url}
done git_pull_scripts ${repo_path}
for ((i=0; i<${#array_own_scripts_path[*]}; i++)); do else
diff_and_copy "$dir_sample/sendNotify.js" "${array_own_scripts_path[i]}/sendNotify.js" git_clone_scripts ${url} ${repo_path}
diff_and_copy "$dir_sample/jdCookie.js" "${array_own_scripts_path[i]}/jdCookie.js" fi
done if [[ $exit_status -eq 0 ]]; then
echo -e "\n更新${repo_path}成功...\n"
else
echo -e "\n更新${repo_path}失败,请检查原因...\n"
fi
diff_scripts $repo_path $author $path $blackword
} }
## 更新所有 raw 文件 ## 更新所有 raw 文件
update_own_raw () { update_raw() {
if [[ ${#RawUrl[*]} -gt 0 ]]; then if [[ ${#RawUrl[*]} -gt 0 ]]; then
echo -e "--------------------------------------------------------------\n" echo -e "--------------------------------------------------------------\n"
make_dir $dir_raw make_dir $dir_raw
diff_and_copy "$dir_sample/sendNotify.js" "$dir_raw/sendNotify.js" for ((i = 0; i < ${#RawUrl[*]}; i++)); do
diff_and_copy "$dir_sample/jdCookie.js" "$dir_raw/jdCookie.js"
for ((i=0; i<${#RawUrl[*]}; i++)); do
raw_file_name[$i]=$(echo ${RawUrl[i]} | awk -F "/" '{print $NF}') raw_file_name[$i]=$(echo ${RawUrl[i]} | awk -F "/" '{print $NF}')
echo -e "开始下载:${RawUrl[i]} \n\n保存路径$dir_raw/${raw_file_name[$i]}\n" echo -e "开始下载:${RawUrl[i]} \n\n保存路径$dir_raw/${raw_file_name[$i]}\n"
wget -q --no-check-certificate -O "$dir_raw/${raw_file_name[$i]}.new" ${RawUrl[i]} wget -q --no-check-certificate -O "$dir_raw/${raw_file_name[$i]}.new" ${RawUrl[i]}
@ -307,10 +292,9 @@ update_own_raw () {
} }
## 调用用户自定义的extra.sh ## 调用用户自定义的extra.sh
run_extra_shell () { run_extra_shell() {
if [[ ${EnableExtraShell} == true ]]; then if [[ ${EnableExtraShell} == true ]]; then
if [ -f $file_extra_shell ] if [ -f $file_extra_shell ]; then
then
echo -e "--------------------------------------------------------------\n" echo -e "--------------------------------------------------------------\n"
. $file_extra_shell . $file_extra_shell
else else
@ -320,17 +304,17 @@ run_extra_shell () {
} }
## 脚本用法 ## 脚本用法
usage () { usage() {
echo -e "本脚本用法:" echo -e "本脚本用法:"
echo -e "1. $cmd_update all # 更新qinglong、所有你设置的仓库和raw文件如果启用了EnableExtraShell还将在最后运行你自己编写的extra.sh" echo -e "2. $cmd_update update # 只更新qinglong不会运行extra.sh"
echo -e "2. $cmd_update ql # 只更新qinglong和输入 $cmd_update qinglong 时功能一样不会运行extra.sh" echo -e "2. $cmd_update rebuild # 重新编译qinglong不会运行extra.sh"
echo -e "3. $cmd_update raw # 只更新raw文件不会运行extra.sh" echo -e "3. $cmd_update raw # 只更新raw文件不会运行extra.sh"
echo -e "4. $cmd_update repo # 更新所有设置的REPO不会运行extra.sh" echo -e "4. $cmd_update repo # 更新所有设置的REPO不会运行extra.sh"
echo -e "5. $cmd_update <folder> # 指定scripts脚本目录下某个文件夹名称只更新这个文件夹中的脚本当该文件夹已经存在并且是git仓库才可使用此命令不会运行extra.sh" echo -e "5. $cmd_update <folder> # 指定scripts脚本目录下某个文件夹名称只更新这个文件夹中的脚本当该文件夹已经存在并且是git仓库才可使用此命令不会运行extra.sh"
} }
## 更新qinglong ## 更新qinglong
update_qinglong () { update_qinglong() {
echo -e "--------------------------------------------------------------\n" echo -e "--------------------------------------------------------------\n"
git_pull_scripts $dir_root git_pull_scripts $dir_root
if [[ $exit_status -eq 0 ]]; then if [[ $exit_status -eq 0 ]]; then
@ -345,77 +329,102 @@ update_qinglong () {
fi fi
} }
## 更新所有脚本 ## 重新编译qinglong
update_all_scripts () { rebuild_qinglong() {
count_own_repo_sum echo -e "--------------------------------------------------------------\n"
gen_own_dir_and_path update_qinglong
if [[ ${#array_own_scripts_path[*]} -gt 0 ]]; then if [[ $exit_status -eq 0 ]]; then
update_own_repo echo -e "重新编译青龙...\n"
update_own_raw yarn install --network-timeout 1000000000 || yarn install --registry=https://registry.npm.taobao.org --network-timeout 1000000000
gen_list_own yarn build
diff_cron $list_own_scripts $list_own_user $list_own_add $list_own_drop yarn build-back
yarn cache clean
echo -e "重新编译青龙完成...\n"
if [ -s $list_own_drop ]; then echo -e "重启青龙...\n"
output_list_add_drop $list_own_drop "失效" pm2 restart panel 2>/dev/null || pm2 start $dir_root/build/app.js -n panel
[[ ${AutoDelCron} == true ]] && del_cron $list_own_drop nginx -s reload
fi echo -e "重启青龙完成...\n"
if [ -s $list_own_add ]; then fi
output_list_add_drop $list_own_add "新" }
if [[ ${AutoAddCron} == true ]]; then
add_cron $list_own_add ## 对比脚本
fi diff_scripts() {
gen_list_repo $1 $2 $3 $4
diff_cron $list_own_scripts $list_own_user $list_own_add $list_own_drop
if [ -s $list_own_drop ]; then
output_list_add_drop $list_own_drop "失效"
[[ ${AutoDelCron} == true ]] && del_cron $list_own_drop
fi
if [ -s $list_own_add ]; then
output_list_add_drop $list_own_add "新"
if [[ ${AutoAddCron} == true ]]; then
add_cron $list_own_add
fi fi
fi fi
} }
## 更新指定仓库 ## 生成脚本的路径清单文件
update_specify_scripts_repo () { gen_list_repo() {
local tmp_dir=$1 local dir_current=$(pwd)
if [ -d $dir_scripts/$tmp_dir ]; then local repo_path="$1"
if [ -d $dir_scripts/$tmp_dir/.git ]; then local author="$2"
git_pull_scripts $dir_scripts/$tmp_dir local path="$3"
else local blackword="$4"
echo -e "$dir_scripts/$tmp_dir 不是一个git仓库...\n" # rm -f $dir_list_tmp/own*.list >/dev/null 2>&1
fi
else cd ${repo_path}
echo -e "$dir_scripts/$tmp_dir 还不存在可能是还没有clone\n" files=$(find . -name "*.js")
usage if [ $path ]; then
files=$(find . -name "*.js" | egrep $path)
fi fi
if [ $blackword ]; then
files=$(find . -name "*.js" | egrep -v $blackword | egrep $path)
fi
for file in ${files}; do
if [ -f $file ]; then
# base=$(basename $file)
# echo $base | head -1 >>$list_own_scripts
perl -ne "{
print if /.*([\d\*]*[\*-\/,\d]*[\d\*] ){4}[\d\*]*[\*-\/,\d]*[\d\*]( |,|\").*\/?$file/
}" $file | perl -pe "{
s|.*(([\d\*]*[\*-\/,\d]*[\d\*] ){4}[\d\*]*[\*-\/,\d]*[\d\*])( \|,\|\").*/?$file.*|${repo_path}/$file|g;
s|$dir_scripts/||
}" | head -1 >> $list_own_scripts
fi
done
grep -E "$cmd_task $author" $list_crontab_user | perl -pe "s|.*ID=(.*) $cmd_task ($author_.*)\.*|\2|" | sort -u >$list_own_user
cd $dir_current
} }
main () { main() {
local p1=$1 local p1=$1
local p2=$2
local p3=$3
local p4=$4
log_time=$(date "+%Y-%m-%d-%H-%M-%S") log_time=$(date "+%Y-%m-%d-%H-%M-%S")
log_path="$dir_log/update/${log_time}_$p1.log" log_path="$dir_log/update/${log_time}_$p1.log"
make_dir "$dir_log/update" make_dir "$dir_log/update"
if [[ $# -ne 1 ]]; then case $p1 in
update)
update_qinglong | tee $log_path
run_extra_shell | tee -a $log_path
;;
rebuild)
rebuild_qinglong | tee $log_path
;;
repo)
update_repo "$p2" "$p3" "$p4" | tee $log_path
;;
raw)
update_raw | tee $log_path
;;
*)
echo -e "命令输入错误...\n" echo -e "命令输入错误...\n"
usage usage
else ;;
case $p1 in esac
all)
update_qinglong | tee $log_path
update_all_scripts | tee -a $log_path
run_extra_shell | tee -a $log_path
;;
ql | qinglong)
update_qinglong | tee $log_path
;;
repo)
count_own_repo_sum
gen_own_dir_and_path
update_own_repo | tee $log_path
;;
raw)
count_own_repo_sum
gen_own_dir_and_path
update_own_raw | tee $log_path
;;
*)
update_specify_scripts_repo "$p1" | tee $log_path
;;
esac
fi
} }
main "$@" main "$@"