From 408cf95a78bb5b20572207f25b77b72e6ee9c9b3 Mon Sep 17 00:00:00 2001 From: Ian Burgwin Date: Wed, 25 Nov 2020 10:48:35 -0800 Subject: [PATCH] better support for local docker testing --- .dockerignore | 2 ++ .gitignore | 3 +++ docker-compose-prod.yml | 6 +++--- docker-compose.yml | 35 +++++++++++++++++++++++++++++++++++ kurisu.py | 30 ++++++++++++++---------------- migrations/env.py | 27 +++++++++++++++------------ 6 files changed, 72 insertions(+), 31 deletions(-) create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore index 1f3effc2a..a668c61b6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,10 +1,12 @@ data +kurisudb .git .github .gitignore .travis.yml .dockerignore Dockerfile +token.txt config.ini config.ini.example dockerbuild.sh diff --git a/.gitignore b/.gitignore index 6b2161048..e29c83f82 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ *.bin ._* .DS_Store +token.txt +config.ini __pycache__ data/ +kurisudb/ .idea/ diff --git a/docker-compose-prod.yml b/docker-compose-prod.yml index 2d90811dc..f106adf26 100644 --- a/docker-compose-prod.yml +++ b/docker-compose-prod.yml @@ -20,9 +20,9 @@ services: kurisu: image: ghcr.io/nh-server/kurisu environment: - KURISU_TOKEN: /run/secrets/kurisu_token - DB_USER: /run/secrets/db_user - DB_PASSWORD: /run/secrets/db_password + KURISU_TOKEN_FILE: /run/secrets/kurisu_token + DB_USER_FILE: /run/secrets/db_user + DB_PASSWORD_FILE: /run/secrets/db_password secrets: - kurisu_token - db_user diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..5b6208d36 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,35 @@ +version: "3.8" +services: + db: + image: postgres:13 + volumes: + - ./kurisudb:/var/lib/postgresql/data + environment: + POSTGRES_USER: kurisu + POSTGRES_PASSWORD: dev123 + healthcheck: + test: ["CMD-SHELL", "pg_isready -U kurisu"] + interval: 10s + timeout: 5s + retries: 5 + restart: always + + kurisu: + image: ghcr.io/nh-server/kurisu + environment: + KURISU_TOKEN_FILE: /run/secrets/kurisu_token + DB_USER: kurisu + DB_PASSWORD: dev123 + secrets: + - kurisu_token + depends_on: + db: + condition: service_healthy + restart: always + +volumes: + kurisudb: + +secrets: + kurisu_token: + file: ./token.txt diff --git a/kurisu.py b/kurisu.py index f3491c557..527b5536b 100644 --- a/kurisu.py +++ b/kurisu.py @@ -32,24 +32,22 @@ # Load config if IS_DOCKER: - token_file = os.environ.get('KURISU_TOKEN') - if token_file: - with open(token_file, 'r', encoding='utf-8') as f: - TOKEN = f.readline().strip() - else: - sys.exit('Token path needs to be provided in the KURISU_TOKEN environment variable') + def get_env(name: str): + contents = os.environ.get(name) + if contents is None: + contents_file = os.environ.get(name + '_FILE') + try: + with open(contents_file, 'r', encoding='utf-8') as f: + contents = f.readline().strip() + except FileNotFoundError: + sys.exit(f"Couldn't find environment variables {name} or {name}_FILE.") - db_user_file = os.environ.get('DB_USER') - db_password_file = os.environ.get('DB_PASSWORD') + return contents - if db_user_file and db_password_file: - with open(db_user_file, 'r', encoding='utf-8') as f: - db_user = f.readline().strip() - with open(db_password_file, 'r', encoding='utf-8') as f: - db_password = f.readline().strip() - DATABASE_URL = f"postgresql://{db_user}:{db_password}@db/{db_user}" - else: - sys.exit('Database user and database password files paths need to be provided') + TOKEN = get_env('KURISU_TOKEN') + db_user = get_env('DB_USER') + db_password = get_env('DB_PASSWORD') + DATABASE_URL = f"postgresql://{db_user}:{db_password}@db/{db_user}" else: kurisu_config = ConfigParser() kurisu_config.read("data/config.ini") diff --git a/migrations/env.py b/migrations/env.py index d10ccfcef..b885d5c5d 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -12,18 +12,21 @@ IS_DOCKER = os.environ.get('IS_DOCKER', 0) if IS_DOCKER: - - db_user_file = os.environ.get('DB_USER') - db_password_file = os.environ.get('DB_PASSWORD') - - if db_user_file and db_password_file: - with open(db_user_file, 'r', encoding='utf-8') as f: - db_user = f.readline().strip() - with open(db_password_file, 'r', encoding='utf-8') as f: - db_password = f.readline().strip() - DATABASE_URL = f"postgresql://{db_user}:{db_password}@db/{db_user}" - else: - sys.exit('Database user and database password files paths need to be provided') + def get_env(name: str): + contents = os.environ.get(name) + if contents is None: + contents_file = os.environ.get(name + '_FILE') + try: + with open(contents_file, 'r', encoding='utf-8') as f: + contents = f.readline().strip() + except FileNotFoundError: + sys.exit(f"Couldn't find environment variables {name} or {name}_FILE.") + + return contents + + db_user = get_env('DB_USER') + db_password = get_env('DB_PASSWORD') + DATABASE_URL = f"postgresql://{db_user}:{db_password}@db/{db_user}" else: configparser = ConfigParser() configparser.read("data/config.ini")