summaryrefslogtreecommitdiff
path: root/curtin/net/deps.py
blob: fd9e3c01627f2fee84c29ec69e208f93c6a0cdd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# This file is part of curtin. See LICENSE file for copyright and license info.

from curtin.distro import DISTROS


def network_config_required_packages(network_config, mapping=None):

    if network_config is None:
        network_config = {}

    if not isinstance(network_config, dict):
        raise ValueError('Invalid network configuration.  Must be a dict')

    if mapping is None:
        mapping = {}

    if not isinstance(mapping, dict):
        raise ValueError('Invalid network mapping.  Must be a dict')

    # allow top-level 'network' key
    if 'network' in network_config:
        network_config = network_config.get('network')

    # v1 has 'config' key and uses type: devtype elements
    if 'config' in network_config:
        dev_configs = set(device['type']
                          for device in network_config['config'])
    else:
        # v2 has no config key
        dev_configs = set()
        for cfgtype, cfg in network_config.items():
            if cfgtype == 'version':
                continue
            dev_configs.add(cfgtype)
            # the renderer type may trigger package adds
            for entry, entry_cfg in cfg.items():
                if entry_cfg.get('renderer'):
                    dev_configs.add(entry_cfg.get('renderer'))

    needed_packages = []
    for dev_type in dev_configs:
        if dev_type in mapping:
            needed_packages.extend(mapping[dev_type])

    return needed_packages


def detect_required_packages_mapping(osfamily=DISTROS.debian):
    """Return a dictionary providing a versioned configuration which maps
       network configuration elements to the packages which are required
       for functionality.
    """
    # keys ending with 's' are v2 values
    distro_mapping = {
        DISTROS.debian: {
            'bond': ['ifenslave'],
            'bonds': ['ifenslave'],
            'bridge': ['bridge-utils'],
            'bridges': ['bridge-utils'],
            'openvswitch': ['openvswitch-switch'],
            'networkd': ['systemd'],
            'NetworkManager': ['network-manager'],
            'vlan': ['vlan'],
            'vlans': ['vlan']},
        DISTROS.redhat: {
            'bond': [],
            'bonds': [],
            'bridge': [],
            'bridges': [],
            'openvswitch': ['openvswitch-switch'],
            'vlan': [],
            'vlans': []},
    }
    if osfamily not in distro_mapping:
        raise ValueError('No net package mapping for distro: %s' % osfamily)

    return {1: {'handler': network_config_required_packages,
                'mapping': distro_mapping.get(osfamily)},
            2: {'handler': network_config_required_packages,
                'mapping': distro_mapping.get(osfamily)}}


# vi: ts=4 expandtab syntax=python