mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-23 06:46:09 +08:00

* 重构shell (#17) * 更新正则 * 更新update命令 * 移除测试代码 * 重构删除日志命令 * 更新entrypoint * 更新dockerfile * 完善shell调用 * 修复share shell引用 * 修复entrypoint * 修复share shell * 修复share.sh * 修改依赖重装逻辑 * 更新docker entrypoint * curl 使用静默模式 * 更新ql raw * 修复添加单个任务 * 修复shell语法 * 添加定时任务进程 * 更新默认定时任务 * 更新定时任务重启schedule * 更新青龙重启逻辑 * 修复定时任务列表创建 * 修复schedule进程 * 修复entrypoint * 更新task命令 * pm2 restart替换成reload * 修复task命令参数引入 * 完善ql repo命令 * 修复update.sh * 更新ql repo命令 * ql repo添加登录验证,修复package.json示例 * 修复定时任务命令补全 * 修改默认cron端口 * 修复cron日志弹框异常 * 修改cron新建label * 修复ql repo命令 * 修复cron格式验证 * 修改日志目录格式 * 修改青龙remote url * 修复添加定时cron匹配 * 添加定时任务超时时间设置 * 暂时移除timeout命令 * 恢复定时任务timeout * 修复cookie.sh引用 * 修复shell变量自加 * 修复ck更新状态同步 * 增加tg bot测试,修改增删任务通知 * 修复shell函数返回值 * 修改添加任务日志打印 * 修改entrypoint日志 * 修复api日志打印 * 修改api日志打印 * 定时任务支持批量启用禁用删除运行 * 修改cron管理操作按钮响应样式 * 更新bot启动脚本 * 更新bot启动脚本 * 增加timeout默认值,修改session管理逻辑 * 更新config示例和通知日志 * 更新bot.sh * 更新启动bot命令 * 更新启动bot命令 * 修复task运行参数合并 * 增加停止定时任务功能 * 修复停止定时任务api * 更新停止定时任务日志 * 更新停止任务日志 * 修复删除cron api * 更新删除cron通知文本 * 更新命令提示 * 更新bot启动脚本
261 lines
7.2 KiB
Bash
Executable File
261 lines
7.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
|
||
## 目录
|
||
dir_root=/ql
|
||
dir_shell=$dir_root/shell
|
||
dir_sample=$dir_root/sample
|
||
dir_config=$dir_root/config
|
||
dir_scripts=$dir_root/scripts
|
||
dir_repo=$dir_root/repo
|
||
dir_raw=$dir_root/raw
|
||
dir_log=$dir_root/log
|
||
dir_db=$dir_root/db
|
||
dir_manual_log=$dir_root/manual_log
|
||
dir_list_tmp=$dir_log/.tmp
|
||
dir_code=$dir_log/code
|
||
dir_update_log=$dir_log/update
|
||
|
||
## 文件
|
||
file_config_sample=$dir_sample/config.sample.sh
|
||
file_cookie=$dir_config/cookie.sh
|
||
file_sharecode=$dir_config/sharecode.sh
|
||
file_config_user=$dir_config/config.sh
|
||
file_auth_sample=$dir_sample/auth.sample.json
|
||
file_auth_user=$dir_config/auth.json
|
||
file_extra_shell=$dir_config/extra.sh
|
||
file_notify_js_sample=$dir_sample/notify.js
|
||
file_notify_py_sample=$dir_sample/notify.py
|
||
file_notify_py=$dir_scripts/notify.py
|
||
file_notify_js=$dir_scripts/sendNotify.js
|
||
|
||
## 清单文件
|
||
list_crontab_user=$dir_config/crontab.list
|
||
list_crontab_sample=$dir_sample/crontab.sample.list
|
||
list_own_scripts=$dir_list_tmp/own_scripts.list
|
||
list_own_user=$dir_list_tmp/own_user.list
|
||
list_own_add=$dir_list_tmp/own_add.list
|
||
list_own_drop=$dir_list_tmp/own_drop.list
|
||
|
||
## 软连接及其原始文件对应关系
|
||
link_name=(
|
||
task
|
||
ql
|
||
notify
|
||
)
|
||
original_name=(
|
||
task.sh
|
||
update.sh
|
||
notify.sh
|
||
)
|
||
|
||
## 导入配置文件
|
||
import_config() {
|
||
[ -f $file_config_user ] && . $file_config_user
|
||
user_sum=0
|
||
for line in $(cat $file_cookie); do
|
||
let user_sum+=1
|
||
eval Cookie${user_sum}="\"$line\""
|
||
done
|
||
|
||
command_timeout_time=${CommandTimeoutTime:-"1h"}
|
||
}
|
||
|
||
## 创建目录,$1:目录的绝对路径
|
||
make_dir() {
|
||
local dir=$1
|
||
if [[ ! -d $dir ]]; then
|
||
mkdir -p $dir
|
||
fi
|
||
}
|
||
|
||
## 检测termux
|
||
detect_termux() {
|
||
if [[ ${ANDROID_RUNTIME_ROOT}${ANDROID_ROOT} ]] || [[ $PATH == *com.termux* ]]; then
|
||
is_termux=1
|
||
else
|
||
is_termux=0
|
||
fi
|
||
}
|
||
|
||
## 检测macos
|
||
detect_macos() {
|
||
[[ $(uname -s) == Darwin ]] && is_macos=1 || is_macos=0
|
||
}
|
||
|
||
## 生成随机数,$1:用来求余的数字
|
||
gen_random_num() {
|
||
local divi=$1
|
||
echo $((${RANDOM} % $divi))
|
||
}
|
||
|
||
## 创建软连接的子函数,$1:软连接文件路径,$2:要连接的对象
|
||
link_shell_sub() {
|
||
local link_path="$1"
|
||
local original_path="$2"
|
||
if [ ! -L $link_path ] || [[ $(readlink -f $link_path) != $original_path ]]; then
|
||
rm -f $link_path 2>/dev/null
|
||
ln -sf $original_path $link_path
|
||
fi
|
||
}
|
||
|
||
## 创建软连接
|
||
link_shell() {
|
||
if [[ $is_termux -eq 1 ]]; then
|
||
local path="/data/data/com.termux/files/usr/bin/"
|
||
elif [[ $PATH == */usr/local/bin* ]] && [ -d /usr/local/bin ]; then
|
||
local path="/usr/local/bin/"
|
||
else
|
||
local path=""
|
||
echo -e "脚本功能受限,请自行添加命令的软连接...\n"
|
||
fi
|
||
if [[ $path ]]; then
|
||
for ((i = 0; i < ${#link_name[*]}; i++)); do
|
||
link_shell_sub "$path${link_name[i]}" "$dir_shell/${original_name[i]}"
|
||
done
|
||
fi
|
||
}
|
||
|
||
## 定义各命令
|
||
define_cmd() {
|
||
local cmd_prefix cmd_suffix
|
||
if type task >/dev/null 2>&1; then
|
||
cmd_suffix=""
|
||
if [ -x "$dir_shell/task.sh" ]; then
|
||
cmd_prefix=""
|
||
else
|
||
cmd_prefix="bash "
|
||
fi
|
||
else
|
||
cmd_suffix=".sh"
|
||
if [ -x "$dir_shell/task.sh" ]; then
|
||
cmd_prefix="$dir_shell/"
|
||
else
|
||
cmd_prefix="bash $dir_shell/"
|
||
fi
|
||
fi
|
||
for ((i = 0; i < ${#link_name[*]}; i++)); do
|
||
export cmd_${link_name[i]}="${cmd_prefix}${link_name[i]}${cmd_suffix}"
|
||
done
|
||
}
|
||
|
||
## 修复配置文件
|
||
fix_config() {
|
||
make_dir $dir_config
|
||
make_dir $dir_log
|
||
make_dir $dir_db
|
||
make_dir $dir_manual_log
|
||
make_dir $dir_scripts
|
||
make_dir $dir_list_tmp
|
||
make_dir $dir_repo
|
||
make_dir $dir_raw
|
||
make_dir $dir_update_log
|
||
|
||
if [ ! -s $file_config_user ]; then
|
||
echo -e "复制一份 $file_config_sample 为 $file_config_user,随后请按注释编辑你的配置文件:$file_config_user\n"
|
||
cp -fv $file_config_sample $file_config_user
|
||
echo
|
||
fi
|
||
|
||
if [ ! -f $file_cookie ]; then
|
||
echo -e "检测到config配置目录下不存在cookie.sh,创建一个空文件用于初始化...\n"
|
||
touch $file_cookie
|
||
echo
|
||
fi
|
||
|
||
if [ ! -s $file_auth_user ]; then
|
||
echo -e "复制一份 $file_auth_sample 为 $file_auth_user\n"
|
||
cp -fv $file_auth_sample $file_auth_user
|
||
echo
|
||
fi
|
||
|
||
if [ ! -s $file_notify_py ]; then
|
||
echo -e "复制一份 $file_notify_py_sample 为 $file_notify_py\n"
|
||
cp -fv $file_notify_py_sample $file_notify_py
|
||
echo
|
||
fi
|
||
|
||
if [ ! -s $file_notify_js ]; then
|
||
echo -e "复制一份 $file_notify_js_sample 为 $file_notify_js\n"
|
||
cp -fv $file_notify_js_sample $file_notify_js
|
||
echo
|
||
fi
|
||
|
||
if [ -s /etc/nginx/conf.d/default.conf ]; then
|
||
echo -e "检测到默认nginx配置文件,删除...\n"
|
||
rm -f /etc/nginx/conf.d/default.conf
|
||
echo
|
||
fi
|
||
}
|
||
|
||
## npm install 子程序,判断是否为安卓,判断是否安装有yarn
|
||
npm_install_sub() {
|
||
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
|
||
elif ! type yarn >/dev/null 2>&1; then
|
||
npm install --production --no-save --registry=https://registry.npm.taobao.org || npm install --production --no-save
|
||
else
|
||
echo -e "检测到本机安装了 yarn,使用 yarn 替代 npm...\n"
|
||
yarn install --production --network-timeout 1000000000 --registry=https://registry.npm.taobao.org || yarn install --production --network-timeout 1000000000
|
||
fi
|
||
}
|
||
|
||
## npm install,$1:package.json文件所在路径
|
||
npm_install_1() {
|
||
local dir_current=$(pwd)
|
||
local dir_work=$1
|
||
|
||
cd $dir_work
|
||
echo -e "运行 npm install...\n"
|
||
npm_install_sub
|
||
[[ $? -ne 0 ]] && echo -e "\nnpm install 运行不成功,请进入 $dir_work 目录后手动运行 npm install...\n"
|
||
cd $dir_current
|
||
}
|
||
|
||
npm_install_2() {
|
||
local dir_current=$(pwd)
|
||
local dir_work=$1
|
||
|
||
cd $dir_work
|
||
echo -e "检测到 $dir_work 的依赖包有变化,运行 npm install...\n"
|
||
npm_install_sub
|
||
if [[ $? -ne 0 ]]; then
|
||
echo -e "\n安装 $dir_work 的依赖包运行不成功,再次尝试一遍...\n"
|
||
npm_install_1 $dir_work
|
||
fi
|
||
cd $dir_current
|
||
}
|
||
|
||
## 比对两个文件,$1比$2新时,将$1复制为$2
|
||
diff_and_copy() {
|
||
local copy_source=$1
|
||
local copy_to=$2
|
||
if [ ! -s $copy_to ] || [[ $(diff $copy_source $copy_to) ]]; then
|
||
cp -f $copy_source $copy_to
|
||
fi
|
||
}
|
||
|
||
## 更新依赖
|
||
update_depend() {
|
||
local dir_current=$(pwd)
|
||
|
||
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
|
||
npm_install_2 $dir_scripts
|
||
fi
|
||
|
||
if [ ! -s $dir_scripts/requirements.txt ] || [[ $(diff $dir_sample/requirements.txt $dir_scripts/requirements.txt) ]]; then
|
||
cp -f $dir_sample/requirements.txt $dir_scripts/requirements.txt
|
||
cd $dir_scripts
|
||
pip3 install -r $dir_scripts/requirements.txt
|
||
fi
|
||
|
||
cd $dir_current
|
||
}
|
||
|
||
## 导入配置文件,检测平台,创建软连接,识别命令,修复配置文件
|
||
detect_termux
|
||
detect_macos
|
||
define_cmd
|
||
fix_config
|
||
import_config
|