From ee37192787ab3fc1951192d64a586aacd415c8c8 Mon Sep 17 00:00:00 2001 From: Maurice Faber Date: Sat, 17 Feb 2024 22:41:41 +0100 Subject: [PATCH] fix: proxy reload after changes --- api/extract-openapi.py | 9 +++++---- api/main.py | 9 ++++++--- lib/certs.py | 5 +++-- lib/data.py | 5 +++-- lib/proxy.py | 21 ++++++++++++++------- lib/upstream.py | 7 ++++--- 6 files changed, 35 insertions(+), 21 deletions(-) diff --git a/api/extract-openapi.py b/api/extract-openapi.py index c258e46..7933672 100755 --- a/api/extract-openapi.py +++ b/api/extract-openapi.py @@ -3,6 +3,7 @@ import json import os import sys +from logging import info import yaml from uvicorn.importer import import_from_string @@ -18,19 +19,19 @@ args = parser.parse_args() if args.app_dir is not None: - print(f"adding {args.app_dir} to sys.path") + info(f"adding {args.app_dir} to sys.path") sys.path.insert(0, args.app_dir) - print(f"importing app from {args.app}") + info(f"importing app from {args.app}") app = import_from_string(args.app) openapi = app.openapi() version = openapi.get("openapi", "unknown version") - print(f"writing openapi spec v{version}") + info(f"writing openapi spec v{version}") with open(args.out, "w", encoding="utf-8") as f: if args.out.endswith(".json"): json.dump(openapi, f, indent=2) else: yaml.dump(openapi, f, sort_keys=False) - print(f"spec written to {args.out}") + info(f"spec written to {args.out}") diff --git a/api/main.py b/api/main.py index f0829f2..bb5cad6 100755 --- a/api/main.py +++ b/api/main.py @@ -1,5 +1,6 @@ #!.venv/bin/python import os +from logging import info from typing import Any, Dict, List import dotenv @@ -21,7 +22,7 @@ ) from lib.git import update_repo from lib.models import Env, PingPayload, Project, Service, WorkflowJobPayload -from lib.proxy import reload_proxy, write_nginx +from lib.proxy import reload_proxy, update_proxy, write_nginx from lib.upstream import check_upstream, update_upstream, write_upstreams dotenv.load_dotenv() @@ -33,11 +34,13 @@ def _after_config_change(project: str, service: str = None) -> None: """Run after a project is updated""" + info("Config change detected") get_certs(project) write_nginx() write_upstreams() update_upstream(project, service, rollout=True) - reload_proxy() + update_proxy() + # reload_proxy() def _handle_update_upstream(project: str, service: str) -> None: @@ -74,7 +77,7 @@ async def github_ping_handler( **_: Any, ) -> str: """Handle incoming github webhook requests for ping events to notify callers we're up""" - print(f"Got ping message: {payload.zen}") + info(f"Got ping message: {payload.zen}") return "pong" diff --git a/lib/certs.py b/lib/certs.py index 2cd8716..c09de4b 100755 --- a/lib/certs.py +++ b/lib/certs.py @@ -1,4 +1,5 @@ import os +from logging import info from lib.proxy import get_domains from lib.utils import run_command @@ -13,7 +14,7 @@ def get_certs(project: str = None) -> bool: change_file = "/data/changed" changed = False - print(f"Running certbot on domains: {' '.join(domains)}") + info(f"Running certbot on domains: {' '.join(domains)}") for domain in domains: # Run certbot command inside docker command = [ @@ -57,7 +58,7 @@ def get_certs(project: str = None) -> bool: # Check if certificates have changed if os.path.isfile("." + change_file): changed = True - print(f"Certificates for {domain} have been updated") + info(f"Certificates for {domain} have been updated") os.remove("." + change_file) return changed diff --git a/lib/data.py b/lib/data.py index 38ae910..1f253c2 100644 --- a/lib/data.py +++ b/lib/data.py @@ -1,3 +1,4 @@ +from logging import info from typing import Callable, Dict, List import yaml @@ -48,7 +49,7 @@ def get_project(name: str, throw: bool = True) -> Project: if item.name == name: return item error = f"Project {name} not found" - print(error) + info(error) if throw: raise ValueError(error) return None @@ -80,7 +81,7 @@ def get_service(project: str | Project, service: str, throw: bool = True) -> Ser if item.name == service: return item error = f"Service {service} not found in project {project}" - print(error) + info(error) if throw: raise ValueError(error) return None diff --git a/lib/proxy.py b/lib/proxy.py index 223269e..3255613 100644 --- a/lib/proxy.py +++ b/lib/proxy.py @@ -1,9 +1,9 @@ +from logging import info from typing import Dict, List from jinja2 import Template from lib.data import get_project, get_projects -from lib.models import Project, Service from lib.utils import run_command @@ -78,8 +78,18 @@ def write_nginx() -> None: write_terminate() +def update_proxy( + service: str = None, +) -> None: + """Reload service(s) in the docker compose config for the proxy""" + info(f"Updating proxy {service}") + run_command(["docker", "compose", "pull"], cwd="proxy") + run_command(["docker", "compose", "up", "-d"], cwd="proxy") + rollout_proxy(service) + + def reload_proxy(service: str = None) -> None: - print("Reloading proxy") + info("Reloading proxy") # Execute docker compose command to reload nginx for both 'proxy' and 'terminate' services for s in [service] if service else ["proxy", "terminate"]: run_command( @@ -89,9 +99,6 @@ def reload_proxy(service: str = None) -> None: def rollout_proxy(service: str = None) -> None: - print(f'Rolling out service "{service}"') + info(f"Rolling out proxy {service}") for s in [service] if service else ["proxy", "terminate"]: - run_command( - ["docker", "rollout", s], - cwd="proxy", - ) + run_command(["docker", "rollout", s], cwd="proxy") diff --git a/lib/upstream.py b/lib/upstream.py index 8fd791b..1104760 100644 --- a/lib/upstream.py +++ b/lib/upstream.py @@ -1,4 +1,5 @@ import os +from logging import info from typing import List from jinja2 import Template @@ -37,7 +38,7 @@ def check_upstream(project: str, service: str = None) -> None: if not service: return if not get_service(project, service): - print(f"Project {project} does not have service {service}") + info(f"Project {project} does not have service {service}") raise ValueError(f"Project {project} does not have service {service}") @@ -47,7 +48,7 @@ def update_upstream( rollout: bool = False, ) -> None: """Reload service(s) in a docker compose config""" - print(f"Updating upstream for project {project}") + info(f"Updating upstream for project {project}") run_command(["docker", "compose", "pull"], cwd=f"upstream/{project}") run_command(["docker", "compose", "up", "-d"], cwd=f"upstream/{project}") if not rollout: @@ -68,5 +69,5 @@ def update_upstreams(rollout: bool = False) -> None: def rollout_service(project: str, service: str) -> None: - print(f'Rolling out service "{project}:{service}"') + info(f'Rolling out service "{project}:{service}"') run_command(["docker", "rollout", f"{project}-{service}"], cwd=f"upstream/{project}")