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 108f88b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/montagu_deploy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
100 changes: 28 additions & 72 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,14 @@ 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"])
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,
)


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
def proxy_metrics_container(cfg):
proxy_name = cfg.containers["proxy"]
return constellation.ConstellationContainer(
cfg.containers["metrics"],
cfg.proxy_metrics_ref,
ports=[9113],
args=["-nginx.scrape-uri", f"http://{proxy_name}/basic_status"],
)

0 comments on commit 108f88b

Please sign in to comment.