diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d3b99ae..2f21c76 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,8 @@ jobs: deploy: runs-on: ubuntu-latest env: # Or as an environment variable - JBOND_BOT_TOKEN: ${{ secrets.JBOND_BOT_TOKEN }} + BRANCH: master + DIRECTORY: /opt/jbond steps: - name: Checkout code uses: actions/checkout@v3 @@ -25,31 +26,28 @@ jobs: run: | python3 -m ensurepip pip3 install aiogram - - name: Pull code - uses: appleboy/ssh-action@v1.2.2 - with: - host: ${{ secrets.JBOND_SSH_HOST }} - username: ${{ secrets.JBOND_SSH_USER }} - password: ${{ secrets.JBOND_SSH_PASS }} - #private_key: ${{ secrets.JBOND_SSH_PRIVATE_KEY }} - script: | - cd /opt - git clone https://github.com/andrevis/jbond.git - cd jbond - git checkout master - git pull origin master - - name: Install - uses: appleboy/ssh-action@v1.2.2 - with: - host: ${{ secrets.JBOND_SSH_HOST }} - username: ${{ secrets.JBOND_SSH_USER }} - password: ${{ secrets.JBOND_SSH_PASS }} - #private_key: ${{ secrets.JBOND_SSH_PRIVATE_KEY }} - script: | - systemctl stop jbond.service - systemctl disable jbond.service - cp -f /opt/jbond/misc/jbond.service /usr/lib/systemd/system/ - systemctl daemon-reload - systemctl enable jbond.service - cp -f /opt/jbond/misc/jbond_logrotate /etc/logrotate.d/ - systemctl restart crond \ No newline at end of file + - name: install ssh keys + # check this thread to understand why its needed: + # + run: | + install -m 600 -D /dev/null ~/.ssh/id_rsa + echo "${{ secrets.JBOND_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa + cat ~/.ssh/id_rsa + ssh-keyscan -H ${{ secrets.JBOND_SSH_HOST }} > ~/.ssh/known_hosts + - name: clone + run: ssh ${{ secrets.JBOND_SSH_USER }}@${{ secrets.JBOND_SSH_HOST }} "rm -rf $DIRECTORY && git clone https://github.com/andrevis/jbond.git $DIRECTORY" + - name: checkout-pull + run: ssh ${{ secrets.JBOND_SSH_USER }}@${{ secrets.JBOND_SSH_HOST }} "cd $DIRECTORY && git checkout $BRANCH && git pull origin $BRANCH" + - name: stop service + run: ssh ${{ secrets.JBOND_SSH_USER }}@${{ secrets.JBOND_SSH_HOST }} "systemctl stop jbond.service && systemctl disable jbond.service" + - name: set token + run: ssh ${{ secrets.JBOND_SSH_USER }}@${{ secrets.JBOND_SSH_HOST }} "echo ${{ secrets.JBOND_BOT_TOKEN }} > $DIRECTORY/token" + - name: setup service + run: ssh ${{ secrets.JBOND_SSH_USER }}@${{ secrets.JBOND_SSH_HOST }} "cp -f $DIRECTORY/misc/jbond.service /usr/lib/systemd/system/" + - name: enable service + run: ssh ${{ secrets.JBOND_SSH_USER }}@${{ secrets.JBOND_SSH_HOST }} "systemctl daemon-reload && systemctl enable jbond.service && systemctl restart jbond.service" + - name: setup logrotate + run: ssh ${{ secrets.JBOND_SSH_USER }}@${{ secrets.JBOND_SSH_HOST }} "cp -f $DIRECTORY/misc/jbond_logrotate /etc/logrotate.d/ && systemctl restart crond" + - name: cleanup keys + if: always() + run: rm -rf ~/.ssh/id_rsa \ No newline at end of file diff --git a/.gitignore b/.gitignore index 7b004e5..e0be194 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +token # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] @@ -174,9 +175,9 @@ cython_debug/ .abstra/ # Visual Studio Code -# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore +# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore -# and can be added to the global gitignore or merged into this file. However, if you prefer, +# and can be added to the global gitignore or merged into this file. However, if you prefer, # you could uncomment the following to ignore the enitre vscode folder # .vscode/ diff --git a/src/jbond.py b/src/jbond.py index d27f8ad..0a322c5 100644 --- a/src/jbond.py +++ b/src/jbond.py @@ -8,28 +8,6 @@ from aiogram import Bot, Dispatcher from aiogram.fsm.storage.memory import MemoryStorage -# def signal_handler(sig, frame): -# raise RuntimeError('Interrupted Ctrl+C') - -# signal.signal(signal.SIGINT, signal_handler) - - -bot = Bot(token=os.environ['JBOND_BOT_TOKEN']) +with open("/opt/jbond/token", 'r') as token: + bot = Bot(token=token.readline().strip()) dispatcher = Dispatcher(storage=MemoryStorage()) - - -# async def main(): -# await dispatcher.start_polling(bot) - -# def main(): -# timer = TimeThread() -# timer.start() - -# jbond = JBond() -# jbond.start() - -# while (True): -# sleep(1) - -# if __name__ == '__main__': -# main() diff --git a/src/logger.py b/src/logger.py index 1c0958b..edfab50 100644 --- a/src/logger.py +++ b/src/logger.py @@ -4,9 +4,3 @@ LOG_FORMAT = '%(asctime)s(%(threadName)s) %(levelname)s - %(message)s' logging.basicConfig(filename='/opt/jbond/jbond.log', level=logging.INFO) - -# logger = logging.getLogger(__name__) - -# handler = logging.StreamHandler(sys.stdout) -# handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT)) -# logging.getLogger().addHandler(handler) diff --git a/src/main.py b/src/main.py index 857975e..37ac28e 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,10 @@ +import os import asyncio from jbond import bot, dispatcher #,scheduler from handlers.start import start_router +from logger import * + +logger = logging.getLogger("Main") async def main(): diff --git a/src/time_source.py b/src/time_source.py deleted file mode 100644 index 9e0fdcd..0000000 --- a/src/time_source.py +++ /dev/null @@ -1,24 +0,0 @@ -from threading import Thread -from time import sleep -from datetime import datetime - -class TimeThread(Thread): - __need_running__ = True - __accuracy__ = 0.1 - __start__ = datetime.now() - __now__ = None - - def __init__(self, accuracy=0.1): - Thread.__init__(self, name="TimeSource") - self.__accuracy__ = accuracy - - def now(self): - return self.__now__ - - def run(self): - while self.__need_running__: - self.__now__ = datetime.now() - sleep(self.__accuracy__) - - def stop(self): - self.__need_running__ = False \ No newline at end of file