This repository has been archived on 2023-02-05. You can view files and clone it, but cannot push or open issues or pull requests.
pyinfra-photoprism/deploy.py

132 lines
3 KiB
Python

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=['mariadb-server', 'skopeo', 'curl'],
update=False,
)
server.group(
name='Create UNIX group',
group=host.data.unix_group,
system=True,
present=True,
)
server.user(
name='Create UNIX user',
user=host.data.unix_user,
group=host.data.unix_group,
home=host.data.unix_home,
shell=host.data.unix_shell,
ensure_home=True,
system=True,
comment='photoprism system user',
present=True,
)
for items in ['storage', 'originals', 'import']:
files.directory(
name='Make required directories {}'.format(items),
path=host.data.unix_home+'/{}'.format(items),
user=host.data.unix_user,
group=host.data.unix_group,
mode=755
)
mysql.user(
name='Create MariaDB user',
user=host.data.photoprism_database_user,
password=host.data.photoprism_database_password,
)
mysql.database(
name='Create MariaDB database',
database=host.data.photoprism_database_name,
user=host.data.photoprism_database_user,
)
files.download(
name='Download undocker',
src=host.data.undocker_url,
dest=host.data.undocker_bin,
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(host.data.undocker_cache+'/photoprism.tar'):
server.shell(
name='Download photoprism Docker image',
chdir=host.data.undocker_cache,
commands=['skopeo copy docker://docker.io/photoprism/photoprism:latest docker-archive:photoprism.tar'],
)
files.directory(
name='Create undocker destination',
path=host.data.undocker_dst,
user='root',
group='root',
mode=755
)
# If the binary is here, undockerized image should be fine, don't extract again
if not host.fact.file(host.data.undocker_dst+'/opt/photoprism/bin/photoprism'):
server.shell(
name='Undocker the Docker image',
chdir=host.data.undocker_dst,
commands=['undocker '+host.data.undocker_cache+'/photoprism.tar - | tar -xv'],
)
files.template(
name='Set env file',
src='templates/env.j2',
dest=host.data.systemd_env,
mode='600',
)
files.template(
name='Set systemd service file',
src='templates/photoprism.service.j2',
dest='/etc/systemd/system/photoprism.service',
mode='644',
)
systemd.daemon_reload(
name='Reload systemd',
user_mode=False,
)
systemd.service(
name='Enable systemd service',
service='photoprism.service',
running=True,
restarted=True,
enabled=True,
)
files.template(
name='Set systemd-run wrapper',
src='templates/systemd-run.sh.j2',
dest='/usr/local/bin/photoprism',
mode='755',
)