forked from lab-in-a-box/liab-deployments
167 lines
3.7 KiB
Python
167 lines
3.7 KiB
Python
#
|
|
# Deploy Gatus (https://github.com/TwiN/gatus) health dashboard
|
|
#
|
|
# An example inventory may look like this:
|
|
#
|
|
# group = [
|
|
# (
|
|
# 'edu.datalad.org',
|
|
# {
|
|
# 'gatus': {
|
|
# 'sites': [
|
|
# {
|
|
# 'serve_address': 'status.example.org',
|
|
# 'container_tag': 'ghcr.io/twin/gatus:stable',
|
|
# # user name and numerical ID
|
|
# 'user': ('gatus', '2000'),
|
|
# 'host_port': 30010,
|
|
# 'config_file_asset': 'assets/status-example-config.yaml',
|
|
# },
|
|
# ],
|
|
# ...
|
|
#
|
|
from pyinfra.api import deploy
|
|
from pyinfra import (
|
|
host,
|
|
)
|
|
from pyinfra.operations import (
|
|
files,
|
|
server,
|
|
)
|
|
from liab_deployments.operations import (
|
|
caddy,
|
|
user,
|
|
user_systemd,
|
|
)
|
|
|
|
|
|
service_unit_tmpl = """\
|
|
[Unit]
|
|
Description=Podman-managed gatus service
|
|
Wants=network-online.target
|
|
After=network-online.target
|
|
RequiresMountsFor=%t/containers
|
|
|
|
[Service]
|
|
Environment=PODMAN_SYSTEMD_UNIT=%n
|
|
Restart=always
|
|
TimeoutStopSec=300
|
|
ExecStartPre=/bin/rm \\
|
|
-f %t/%n.ctr-id
|
|
|
|
ExecStart=/usr/bin/podman container run \\
|
|
--cidfile=%t/%n.ctr-id \\
|
|
--cgroups=no-conmon \\
|
|
--rm \\
|
|
--sdnotify=conmon \\
|
|
--network=slirp4netns \\
|
|
-d \\
|
|
--replace \\
|
|
--pull always \\
|
|
--name gatus \\
|
|
--userns=keep-id \\
|
|
-p {host_port}:8080 \\
|
|
-v {user_home}/config:/config:ro \\
|
|
-v {user_home}/data:/data \\
|
|
{container_tag}
|
|
ExecStop=/usr/bin/podman stop \\
|
|
--ignore -t 10 \\
|
|
--cidfile=%t/%n.ctr-id
|
|
ExecStopPost=/usr/bin/podman rm \\
|
|
-f \\
|
|
--ignore -t 10 \\
|
|
--cidfile=%t/%n.ctr-id
|
|
Type=notify
|
|
NotifyAccess=all
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
"""
|
|
|
|
caddyfile_block_tmpl = """\
|
|
{serve_address} {{
|
|
reverse_proxy localhost:{host_port}
|
|
}}
|
|
"""
|
|
|
|
|
|
@deploy("Deploy gatus")
|
|
def deploy_sites():
|
|
if not hasattr(host.data, 'gatus'):
|
|
return
|
|
for spec in host.data.gatus.get('sites', []):
|
|
_deploy_site(
|
|
spec['serve_address'],
|
|
spec['container_tag'],
|
|
spec['config_file_asset'],
|
|
spec['user'],
|
|
spec['host_port'],
|
|
name=spec.get('name', 'gatus'),
|
|
caddyfile_block_tmpl=spec.get(
|
|
'caddyfile_block_tmpl', caddyfile_block_tmpl
|
|
),
|
|
)
|
|
|
|
|
|
def _deploy_site(
|
|
serve_address,
|
|
container_tag,
|
|
config_file_asset,
|
|
user_spec: tuple[str, int],
|
|
host_port,
|
|
*,
|
|
name,
|
|
caddyfile_block_tmpl,
|
|
):
|
|
user_name, uid = user_spec
|
|
user_home = f'/home/{user_name}'
|
|
|
|
user.systemd_service(
|
|
user_name,
|
|
uid,
|
|
user_home,
|
|
)
|
|
|
|
for i in ['config', 'data']:
|
|
files.directory(
|
|
path=f'{user_home}/{i}',
|
|
present=True,
|
|
_sudo_user=user_name,
|
|
)
|
|
|
|
user_systemd.service_unit(
|
|
user_name,
|
|
user_home,
|
|
name,
|
|
service_unit_tmpl.format(
|
|
user_home=user_home,
|
|
container_tag=container_tag,
|
|
host_port=host_port,
|
|
),
|
|
)
|
|
|
|
files.put(
|
|
name='Gatus config',
|
|
src=config_file_asset,
|
|
dest=f'{user_home}/config/config.yaml',
|
|
_sudo_user=user_name,
|
|
)
|
|
|
|
user_systemd.run_service(
|
|
user_name,
|
|
uid,
|
|
'gatus',
|
|
)
|
|
|
|
server.wait(
|
|
name=f"Wait for gatus {serve_address!r} to start",
|
|
port=host_port,
|
|
)
|
|
|
|
caddy.caddyfile_block(
|
|
marker=f'GATUS {serve_address}',
|
|
content=caddyfile_block_tmpl.format(
|
|
serve_address=serve_address,
|
|
host_port=host_port,
|
|
),
|
|
)
|