coss_archiving/news_fetch/configuration.py

69 lines
2.3 KiB
Python

import os
import configparser
import logging
import time
# import shutil
# from datetime import datetime
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
main_config = configparser.ConfigParser()
main_config.read("/app/containerdata/config/news_fetch.config.ini")
db_config = configparser.ConfigParser()
db_config.read("/app/containerdata/config/db.config.ini")
# DEBUG MODE:
if os.getenv("DEBUG", "false") == "true":
logger.warning("Found 'DEBUG=true', setting up dummy databases")
main_config["SLACK"]["archive_id"] = main_config["SLACK"]["debug_id"]
main_config["MAIL"]["recipient"] = main_config["MAIL"]["sender"]
main_config["DOWNLOADS"]["local_storage_path"] = main_config["DOWNLOADS"]["debug_storage_path"]
download_db = SqliteDatabase(
main_config["DATABASE"]["download_db_debug"],
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 = db_config["DATABASE"]
download_db = PostgresqlDatabase(
cred["db_name"], user=cred["user_name"], password=cred["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
models.set_db(download_db)