2020-05-16 21:14:26 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""This file needs to be run from the root of the project to correctly
|
2020-12-05 21:08:42 +00:00
|
|
|
import authentik. This is done by the dockerfile."""
|
2020-09-20 19:31:02 +01:00
|
|
|
from json import dumps
|
|
|
|
from sys import stderr
|
2020-11-16 00:41:33 +00:00
|
|
|
from time import sleep, time
|
2020-05-16 21:56:14 +01:00
|
|
|
|
|
|
|
from psycopg2 import OperationalError, connect
|
2020-05-16 21:14:26 +01:00
|
|
|
from redis import Redis
|
|
|
|
from redis.exceptions import RedisError
|
2020-05-16 21:56:14 +01:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.lib.config import CONFIG
|
2020-05-16 21:14:26 +01:00
|
|
|
|
2020-09-20 19:31:02 +01:00
|
|
|
|
|
|
|
def j_print(event: str, log_level: str = "info", **kwargs):
|
|
|
|
"""Print event in the same format as structlog with JSON.
|
|
|
|
Used before structlog is configured."""
|
|
|
|
data = {
|
|
|
|
"event": event,
|
|
|
|
"level": log_level,
|
|
|
|
"logger": __name__,
|
2020-11-16 00:41:33 +00:00
|
|
|
"timestamp": time(),
|
2020-09-20 19:31:02 +01:00
|
|
|
}
|
|
|
|
data.update(**kwargs)
|
|
|
|
print(dumps(data), file=stderr)
|
|
|
|
|
2020-05-16 21:14:26 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
conn = connect(
|
|
|
|
dbname=CONFIG.y("postgresql.name"),
|
|
|
|
user=CONFIG.y("postgresql.user"),
|
|
|
|
password=CONFIG.y("postgresql.password"),
|
|
|
|
host=CONFIG.y("postgresql.host"),
|
|
|
|
)
|
|
|
|
conn.cursor()
|
|
|
|
break
|
|
|
|
except OperationalError:
|
|
|
|
sleep(1)
|
2020-09-20 19:31:02 +01:00
|
|
|
j_print("PostgreSQL Connection failed, retrying...")
|
2020-05-16 21:14:26 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
redis = Redis(
|
|
|
|
host=CONFIG.y("redis.host"),
|
|
|
|
port=6379,
|
|
|
|
db=CONFIG.y("redis.message_queue_db"),
|
|
|
|
password=CONFIG.y("redis.password"),
|
|
|
|
)
|
|
|
|
redis.ping()
|
|
|
|
break
|
|
|
|
except RedisError:
|
|
|
|
sleep(1)
|
2020-09-20 19:31:02 +01:00
|
|
|
j_print("Redis Connection failed, retrying...")
|