Skip to content

Commit

Permalink
better support for local docker testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ihaveamac committed Nov 25, 2020
1 parent c8486c4 commit 408cf95
Showing 6 changed files with 72 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
data
kurisudb
.git
.github
.gitignore
.travis.yml
.dockerignore
Dockerfile
token.txt
config.ini
config.ini.example
dockerbuild.sh
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -7,6 +7,9 @@
*.bin
._*
.DS_Store
token.txt
config.ini
__pycache__
data/
kurisudb/
.idea/
6 changes: 3 additions & 3 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
30 changes: 14 additions & 16 deletions kurisu.py
Original file line number Diff line number Diff line change
@@ -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")
27 changes: 15 additions & 12 deletions migrations/env.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 408cf95

Please sign in to comment.