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
2021-09-27 17:31:30 +09:00

122 lines
2.7 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='/usr/local/bin/undocker',
user='root',
group='root',
mode='755',
sha256sum='b937e69e774c530c080c1f6685763ca7db4dc58520a5a2054d111e1504f47688',
)
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
)
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,
)