From 95b5ed5705b87578d12cdf4cf68c0e12798ec159 Mon Sep 17 00:00:00 2001 From: BennyThink Date: Mon, 3 Jan 2022 10:26:03 +0800 Subject: [PATCH] add upgrade actions, add Makefile --- .dockerignore | 4 +++- .github/workflows/upgrade.yml | 24 ++++++++++++++++++++++++ .gitignore | 5 ++++- Dockerfile | 2 +- Makefile | 26 ++++++++++++++++++++++++++ README.md | 12 ++++++++++++ ytdlbot/utils.py | 5 +++++ ytdlbot/ytdl_bot.py | 4 +++- 8 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/upgrade.yml create mode 100644 Makefile diff --git a/.dockerignore b/.dockerignore index 4057a7e..cd3a167 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,4 @@ env -db_data \ No newline at end of file +db_data +.ash_history +.DS_Store \ No newline at end of file diff --git a/.github/workflows/upgrade.yml b/.github/workflows/upgrade.yml new file mode 100644 index 0000000..3ed4d51 --- /dev/null +++ b/.github/workflows/upgrade.yml @@ -0,0 +1,24 @@ +name: Upgrade +on: + push: + tags: + - "v*" + +jobs: + update: + runs-on: ubuntu-latest + steps: + - name: executing remote ssh commands using ssh key + uses: appleboy/ssh-action@master + env: + BOT_TOKEN: ${{ secrets.BOT_TOKEN }} + with: + envs: BOT_TOKEN + host: ${{ secrets.YTDL_BOT_HOST }} + username: root + key: ${{ secrets.SSH_KEY }} + script: | + make bot + make upgrade-all-worker + curl "https://api.telegram.org/bot$BOT_TOKEN/sendMessage?chat_id=260260121&text=ytdl%20upgrade%20complete!" + echo "YouTube Download upgrade complete!" \ No newline at end of file diff --git a/.gitignore b/.gitignore index ca82b57..77bac53 100644 --- a/.gitignore +++ b/.gitignore @@ -154,5 +154,8 @@ dmypy.json db_data/* env/* .ash_history +.DS_Store ytdlbot/ytdl.session -data/* \ No newline at end of file +data/* +upgrade_worker.sh +ytdl.session \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 8e6766a..b23e422 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ FROM python:3.9-alpine WORKDIR /ytdlbot/ytdlbot ENV TZ=Asia/Shanghai -RUN apk update && apk add --no-cache ffmpeg vnstat +RUN apk update && apk add --no-cache ffmpeg vnstat git COPY --from=builder /root/.local /usr/local COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7b1ca0c --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +default: + docker pull bennythink/ytdlbot + +bot: + make + docker-compose up -d + docker system prune -a --volumes -f + +worker: + make + docker-compose -f worker.yml up -d + docker system prune -a --volumes -f + sleep 5 + +weak-worker: + make + docker-compose --compatibility -f worker.yml up -d + docker system prune -a --volumes -f + sleep 5 + +upgrade-all-worker: + bash upgrade_worker.sh + +tag: + git tag -a v$(date "+%Y-%m-%d") -m v$(date "+%Y-%m-%d") + git push --tags \ No newline at end of file diff --git a/README.md b/README.md index 532ce63..8e43f35 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,18 @@ vim data/instagram.com_cookies.txt In `flower` service section, you may want to change your basic authentication username password and publish port. +You can also limit CPU and RAM usage by adding an `deploy' key: + +```docker + deploy: + resources: + limits: + cpus: '0.5' + memory: 1500M +``` + +Be sure to use `--compatibility` when deploying. + ## 4. run ### 4.1. standalone mode diff --git a/ytdlbot/utils.py b/ytdlbot/utils.py index 92b08fa..7300c8d 100644 --- a/ytdlbot/utils.py +++ b/ytdlbot/utils.py @@ -9,6 +9,7 @@ __author__ = "Benny " import logging import pathlib +import subprocess import time import uuid @@ -97,3 +98,7 @@ def current_time(): return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) +def get_revision_tag(): + revision = subprocess.check_output("git -C ../ rev-parse --short HEAD".split()).decode("u8").replace("\n", "") + tag = subprocess.check_output("git -C ../ describe --tags".split()).decode("u8").replace("\n", "") + return revision, tag diff --git a/ytdlbot/ytdl_bot.py b/ytdlbot/ytdl_bot.py index 4982a13..25b3975 100644 --- a/ytdlbot/ytdl_bot.py +++ b/ytdlbot/ytdl_bot.py @@ -27,7 +27,7 @@ from db import MySQL, Redis from downloader import convert_flac from limit import verify_payment from tasks import download_entrance -from utils import customize_logger, get_user_settings, set_user_settings +from utils import customize_logger, get_user_settings, set_user_settings, get_revision_tag customize_logger(["pyrogram.client", "pyrogram.session.session", "pyrogram.connection.connection"]) app = create_app() @@ -219,6 +219,7 @@ def audio_callback(client: "Client", callback_query: types.CallbackQuery): if __name__ == '__main__': MySQL() + revision_tag = get_revision_tag() scheduler = BackgroundScheduler() scheduler.add_job(Redis().reset_today, 'cron', hour=0, minute=0) scheduler.start() @@ -229,6 +230,7 @@ if __name__ == '__main__': ▘ ▝▀ ▝▀▘ ▘ ▝▀▘ ▀▀ ▝▀▘ ▀▀ ▝▀ ▘▘ ▘ ▘ ▘ ▝▀ ▝▀▘ ▝▀▘ By @BennyThink, VIP mode: {ENABLE_VIP}, Distribution: {ENABLE_CELERY} +Version: {revision_tag[0]}@{revision_tag[1]} """ print(banner) app.run()