2020-05-16 20:14:26 +00: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 18:31:02 +00: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 20:56:14 +00:00
|
|
|
|
|
|
|
from psycopg2 import OperationalError, connect
|
2020-05-16 20:14:26 +00:00
|
|
|
from redis import Redis
|
|
|
|
from redis.exceptions import RedisError
|
2020-05-16 20:56:14 +00:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.lib.config import CONFIG
|
2020-05-16 20:14:26 +00:00
|
|
|
|
2020-09-20 18:31:02 +00: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 18:31:02 +00:00
|
|
|
}
|
|
|
|
data.update(**kwargs)
|
|
|
|
print(dumps(data), file=stderr)
|
|
|
|
|
2020-05-16 20:14:26 +00: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
|
2021-05-05 18:15:01 +00:00
|
|
|
except OperationalError as exc:
|
2020-05-16 20:14:26 +00:00
|
|
|
sleep(1)
|
2021-05-05 18:15:01 +00:00
|
|
|
j_print(f"PostgreSQL Connection failed, retrying... ({exc})")
|
2020-05-16 20:14:26 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
2021-05-09 18:13:58 +00:00
|
|
|
redis = Redis.from_url(
|
|
|
|
f"redis://:{CONFIG.y('redis.password')}@{CONFIG.y('redis.host')}:6379"
|
|
|
|
f"/{CONFIG.y('redis.ws_db')}"
|
2020-05-16 20:14:26 +00:00
|
|
|
)
|
|
|
|
redis.ping()
|
|
|
|
break
|
2021-05-05 18:15:01 +00:00
|
|
|
except RedisError as exc:
|
2020-05-16 20:14:26 +00:00
|
|
|
sleep(1)
|
2021-05-05 18:15:01 +00:00
|
|
|
j_print(f"Redis Connection failed, retrying... ({exc})")
|