users = {
    'paperless': {
        'home': '/opt/paperless',
    },
}

directories = {
    '/opt/paperless/src': {},
    '/opt/paperless/static': {
        'owner': 'paperless',
    },
}

git_deploy = {
    '/opt/paperless/src': {
        'repo': 'https://github.com/jonaswinkler/paperless-ng.git',
        'rev': node.metadata.get('paperless/version'),
        'triggers': {
            'action:paperless_collectstatic',
            'action:paperless_compile_frontend',
            'action:paperless_install_deps',
            'action:paperless_migrate_database',
            'svc_systemd:paperless-consumer:restart',
            'svc_systemd:paperless-scheduler:restart',
            'svc_systemd:paperless-webserver:restart',
        },
    },
}

files = {
    '/etc/systemd/system/paperless-consumer.service': {
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:paperless-consumer:restart',
        },
    },
    '/etc/systemd/system/paperless-scheduler.service': {
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:paperless-scheduler:restart',
        },
    },
    '/etc/systemd/system/paperless-webserver.service': {
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:paperless-webserver:restart',
        },
    },
    '/opt/paperless/src/paperless.conf': {
        'content_type': 'mako',
        'needs': {
            'git_deploy:/opt/paperless/src',
        },
        'triggers': {
            'svc_systemd:paperless-consumer:restart',
            'svc_systemd:paperless-scheduler:restart',
            'svc_systemd:paperless-webserver:restart',
        },
    },
}

actions = {
    'paperless_create_virtualenv': {
        'command': '/usr/bin/python3 -m virtualenv -p python3 /opt/paperless/venv/',
        'unless': 'test -d /opt/paperless/venv/',
        'needs': {
            # actually /opt/paperless, but we don't create that
            'directory:/opt/paperless/src',
            'pkg_apt:python3',
            'pkg_apt:python3-pip',
            'pkg_apt:python3-virtualenv',
        },
    },
    'paperless_install_deps': {
        'command':
            'cd /opt/paperless/src && '
            '/opt/paperless/venv/bin/pip install --upgrade pip && '
            '/opt/paperless/venv/bin/pip install --upgrade -r requirements.txt',
        'triggered': True,
        'needs': {
            'action:paperless_create_virtualenv',
        },
    },
    'paperless_migrate_database': {
        'command':
            'cd /opt/paperless/src/src && '
            'sudo -Hu paperless /opt/paperless/venv/bin/python manage.py migrate',
        'triggered': True,
        'needs': {
            # /mnt/paperless is NOT created by this bundle.
            'action:paperless_install_deps',
            'directory:/mnt/paperless',
            'directory:/opt/paperless/static',
            'file:/opt/paperless/src/paperless.conf',
            'user:paperless',
            'postgres_db:paperless',
        },
    },
    'paperless_compile_frontend': {
        'command':
            'cd /opt/paperless/src/src-ui && '
            'npm install && '
            'node_modules/.bin/ng build --prod',
        'triggered': True,
        'needs': {
            'file:/opt/paperless/src/paperless.conf',
            'pkg_apt:nodejs',
        },
    },
    'paperless_collectstatic': {
        'command':
            'cd /opt/paperless/src/src && '
            'sudo -Hu paperless /opt/paperless/venv/bin/python manage.py collectstatic',
        'triggered': True,
        'needs': {
            'directory:/opt/paperless/static',
            'file:/opt/paperless/src/paperless.conf',
            'action:paperless_create_virtualenv',
        },
    },
}

svc_systemd = {
    'paperless-consumer': {
        'needs': {
            'action:paperless_migrate_database',
            'file:/etc/systemd/system/paperless-consumer.service',
            'git_deploy:/opt/paperless/src',
        },
    },
    'paperless-scheduler': {
        'needs': {
            'action:paperless_migrate_database',
            'file:/etc/systemd/system/paperless-scheduler.service',
            'git_deploy:/opt/paperless/src',
        },
    },
    'paperless-webserver': {
        'needs': {
            'action:paperless_compile_frontend',
            'action:paperless_migrate_database',
            'file:/etc/systemd/system/paperless-webserver.service',
            'git_deploy:/opt/paperless/src',
        },
    },
}
