diff --git a/pyproject.toml b/pyproject.toml index a03207b..a290cf9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -89,7 +89,7 @@ style = [ ] fmt = [ "black {args:.}", - "ruff --fix {args:.}", + "ruff check --fix {args:.}", "style", ] all = [ @@ -105,6 +105,8 @@ skip-string-normalization = true [tool.ruff] target-version = "py37" line-length = 120 + +[tool.ruff.lint] select = [ "A", "ARG", @@ -149,13 +151,13 @@ unfixable = [ "F401", ] -[tool.ruff.isort] +[tool.ruff.lint.isort] known-first-party = ["montagu_deploy"] -[tool.ruff.flake8-tidy-imports] +[tool.ruff.lint.flake8-tidy-imports] ban-relative-imports = "all" -[tool.ruff.per-file-ignores] +[tool.ruff.lint.per-file-ignores] # Tests can use magic values, assertions, and relative imports "tests/**/*" = ["PLR2004", "S101", "TID252"] diff --git a/src/montagu_deploy/cli.py b/src/montagu_deploy/cli.py index 9ee1872..072456f 100644 --- a/src/montagu_deploy/cli.py +++ b/src/montagu_deploy/cli.py @@ -26,7 +26,7 @@ import montagu_deploy.__about__ as about from montagu_deploy.config import MontaguConfig -from montagu_deploy.montagu_constellation import MontaguConstellation +from montagu_deploy.montagu_constellation import montagu_constellation def main(argv=None): @@ -35,7 +35,7 @@ def main(argv=None): return about.__version__ else: cfg = MontaguConfig(path, extra, options) - obj = MontaguConstellation(cfg) + obj = montagu_constellation(cfg) if args.action == "start": montagu_start(obj, args) elif args.action == "status": diff --git a/src/montagu_deploy/montagu_constellation.py b/src/montagu_deploy/montagu_constellation.py index 7d767dd..ca7653e 100644 --- a/src/montagu_deploy/montagu_constellation.py +++ b/src/montagu_deploy/montagu_constellation.py @@ -9,44 +9,26 @@ from montagu_deploy import database -class MontaguConstellation: - def __init__(self, cfg): - api = api_container(cfg) - db = db_container(cfg) - admin = admin_container(cfg) - contrib = contrib_container(cfg) - proxy = proxy_container(cfg) - mq = mq_container(cfg) - flower = flower_container(cfg) - task_queue = task_queue_container(cfg) - - containers = [api, db, admin, contrib, proxy, mq, flower, task_queue] - - if cfg.fake_smtp_ref: - fake_smtp = fake_smtp_container(cfg) - containers.append(fake_smtp) - - self.cfg = cfg - self.obj = constellation.Constellation( - "montagu", cfg.container_prefix, containers, cfg.network, cfg.volumes, data=cfg, vault_config=cfg.vault - ) - - def start(self, **kwargs): - self.obj.start(**kwargs) - # The proxy metrics container cannot be started via constellation, because - # it has to belong to the same network as the proxy as soon as it is started - # and constellation starts containers on the 'none' network. So we provide - # start/stop/status methods for the metrics container that mimic the - # constellation behaviour - start_proxy_metrics(self.cfg) - - def stop(self, **kwargs): - stop_proxy_metrics(self.cfg) - self.obj.stop(**kwargs) - - def status(self): - self.obj.status() - status_proxy_metrics(self.cfg) +def montagu_constellation(cfg): + containers = [ + api_container(cfg), + db_container(cfg), + admin_container(cfg), + contrib_container(cfg), + proxy_container(cfg), + proxy_metrics_container(cfg), + mq_container(cfg), + flower_container(cfg), + task_queue_container(cfg), + ] + + if cfg.fake_smtp_ref: + fake_smtp = fake_smtp_container(cfg) + containers.append(fake_smtp) + + return constellation.Constellation( + "montagu", cfg.container_prefix, containers, cfg.network, cfg.volumes, data=cfg, vault_config=cfg.vault + ) def admin_container(cfg): @@ -250,40 +232,11 @@ def proxy_configure(container, cfg): docker_util.string_into_container(cfg.dhparam, container, join(ssl_path, "dhparam.pem")) -def start_proxy_metrics(cfg): - name = "{}-{}".format(cfg.container_prefix, cfg.containers["metrics"]) +def proxy_metrics_container(cfg): proxy_name = cfg.containers["proxy"] - image = str(cfg.proxy_metrics_ref) - print("Starting {} ({})".format(cfg.containers["metrics"], image)) - docker.from_env().containers.run( - image, - restart_policy={"Name": "always"}, - ports={"9113/tcp": 9113}, - command=f'-nginx.scrape-uri "http://{proxy_name}/basic_status"', - network=cfg.network, - name=name, - detach=True, + return constellation.ConstellationContainer( + cfg.containers["metrics"], + cfg.proxy_metrics_ref, + ports=[9113], + args=["-nginx.scrape-uri", f"http://{proxy_name}/basic_status"], ) - - -def stop_proxy_metrics(cfg): - name = "{}-{}".format(cfg.container_prefix, cfg.containers["metrics"]) - container = get_container(name) - if container: - print(f"Killing '{name}'") - container.remove(force=True) - - -def status_proxy_metrics(cfg): - name = "{}-{}".format(cfg.container_prefix, cfg.containers["metrics"]) - container = get_container(name) - status = container.status if container else "missing" - print(" - {} ({}): {}".format(cfg.containers["metrics"], name, status)) - - -def get_container(name): - client = docker.client.from_env() - try: - return client.containers.get(name) - except docker.errors.NotFound: - return None