from bundlewrap.exceptions import BundleError

CONFLICTING_BUNDLES = {
    'apt',
    'nginx',
    'telegraf',
    'users',
}

if any(node.has_bundle(i) for i in CONFLICTING_BUNDLES):
    raise BundleError(f'{node.name}: bundle:c3voc-addons conflicts with bundles: {", ".join(sorted(CONFLICTING_BUNDLES))}')

pkg_apt = {
    'apt-transport-https': {},

    'build-essential': {},
    'curl': {},
    'git': {},
    'grep': {},
    'gzip': {},
    'htop': {},
    'jq': {},
    'less': {},
    'mtr': {},
    'ncdu': {},
    'netcat': {},
    'python3': {},
    'python3-dev': {},
    'python3-setuptools': {
        'needed_by': {
            'pkg_pip:',
        },
    },
    'python3-pip': {
        'needed_by': {
            'pkg_pip:',
        },
    },
    'python3-virtualenv': {},
    'rsync': {},
    'tar': {},
    'tmux': {},
    'tree': {},
    'wget': {},
}

if node.metadata.get('apt/packages', {}):
    for package, options in node.metadata['apt']['packages'].items():
        pkg_apt[package] = options

actions = {
    'systemd-reload': {
        'command': 'systemctl daemon-reload',
        'cascade_skip': False,
        'triggered': True,
        'needed_by': {
            'svc_systemd:',
        },
    },
}

directories = {
    '/etc/nginx/sites-enabled': {
        'purge': True,
        'triggers': {
            'svc_systemd:nginx:restart',
        },
    },
}

files = {
    '/etc/kernel/postinst.d/unattended-upgrades': {
        'source': 'kernel-postinst.d',
    },
    '/etc/upgrade-and-reboot.conf': {
        'content_type': 'mako',
    },
    '/usr/local/share/icinga/plugins/check_unattended_upgrades': {
        'mode': '0755',
    },
    '/usr/local/sbin/upgrade-and-reboot': {
        'mode': '0700',
    },
    '/usr/local/sbin/do-unattended-upgrades': {
        'content_type': 'mako',
        'mode': '0700',
        'context': {
            'additional_update_commands': node.metadata.get('apt/additional_update_commands', set()),
            'clean_old_kernels': node.metadata.get('apt/clean_old_kernels', True),
            'restart_triggers': node.metadata.get('apt/restart_triggers', {}),
        }
    },
}

for crontab, content in node.metadata.get('cron/jobs', {}).items():
    files['/etc/cron.d/{}'.format(crontab)] = {
        'source': 'cron_template',
        'content_type': 'mako',
        'context': {
            'cron': content,
        }
    }

for vhost, config in node.metadata.get('nginx/vhosts', {}).items():
    if not 'domain' in config:
        config['domain'] = vhost

    files['/etc/nginx/sites-available/{}'.format(vhost)] = {
        'source': 'site_template',
        'content_type': 'mako',
        'context': {
            'vhost': vhost,
            **config,
        },
        'triggers': {
            'svc_systemd:nginx:restart',
        },
    }
    symlinks['/etc/nginx/sites-enabled/{}'.format(vhost)] = {
        'target': '/etc/nginx/sites-available/{}'.format(vhost),
        'triggers': {
            'svc_systemd:nginx:restart',
        },
    }

    if not 'webroot' in config:
        directories['/var/www/{}'.format(vhost)] = config.get('webroot_config', {})

svc_systemd = {
    'nginx': {},
    'apt-daily.timer': {
        'running': False,
        'enabled': False,
    },
    'apt-daily-upgrade.timer': {
        'running': False,
        'enabled': False,
    },
}