liab-deployments/liab_deployments/deploy/gitannex_staticwww.py
Michael Hanke 7d4afb6a1d
feat: simplify gitannex_staticwww deployment
Avoid Git repo ownership complexity and thereby having to use Git's
"safe" mechanism. Webserver readability is now implemented via the group
membership of the clone.
2025-12-02 09:30:19 +01:00

100 lines
2.5 KiB
Python

#
# Deploy a git-annex repo with slides that has media files in submodules too.
#
# The repository is cloned/updated from a given URL, and placed into given
# target directory. Caddy is configured to serve this directory as static
# content at a particular FQDN.
#
# An example inventory may look like this:
#
# group = [
# (
# 'edu.datalad.org',
# {
# 'gitannex_staticwww': {
# 'repos': [
# {
# 'serve_address': 'slides.edu.datalad.org',
# 'fetch_url': 'https://hub.datalad.org/edu/slides.git',
# 'target_dir': '/var/www/slides.edu.datalad.org',
# },
# ],
# },
# },
# ),
# ]
from pyinfra.api import deploy
from pyinfra import host
from pyinfra.operations import (
git,
server,
)
from liab_deployments.operations.caddy import caddyfile_block
caddyfile_block_tmpl = """\
{serve_address} {{
root * {serve_path}
file_server
}}
"""
@deploy("Deploy git-annex-repos")
def deploy_repos():
if not hasattr(host.data, 'gitannex_staticwww'):
return
for spec in host.data.gitannex_staticwww.get('repos', []):
_deploy_repo(
spec['serve_address'],
spec['fetch_url'],
spec['target_dir'],
# block template override is optional
caddyfile_block_tmpl=spec.get(
'caddyfile_block_tmpl', caddyfile_block_tmpl
),
)
def _deploy_repo(
serve_address,
fetch_url,
target_dir,
*,
caddyfile_block_tmpl,
):
www_group = host.data.get('www_group', 'caddy')
git.repo(
name=f'Fetch slides repo from {fetch_url}',
src=fetch_url,
dest=target_dir,
pull=True,
rebase=True,
update_submodules=True,
recursive_submodules=True,
# such that the deposit is readable by the webserver
group=www_group,
)
server.shell(
name=f"Get annex'ed content for {target_dir}",
commands=[
f'cd {target_dir} && git annex init && git annex get .',
f"cd {target_dir}"
" && git submodule foreach --recursive "
" 'git annex init && git annex get .'",
],
)
caddyfile_block(
marker=f'GITANNEXREPO {serve_address}',
content=caddyfile_block_tmpl.format(
serve_address=serve_address,
serve_path=target_dir,
),
)