コード例 #1
0
def test_del():
    import hooks
    _execCmd([EXT_OVS_VSCTL, 'show'])
    json_input = {
        'request': {
            'networks': {
                'ovs-test-net': {
                    'remove': True
                }
            },
            'bondings': {
                'bond1515': {
                    'remove': True
                }
            }
        }
    }
    print("\nexecuting hook with fake json input:")
    print(json_input)
    hooks.before_network_setup(json_input)
    print("hook finished\n")
    _execCmd([EXT_OVS_VSCTL, 'show'])
    _execCmd(['cat', '/var/run/vdsm/netconf/nets/ovs-test-net'], exit=False)
    _execCmd(['cat', '/var/run/vdsm/netconf/bonds/bond1515'], exit=False)
    _execCmd([EXT_IP, 'link', 'del', 'dummy_1'])
    _execCmd([EXT_IP, 'link', 'del', 'dummy_2'])
コード例 #2
0
def test_add():
    import hooks
    _execCmd([EXT_OVS_VSCTL, 'show'])
    json_input = {
        'request': {
            'networks': {
                'ovs-test-net': {
                    'bonding': 'bond1515',
                    'bridged': True,
                    'vlan': 122,
                    'custom': {
                        'ovs': True
                    }
                }
            },
            'bondings': {
                'bond1515': {
                    'nics': ['dummy_1', 'dummy_2'],
                    'custom': {
                        'ovs': True,
                        'ovs_bond_mode': 'active-backup'
                    }
                }
            }
        }
    }
    _execCmd([EXT_IP, 'link', 'add', 'dummy_1', 'type', 'dummy'])
    _execCmd([EXT_IP, 'link', 'add', 'dummy_2', 'type', 'dummy'])
    print("> executing hook with fake json input: " + str(json_input))
    hooks.before_network_setup(json_input)
    print("hook finished")
    _execCmd([EXT_OVS_VSCTL, 'show'])
    _execCmd(['cat', '/var/run/vdsm/netconf/nets/ovs-test-net'])
    _execCmd(['cat', '/var/run/vdsm/netconf/bonds/bond1515'])
コード例 #3
0
def test_del():
    import hooks
    _execCmd([EXT_OVS_VSCTL, 'show'])
    json_input = {'request': {'networks': {'ovs-test-net': {'remove': True}},
                              'bondings': {'bond1515': {'remove': True}}}}
    print("\nexecuting hook with fake json input:")
    print(json_input)
    hooks.before_network_setup(json_input)
    print("hook finished\n")
    _execCmd([EXT_OVS_VSCTL, 'show'])
    _execCmd(['cat', '/var/run/vdsm/netconf/nets/ovs-test-net'], exit=False)
    _execCmd(['cat', '/var/run/vdsm/netconf/bonds/bond1515'], exit=False)
    _execCmd([EXT_IP, 'link', 'del', 'dummy_1'])
    _execCmd([EXT_IP, 'link', 'del', 'dummy_2'])
コード例 #4
0
ファイル: api.py プロジェクト: nickxiao/vdsm
def _apply_hook(bondings, networks, options):
    results = hooks.before_network_setup(_buildSetupHookDict(networks, bondings, options))
    # gather any changes that could have been done by the hook scripts
    networks = results["request"]["networks"]
    bondings = results["request"]["bondings"]
    options = results["request"]["options"]
    return bondings, networks, options
コード例 #5
0
ファイル: api.py プロジェクト: rexhsu/vdsm-debian
def _apply_hook(bondings, networks, options):
    results = hooks.before_network_setup(
        _buildSetupHookDict(networks, bondings, options))
    # gather any changes that could have been done by the hook scripts
    networks = results['request']['networks']
    bondings = results['request']['bondings']
    options = results['request']['options']
    return bondings, networks, options
コード例 #6
0
def test_add():
    import hooks
    _execCmd([EXT_OVS_VSCTL, 'show'])
    json_input = {
        'request': {
            'networks': {
                'ovs-test-net': {'bonding': 'bond1515', 'bridged': True,
                                 'vlan': 122, 'custom': {'ovs': True}}},
            'bondings': {
                'bond1515': {'nics': ['dummy_1', 'dummy_2'],
                             'custom': {'ovs': True,
                                        'ovs_bond_mode': 'active-backup'}}}}}
    _execCmd([EXT_IP, 'link', 'add', 'dummy_1', 'type', 'dummy'])
    _execCmd([EXT_IP, 'link', 'add', 'dummy_2', 'type', 'dummy'])
    print("> executing hook with fake json input: " + str(json_input))
    hooks.before_network_setup(json_input)
    print("hook finished")
    _execCmd([EXT_OVS_VSCTL, 'show'])
    _execCmd(['cat', '/var/run/vdsm/netconf/nets/ovs-test-net'])
    _execCmd(['cat', '/var/run/vdsm/netconf/bonds/bond1515'])
コード例 #7
0
def setupNetworks(networks, bondings, **options):
    """Add/Edit/Remove configuration for networks and bondings.

    Params:
        networks - dict of key=network, value=attributes
            where 'attributes' is a dict with the following optional items:
                        vlan=<id>
                        bonding="<name>" | nic="<name>"
                        (bonding and nics are mutually exclusive)
                        ipaddr="<ipv4>"
                        netmask="<ipv4>"
                        gateway="<ipv4>"
                        bootproto="..."
                        ipv6addr="<ipv6>[/<prefixlen>]"
                        ipv6gateway="<ipv6>"
                        ipv6autoconf="0|1"
                        dhcpv6="0|1"
                        defaultRoute=True|False
                        (other options will be passed to the config file AS-IS)
                        -- OR --
                        remove=True (other attributes can't be specified)

        bondings - dict of key=bonding, value=attributes
            where 'attributes' is a dict with the following optional items:
                        nics=["<nic1>" , "<nic2>", ...]
                        options="<bonding-options>"
                        -- OR --
                        remove=True (other attributes can't be specified)

        options - dict of options, such as:
                        force=0|1
                        connectivityCheck=0|1
                        connectivityTimeout=<int>
                        _inRollback=True|False

    Notes:
        When you edit a network that is attached to a bonding, it's not
        necessary to re-specify the bonding (you need only to note
        the attachment in the network's attributes). Similarly, if you edit
        a bonding, it's not necessary to specify its networks.
    """
    logger = logging.getLogger("setupNetworks")

    libvirt_nets = netinfo.networks()
    _netinfo = netinfo.NetInfo(_netinfo=netinfo.get(
        netinfo._libvirtNets2vdsm(libvirt_nets)))
    networksAdded = set()

    logger.debug("Setting up network according to configuration: "
                 "networks:%r, bondings:%r, options:%r" % (networks,
                                                           bondings, options))

    force = options.get('force', False)
    if not utils.tobool(force):
        logging.debug("Validating configuration")
        _validateNetworkSetup(dict(networks), dict(bondings))

    results = hooks.before_network_setup(_buildSetupHookDict(networks,
                                                             bondings,
                                                             options))

    # gather any changes that could have been done by the hook scripts
    networks = results['request']['networks']
    bondings = results['request']['bondings']
    options = results['request']['options']

    logger.debug("Applying...")
    with ConfiguratorClass(options.get('_inRollback', False)) as configurator:
        # Remove edited networks and networks with 'remove' attribute
        for network, networkAttrs in networks.items():
            if network in _netinfo.networks:
                logger.debug("Removing network %r", network)
                delNetwork(network, configurator=configurator, force=force,
                           implicitBonding=False, _netinfo=_netinfo)
                if 'remove' in networkAttrs:
                    del networks[network]
                    del libvirt_nets[network]
                _netinfo.updateDevices()
                del _netinfo.networks[network]
            elif network in libvirt_nets:
                # If the network was not in _netinfo but is in the networks
                # returned by libvirt, it means that we are dealing with
                # a broken network.
                logger.debug('Removing broken network %r', network)
                _delBrokenNetwork(network, libvirt_nets[network],
                                  configurator=configurator)
                if 'remove' in networkAttrs:
                    del networks[network]
                    del libvirt_nets[network]
                _netinfo.updateDevices()
            elif 'remove' in networkAttrs:
                raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, "Cannot delete "
                                         "network %r: It doesn't exist in the "
                                         "system" % network)
            else:
                networksAdded.add(network)

        _handleBondings(bondings, configurator)

        # We need to use the newest host info
        _netinfo.updateDevices()
        for network, networkAttrs in networks.iteritems():
            d = dict(networkAttrs)
            if 'bonding' in d:
                d.update(_buildBondOptions(d['bonding'], bondings, _netinfo))
            else:
                d['nics'] = [d.pop('nic')] if 'nic' in d else []
            d['force'] = force

            logger.debug("Adding network %r", network)
            try:
                addNetwork(network, configurator=configurator,
                           implicitBonding=True, _netinfo=_netinfo, **d)
            except ConfigNetworkError as cne:
                if cne.errCode == ne.ERR_FAILED_IFUP:
                    logger.debug("Adding network %r failed. Running "
                                 "orphan-devices cleanup", network)
                    _emergencyNetworkCleanup(network, networkAttrs,
                                             configurator)
                raise

            _netinfo.updateDevices()  # Things like a bond mtu can change

        if utils.tobool(options.get('connectivityCheck', True)):
            logger.debug('Checking connectivity...')
            if not clientSeen(int(options.get('connectivityTimeout',
                                  CONNECTIVITY_TIMEOUT_DEFAULT))):
                logger.info('Connectivity check failed, rolling back')
                for network in networksAdded:
                    # If the new added network was created on top of
                    # existing bond, we need to keep the bond on rollback
                    # flow, else we will break the new created bond.
                    delNetwork(network, force=True,
                               implicitBonding=networks[network].
                               get('bonding') in bondings)
                raise ConfigNetworkError(ne.ERR_LOST_CONNECTION,
                                         'connectivity check failed')

    hooks.after_network_setup(_buildSetupHookDict(networks, bondings, options))
コード例 #8
0
ファイル: configNetwork.py プロジェクト: int64ago-public/vdsm
def setupNetworks(networks, bondings, **options):
    """Add/Edit/Remove configuration for networks and bondings.

    Params:
        networks - dict of key=network, value=attributes
            where 'attributes' is a dict with the following optional items:
                        vlan=<id>
                        bonding="<name>" | nic="<name>"
                        (bonding and nics are mutually exclusive)
                        ipaddr="<ipv4>"
                        netmask="<ipv4>"
                        gateway="<ipv4>"
                        bootproto="..."
                        ipv6addr="<ipv6>[/<prefixlen>]"
                        ipv6gateway="<ipv6>"
                        ipv6autoconf="0|1"
                        dhcpv6="0|1"
                        defaultRoute=True|False
                        (other options will be passed to the config file AS-IS)
                        -- OR --
                        remove=True (other attributes can't be specified)

        bondings - dict of key=bonding, value=attributes
            where 'attributes' is a dict with the following optional items:
                        nics=["<nic1>" , "<nic2>", ...]
                        options="<bonding-options>"
                        -- OR --
                        remove=True (other attributes can't be specified)

        options - dict of options, such as:
                        force=0|1
                        connectivityCheck=0|1
                        connectivityTimeout=<int>
                        _inRollback=True|False

    Notes:
        When you edit a network that is attached to a bonding, it's not
        necessary to re-specify the bonding (you need only to note
        the attachment in the network's attributes). Similarly, if you edit
        a bonding, it's not necessary to specify its networks.
    """
    logger = logging.getLogger("setupNetworks")

    libvirt_nets = netinfo.networks()
    _netinfo = netinfo.NetInfo(_netinfo=netinfo.get(
        netinfo._libvirtNets2vdsm(libvirt_nets)))
    networksAdded = set()

    logger.debug("Setting up network according to configuration: "
                 "networks:%r, bondings:%r, options:%r" % (networks,
                                                           bondings, options))

    force = options.get('force', False)
    if not utils.tobool(force):
        logging.debug("Validating configuration")
        _validateNetworkSetup(dict(networks), dict(bondings))

    results = hooks.before_network_setup(_buildSetupHookDict(networks,
                                                             bondings,
                                                             options))

    # gather any changes that could have been done by the hook scripts
    networks = results['request']['networks']
    bondings = results['request']['bondings']
    options = results['request']['options']

    logger.debug("Applying...")
    with ConfiguratorClass(options.get('_inRollback', False)) as configurator:
        # Remove edited networks and networks with 'remove' attribute
        for network, networkAttrs in networks.items():
            if network in _netinfo.networks:
                logger.debug("Removing network %r" % network)
                delNetwork(network, configurator=configurator, force=force,
                           implicitBonding=False, _netinfo=_netinfo)
                if 'remove' in networkAttrs:
                    del networks[network]
                    del libvirt_nets[network]
                _netinfo.updateDevices()
                del _netinfo.networks[network]
            elif network in libvirt_nets:
                # If the network was not in _netinfo but is in the networks
                # returned by libvirt, it means that we are dealing with
                # a broken network.
                logger.debug('Removing broken network %r' % network)
                _delBrokenNetwork(network, libvirt_nets[network],
                                  configurator=configurator)
                if 'remove' in networkAttrs:
                    del networks[network]
                    del libvirt_nets[network]
                _netinfo.updateDevices()
            elif 'remove' in networkAttrs:
                raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, "Cannot delete "
                                         "network %r: It doesn't exist in the "
                                         "system" % network)
            else:
                networksAdded.add(network)

        _handleBondings(bondings, configurator)

        # We need to use the newest host info
        _netinfo.updateDevices()
        for network, networkAttrs in networks.iteritems():
            d = dict(networkAttrs)
            if 'bonding' in d:
                d.update(_buildBondOptions(d['bonding'], bondings, _netinfo))
            else:
                d['nics'] = [d.pop('nic')] if 'nic' in d else []
            d['force'] = force

            logger.debug("Adding network %r" % network)
            addNetwork(network, configurator=configurator,
                       implicitBonding=True, _netinfo=_netinfo, **d)
            _netinfo.updateDevices()  # Things like a bond mtu can change

        if utils.tobool(options.get('connectivityCheck', True)):
            logger.debug('Checking connectivity...')
            if not clientSeen(int(options.get('connectivityTimeout',
                                  CONNECTIVITY_TIMEOUT_DEFAULT))):
                logger.info('Connectivity check failed, rolling back')
                for network in networksAdded:
                    # If the new added network was created on top of
                    # existing bond, we need to keep the bond on rollback
                    # flow, else we will break the new created bond.
                    delNetwork(network, force=True,
                               implicitBonding=networks[network].
                               get('bonding') in bondings)
                raise ConfigNetworkError(ne.ERR_LOST_CONNECTION,
                                         'connectivity check failed')

    hooks.after_network_setup(_buildSetupHookDict(networks, bondings, options))