forked from lab-in-a-box/liab-deployments
285 lines
8 KiB
Python
285 lines
8 KiB
Python
"""
|
|
# Deploy the git-pages service as a rootless Podman container
|
|
|
|
This deployment relies on container images that are specifically crafted
|
|
to function as a component of an already existing caddy deployment
|
|
(see https://hub.datalad.org/forgejo/git-pages/src/branch/main/nonstandalone.Dockerfile).
|
|
This is in contrast to the upstream-provided container, which embeds caddy
|
|
itself and aims at a more monolithic deployment.
|
|
|
|
Caddy is configured to reverse-proxy all otherwise unmatched HTTPS and HTTP
|
|
requests to the service. HTTPS certificates are obtained on demand. Caddy's
|
|
on-demand TLS permission check is directed to the service's authorization
|
|
endpoint at ``caddy_port``. The Caddyfile path defaults to
|
|
``/etc/caddy/Caddyfile`` and may be overridden with the host inventory key
|
|
``caddyfile_path``.
|
|
|
|
The deployment expects:
|
|
- Podman and Skopeo to be available on the host.
|
|
- A Caddy instance and Caddyfile compatible with the generated global
|
|
options and site block.
|
|
- A configuration asset accessible as ``config_file_asset``.
|
|
- A service identity given by ``user_spec`` as either
|
|
``(username, uid)`` or ``(username, uid, home_directory)``.
|
|
|
|
Port assignments must not conflict with other services on the host.
|
|
|
|
An example inventory may look like this:
|
|
|
|
```python
|
|
group = [
|
|
(
|
|
'myhost',
|
|
{
|
|
'gitpages': {
|
|
# User identity for the gitpages service
|
|
# The expanded form is: (username, uid, home_directory).
|
|
'user_spec': (
|
|
'myservice-gitpages',
|
|
2003,
|
|
'/data/gitpages',
|
|
),
|
|
|
|
'container_tag': 'hub.datalad.org/forgejo/git-pages:v0.9.0-83-g7f38332',
|
|
|
|
# git-pages config file to deposit
|
|
'config_file_asset': 'assets/gitpages-config-loki.toml',
|
|
|
|
# Local TCP ports on which the service listens. This
|
|
# deployment binds the service to 127.0.0.1.
|
|
# main git-pages service
|
|
'host_port': 10001,
|
|
# caddy TLS authorization helper service
|
|
# (asked by caddy whether it is OK to demand TLS)
|
|
'caddy_port': 10002,
|
|
# prometheus metrics port
|
|
'metrics_port': 10003,
|
|
},
|
|
],
|
|
},
|
|
]
|
|
```
|
|
|
|
## Notes on using this deployment with a (wildcard) hosting setup for forgejo
|
|
|
|
When deploying via the git-pages action, use the `server` argument to point to
|
|
the machine running git-pages via a hostname for which an SSL certificate
|
|
already exists.
|
|
|
|
When deploying via webhook, add the webhook as documented for codeberg-pages.
|
|
Whenever a very first pages deployment is done for a specific
|
|
user/organization, use the `http://` protocol for the webhook initially. Click
|
|
the "test delivery" button once, wait for the site to be deployed. Afterwards,
|
|
switch the webhook URL to `https://`. Now the on-demand certificate generation
|
|
can work, because the site actually exists.
|
|
"""
|
|
|
|
from pyinfra import host
|
|
from pyinfra.api import deploy as pyinfra_deploy
|
|
from pyinfra.operations import (
|
|
files,
|
|
systemd,
|
|
)
|
|
from liab_deployments.deploy import (
|
|
all_sites,
|
|
check_dependencies,
|
|
check_no_duplicates,
|
|
get_user_info,
|
|
)
|
|
from liab_deployments.operations import (
|
|
caddy,
|
|
network,
|
|
podman,
|
|
user,
|
|
user_systemd,
|
|
)
|
|
|
|
|
|
service_unit_tmpl = """\
|
|
[Unit]
|
|
Description=Podman container-{name}.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 run \\
|
|
--cidfile=%t/%n.ctr-id \\
|
|
--cgroups=no-conmon \\
|
|
--rm \\
|
|
--sdnotify=conmon \\
|
|
-d \\
|
|
--replace \\
|
|
--name {name} \\
|
|
-p 127.0.0.1:{host_port}:3000 \\
|
|
-p 127.0.0.1:{caddy_port}:3001 \\
|
|
-p 127.0.0.1:{metrics_port}:3002 \\
|
|
-v /etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt:ro \\
|
|
-v {user_home}/data:/app/data:Z \\
|
|
-v {user_home}/gitpages.toml:/app/config.toml:Z \\
|
|
{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_globalopts_tmpl = """\
|
|
{{
|
|
auto_https disable_redirects
|
|
on_demand_tls {{
|
|
permission http http://localhost:{caddy_port}
|
|
}}
|
|
}}
|
|
"""
|
|
|
|
caddyfile_block_tmpl = """\
|
|
https://, http:// {{
|
|
tls {{
|
|
on_demand
|
|
}}
|
|
|
|
reverse_proxy http://localhost:{host_port}
|
|
}}
|
|
"""
|
|
|
|
|
|
# used for the systemd service (lower), and the caddy block marker (upper),
|
|
# and in documentation (verbatim capitalization)
|
|
deploy_name = 'gitpages'
|
|
service_name = deploy_name.lower()
|
|
service_marker = deploy_name.upper()
|
|
|
|
|
|
@pyinfra_deploy(f"Deploy {deploy_name}")
|
|
def deploy():
|
|
if not hasattr(host.data, service_name):
|
|
return
|
|
|
|
check_dependencies(('podman', 'skopeo'))
|
|
check_no_duplicates(all_sites(host.data.dict()), ('host_port',), context='host')
|
|
|
|
kwargs = {
|
|
# fallback values from the deployment itself
|
|
"caddyfile_globalopts_tmpl": caddyfile_globalopts_tmpl,
|
|
"caddyfile_block_tmpl": caddyfile_block_tmpl,
|
|
"service_unit_tmpl": service_unit_tmpl,
|
|
# override with inventory-provided specifics
|
|
**host.data.gitpages
|
|
}
|
|
_deploy_gitpages(**kwargs)
|
|
|
|
|
|
def _deploy_gitpages(
|
|
*,
|
|
container_tag: str,
|
|
user_spec: tuple[str, int] | tuple[str, int, str],
|
|
host_port: int,
|
|
caddy_port: int,
|
|
metrics_port: int,
|
|
caddyfile_globalopts_tmpl: str,
|
|
caddyfile_block_tmpl: str,
|
|
service_unit_tmpl: str,
|
|
config_file_asset: str,
|
|
):
|
|
user_name, uid, user_home = get_user_info(user_spec)
|
|
|
|
user.systemd_service(
|
|
name=f'user {user_name}({uid})',
|
|
user_name=user_name,
|
|
user_id=uid,
|
|
user_home=user_home,
|
|
)
|
|
files.directory(
|
|
path=f'{user_home}/data',
|
|
present=True,
|
|
user=user_name,
|
|
group=user_name,
|
|
)
|
|
|
|
user_systemd.service_unit(
|
|
user_name,
|
|
user_home,
|
|
service_name,
|
|
service_unit_tmpl.format(
|
|
name=service_name,
|
|
user_home=user_home,
|
|
container_tag=container_tag,
|
|
host_port=host_port,
|
|
caddy_port=caddy_port,
|
|
metrics_port=metrics_port,
|
|
),
|
|
name=f'{service_name!r} service unit',
|
|
)
|
|
|
|
files.put(
|
|
name=f'{deploy_name} config',
|
|
src=config_file_asset,
|
|
dest=f'{user_home}/gitpages.toml',
|
|
mode='0600',
|
|
_sudo_user=user_name,
|
|
)
|
|
|
|
podman.pull(
|
|
container_tag,
|
|
name="Pull container image (update)",
|
|
_sudo_user=user_name,
|
|
)
|
|
|
|
service = systemd.service(
|
|
name=f'Launch {user_name} > {service_name}',
|
|
service=service_name,
|
|
running=True,
|
|
restarted=True,
|
|
enabled=True,
|
|
daemon_reload=True,
|
|
user_mode=True,
|
|
user_name=user_name,
|
|
)
|
|
|
|
network.wait_host_port_active(
|
|
host_port,
|
|
name=f"Wait for {deploy_name} to start",
|
|
_timeout=120,
|
|
_if=service.did_change,
|
|
)
|
|
|
|
files.block(
|
|
name='Caddy global options',
|
|
path=host.data.get('caddyfile_path', '/etc/caddy/Caddyfile'),
|
|
# it appears to be very very important that the marker
|
|
# here does not say GITPAGES (one word), as otherwise
|
|
# it will be replaced with the next block
|
|
marker='# {mark} GIT PAGES GLOBAL',
|
|
content=caddyfile_globalopts_tmpl.format(
|
|
host_port=host_port,
|
|
caddy_port=caddy_port,
|
|
metrics_port=metrics_port,
|
|
),
|
|
present=True,
|
|
before=True,
|
|
after=True,
|
|
try_prevent_shell_expansion=True,
|
|
)
|
|
|
|
caddy.caddyfile_block(
|
|
marker=f'{service_marker}',
|
|
content=caddyfile_block_tmpl.format(
|
|
host_port=host_port,
|
|
caddy_port=caddy_port,
|
|
metrics_port=metrics_port,
|
|
),
|
|
)
|