From 4259874bde66d73af5d116d2e629cd65986753f6 Mon Sep 17 00:00:00 2001 From: shitao <133397418@qq.com> Date: Sun, 14 Jan 2024 13:03:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0(.dockerignore):?= =?UTF-8?q?=20=E5=BF=BD=E7=95=A5=E6=96=B0=E9=85=8D=E7=BD=AE=E5=92=8C?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E6=96=87=E4=BB=B6=EF=BC=8C=E6=8F=90=E5=8D=87?= =?UTF-8?q?Docker=E6=9E=84=E5=BB=BA=E6=95=88=E7=8E=87=20=E2=AC=86=EF=B8=8F?= =?UTF-8?q?=20=E5=8D=87=E7=BA=A7(Dockerfile):=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E9=95=9C=E5=83=8F=E4=BB=8Epython:3.8-alpine?= =?UTF-8?q?=E5=88=B0python:3.9-alpine=20=F0=9F=93=9D=20=E6=9B=B4=E6=96=B0(?= =?UTF-8?q?docker-compose.yml):=20=E6=8C=82=E8=BD=BD=E6=96=B0=E7=9A=84cron?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E7=94=A8=E4=BA=8E=E5=AD=98=E6=94=BEcrontab?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=20=F0=9F=94=A7=20=E9=85=8D=E7=BD=AE(crontab?= =?UTF-8?q?=5Flist.sh):=20=E5=88=9B=E5=BB=BA=E6=96=B0=E7=9A=84=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1=E8=84=9A=E6=9C=AC=20=F0=9F=97=91?= =?UTF-8?q?=EF=B8=8F=20=E5=88=A0=E9=99=A4(default=5Flist.sh,=20custom-*-do?= =?UTF-8?q?cker-compose.yml,=20my=5Fcrontab=5Flist.sh):=20=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E8=BF=87=E6=97=B6=E7=9A=84=E7=A4=BA=E4=BE=8B=E5=92=8C?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=20=E2=99=BB=EF=B8=8F=20=E9=87=8D=E6=9E=84(st?= =?UTF-8?q?art.sh):=20=E7=AE=80=E5=8C=96=E5=90=AF=E5=8A=A8=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=EF=BC=8C=E4=BD=BF=E7=94=A8`cron`=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20=E2=9C=A8=20=E5=8A=9F=E8=83=BD(docker=5Fstart.sh):?= =?UTF-8?q?=20=E6=B7=BB=E5=8A=A0cron=E7=9B=AE=E5=BD=95=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=8B=E8=BD=BDcrontab=5Flist.sh=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/.dockerignore | 4 ++ docker/Dockerfile | 6 +- docker/config.template.json | 2 +- docker/crontab_list.sh | 9 +++ docker/default_list.sh | 9 --- docker/default_task.sh | 57 +--------------- docker/docker-compose.yml | 5 +- .../example/custom-append-docker-compose.yml | 14 ---- .../custom-overwrite-docker-compose.yml | 14 ---- docker/example/my_crontab_list.sh | 1 - docker/start.sh | 68 ++----------------- docker_start.sh | 16 +++-- 12 files changed, 37 insertions(+), 168 deletions(-) create mode 100644 docker/.dockerignore create mode 100644 docker/crontab_list.sh delete mode 100644 docker/default_list.sh delete mode 100644 docker/example/custom-append-docker-compose.yml delete mode 100644 docker/example/custom-overwrite-docker-compose.yml delete mode 100644 docker/example/my_crontab_list.sh diff --git a/docker/.dockerignore b/docker/.dockerignore new file mode 100644 index 0000000..fd2c48c --- /dev/null +++ b/docker/.dockerignore @@ -0,0 +1,4 @@ +config.template.json +crontab_list.sh +Makefile +docker-compose.yml \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index c04696e..ea2dc3f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,12 +1,11 @@ -FROM python:3.8-alpine -MAINTAINER Sitoi +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"] \ No newline at end of file diff --git a/docker/config.template.json b/docker/config.template.json index 1b45482..4c4ad63 100644 --- a/docker/config.template.json +++ b/docker/config.template.json @@ -68,7 +68,7 @@ "silver2coin": true } ], - + "IMAOTAI": [ { "city": "上海市", diff --git a/docker/crontab_list.sh b/docker/crontab_list.sh new file mode 100644 index 0000000..6174840 --- /dev/null +++ b/docker/crontab_list.sh @@ -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 diff --git a/docker/default_list.sh b/docker/default_list.sh deleted file mode 100644 index de6ac8c..0000000 --- a/docker/default_list.sh +++ /dev/null @@ -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 diff --git a/docker/default_task.sh b/docker/default_task.sh index 5ad377a..2d5334e 100644 --- a/docker/default_task.sh +++ b/docker/default_task.sh @@ -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 file,the 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 diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 7f63878..ac45cfa 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -6,5 +6,6 @@ services: restart: always tty: true volumes: - - ./config:/dailycheckin/config - - ./logs:/dailycheckin/logs \ No newline at end of file + - ./config/:/dailycheckin/config/ + - ./logs/:/dailycheckin/logs/ + - ./cron/:/dailycheckin/cron/ diff --git a/docker/example/custom-append-docker-compose.yml b/docker/example/custom-append-docker-compose.yml deleted file mode 100644 index 52bd298..0000000 --- a/docker/example/custom-append-docker-compose.yml +++ /dev/null @@ -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 \ No newline at end of file diff --git a/docker/example/custom-overwrite-docker-compose.yml b/docker/example/custom-overwrite-docker-compose.yml deleted file mode 100644 index 5a3a526..0000000 --- a/docker/example/custom-overwrite-docker-compose.yml +++ /dev/null @@ -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 \ No newline at end of file diff --git a/docker/example/my_crontab_list.sh b/docker/example/my_crontab_list.sh deleted file mode 100644 index 9879bf3..0000000 --- a/docker/example/my_crontab_list.sh +++ /dev/null @@ -1 +0,0 @@ -5 14 * * * cd /dailycheckin && dailycheckin >> /dailycheckin/logs/dailycheckin.log 2>&1 \ No newline at end of file diff --git a/docker/start.sh b/docker/start.sh index ac8ad57..6b4528e 100644 --- a/docker/start.sh +++ b/docker/start.sh @@ -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 file,the 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 \ No newline at end of file diff --git a/docker_start.sh b/docker_start.sh index b0e42a8..1be1265 100644 --- a/docker_start.sh +++ b/docker_start.sh @@ -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