#import re

defaults = {
    'apt': {
        'packages': {
            'linux-headers-amd64': {
                'needed_by': {
                    'pkg_apt:zfs-dkms',
                },
            },
            'zfs-dkms': {
                'needed_by': {
                    'pkg_apt:zfs-zed',
                    'pkg_apt:zfsutils-linux',
                },
            },
            'zfs-zed': {
                'needed_by': {
                    'svc_systemd:zfs-zed',
                    'zfs_dataset:',
                    'zfs_pool:',
                },
            },
            'zfsutils-linux': {
                'needed_by': {
                    'pkg_apt:zfs-zed',
                    'zfs_dataset:',
                    'zfs_pool:',
                },
            },
            'parted': {
                'needed_by': {
                    'zfs_pool:',
                },
            },
        },
    },
    'icinga2_api': {
        'zfs': {
            'services': {
                'ZFS AUTO SNAPSHOT': {
                    'command_on_monitored_host': 'sudo /usr/local/share/icinga/plugins/check_zfs_auto_snapshot',
                },
                'ZFS MOUNTED VOLUMES': {
                    'command_on_monitored_host': 'sudo /usr/local/share/icinga/plugins/check_zfs_volumes',
                    'vars.notification.mail': True,
                },
                'ZFS OLD SNAPSHOTS': {
                    'command_on_monitored_host': 'sudo /usr/local/share/icinga/plugins/check_zfs_old_snapshots',
                }
            },
        },
    },
    'pacman': {
        'additional_config': {
            '[archzfs]\nServer = http://archzfs.com/archzfs/x86_64',
        },
        'packages': {
            'zfs-linux-lts': {
                'needed_by': {
                    'zfs_dataset:',
                    'zfs_pool:',
                },
            },
            'zfs-utils': {
                'needed_by': {
                    'svc_systemd:zfs-zed',
                },
            },
        },
    },
    'systemd-timers': {
        'timers': {
            'zfs-auto-snapshot-daily': {
                'when': 'daily',
                'command': '/usr/local/sbin/zfs-auto-snapshot daily',
            },
            'zfs-auto-snapshot-hourly': {
                'when': 'hourly',
                'command': '/usr/local/sbin/zfs-auto-snapshot hourly',
            },
            'zfs-auto-snapshot-monthly': {
                'when': 'monthly',
                'command': '/usr/local/sbin/zfs-auto-snapshot monthly',
            },
            'zfs-auto-snapshot-weekly': {
                'when': 'weekly',
                'command': '/usr/local/sbin/zfs-auto-snapshot weekly',
            },
        },
    },
    'zfs': {
        'datasets': {},
        'pools': {},
        'snapshots': {
            'retain_defaults': {
                'hourly': 24,
                'daily': 7,
                'weekly': 2,
                'monthly': 1,
            },
        },
    },
}

if node.os == 'debian' and node.os_version[0] <= 10:
    defaults['apt']['repos'] = {
        'backports': {
            'install_gpg_key': False, # default debian signing key
            'items': {
                'deb http://deb.debian.org/debian {os_release}-backports main',
            },
        },
    }

if node.has_bundle('telegraf'):
    defaults['telegraf'] = {
        'input_plugins': {
            'builtin': {
                'zfs': [{
                    'poolMetrics': True,
                }],
            },
            'exec': {
                'zfs-dataset': {
                    'commands': ['sudo /usr/local/sbin/telegraf-per-dataset'],
                    'data_format': 'influx',
                    'timeout': '5s',
                },
            },
        },
        'sudo_commands': {
            '/usr/local/sbin/telegraf-per-dataset',
        },
    }


@metadata_reactor.provides(
    'cron/jobs/zfs-scrub',
)
def zfs_scrub_cronjob(metadata):
    when = metadata.get('zfs/scrub/cron', '{} 0 * * sun'.format((node.magic_number % 60)))
    return {
        'cron': {
            'jobs': {
                'zfs-scrub': '{} root /usr/lib/zfs-linux/scrub'.format(when),
            },
        },
    }


@metadata_reactor.provides(
    'icinga2_api/zfs/services',
)
def monitoring(metadata):
    if not node.has_bundle('sshmon'):
        raise DoNotRunAgain

    services = {}

    for poolname, _ in metadata.get('zfs/pools').items():
        services['ZFS ZPOOL ONLINE {}'.format(poolname)] = {
            'command_on_monitored_host': 'sudo /usr/local/share/icinga/plugins/check_zpool_online {}'.format(poolname),
            'vars.notification.mail': True,
        }

        services['ZFS ZPOOL SPACE ' + poolname] = {
            'command_on_monitored_host': 'sudo /usr/local/share/icinga/plugins/check_zpool_space {} 90'.format(poolname),
            'vars.notification.mail': True,
        }

    return {
        'icinga2_api': {
            'zfs': {
                'services': services,
            },
        },
    }


@metadata_reactor.provides(
    'zfs/filesystems_with_backup_snapshots',
)
def backups_with_snapshot(metadata):
    backups = metadata.get('backups/paths', set())
    datasets = metadata.get('zfs/datasets', {})

    backups_in_zfs_datasets = {}

    for path in backups:
        for dname, dconfig in datasets.items():
            if 'mountpoint' in dconfig:
                if path[:len(dconfig['mountpoint'])] == dconfig['mountpoint']:
                    backups_in_zfs_datasets.setdefault(dname, set()).add(path)

    return {
        'zfs': {
            'filesystems_with_backup_snapshots': backups_in_zfs_datasets,
        },
    }
