Skip to content

Commit

Permalink
Merge pull request #3 from KGBTR/refactor/overhaul
Browse files Browse the repository at this point in the history
refactor: overhaul service after production release
  • Loading branch information
Asim-Tahir authored Jan 21, 2024
2 parents 90d2998 + 4c9a279 commit 36d6551
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 21 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
NO_REPLY=True
PYTHONUNBUFFERED=true
BASE_LOG_DIR=logs
PYTHONENV=development

BOT_CLIENT_ID='<client_id>'
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Release new version

on:
push:
tags:
- v*.*.*
workflow_dispatch:

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Clone repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Copy service files via SSH password
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
source: "koyunotlatan/*,koyunotlatan/data/*,scripts/*,koyunotlatan.service,koyunotlatan.timer,koyunotlatan.pkla,praw.example.ini,requirements.txt,LICENSE,pyproject.toml"
target: /home/koyunotlatan/service

- name: Setup service via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
script: |
/home/koyunotlatan/service/scripts/release.sh
# Fulfill credentials for praw.ini
perl -pe 's/<username>/${{ secrets.PRAW_USERNAME }}/g; s/<password>/${{ secrets.PRAW_PASSWORD }}/g; s/<client_id>/${{ secrets.PRAW_CLIENT_ID }}/g; s/<client_secret>/${{ secrets.PRAW_CLIENT_SECRET }}/g' /home/koyunotlatan/service/praw.example.ini > /home/koyunotlatan/service/praw.ini
15 changes: 3 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ venv.bak/
.env*
!.env*.example

# Github Act Secrets
*.secrets

# Spyder project settings
.spyderproject
.spyproject
Expand All @@ -145,18 +148,6 @@ dmypy.json
# Cython debug symbols
cython_debug/

### venv ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
pip-selfcheck.json

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
Expand Down
1 change: 1 addition & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.black-formatter",
"ms-python.isort",
"ms-azuretools.vscode-docker",
"hangxingliu.vscode-systemd-support",
"benspaulding.procfile"
Expand Down
2 changes: 1 addition & 1 deletion koyunotlatan.pkla
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Allow koyunotlatan user to manage start/stop/restart/reload/enable/disable systemd services]
Identity=unix-user:koyunotlatan
Identity=unix-group:koyunotlatan
Action=org.freedesktop.systemd1.manage-units;org.freedesktop.systemd1.manage-unit-files;org.freedesktop.systemd1.manage-unit-files;org.freedesktop.systemd1.set-environment;org.freedesktop.systemd1.reload-daemon
ResultAny=yes
ResultInactive=yes
Expand Down
2 changes: 1 addition & 1 deletion koyunotlatan.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ After=network-online.target local-fs.target
[Service]
Type=exec
ExecStart=/home/koyunotlatan/service/.venv/bin/python /home/koyunotlatan/service/koyunotlatan/main.py $OPTIONS
Environment=PYTHONUNBUFFERED=true PYTHONPATH=/home/koyunotlatan/service PYTHONENV=production
Environment=PYTHONUNBUFFERED=true PYTHONPATH=/home/koyunotlatan/service
SyslogIdentifier=koyunotlatan
NotifyAccess=all
User=koyunotlatan
Expand Down
2 changes: 1 addition & 1 deletion koyunotlatan.timer
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Unit]
Description="Koyun Otlatan timer that runs on every hour"
Description="Koyun Otlatan timer"
Wants=koyunotlatan.service

[Timer]
Expand Down
2 changes: 1 addition & 1 deletion koyunotlatan/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
BOT_ACTIVE_SUBREDDITS: str

try:
NO_REPLY = bool(strtobool(os.environ.get('NO_REPLY', default='True')))
PYTHONENV = os.environ.get('PYTHONENV', default='development')
NO_REPLY = bool(strtobool(os.environ.get('NO_REPLY', default='true' if PYTHONENV == 'production' else 'false')))
BASE_LOG_DIR = '/var/log/koyunotlatan'

BOT_CLIENT_ID = os.environ.get('BOT_CLIENT_ID', default='')
Expand Down
9 changes: 5 additions & 4 deletions koyunotlatan/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ def __init__(self, reddit: Reddit):
self.search_limit = 20
self.post_limit = 50
self.alike_value = 1.35
self.reply_comment = """
{comment}
self.reply_comment = "{comment}"
# self.reply_comment = """
# {comment}

^(If you have **any comments or suggestions** about the bot, please mention **Asim-Abi** by replying to **the bot comment**)
# ^(If you have **any comments or suggestions** about the bot, please mention **Asim-Abi** by replying to **the bot comment**)

^(This comment was automatically picked by **the koyunkırpan algorithm**)"""
# ^(This comment was automatically picked by **the koyunkırpan algorithm**)"""
self.load_replies()
self.load_commented_on()

Expand Down
23 changes: 23 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

export WORKDIR=/home/koyunotlatan/service

# Set virtual enviroment directory as enviroment variable
export VIRTUAL_ENV=$WORKDIR/.venv

# Check $VIRTUAL_ENV enviroment variable exist
if [[ -n "$VIRTUAL_ENV" ]]; then
# Create virtual enviroment if not exist
if [[ ! -d "$VIRTUAL_ENV" ]]; then
python3 -m venv $VIRTUAL_ENV
chown -R koyunotlatan_ci:koyunotlatan $VIRTUAL_ENV
fi

# Install dependencies with virtual enviroment
$VIRTUAL_ENV/bin/pip install -r $WORKDIR/requirements.txt
fi

systemctl daemon-reload
systemctl enable koyunotlatan.timer
systemctl start koyunotlatan.timer
systemctl start koyunotlatan.service

0 comments on commit 36d6551

Please sign in to comment.