liab-deployments/deployments/forgejo_aneksajo_users.py
2025-10-26 15:50:17 +01:00

87 lines
2.3 KiB
Python

#
# Create accounts in a forgejo instance that are given in a TSV file.
#
# An example inventory may look like this:
#
# group = [
# (
# 'edu.datalad.org',
# {
# 'forgejo_aneksajo': [
# {
# 'fqdn': "hub.edu.datalad.org",
# 'user': 'git',
# 'accounts_specfile': 'assets/forgejo_eduhub_accounts.tsv',
# }
# ],
# },
# ),
# ]
#
# An example row of an account table looks like this (tab-separated!):
#
#Michael Hanke mih m.hanke@...de apod2439slmc3(*)&@'d
from pyinfra.api import deploy
from pyinfra import (
host,
)
from pyinfra.operations import (
python,
server,
)
@deploy("Deploy forgejo-aneksajo users")
def deploy_forgejo_aneksajo_users(
hostname,
accounts_specfile,
forgejo_user='git',
):
user_home = f'/home/{forgejo_user}'
existing_users_op = server.shell(
name='Get existing accounts',
commands=[
f'cd {user_home} && podman exec forgejo forgejo admin user list',
],
_sudo=True,
_sudo_user=forgejo_user,
)
# act on existing users list
def callback():
users = [l.split()[1] for l in existing_users_op.stdout.splitlines()[1:]]
with open(accounts_specfile) as af:
for line in af:
spec = line.strip().split('\t')
if spec[1] in users:
continue
server.shell(
name=f'Register forgejo {spec[1]}',
commands=[
f'cd {user_home} && podman exec forgejo forgejo admin user create --username {spec[1]} --email {spec[2]} --password {spec[3]} --must-change-password false',
],
_sudo=True,
_sudo_user=forgejo_user,
)
python.call(
name='Create missing accounts',
function=callback,
)
if hasattr(host.data, 'forgejo_aneksajo'):
for spec in host.data.forgejo_aneksajo:
if 'accounts_specfile' not in spec:
continue
deploy_forgejo_aneksajo_users(
hostname=spec['fqdn'],
accounts_specfile=spec['accounts_specfile'],
forgejo_user=spec['user'],
)