defaults = {
    'apt': {
        'packages': {
            'postfix-pgsql': {},
        },
    },
    'backups': {
        'paths': {
            '/var/mail/vmail',
        },
    },
    'postfixadmin': {
        'admin_email': 'hostmaster@kunbox.net',
    },
    'postgresql': {
        'databases': {
            'postfixadmin': {
                'owner': 'postfixadmin',
            },
        },
        'roles': {
            'postfixadmin': {
                'password': repo.vault.password_for(node.name + ' postgresql postfixadmin'),
            },
        },
    },
    'zfs': {
        'datasets': {
            'tank/mail': {
                'mountpoint': '/var/mail/vmail',
                'needed_by': {
                    'directory:/var/mail/vmail',
                },
            },
            'tank/postfixadmin': {
                'mountpoint': '/opt/postfixadmin',
                'needed_by': {
                    'directory:/opt/postfixadmin',
                },
            },
        },
    },
}


@metadata_reactor.provides(
    'icinga2_api/postfixadmin/services',
)
def icinga_check_for_new_release(metadata):
    return {
        'icinga2_api': {
            'postfixadmin': {
                'services': {
                    'POSTFIXADMIN UPDATE': {
                        'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_github_for_new_release postfixadmin/postfixadmin postfixadmin-{}'.format(metadata.get('postfixadmin/version')),
                        'vars.notification.mail': True,
                        'check_interval': '60m',
                    },
                },
            },
        },
    }


@metadata_reactor.provides(
    'nginx/vhosts/postfixadmin',
)
def nginx(metadata):
    if not node.has_bundle('nginx'):
        raise DoNotRunAgain

    return {
        'nginx': {
            'vhosts': {
                'postfixadmin': {
                    'domain': metadata.get('postfixadmin/domain'),
                    'webroot': '/opt/postfixadmin/public/',
                    'php': True,
                },
            },
        },
    }
