repo.libs.tools.require_bundle(node, 'nodejs')


directories = {
    node.metadata['matrix-dimension']['install_dir']: {
        'owner': 'matrix-dimension',
        'group': 'matrix-dimension',
    },
}

git_deploy = {
    node.metadata['matrix-dimension']['install_dir']: {
        'rev': node.metadata.get('matrix-dimension/version', 'master'), # doesn't have releases yet
        'repo': 'https://github.com/turt2live/matrix-dimension.git',
        'triggers': {
            'action:matrix_dimension_build',
        },
        'needs': {
            'directory:{}'.format(node.metadata.get('matrix-dimension/install_dir')),
            'directory:{}'.format(node.metadata.get('matrix-dimension/data_dir')),
        },
    },
}

files = {
    '{}/config/production.yaml'.format(node.metadata.get('matrix-dimension/install_dir')): {
        'owner': 'matrix-dimension',
        'group': 'matrix-dimension',
        'content_type': 'mako',
        'context': {
            'config': node.metadata.get('matrix-dimension', {}),
        },
        'needs': {
            'git_deploy:{}'.format(node.metadata.get('matrix-dimension/install_dir')),
        },
        'triggers': {
            'svc_systemd:matrix-dimension:restart',
        },
    },
    '/etc/systemd/system/matrix-dimension.service': {
        'content_type': 'mako',
        'context': {
            'config': node.metadata.get('matrix-dimension', {}),
        },
        'triggers': {
            'action:systemd-reload',
            'svc_systemd:matrix-dimension:restart',
        },
    },
}

actions = {
    'matrix_dimension_build': {
        'command': 'cd ' + node.metadata.get('matrix-dimension/install_dir') + ' && sudo -u matrix-dimension npm install && sudo -u matrix-dimension npm run build',
        'needs': {
            'pkg_apt:nodejs',
        },
        'triggered': True,
        'triggers': {
            'svc_systemd:matrix-dimension:restart',
        },
    },
}

svc_systemd = {
    'matrix-dimension': {
        'needs': {
            'action:matrix_dimension_build',
            'file:{}/config/production.yaml'.format(node.metadata.get('matrix-dimension/install_dir')),
            'postgres_db:matrix-dimension',
            'postgres_role:matrix-dimension',
        },
    },
}
