From a992ab497ae19bb984eba03e9a25639d4039f481 Mon Sep 17 00:00:00 2001 From: Lev Vereshchagin Date: Wed, 15 Dec 2021 13:37:06 +0300 Subject: [PATCH] Use Python for configurator service: faster and more robust --- build/worker/Dockerfile | 5 ++-- build/worker/configure.py | 55 +++++++++++++++++++++++++++++++++++++++ build/worker/configure.sh | 21 --------------- compose.yaml | 2 +- 4 files changed, 59 insertions(+), 24 deletions(-) create mode 100755 build/worker/configure.py delete mode 100755 build/worker/configure.sh diff --git a/build/worker/Dockerfile b/build/worker/Dockerfile index bf33abe2..c53fb329 100644 --- a/build/worker/Dockerfile +++ b/build/worker/Dockerfile @@ -90,11 +90,12 @@ USER frappe COPY pretend-bench.sh /usr/local/bin/bench COPY push_backup.py /usr/local/bin/push-backup # healthcheck.sh used in helm chart -COPY configure.sh patched_bench_helper.py healthcheck.sh /usr/local/bin/ +COPY configure.py patched_bench_helper.py healthcheck.sh /usr/local/bin/ WORKDIR /home/frappe/frappe-bench/sites -VOLUME [ "/home/frappe/frappe-bench/sites", "/home/frappe/frappe-bench/logs" ] +# TODO: Do volumes are being overwritten after copying from other stages? +VOLUME [ "/home/frappe/frappe-bench/sites" ] CMD [ "/home/frappe/frappe-bench/env/bin/gunicorn", "-b", "0.0.0.0:8000", "frappe.app:application", "--access-logfile", "-" ] diff --git a/build/worker/configure.py b/build/worker/configure.py new file mode 100755 index 00000000..76ba6d61 --- /dev/null +++ b/build/worker/configure.py @@ -0,0 +1,55 @@ +#!/usr/local/bin/python + +import json +import os +from typing import Any, Type, TypeVar + + +def update_config(**values: Any): + fname = "common_site_config.json" + if not os.path.exists(fname): + with open(fname, "a") as f: + json.dump({}, f) + + with open(fname, "r+") as f: + config: dict[str, Any] = json.load(f) + config.update(values) + f.seek(0) + f.truncate() + json.dump(config, f) + + +_T = TypeVar("_T") + + +def env(name: str, type_: Type[_T] = str) -> _T: + value = os.getenv(name) + if not value: + raise RuntimeError(f'Required environment variable "{name}" not set') + try: + value = type_(value) + except Exception: + raise RuntimeError( + f'Cannot convert environment variable "{name}" to type "{type_}"' + ) + return value + + +def generate_redis_url(url: str): + return f"redis://{url}" + + +def main() -> int: + update_config( + db_host=env("DB_HOST"), + db_port=env("DB_PORT", int), + redis_cache=generate_redis_url(env("REDIS_CACHE")), + redis_queue=generate_redis_url(env("REDIS_QUEUE")), + redis_socketio=generate_redis_url(env("REDIS_SOCKETIO")), + socketio_port=env("SOCKETIO_PORT", int), + ) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/build/worker/configure.sh b/build/worker/configure.sh deleted file mode 100755 index dadef1f6..00000000 --- a/build/worker/configure.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -set -e -set -x - -CUR_DIR="$(pwd)" -cd /home/frappe/frappe-bench/sites - -if [[ ! -f common_site_config.json ]]; then - echo "{}" >common_site_config.json -fi - -bench set-config --global --parse socketio_port "${SOCKETIO_PORT}" -bench set-config --global db_host "${DB_HOST}" -bench set-config --global --parse db_port "${DB_PORT}" -bench set-config --global redis_cache "redis://${REDIS_CACHE}" -bench set-config --global redis_queue "redis://${REDIS_QUEUE}" -bench set-config --global redis_socketio "redis://${REDIS_SOCKETIO}" - -cd "$CUR_DIR" -exec "$@" diff --git a/compose.yaml b/compose.yaml index 6145bf41..00a0fc1b 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,7 +1,7 @@ services: configurator: image: frappe/frappe-worker:${FRAPPE_VERSION} - command: configure.sh + command: configure.py environment: DB_HOST: db DB_PORT: 3306