Skip to content

Commit

Permalink
Remove custom proxy-metrics code.
Browse files Browse the repository at this point in the history
The only reason we had a custom implementation for proxy-metrics was
because constellation used to start containers without any networking,
and would only attach to the network as a second step, which broke the
proxy's expectation. This issue was solved in
reside-ic/constellation#23.

Thanks to this we can simplify our setup and use the standard
declarative approach even for the proxy-metrics container.
  • Loading branch information
plietar committed Dec 2, 2024
1 parent d8b6856 commit ab2469a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 79 deletions.
10 changes: 6 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ style = [
]
fmt = [
"black {args:.}",
"ruff --fix {args:.}",
"ruff check --fix {args:.}",
"style",
]
all = [
Expand All @@ -105,6 +105,8 @@ skip-string-normalization = true
[tool.ruff]
target-version = "py37"
line-length = 120

[tool.ruff.lint]
select = [
"A",
"ARG",
Expand Down Expand Up @@ -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"]

Expand Down
4 changes: 2 additions & 2 deletions src/montagu_deploy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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":
Expand Down
99 changes: 26 additions & 73 deletions src/montagu_deploy/montagu_constellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit ab2469a

Please sign in to comment.