from os.path import isfile, join

pkg_apt = {
    'perl': {},
    'carton': {},
}

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

directories = {
    '/opt/travelynx': {},
    '/var/cache/travelynx': {
        'owner': 'travelynx',
        'mode': '0700',
    },
}

files = {
    '/usr/local/lib/systemd/system/travelynx.service': {
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:travelynx:restart',
            'svc_systemd:travelynx-worker:restart',
        },
    },
    '/usr/local/lib/systemd/system/travelynx-worker.service': {
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:travelynx:restart',
            'svc_systemd:travelynx-worker:restart',
        },
    },
    '/opt/travelynx/travelynx.conf': {
        'content_type': 'mako',
        'context': node.metadata.get('travelynx'),
        'needs': {
            'git_deploy:/opt/travelynx',
        },
        'triggers': {
            'svc_systemd:travelynx:restart',
            'svc_systemd:travelynx-worker:restart',
        },
    },
}

if isfile(join(repo.path, 'data', 'travelynx', 'files', 'imprint', node.name)):
    files['/opt/travelynx/templates/imprint.html.ep'] = {
        'source': 'imprint/{}'.format(node.name),
        'needs': {
            'git_deploy:/opt/travelynx',
        },
        'triggers': {
            'svc_systemd:travelynx:restart',
        },
    }

git_deploy = {
    '/opt/travelynx': {
        'repo': 'https://github.com/derf/travelynx.git',
        'rev': node.metadata.get('travelynx/version'),
        'needs': {
            'directory:/opt/travelynx',
        },
        'triggers': {
            'action:travelynx_install_deps',
            'action:travelynx_database_migrate',
            'svc_systemd:travelynx:restart',
            'svc_systemd:travelynx-worker:restart',
        },
    },
}

actions = {
    'travelynx_install_deps': {
        'command': 'cd /opt/travelynx && carton install',
        'needs': {
            'pkg_apt:perl',
            'pkg_apt:carton',
        },
        'triggered': True,
    },
    'travelynx_database_migrate': {
        'command': 'export PERL5LIB=/opt/travelynx/local/lib/perl5; cd /opt/travelynx && perl index.pl database migrate',
        # Because git_deploy does not put .git onto the server, the script
        # will complain on STDERR about not finding a git repository.
        # That's why we need to redirect stderr to /dev/null.
        'unless': 'export PERL5LIB=/opt/travelynx/local/lib/perl5; cd /opt/travelynx && [ $(perl index.pl database has-current-schema 2>/dev/null) = "yes" ]',
        'needs': {
            'action:travelynx_install_deps',
            'file:/opt/travelynx/travelynx.conf',
            'postgres_db:travelynx',
            'postgres_role:travelynx',
        },
        'triggered': True,
    },
}

svc_systemd = {
    'travelynx': {
        'needs': {
            'file:/usr/local/lib/systemd/system/travelynx.service',
            'action:travelynx_database_migrate',
            'directory:/var/cache/travelynx',
            'pkg_apt:libmojolicious-perl',
        },
    },
    'travelynx-worker': {
        'needs': {
            'file:/usr/local/lib/systemd/system/travelynx-worker.service',
            'svc_systemd:travelynx',
        },
    },
}
