-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new network configurers to bring up interfaces
Currently _bring_up_interfaces() is a no-op for any distro using renderers. We need to be able to support bringing up a single interfaces, a list of interfaces, and all interfaces. This should be independent of the renderers, as the network config is often generated independent of the mechanism used to apply it. Additionally, I included a refactor to remove "_supported_write_network_config". We had a confusing call chain of apply_network_config->_write_network_config->_supported_write_network_config. The last two have been combined.
- Loading branch information
1 parent
05b0e35
commit eb7d43d
Showing
19 changed files
with
257 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# This file is part of cloud-init. See LICENSE file for license information. | ||
from abc import ABC, abstractmethod | ||
from typing import Iterable | ||
|
||
from cloudinit.net.network_state import NetworkState | ||
|
||
|
||
class NetworkConfigurer(ABC): | ||
@staticmethod | ||
@abstractmethod | ||
def available() -> bool: | ||
raise NotImplementedError() | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def bring_up_interface(device_name: str) -> bool: | ||
raise NotImplementedError() | ||
|
||
@classmethod | ||
def bring_up_interfaces(cls, device_names: Iterable[str]) -> bool: | ||
all_succeeded = True | ||
for device in device_names: | ||
if not cls.bring_up_interface(device): | ||
all_succeeded = False | ||
return all_succeeded | ||
|
||
@classmethod | ||
def bring_up_all_interfaces(cls, network_state: NetworkState) -> bool: | ||
return cls.bring_up_interfaces( | ||
[i['name'] for i in network_state.iter_interfaces()] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# This file is part of cloud-init. See LICENSE file for license information. | ||
|
||
# This file is mostly copied and pasted from renderers.py. An abstract | ||
# version to encompass both seems overkill at this point | ||
from typing import List, Type | ||
|
||
from cloudinit.net.configurer import NetworkConfigurer | ||
from cloudinit.net.ifupdown import IfUpDownConfigurer | ||
from cloudinit.net.netplan import NetplanConfigurer | ||
from cloudinit.net.network_manager import NetworkManagerConfigurer | ||
|
||
DEFAULT_PRIORITY = [ | ||
IfUpDownConfigurer, | ||
NetworkManagerConfigurer, | ||
NetplanConfigurer, | ||
] | ||
|
||
|
||
def search_configurer( | ||
priority=None, target=None | ||
) -> List[Type[NetworkConfigurer]]: | ||
if priority is None: | ||
priority = DEFAULT_PRIORITY | ||
|
||
unknown = [i for i in priority if i not in DEFAULT_PRIORITY] | ||
if unknown: | ||
raise ValueError( | ||
"Unknown configurers provided in priority list: %s" % unknown) | ||
|
||
found = [] | ||
for configurer in priority: | ||
if configurer.available(target): | ||
found.append(configurer) | ||
return found | ||
|
||
|
||
def select_configurer(priority=None, target=None) -> Type[NetworkConfigurer]: | ||
found = search_configurer(priority, target) | ||
if not found: | ||
if priority is None: | ||
priority = DEFAULT_PRIORITY | ||
tmsg = "" | ||
if target and target != "/": | ||
tmsg = " in target=%s" % target | ||
raise ValueError( | ||
"No available network configurers found%s. Searched " | ||
"through list: %s" % (tmsg, priority)) | ||
return found[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import logging | ||
|
||
from cloudinit import subp | ||
from cloudinit import util | ||
from cloudinit.net.configurer import NetworkConfigurer | ||
from cloudinit.net.eni import available as eni_available | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class IfUpDownConfigurer(NetworkConfigurer): | ||
# Note that we're not overriding bring_up_interfaces to pass something | ||
# like ifup --all because it isn't supported everywhere. | ||
# E.g., NetworkManager has a ifupdown plugin that requires the name | ||
# of a specific connection. | ||
@staticmethod | ||
def available(target=None) -> bool: | ||
"""Return true if ifupdown can be used on this system.""" | ||
return eni_available(target=target) | ||
|
||
@staticmethod | ||
def bring_up_interface(device_name: str) -> bool: | ||
"""Bring up interface using ifup.""" | ||
cmd = ['ifup', device_name] | ||
LOG.debug("Attempting to run bring up interface %s using command %s", | ||
device_name, cmd) | ||
try: | ||
(_out, err) = subp.subp(cmd) | ||
if len(err): | ||
LOG.warning("Running %s resulted in stderr output: %s", | ||
cmd, err) | ||
return True | ||
except subp.ProcessExecutionError: | ||
util.logexc(LOG, "Running interface command %s failed", cmd) | ||
return False |
Oops, something went wrong.