actions = {
    'pretalx_create_virtualenv': {
        'command': '/usr/bin/python3 -m virtualenv -p python3 /opt/pretalx/venv/',
        'unless': 'test -d /opt/pretalx/venv/',
        'needs': {
            # actually /opt/pretalx, but we don't create that
            'directory:/opt/pretalx/src',
        },
    },
    'pretalx_install': {
        'command':
            'cd /opt/pretalx/src/src && '
            '/opt/pretalx/venv/bin/pip install --upgrade pip wheel gunicorn psycopg2-binary && '
            '/opt/pretalx/venv/bin/pip install --upgrade -e .[redis]',
        'needs': {
            'action:pretalx_create_virtualenv',
        },
        'triggered': True,
    },
    'pretalx_migrate': {
        'command': 'PRETALX_CONFIG_FILE=/opt/pretalx/pretalx.cfg /opt/pretalx/venv/bin/python -m pretalx migrate',
        'needs': {
            'action:pretalx_install',
            'file:/opt/pretalx/pretalx.cfg',
            'postgres_db:pretalx',
            'postgres_role:pretalx',
        },
        'triggered': True,
    },
    'pretalx_rebuild': {
        'command': 'PRETALX_CONFIG_FILE=/opt/pretalx/pretalx.cfg /opt/pretalx/venv/bin/python -m pretalx rebuild',
        'needs': {
            'action:pretalx_install',
            'action:pretalx_migrate',
            'directory:/opt/pretalx/data',
            'directory:/opt/pretalx/static',
            'file:/opt/pretalx/pretalx.cfg',
        },
        'triggered': True,
    },
    'pretalx_regenerate-css': {
        'command': 'sudo -u pretalx PRETALX_CONFIG_FILE=/opt/pretalx/pretalx.cfg /opt/pretalx/venv/bin/python -m pretalx regenerate_css',
        'needs': {
            'action:pretalx_install',
            'action:pretalx_migrate',
            'directory:/opt/pretalx/data',
            'directory:/opt/pretalx/static',
            'file:/opt/pretalx/pretalx.cfg',
        },
        'triggered': True,
    },
}

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

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

git_deploy = {
    '/opt/pretalx/src': {
        'repo': 'https://github.com/pretalx/pretalx.git',
        'rev': node.metadata['pretalx']['version'],
        'triggers': {
            'action:pretalx_install',
            'action:pretalx_migrate',
            'action:pretalx_rebuild',
            'action:pretalx_regenerate-css',
            'svc_systemd:pretalx-web:restart',
            'svc_systemd:pretalx-worker:restart',
        },
    },
}


svc_systemd = {
    'pretalx-runperiodic.timer': {
        'needs': {
            'action:pretalx_install',
            'action:pretalx_migrate',
            'file:/etc/systemd/system/pretalx-runperiodic.timer',
            'file:/etc/systemd/system/pretalx-runperiodic.service',
            'file:/opt/pretalx/pretalx.cfg',
        },
    },
    'pretalx-clearsessions.timer': {
        'needs': {
            'action:pretalx_install',
            'action:pretalx_migrate',
            'file:/etc/systemd/system/pretalx-clearsessions.timer',
            'file:/etc/systemd/system/pretalx-clearsessions.service',
            'file:/opt/pretalx/pretalx.cfg',
        },
    },
    'pretalx-web': {
        'needs': {
            'action:pretalx_install',
            'action:pretalx_migrate',
            'action:pretalx_rebuild',
            'action:pretalx_regenerate-css',
            'file:/etc/systemd/system/pretalx-web.service',
            'file:/opt/pretalx/pretalx.cfg',
        },
    },
    'pretalx-worker': {
        'needs': {
            'action:pretalx_install',
            'action:pretalx_migrate',
            'file:/etc/systemd/system/pretalx-worker.service',
            'file:/opt/pretalx/pretalx.cfg',
        },
    },
}

files = {
    '/opt/pretalx/pretalx.cfg': {
        'content_type': 'mako',
        'context': node.metadata['pretalx'],
        'triggers': {
            'svc_systemd:pretalx-web:restart',
            'svc_systemd:pretalx-worker:restart',
        },
    },
    '/opt/pretalx/pretalx-administrators-from-group': {
        'mode': '0755',
    },
    '/etc/systemd/system/pretalx-runperiodic.timer': {
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:pretalx-runperiodic.timer:restart',
        },
    },
    '/etc/systemd/system/pretalx-runperiodic.service': {
        'triggers': {
            'action:systemd-reload',
        },
    },
    '/etc/systemd/system/pretalx-clearsessions.timer': {
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:pretalx-clearsessions.timer:restart',
        },
    },
    '/etc/systemd/system/pretalx-clearsessions.service': {
        'triggers': {
            'action:systemd-reload',
        },
    },
    '/etc/systemd/system/pretalx-web.service': {
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:pretalx-web:restart',
        },
    },
    '/etc/systemd/system/pretalx-worker.service': {
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:pretalx-worker:restart',
        },
    },
}

if node.metadata.get('pretalx/administrators-from-group-id', None):
    files['/etc/cron.d/pretalx-administrators-from-group'] = {
        'source': 'cron-pretalx-administrators-from-group',
        'content_type': 'mako',
    }
else:
    files['/etc/cron.d/pretalx-administrators-from-group'] = {
        'delete': True,
    }

# run `pip install` one after another due to concurrency issues
last_action = 'action:pretalx_install'
for plugin_name, plugin_config in node.metadata.get('pretalx/plugins', {}).items():
    directories[f'/opt/pretalx/plugin_{plugin_name}'] = {}

    git_deploy[f'/opt/pretalx/plugin_{plugin_name}'] = {
        'repo': plugin_config['repo'],
        'rev': plugin_config['rev'],
        'triggers': {
            f'action:pretalx_install_plugin_{plugin_name}',
        },
    }

    actions[f'pretalx_install_plugin_{plugin_name}'] = {
        'command': f'cd /opt/pretalx/plugin_{plugin_name} && /opt/pretalx/venv/bin/pip install --upgrade -e .',
        'needs': {
            last_action,
        },
        'triggers': {
            'action:pretalx_migrate',
            'action:pretalx_rebuild',
            'action:pretalx_regenerate-css',
            'svc_systemd:pretalx-web:restart',
            'svc_systemd:pretalx-worker:restart',
        },
        'triggered': True,
    }
    last_action = f'action:pretalx_install_plugin_{plugin_name}'