Skip to content

Commit

Permalink
fix: proxy reload after changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Morriz committed Feb 17, 2024
1 parent ffa5a5d commit ee37192
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
9 changes: 5 additions & 4 deletions api/extract-openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import os
import sys
from logging import info

import yaml
from uvicorn.importer import import_from_string
Expand All @@ -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}")
9 changes: 6 additions & 3 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!.venv/bin/python
import os
from logging import info
from typing import Any, Dict, List

import dotenv
Expand All @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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"


Expand Down
5 changes: 3 additions & 2 deletions lib/certs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from logging import info

from lib.proxy import get_domains
from lib.utils import run_command
Expand All @@ -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 = [
Expand Down Expand Up @@ -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
5 changes: 3 additions & 2 deletions lib/data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from logging import info
from typing import Callable, Dict, List

import yaml
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions lib/proxy.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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(
Expand All @@ -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")
7 changes: 4 additions & 3 deletions lib/upstream.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from logging import info
from typing import List

from jinja2 import Template
Expand Down Expand Up @@ -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}")


Expand All @@ -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:
Expand All @@ -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}")

0 comments on commit ee37192

Please sign in to comment.