添加(.dockerignore): 忽略新配置和构建文件,提升Docker构建效率

⬆️ 升级(Dockerfile): 更新基础镜像从python:3.8-alpine到python:3.9-alpine
📝 更新(docker-compose.yml): 挂载新的cron目录用于存放crontab任务
🔧 配置(crontab_list.sh): 创建新的定时任务脚本
🗑️ 删除(default_list.sh, custom-*-docker-compose.yml, my_crontab_list.sh): 移除过时的示例和脚本
♻️ 重构(start.sh): 简化启动脚本,使用`cron`目录下的定时任务文件
 功能(docker_start.sh): 添加cron目录,并下载crontab_list.sh脚本
This commit is contained in:
shitao 2024-01-14 13:03:08 +08:00
parent 9c705125f3
commit 4259874bde
12 changed files with 37 additions and 168 deletions

4
docker/.dockerignore Normal file
View File

@ -0,0 +1,4 @@
config.template.json
crontab_list.sh
Makefile
docker-compose.yml

View File

@ -1,12 +1,11 @@
FROM python:3.8-alpine
MAINTAINER Sitoi <Sitoi0418@gmail.com>
FROM python:3.9-alpine
WORKDIR /dailycheckin
COPY ./start.sh /usr/local/bin
RUN set -ex \
&& apk update && apk upgrade\
# && apk add --no-cache tzdata moreutils git gcc g++ py-pip mysql-dev linux-headers libffi-dev openssl-dev\
&& apk add --no-cache gcc g++ make libffi-dev openssl-dev\
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& chmod +x /usr/local/bin/start.sh \
@ -15,5 +14,4 @@ RUN set -ex \
ADD . /dailycheckin
ENTRYPOINT ["start.sh"]

9
docker/crontab_list.sh Normal file
View File

@ -0,0 +1,9 @@
##############默认任务##############
# 每 12 小时更新 Pipy 包,如果不需要更新 pypi 包请注释掉下面这行
0 */12 * * * echo "定时任务更新依赖..." && pip install dailycheckin --upgrade --user >> /dailycheckin/logs/update-pypi.log 2>&1
# 每天的 23:50 分清理一次日志
50 23 */2 * * rm -rf /dailycheckin/logs/*.log
##############每日签到一次任务##############
# 每日签到(9:00 执行全部签到)
0 9 * * * cd /dailycheckin && dailycheckin >> /dailycheckin/logs/dailycheckin.log 2>&1

View File

@ -1,9 +0,0 @@
#必须要的默认定时任务请勿删除
0 */12 * * * sh /dailycheckin/default_task.sh >> /dailycheckin/logs/default_task.log 2>&1
# 每天的 23:50 分清理一次日志
50 23 */2 * * rm -rf /dailycheckin/logs/*.log
##############每日签到一次任务##############
# 每日签到(9:00 执行一次)
0 9 * * * cd /dailycheckin && dailycheckin >> /dailycheckin/logs/dailycheckin.log 2>&1

View File

@ -4,61 +4,8 @@ set -e
export LANG="zh_CN.UTF-8"
echo "定时任务更新依赖..."
pip install dailycheckin --upgrade
defaultListFile="/dailycheckin/default_list.sh"
customListFile="/dailycheckin/$CUSTOM_LIST_FILE"
mergedListFile="/dailycheckin/merged_list_file.sh"
if type ts >/dev/null 2>&1; then
echo 'moreutils tools installed, default task append |ts output'
echo '系统已安装 moreutils 工具包默认定时任务增加ts 输出'
##复制一个新文件来追加|ts防止git pull的时候冲突
cp $defaultListFile /dailycheckin/crontab_list.sh
defaultListFile="/dailycheckin/crontab_list.sh"
sed -i 's/>>/|ts >>/g' $defaultListFile
fi
if [ $CUSTOM_LIST_FILE ]; then
echo "You have configured a custom list file: $CUSTOM_LIST_FILE, custom list merge type: $CUSTOM_LIST_MERGE_TYPE..."
echo "您配置了自定义任务文件:$CUSTOM_LIST_FILE,自定义任务类型为:$CUSTOM_LIST_MERGE_TYPE..."
if [ -f "$customListFile" ]; then
if [ $CUSTOM_LIST_MERGE_TYPE == "append" ]; then
echo "merge default list file: $DEFAULT_LIST_FILE and custom list file: $CUSTOM_LIST_FILE"
echo "合并默认定时任务文件:$DEFAULT_LIST_FILE 和 自定义定时任务文件:$CUSTOM_LIST_FILE"
cat $defaultListFile >$mergedListFile
echo -e "" >>$mergedListFile
cat $customListFile >>$mergedListFile
elif [ $CUSTOM_LIST_MERGE_TYPE == "overwrite" ]; then
cat $customListFile >$mergedListFile
echo "merge custom list file: $CUSTOM_LIST_FILE..."
echo "合并自定义任务文件:$CUSTOM_LIST_FILE"
touch "$customListFile"
else
echo "配置配置了错误的自定义定时任务类型:$CUSTOM_LIST_MERGE_TYPE自定义任务类型为只能为append或者overwrite..."
cat $defaultListFile >$mergedListFile
fi
else
echo "Not found custom list file: $CUSTOM_LIST_FILE ,use default list file: $DEFAULT_LIST_FILE"
echo "自定义任务文件:$CUSTOM_LIST_FILE 未找到,使用默认配置$DEFAULT_LIST_FILE..."
cat $defaultListFile >$mergedListFile
fi
else
echo "The currently used is the default crontab task file: $DEFAULT_LIST_FILE ..."
echo "当前使用的为默认定时任务文件 $DEFAULT_LIST_FILE ..."
cat $defaultListFile >$mergedListFile
fi
# 判断最后要加载的定时任务是否包含默认定时任务,不包含的话就加进去
if [ $(grep -c "default_task.sh" $mergedListFile) -eq '0' ]; then
echo "Merged crontab task filethe required default task is not included, append default task..."
echo "合并后的定时任务文件,未包含必须的默认定时任务,增加默认定时任务..."
echo -e >>$mergedListFile
echo "0 */12 * * * sh /dailycheckin/default_task.sh >> /dailycheckin/logs/default_task.log 2>&1" >>$mergedListFile
fi
pip install dailycheckin --upgrade --user
echo "Load the latest crontab task file..."
echo "加载最新的定时任务文件..."
crontab $mergedListFile
crontab /dailycheckin/cron/crontab_list.sh

View File

@ -6,5 +6,6 @@ services:
restart: always
tty: true
volumes:
- ./config:/dailycheckin/config
- ./logs:/dailycheckin/logs
- ./config/:/dailycheckin/config/
- ./logs/:/dailycheckin/logs/
- ./cron/:/dailycheckin/cron/

View File

@ -1,14 +0,0 @@
version: '3'
services:
dailycheckin:
image: sitoi/dailycheckin:latest
container_name: dailycheckin
restart: always
tty: true
volumes:
- ./my_crontab_list.sh:/dailycheckin/my_crontab_list.sh
- ./config:/dailycheckin/config
- ./logs:/dailycheckin/logs
environment:
- CUSTOM_LIST_FILE=my_crontab_list.sh
- CUSTOM_LIST_MERGE_TYPE=append

View File

@ -1,14 +0,0 @@
version: '3'
services:
dailycheckin:
image: sitoi/dailycheckin:latest
container_name: dailycheckin
restart: always
tty: true
volumes:
- ./my_crontab_list.sh:/dailycheckin/my_crontab_list.sh
- ./config:/dailycheckin/config
- ./logs:/dailycheckin/logs
environment:
- CUSTOM_LIST_FILE=my_crontab_list.sh
- CUSTOM_LIST_MERGE_TYPE=overwrite

View File

@ -1 +0,0 @@
5 14 * * * cd /dailycheckin && dailycheckin >> /dailycheckin/logs/dailycheckin.log 2>&1

View File

@ -3,71 +3,17 @@ set -e
export LANG="zh_CN.UTF-8"
CRONTAB_FILE="/dailycheckin/cron/crontab_list.sh"
echo "安装最新依赖..."
pip install dailycheckin --upgrade
pip install dailycheckin --upgrade --user
defaultListFile="/dailycheckin/default_list.sh"
customListFile="/dailycheckin/$CUSTOM_LIST_FILE"
mergedListFile="/dailycheckin/merged_list_file.sh"
if type ts >/dev/null 2>&1; then
echo 'moreutils tools installed, default task append |ts output'
echo '系统已安装 moreutils 工具包默认定时任务增加ts 输出'
##复制一个新文件来追加|ts防止git pull的时候冲突
cp $defaultListFile /dailycheckin/crontab_list.sh
defaultListFile="/dailycheckin/crontab_list.sh"
sed -i 's/>>/|ts >>/g' $defaultListFile
fi
if [ $CUSTOM_LIST_FILE ]; then
echo "You have configured a custom list file: $CUSTOM_LIST_FILE, custom list merge type: $CUSTOM_LIST_MERGE_TYPE..."
echo "您配置了自定义任务文件:$CUSTOM_LIST_FILE,自定义任务类型为:$CUSTOM_LIST_MERGE_TYPE..."
if [ -f "$customListFile" ]; then
if [ $CUSTOM_LIST_MERGE_TYPE == "append" ]; then
echo "merge default list file: $DEFAULT_LIST_FILE and custom list file: $CUSTOM_LIST_FILE"
echo "合并默认定时任务文件:$DEFAULT_LIST_FILE 和 自定义定时任务文件:$CUSTOM_LIST_FILE"
cat $defaultListFile >$mergedListFile
echo -e "" >>$mergedListFile
cat $customListFile >>$mergedListFile
elif [ $CUSTOM_LIST_MERGE_TYPE == "overwrite" ]; then
cat $customListFile >$mergedListFile
echo "merge custom list file: $CUSTOM_LIST_FILE..."
echo "合并自定义任务文件:$CUSTOM_LIST_FILE"
touch "$customListFile"
else
echo "配置配置了错误的自定义定时任务类型:$CUSTOM_LIST_MERGE_TYPE自定义任务类型为只能为append或者overwrite..."
cat $defaultListFile >$mergedListFile
fi
else
echo "Not found custom list file: $CUSTOM_LIST_FILE ,use default list file: $DEFAULT_LIST_FILE"
echo "自定义任务文件:$CUSTOM_LIST_FILE 未找到,使用默认配置$DEFAULT_LIST_FILE..."
cat $defaultListFile >$mergedListFile
fi
else
echo "The currently used is the default crontab task file: $DEFAULT_LIST_FILE ..."
echo "当前使用的为默认定时任务文件 $DEFAULT_LIST_FILE ..."
cat $defaultListFile >$mergedListFile
fi
# 判断最后要加载的定时任务是否包含默认定时任务,不包含的话就加进去
if [ $(grep -c "default_task.sh" $mergedListFile) -eq '0' ]; then
echo "Merged crontab task filethe required default task is not included, append default task..."
echo "合并后的定时任务文件,未包含必须的默认定时任务,增加默认定时任务..."
echo -e >>$mergedListFile
echo "0 */12 * * * sh /dailycheckin/default_task.sh >> /dailycheckin/logs/default_task.log 2>&1" >>$mergedListFile
fi
echo "Load the latest crontab task file..."
echo "加载最新的定时任务文件..."
crontab $mergedListFile
crontab $CRONTAB_FILE
echo "Start crontab task main process..."
echo "启动 crondtab 定时任务主进程..."
if [ $CUSTOM_LIST_FILE ]; then
chmod -R 777 $customListFile
if [ $CRONTAB_FILE ]; then
chmod -R 777 $CRONTAB_FILE
fi
echo "启动 crondtab 定时任务主进程..."
crond -f

View File

@ -1,12 +1,15 @@
#!/bin/bash
echo "在当前目录下创建 config 文件夹"
echo "在当前目录下创建 config 和 cron 文件夹"
mkdir -p config
mkdir -p cron
echo "下载渲染 config 文件的脚本,并执行渲染"
echo "下载 config 文件"
curl https://raw.githubusercontent.com/sitoi/dailycheckin/main/docker/config.template.json -o config/config.json
echo "下载 crontab_list.sh 文件"
curl https://raw.githubusercontent.com/sitoi/dailycheckin/main/docker/crontab_list.sh -o cron/crontab_list.sh
curl https://ghproxy.com/https://raw.githubusercontent.com/sitoi/dailycheckin/main/docker/config.template.json -o config/config.json
docker --version
if [ $? -ne 0 ];then
@ -19,15 +22,14 @@ else
echo "开始通过 docker 命令创建容器"
docker run -d -v $(pwd)/config:/dailycheckin/config \
-v $(pwd)/logs:/dailycheckin/logs \
-v $(pwd)/cron:/dailycheckin/cron \
--name dailycheckin \
--restart always \
sitoi/dailycheckin:latest
else
echo "docker-compose 环境存在,将使用 docker-compose 命令启动容器..."
echo "下载 docker-compose.yml 文件"
curl -O https://ghproxy.com/https://raw.githubusercontent.com/sitoi/dailycheckin/main/docker/docker-compose.yml
echo "下载 Makefile 文件(可以无视)"
curl -O https://ghproxy.com/https://raw.githubusercontent.com/sitoi/dailycheckin/main/docker/Makefile
curl -O https://raw.githubusercontent.com/sitoi/dailycheckin/main/docker/docker-compose.yml
echo "开始通过 docker-compose 命令创建容器"
docker-compose up -d
fi