From c22085eeb3e5a99aea8a274b4dea1d1cd486e203 Mon Sep 17 00:00:00 2001 From: kr0sh512 Date: Wed, 12 Feb 2025 00:21:38 +0300 Subject: [PATCH] some updates --- Dockerfile | 10 ++++ app/install.sh | 5 ++ app/transfer_in_sql.py | 103 +++++++++++++++++++++++++++++++++++++++++ docker-compose.yaml | 20 ++++++++ 4 files changed, 138 insertions(+) create mode 100644 Dockerfile create mode 100644 app/transfer_in_sql.py create mode 100644 docker-compose.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1aaecdf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.9-slim + +WORKDIR /app + +COPY requirements.txt requirements.txt +RUN pip install -r requirements.txt + +COPY ./app . + +CMD ["./install.sh"] \ No newline at end of file diff --git a/app/install.sh b/app/install.sh index 846c9ad..19e9eed 100644 --- a/app/install.sh +++ b/app/install.sh @@ -12,3 +12,8 @@ for var in "${env_vars[@]}"; do fi done +# Add cron jobs to crontab +(crontab -l 2>/dev/null; echo "0 * * * * python3 /app/upload_sql.py") | crontab - + +# Start the schedules.py script +python3 -u /app/schedules.py & diff --git a/app/transfer_in_sql.py b/app/transfer_in_sql.py new file mode 100644 index 0000000..528b7fb --- /dev/null +++ b/app/transfer_in_sql.py @@ -0,0 +1,103 @@ +#!/usr/bin/python3.3 +from datetime import datetime +import os +from dotenv import load_dotenv +from sshtunnel import SSHTunnelForwarder +from psycopg2 import pool +import json + + +allow_update = True + +load_dotenv() + +""" +{ + "1025872648": { + "username": "ahunuin", + "first_name": "Arseniy", + "last_name": "", + "group": "205", + "timeout": "10", + "allow_message": "yes", + "thread": "General" + }, + "856850518": { + "username": "kr0sh_512", + "first_name": "Дмитрий", + "last_name": "", + "group": "202", + "timeout": "10", + "allow_message": "no", + "thread": "General" + } +} +""" + +""" +CREATE TABLE IF NOT EXISTS users ( + id BIGINT PRIMARY KEY, + username VARCHAR(255), + first_name VARCHAR(255), + last_name VARCHAR(255), + "group" VARCHAR(255) DEFAULT 'other', + timeout INT DEFAULT 10, + allow_message BOOLEAN DEFAULT TRUE, + thread VARCHAR(255) DEFAULT 'General', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); +""" + +if os.environ.get("ENV") == "dev": + print("DEV: Connecting to local database") + server = SSHTunnelForwarder( + (os.environ.get("SSH_HOST"), 22), + ssh_private_key=os.environ.get("SSH_KEY"), + ssh_username=os.environ.get("SSH_USER"), + ssh_password=( + os.environ.get("SSH_PASSWORD") if os.environ.get("SSH_PASSWORD") else None + ), + remote_bind_address=("localhost", int(os.environ.get("DB_PORT"))), + ) + server.start() + +db = pool.SimpleConnectionPool( + 1, + 20, + user=os.environ.get("DB_USER"), + password=os.environ.get("DB_PASSWORD"), + host="localhost" if os.environ.get("ENV") == "dev" else os.environ.get("DB_HOST"), + port=( + server.local_bind_port + if os.environ.get("ENV") == "dev" + else os.environ.get("DB_PORT") + ), + database=os.environ.get("DB_NAME"), +) + +# Load users from JSON file +with open("users.json", "r", encoding="utf-8") as file: + users = json.load(file) + +# Insert users into the database +with db.getconn() as conn: + with conn.cursor() as cursor: + for user_id, user_data in users.items(): + cursor.execute( + """ + INSERT INTO users (id, username, first_name, last_name, "group", timeout, allow_message, thread) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s) + ON CONFLICT (id) DO NOTHING + """, + ( + user_id, + user_data.get("username"), + user_data.get("first_name"), + user_data.get("last_name"), + user_data.get("group", "other"), + user_data.get("timeout", 10), + user_data.get("allow_message", "yes") == "yes", + user_data.get("thread", "General"), + ), + ) + conn.commit() diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..9b006b5 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,20 @@ +services: + schedule_bot: + build: + context: . + dockerfile: Dockerfile + container_name: schedule_bot + environment: + - ENV=production + restart: unless-stopped + networks: + - postgres_postgres + deploy: + resources: + limits: + cpus: '0.3' + memory: 200M + +networks: + postgres_postgres: + external: true \ No newline at end of file