from pyinfra import host from pyinfra.operations import apt, server, files, systemd, mysql apt.update( name='Update apt repositories', ) apt.upgrade( name='Upgrade apt packages', ) apt.packages( name='Install dependencies', packages=host.data.app['deps'], update=False, ) server.user( name='Create UNIX user', user=host.data.unix_account['user'], home=host.data.unix_account['home'], shell=host.data.unix_account['shell'], ensure_home=True, system=True, comment=f"{host.data.unix_account['user']} system user", present=True, ) for directory in host.data.data_directories: files.directory( name=f'Make required directory {directory}', path=directory, user=host.data.unix_account['user'], group=host.data.unix_account['group'], mode=755 ) files.download( name='Download undocker', src=host.data.undocker['url'], dest=host.data.undocker['bin_path'], user='root', group='root', mode='755', sha256sum=host.data.undocker['sha256'], ) files.directory( name='Create undocker cache', path=host.data.undocker['cache'], user='root', group='root', mode=755 ) if not host.fact.file(f"{host.data.undocker['cache']}/{host.data.app['name']}.tar"): server.shell( name='Download Docker image', chdir=host.data.undocker['cache'], commands=[ f"skopeo copy {host.data.app['image']} docker-archive:{host.data.app['name']}.tar" ], ) files.directory( name='Create undocker app destination', path=host.data.undocker['app_dst'], user='root', group='root', mode=755 ) # If the bootstrap file is here, undockerized image should be fine, don't extract again if not host.fact.file(f"{host.data.undocker['app_dst']}/etc/linkding/bootstrap.sh"): server.shell( name='Undocker the Docker image', chdir=host.data.undocker['app_dst'], commands=[ f"undocker {host.data.undocker['cache']}/{host.data.app['name']}.tar - | tar -xv" ], ) # Since the app is badly Dockerized we need to do a chown files.directory( name=f"Ensure {host.data.undocker['app_dst']}/etc/linkding has correct rights", path=f"{host.data.undocker['app_dst']}/etc/linkding", user=host.data.unix_account['user'], group=host.data.unix_account['group'], ) files.template( name='Set env file', src='templates/env.j2', dest=host.data.systemd['EnvironmentFile'], mode='600', ) files.template( name='Set systemd service file', src=f'templates/systemd.service.j2', dest=f"/etc/systemd/system/{host.data.app['name']}.service", mode='644', ) systemd.daemon_reload( name='Reload systemd', user_mode=False, ) systemd.service( name='Enable systemd service', service=f"{host.data.app['name']}.service", running=True, restarted=True, enabled=True, ) files.template( name='Set systemd-run wrapper', src='templates/systemd-run.sh.j2', dest=f"/usr/local/bin/{host.data.app['name']}", mode='755', )