2
0
mirror of https://github.com/frappe/frappe_docker.git synced 2024-11-10 07:11:00 +00:00

Use Python for configurator service: faster and more robust

This commit is contained in:
Lev Vereshchagin 2021-12-15 13:37:06 +03:00
parent 28c0a2d564
commit a992ab497a
4 changed files with 59 additions and 24 deletions

View File

@ -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", "-" ]

55
build/worker/configure.py Executable file
View File

@ -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())

View File

@ -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 "$@"

View File

@ -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