coss_archiving/news_fetch/configuration.py

66 lines
2.2 KiB
Python

import time
import os
import logging
import yaml
from peewee import SqliteDatabase, PostgresqlDatabase
from rich.logging import RichHandler
# first things first: logging
logging.basicConfig(
format='%(message)s',
level=logging.INFO,
datefmt='%H:%M:%S', # add %Y-%m-%d if needed
handlers=[RichHandler()]
)
logger = logging.getLogger(__name__)
# load config file containing constants and secrets
config_location = os.getenv("CONFIG_FILE")
with open(config_location, "r") as f:
config = yaml.safe_load(f)
# DEBUG MODE:
if os.getenv("DEBUG", "false") == "true":
logger.warning("Found 'DEBUG=true', setting up dummy databases")
config["slack"]["archive_id"] = config["slack"]["debug_id"]
config["mail"]["recipient"] = config["mail"]["sender"]
config["downloads"]["local_storage_path"] = config["downloads"]["debug_storage_path"]
download_db = SqliteDatabase(
config["database"]["debug_db"],
pragmas = {'journal_mode': 'wal'} # mutliple threads can read at once
)
# PRODUCTION MODE:
else:
logger.warning("Found 'DEBUG=false' and running on production databases, I hope you know what you're doing...")
time.sleep(10) # wait for the vpn to connect (can't use a healthcheck because there is no depends_on)
cred = config["database"]
download_db = PostgresqlDatabase(
cred["production_db_name"], user=cred["production_user_name"], password=cred["production_password"], host="vpn", port=5432
)
# TODO Reimplement backup/printout
# logger.info("Backing up databases")
# backup_dst = main_config["DATABASE"]["db_backup"]
# today = datetime.today().strftime("%Y.%m.%d")
# shutil.copyfile(
# os.path.join(db_base_path, main_config["DATABASE"]["chat_db_name"]),
# os.path.join(backup_dst, today + "." + main_config["DATABASE"]["chat_db_name"]),
# )
# shutil.copyfile(
# os.path.join(db_base_path, main_config["DATABASE"]["download_db_name"]),
# os.path.join(backup_dst, today + "." + main_config["DATABASE"]["download_db_name"]),
# )
from utils_storage import models
# Set up the database connection (also creates tables if they don't exist)
models.set_db(download_db)