forked from lab-in-a-box/liab-deployments
315 lines
9.6 KiB
Python
315 lines
9.6 KiB
Python
"""
|
|
# Deploy Forgejo(-aneksajo)
|
|
|
|
For general information on Forgejo-aneksajo see
|
|
https://codeberg.org/forgejo-aneksajo/forgejo-aneksajo
|
|
|
|
This deployment does NOT support multiple deployments under the SAME user
|
|
account.
|
|
|
|
The deployment has two principle modes of operation:
|
|
1) Providing `config_file_asset` allows for deploying or (re)configuring
|
|
a fully set up forgejo instance; 2) Not providing `config_file_asset` leaves
|
|
the instance unconfigured, and forgejo will present the initialization wizard
|
|
that allows for a convenient, basic setup, incl. the creation of an admin account.
|
|
With (1), no forgejo accounts will be set up, also no admin account. This needs
|
|
to be achieved with a different method (e.g., using the CLI, or a dedicated
|
|
user account deployment.
|
|
|
|
Example inventory
|
|
|
|
```python
|
|
group = [
|
|
(
|
|
'<host-FQDN>',
|
|
{
|
|
'forgejo': {
|
|
# defaults can provide setting for multiple site deployments
|
|
'defaults': {
|
|
'container_tag': 'hub.datalad.org/forgejo/forgejo-aneksajo:14-rootless',
|
|
# repo with customizations to fetch
|
|
# (should be from other host for robust behavior)
|
|
# can also be a (url, branch) tuple
|
|
'customizations_fetch_url':
|
|
'https://<otherhost>/some.git',
|
|
# (username, env_var_name) — token is read from the
|
|
# named env var on the controller
|
|
# 'customizations_fetch_auth': ('bot', 'MY_FORGEJO_TOKEN'),
|
|
# if path to annex-get are given, repo is annex-init'ed too
|
|
'customizations_annex_get': '.',
|
|
'caddyfile_block_tmpl':
|
|
'''\
|
|
{serve_address} {{
|
|
reverse_proxy localhost:{host_port}
|
|
import cors *
|
|
}}
|
|
''',
|
|
# additional directories to create in the deployment
|
|
# HOME (as relative dirs). This can be necessary, when
|
|
# a particular forgejo configuration puts information
|
|
# into a non-standard location
|
|
'ensure_dirs': ['git/repositories', 'gitea/log'],
|
|
},
|
|
'sites': [
|
|
{
|
|
'serve_address': 'hub.example.org',
|
|
'user': ('hub', 2000, '/data/big/hub'),
|
|
'host_port': 30001,
|
|
'config_file_asset': 'assets/hub-example-org-app.ini',
|
|
'podman_container_run_args': '--cpus 0.5 --memory 8G',
|
|
},
|
|
],
|
|
}
|
|
},
|
|
),
|
|
]
|
|
```
|
|
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
|
|
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 (
|
|
annex,
|
|
caddy,
|
|
git as liab_git,
|
|
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 \\
|
|
-v {user_home}/gitea:/var/lib/gitea:Z \\
|
|
-v {user_home}/custom:/var/lib/gitea/custom:Z \\
|
|
-v {user_home}/conf:/var/lib/gitea/custom/conf:Z \\
|
|
-v {user_home}/git:/var/lib/gitea/git:Z \\
|
|
--userns keep-id:uid=1000,gid=1000 \\
|
|
{podman_container_run_args} \\
|
|
{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}
|
|
}}
|
|
"""
|
|
|
|
|
|
# used for the systemd service (lower), and the caddy block marker (upper),
|
|
# and in documentation (verbatim capitalization)
|
|
deploy_name = 'Forgejo'
|
|
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
|
|
|
|
defaults = host.data.forgejo.get('defaults', {})
|
|
sites = host.data.forgejo.get('sites', [])
|
|
|
|
check_dependencies(('podman', 'git', 'git-annex', 'skopeo'))
|
|
check_no_duplicates(sites, ('user', 0), context='forgejo')
|
|
check_no_duplicates(all_sites(host.data.dict()), ('host_port',), context='host')
|
|
|
|
for spec in sites:
|
|
kwargs = {
|
|
# fallback values from the deployment itself
|
|
"caddyfile_block_tmpl": caddyfile_block_tmpl,
|
|
"service_unit_tmpl": service_unit_tmpl,
|
|
"podman_container_run_args": '',
|
|
"config_file_asset": None,
|
|
"customizations_fetch_url": None,
|
|
"customizations_fetch_auth": None,
|
|
"customizations_annex_get": None,
|
|
"ensure_dirs": None,
|
|
# override with inventory-provided site defaults
|
|
**defaults,
|
|
# override with inventory-provided site specifics
|
|
**spec,
|
|
}
|
|
_deploy_forgejo(**kwargs)
|
|
|
|
|
|
def _deploy_forgejo(
|
|
*,
|
|
serve_address: str,
|
|
container_tag: str,
|
|
user_spec: tuple[str, int] | tuple[str, int, str],
|
|
host_port: int,
|
|
caddyfile_block_tmpl: str,
|
|
service_unit_tmpl: str,
|
|
podman_container_run_args: str,
|
|
config_file_asset: str | None = None,
|
|
customizations_fetch_url: str | tuple[str, str] | None = None,
|
|
customizations_fetch_auth: tuple[str, str] | None = None,
|
|
customizations_annex_get: str | Sequence[str] | None = None,
|
|
ensure_dirs: list[str] | None = None,
|
|
):
|
|
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,
|
|
)
|
|
if ensure_dirs is None:
|
|
# this is the set of directories that a "default" config
|
|
# expects, but that is NOT auto-created by forgejo when
|
|
# the installer is NOT running (when a full config is
|
|
# deployed)
|
|
ensure_dirs = ['conf', 'git/repositories', 'gitea/log', 'custom']
|
|
else:
|
|
# even if dirs are given, the container setup requires these
|
|
ensure_dirs = [*ensure_dirs, 'conf', 'git', 'gitea', 'custom']
|
|
|
|
for i in ensure_dirs:
|
|
files.directory(
|
|
path=f'{user_home}/{i}',
|
|
present=True,
|
|
_sudo_user=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,
|
|
podman_container_run_args=podman_container_run_args,
|
|
),
|
|
name=f'{service_name!r} service unit',
|
|
)
|
|
|
|
if customizations_fetch_url:
|
|
# do we need to account for a specific source branch?
|
|
if isinstance(customizations_fetch_url, tuple):
|
|
furl, fbranch = customizations_fetch_url
|
|
else:
|
|
furl = customizations_fetch_url
|
|
fbranch = None
|
|
|
|
cred_file = f'{user_home}/.git-fetch-credentials'
|
|
if customizations_fetch_auth:
|
|
cred_user_name, cred_token_env_var = customizations_fetch_auth
|
|
liab_git.gitcredential_from_env(
|
|
name=f'Credential store at {cred_file}',
|
|
path=cred_file,
|
|
url=furl,
|
|
user=cred_user_name,
|
|
env_var=cred_token_env_var,
|
|
_sudo_user=user_name,
|
|
)
|
|
|
|
annex.annexrepo_w_credential(
|
|
src=furl,
|
|
dest=f'{user_home}/custom',
|
|
cred_file=cred_file,
|
|
annex_get=customizations_annex_get,
|
|
pull=True,
|
|
rebase=True,
|
|
branch=fbranch,
|
|
name="Forgejo customization repo",
|
|
_sudo_user=user_name,
|
|
)
|
|
|
|
# needs to be done after a clone of the customization repo
|
|
if config_file_asset:
|
|
files.directory(
|
|
path=f'{user_home}/conf',
|
|
present=True,
|
|
mode='0700',
|
|
_sudo_user=user_name,
|
|
)
|
|
files.put(
|
|
name=f'{deploy_name} config',
|
|
src=config_file_asset,
|
|
dest=f'{user_home}/conf/app.ini',
|
|
mode='0600',
|
|
_sudo_user=user_name,
|
|
)
|
|
|
|
podman.pull(
|
|
container_tag,
|
|
name="Pull container image (update)",
|
|
_sudo_user=user_name,
|
|
)
|
|
|
|
forgejo_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} {serve_address!r} to start",
|
|
_timeout=120,
|
|
_if=forgejo_service.did_change,
|
|
)
|
|
|
|
caddy.caddyfile_block(
|
|
marker=f'{service_marker} {serve_address}',
|
|
content=caddyfile_block_tmpl.format(
|
|
serve_address=serve_address,
|
|
host_port=host_port,
|
|
),
|
|
)
|