Skip to content

Commit

Permalink
Add networkd activator
Browse files Browse the repository at this point in the history
Signed-off-by: Shreenidhi Shedi <sshedi@vmware.com>
  • Loading branch information
sshedi committed Aug 6, 2021
1 parent acca344 commit 41062c5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
9 changes: 0 additions & 9 deletions cloudinit/distros/photon.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,6 @@ def install_packages(self, pkglist):
# self.update_package_sources()
self.package_command('install', pkgs=pkglist)

def _bring_up_interfaces(self, device_names):
cmd = ['systemctl', 'restart', 'systemd-networkd', 'systemd-resolved']
LOG.debug('Attempting to run bring up interfaces using command %s',
cmd)
ret, _out, err = self.exec_cmd(cmd)
if ret:
LOG.error('Failed to bringup interfaces: %s', err)
return ret

def _write_hostname(self, hostname, filename):
if filename and filename.endswith('/previous-hostname'):
util.write_file(filename, hostname)
Expand Down
27 changes: 27 additions & 0 deletions cloudinit/net/activators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from cloudinit import util
from cloudinit.net.eni import available as eni_available
from cloudinit.net.netplan import available as netplan_available
from cloudinit.net.networkd import available as networkd_available
from cloudinit.net.network_state import NetworkState
from cloudinit.net.sysconfig import NM_CFG_FILE

Expand Down Expand Up @@ -213,12 +214,38 @@ def bring_down_all_interfaces(network_state: NetworkState) -> bool:
return _alter_interface(NetplanActivator.NETPLAN_CMD, 'all')


class NetworkdActivator(NetworkActivator):
@staticmethod
def available(target=None) -> bool:
"""Return true if ifupdown can be used on this system."""
return networkd_available(target=target)

@staticmethod
def bring_up_interface(device_name: str) -> bool:
""" Return True is successful, otherwise return False """
cmd = ['ip', 'link', 'set', 'up', device_name]
return _alter_interface(cmd, device_name)

@staticmethod
def bring_up_all_interfaces(network_state: NetworkState) -> bool:
""" Return True is successful, otherwise return False """
cmd = ['systemctl', 'restart', 'systemd-networkd', 'systemd-resolved']
return _alter_interface(cmd, 'all')

@staticmethod
def bring_down_interface(device_name: str) -> bool:
""" Return True is successful, otherwise return False """
cmd = ['ip', 'link', 'set', 'down', device_name]
return _alter_interface(cmd, device_name)


# This section is mostly copied and pasted from renderers.py. An abstract
# version to encompass both seems overkill at this point
DEFAULT_PRIORITY = [
IfUpDownActivator,
NetworkManagerActivator,
NetplanActivator,
NetworkdActivator,
]


Expand Down
2 changes: 1 addition & 1 deletion cloudinit/net/networkd.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def _render_content(self, ns):


def available(target=None):
expected = ['systemctl']
expected = ['ip', 'systemctl']
search = ['/usr/bin', '/bin']
for p in expected:
if not subp.which(p, search=search, target=target):
Expand Down
27 changes: 25 additions & 2 deletions tests/unittests/test_net_activators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from cloudinit.net.activators import (
IfUpDownActivator,
NetplanActivator,
NetworkManagerActivator
NetworkManagerActivator,
NetworkdActivator
)
from cloudinit.net.network_state import parse_net_config_data
from cloudinit.safeyaml import load
Expand Down Expand Up @@ -116,11 +117,17 @@ def test_none_available(self, unavailable_mocks):
(('nmcli',), {'target': None}),
]

NETWORKD_AVAILABLE_CALLS = [
(('ip',), {'search': ['/usr/bin', '/bin'], 'target': None}),
(('systemctl',), {'search': ['/usr/bin', '/bin'], 'target': None}),
]


@pytest.mark.parametrize('activator, available_calls', [
(IfUpDownActivator, IF_UP_DOWN_AVAILABLE_CALLS),
(NetplanActivator, NETPLAN_AVAILABLE_CALLS),
(NetworkManagerActivator, NETWORK_MANAGER_AVAILABLE_CALLS),
(NetworkdActivator, NETWORKD_AVAILABLE_CALLS),
])
class TestActivatorsAvailable:
def test_available(
Expand All @@ -140,11 +147,18 @@ def test_available(
((['nmcli', 'connection', 'up', 'ifname', 'eth1'], ), {}),
]

NETWORKD_BRING_UP_CALL_LIST = [
((['ip', 'link', 'set', 'up', 'eth0'], ), {}),
((['ip', 'link', 'set', 'up', 'eth1'], ), {}),
((['systemctl', 'restart', 'systemd-networkd', 'systemd-resolved'], ), {}),
]


@pytest.mark.parametrize('activator, expected_call_list', [
(IfUpDownActivator, IF_UP_DOWN_BRING_UP_CALL_LIST),
(NetplanActivator, NETPLAN_CALL_LIST),
(NetworkManagerActivator, NETWORK_MANAGER_BRING_UP_CALL_LIST),
(NetworkdActivator, NETWORKD_BRING_UP_CALL_LIST),
])
class TestActivatorsBringUp:
@patch('cloudinit.subp.subp', return_value=('', ''))
Expand All @@ -159,8 +173,11 @@ def test_bring_up_interface(
def test_bring_up_interfaces(
self, m_subp, activator, expected_call_list, available_mocks
):
index = 0
activator.bring_up_interfaces(['eth0', 'eth1'])
assert expected_call_list == m_subp.call_args_list
for call in m_subp.call_args_list:
assert call == expected_call_list[index]
index += 1

@patch('cloudinit.subp.subp', return_value=('', ''))
def test_bring_up_all_interfaces_v1(
Expand Down Expand Up @@ -191,11 +208,17 @@ def test_bring_up_all_interfaces_v2(
((['nmcli', 'connection', 'down', 'eth1'], ), {}),
]

NETWORKD_BRING_DOWN_CALL_LIST = [
((['ip', 'link', 'set', 'down', 'eth0'], ), {}),
((['ip', 'link', 'set', 'down', 'eth1'], ), {}),
]


@pytest.mark.parametrize('activator, expected_call_list', [
(IfUpDownActivator, IF_UP_DOWN_BRING_DOWN_CALL_LIST),
(NetplanActivator, NETPLAN_CALL_LIST),
(NetworkManagerActivator, NETWORK_MANAGER_BRING_DOWN_CALL_LIST),
(NetworkdActivator, NETWORKD_BRING_DOWN_CALL_LIST),
])
class TestActivatorsBringDown:
@patch('cloudinit.subp.subp', return_value=('', ''))
Expand Down

0 comments on commit 41062c5

Please sign in to comment.