actions = {
    'infobeamer-cms_create_virtualenv': {
        'command': '/usr/bin/python3 -m virtualenv -p python3 /opt/infobeamer-cms/venv/',
        'unless': 'test -d /opt/infobeamer-cms/venv/',
        'needs': {
            # actually /opt/infobeamer-cms, but we don't create that
            'directory:/opt/infobeamer-cms/src',
        },
    },
    'infobeamer-cms_install_requirements': {
        'command': ' && '.join([
            'cd /opt/infobeamer-cms/src',
            '/opt/infobeamer-cms/venv/bin/pip install --upgrade pip gunicorn -r requirements.txt',
            'rsync /opt/infobeamer-cms/src/static/* /opt/infobeamer-cms/static/',
        ]),
        'needs': {
            'action:infobeamer-cms_create_virtualenv',
        },
        'triggered': True,
    },
}

git_deploy = {
    '/opt/infobeamer-cms/src': {
        'rev': 'master',
        'repo': 'https://github.com/sophieschi/36c3-cms.git',
        'needs': {
            'directory:/opt/infobeamer-cms/src',
        },
        'triggers': {
            'svc_systemd:infobeamer-cms:restart',
            'action:infobeamer-cms_install_requirements',
        },
    },
}

directories = {
    '/opt/infobeamer-cms/src': {},
    '/opt/infobeamer-cms/static': {
        'owner': 'infobeamer-cms',
    },
}

config = node.metadata.get('infobeamer-cms/config', {})
config.setdefault('EXTRA_ASSETS', [])
config['ROOMS'] = []

interrupts = []
for name, data in sorted(node.metadata.get('infobeamer-cms/interrupts', {}).items()):
    interrupts.append({
        'name': name,
        'data': data,
    })

if interrupts:
    interrupts_dict = {
        'interrupts': interrupts,
    }
else:
    interrupts_dict = {}

for room, device_id in sorted(node.metadata.get('infobeamer-cms/rooms', {}).items()):
    config['ROOMS'].append({
        'name': room,
        'device_id': device_id,
        **interrupts_dict,
    })

files = {
    '/opt/infobeamer-cms/settings.toml': {
        'content': repo.libs.faults.dict_as_toml(config),
        'triggers': {
            'svc_systemd:infobeamer-cms:restart',
        },
    },
    '/etc/systemd/system/infobeamer-cms.service': {
        'content_type': 'mako',
        'context': node.metadata.get('infobeamer-cms'),
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:infobeamer-cms:restart',
        },
    },
    '/etc/systemd/system/infobeamer-cms-runperiodic.timer': {
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:infobeamer-cms-runperiodic.timer:restart',
        },
    },
    '/etc/systemd/system/infobeamer-cms-runperiodic.service': {
        'content_type': 'mako',
        'context': node.metadata.get('infobeamer-cms'),
        'triggers': {
            'action:systemd-reload',
        },
    },
}

pkg_pip = {
    'github-flask': {
        'needed_by': {
            'svc_systemd:infobeamer-cms',
        },
    },
}

svc_systemd = {
    'infobeamer-cms': {
        'needs': {
            'action:infobeamer-cms_install_requirements',
            'directory:/opt/infobeamer-cms/static',
            'file:/etc/systemd/system/infobeamer-cms.service',
            'file:/opt/infobeamer-cms/settings.toml',
            'git_deploy:/opt/infobeamer-cms/src',
        },
    },
    'infobeamer-cms-runperiodic.timer': {
        'needs': {
            'action:infobeamer-cms_install_requirements',
            'directory:/opt/infobeamer-cms/static',
            'file:/etc/systemd/system/infobeamer-cms-runperiodic.service',
            'file:/etc/systemd/system/infobeamer-cms-runperiodic.timer',
            'file:/opt/infobeamer-cms/settings.toml',
            'git_deploy:/opt/infobeamer-cms/src',
        },
    },
}

users = {
    'infobeamer-cms': {
        'home': '/opt/infobeamer-cms',
    },
}
