Skip to content

Commit 81299de

Browse files
Add new network activators to bring up interfaces (canonical#919)
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.
1 parent 78e89b0 commit 81299de

File tree

18 files changed

+413
-126
lines changed

18 files changed

+413
-126
lines changed

cloudinit/cmd/devel/net_convert.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ def handle_args(name, args):
9696
pre_ns = ovf.get_network_config_from_conf(config, False)
9797

9898
ns = network_state.parse_net_config_data(pre_ns)
99-
if not ns:
100-
raise RuntimeError("No valid network_state object created from"
101-
" input data")
10299

103100
if args.debug:
104101
sys.stderr.write('\n'.join(

cloudinit/distros/__init__.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
import string
1717
import urllib.parse
1818
from io import StringIO
19+
from typing import Any, Mapping
1920

2021
from cloudinit import importer
2122
from cloudinit import log as logging
2223
from cloudinit import net
24+
from cloudinit.net import activators
2325
from cloudinit.net import eni
2426
from cloudinit.net import network_state
2527
from cloudinit.net import renderers
28+
from cloudinit.net.network_state import parse_net_config_data
2629
from cloudinit import persistence
2730
from cloudinit import ssh_util
2831
from cloudinit import type_utils
@@ -72,7 +75,7 @@ class Distro(persistence.CloudInitPickleMixin, metaclass=abc.ABCMeta):
7275
hostname_conf_fn = "/etc/hostname"
7376
tz_zone_dir = "/usr/share/zoneinfo"
7477
init_cmd = ['service'] # systemctl, service etc
75-
renderer_configs = {}
78+
renderer_configs = {} # type: Mapping[str, Mapping[str, Any]]
7679
_preferred_ntp_clients = None
7780
networking_cls = LinuxNetworking
7881
# This is used by self.shutdown_command(), and can be overridden in
@@ -106,23 +109,20 @@ def install_packages(self, pkglist):
106109
raise NotImplementedError()
107110

108111
def _write_network(self, settings):
109-
raise RuntimeError(
112+
"""Deprecated. Remove if/when arch and gentoo support renderers."""
113+
raise NotImplementedError(
110114
"Legacy function '_write_network' was called in distro '%s'.\n"
111115
"_write_network_config needs implementation.\n" % self.name)
112116

113-
def _write_network_config(self, settings):
114-
raise NotImplementedError()
115-
116-
def _supported_write_network_config(self, network_config):
117+
def _write_network_state(self, network_state):
117118
priority = util.get_cfg_by_path(
118119
self._cfg, ('network', 'renderers'), None)
119120

120121
name, render_cls = renderers.select(priority=priority)
121122
LOG.debug("Selected renderer '%s' from priority list: %s",
122123
name, priority)
123124
renderer = render_cls(config=self.renderer_configs.get(name))
124-
renderer.render_network_config(network_config)
125-
return []
125+
renderer.render_network_state(network_state)
126126

127127
def _find_tz_file(self, tz):
128128
tz_file = os.path.join(self.tz_zone_dir, str(tz))
@@ -174,6 +174,7 @@ def get_package_mirror_info(self, arch=None, data_source=None):
174174
mirror_info=arch_info)
175175

176176
def apply_network(self, settings, bring_up=True):
177+
"""Deprecated. Remove if/when arch and gentoo support renderers."""
177178
# this applies network where 'settings' is interfaces(5) style
178179
# it is obsolete compared to apply_network_config
179180
# Write it out
@@ -188,6 +189,7 @@ def apply_network(self, settings, bring_up=True):
188189
return False
189190

190191
def _apply_network_from_network_config(self, netconfig, bring_up=True):
192+
"""Deprecated. Remove if/when arch and gentoo support renderers."""
191193
distro = self.__class__
192194
LOG.warning("apply_network_config is not currently implemented "
193195
"for distribution '%s'. Attempting to use apply_network",
@@ -208,16 +210,18 @@ def apply_network_config(self, netconfig, bring_up=False):
208210
# apply network config netconfig
209211
# This method is preferred to apply_network which only takes
210212
# a much less complete network config format (interfaces(5)).
213+
network_state = parse_net_config_data(netconfig)
211214
try:
212-
dev_names = self._write_network_config(netconfig)
215+
self._write_network_state(network_state)
213216
except NotImplementedError:
214217
# backwards compat until all distros have apply_network_config
215218
return self._apply_network_from_network_config(
216219
netconfig, bring_up=bring_up)
217220

218221
# Now try to bring them up
219222
if bring_up:
220-
return self._bring_up_interfaces(dev_names)
223+
network_activator = activators.select_activator()
224+
network_activator.bring_up_all_interfaces(network_state)
221225
return False
222226

223227
def apply_network_config_names(self, netconfig):
@@ -393,20 +397,11 @@ def preferred_ntp_clients(self):
393397
return self._preferred_ntp_clients
394398

395399
def _bring_up_interface(self, device_name):
396-
cmd = ['ifup', device_name]
397-
LOG.debug("Attempting to run bring up interface %s using command %s",
398-
device_name, cmd)
399-
try:
400-
(_out, err) = subp.subp(cmd)
401-
if len(err):
402-
LOG.warning("Running %s resulted in stderr output: %s",
403-
cmd, err)
404-
return True
405-
except subp.ProcessExecutionError:
406-
util.logexc(LOG, "Running interface command %s failed", cmd)
407-
return False
400+
"""Deprecated. Remove if/when arch and gentoo support renderers."""
401+
raise NotImplementedError
408402

409403
def _bring_up_interfaces(self, device_names):
404+
"""Deprecated. Remove if/when arch and gentoo support renderers."""
410405
am_failed = 0
411406
for d in device_names:
412407
if not self._bring_up_interface(d):

cloudinit/distros/alpine.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,6 @@ def install_packages(self, pkglist):
7373
self.update_package_sources()
7474
self.package_command('add', pkgs=pkglist)
7575

76-
def _write_network_config(self, netconfig):
77-
return self._supported_write_network_config(netconfig)
78-
79-
def _bring_up_interfaces(self, device_names):
80-
use_all = False
81-
for d in device_names:
82-
if d == 'all':
83-
use_all = True
84-
if use_all:
85-
return distros.Distro._bring_up_interface(self, '-a')
86-
else:
87-
return distros.Distro._bring_up_interfaces(self, device_names)
88-
8976
def _write_hostname(self, your_hostname, out_fn):
9077
conf = None
9178
try:

cloudinit/distros/arch.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ def install_packages(self, pkglist):
6161
self.update_package_sources()
6262
self.package_command('', pkgs=pkglist)
6363

64-
def _write_network_config(self, netconfig):
64+
def _write_network_state(self, network_state):
6565
try:
66-
return self._supported_write_network_config(netconfig)
66+
super()._write_network_state(network_state)
6767
except RendererNotFoundError as e:
6868
# Fall back to old _write_network
6969
raise NotImplementedError from e
@@ -101,12 +101,6 @@ def _bring_up_interface(self, device_name):
101101
util.logexc(LOG, "Running interface command %s failed", cmd)
102102
return False
103103

104-
def _bring_up_interfaces(self, device_names):
105-
for d in device_names:
106-
if not self._bring_up_interface(d):
107-
return False
108-
return True
109-
110104
def _write_hostname(self, your_hostname, out_fn):
111105
conf = None
112106
try:

cloudinit/distros/bsd.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ def package_command(self, command, args=None, pkgs=None):
120120
# Allow the output of this to flow outwards (ie not be captured)
121121
subp.subp(cmd, env=self._get_pkg_cmd_environ(), capture=False)
122122

123-
def _write_network_config(self, netconfig):
124-
return self._supported_write_network_config(netconfig)
125-
126123
def set_timezone(self, tz):
127124
distros.set_etc_timezone(tz=tz, tz_file=self._find_tz_file(tz))
128125

cloudinit/distros/debian.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,9 @@ def install_packages(self, pkglist):
111111
self.update_package_sources()
112112
self.package_command('install', pkgs=pkglist)
113113

114-
def _write_network_config(self, netconfig):
114+
def _write_network_state(self, network_state):
115115
_maybe_remove_legacy_eth0()
116-
return self._supported_write_network_config(netconfig)
117-
118-
def _bring_up_interfaces(self, device_names):
119-
use_all = False
120-
for d in device_names:
121-
if d == 'all':
122-
use_all = True
123-
if use_all:
124-
return distros.Distro._bring_up_interface(self, '--all')
125-
else:
126-
return distros.Distro._bring_up_interfaces(self, device_names)
116+
return super()._write_network_state(network_state)
127117

128118
def _write_hostname(self, your_hostname, out_fn):
129119
conf = None

cloudinit/distros/opensuse.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,6 @@ def update_package_sources(self):
116116
self._runner.run("update-sources", self.package_command,
117117
['refresh'], freq=PER_INSTANCE)
118118

119-
def _bring_up_interfaces(self, device_names):
120-
if device_names and 'all' in device_names:
121-
raise RuntimeError(('Distro %s can not translate '
122-
'the device name "all"') % (self.name))
123-
return distros.Distro._bring_up_interfaces(self, device_names)
124-
125119
def _read_hostname(self, filename, default=None):
126120
if self.uses_systemd() and filename.endswith('/previous-hostname'):
127121
return util.load_file(filename).strip()
@@ -174,9 +168,6 @@ def _write_hostname(self, hostname, out_fn):
174168
conf.set_hostname(hostname)
175169
util.write_file(out_fn, str(conf), 0o644)
176170

177-
def _write_network_config(self, netconfig):
178-
return self._supported_write_network_config(netconfig)
179-
180171
@property
181172
def preferred_ntp_clients(self):
182173
"""The preferred ntp client is dependent on the version."""

cloudinit/distros/photon.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ def install_packages(self, pkglist):
7676
# self.update_package_sources()
7777
self.package_command('install', pkgs=pkglist)
7878

79-
def _write_network_config(self, netconfig):
80-
return self._supported_write_network_config(netconfig)
81-
8279
def _bring_up_interfaces(self, device_names):
8380
cmd = ['systemctl', 'restart', 'systemd-networkd', 'systemd-resolved']
8481
LOG.debug('Attempting to run bring up interfaces using command %s',

cloudinit/distros/rhel.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ def __init__(self, name, cfg, paths):
6565
def install_packages(self, pkglist):
6666
self.package_command('install', pkgs=pkglist)
6767

68-
def _write_network_config(self, netconfig):
69-
return self._supported_write_network_config(netconfig)
70-
7168
def apply_locale(self, locale, out_fn=None):
7269
if self.uses_systemd():
7370
if not out_fn:
@@ -117,12 +114,6 @@ def _read_hostname(self, filename, default=None):
117114
else:
118115
return default
119116

120-
def _bring_up_interfaces(self, device_names):
121-
if device_names and 'all' in device_names:
122-
raise RuntimeError(('Distro %s can not translate '
123-
'the device name "all"') % (self.name))
124-
return distros.Distro._bring_up_interfaces(self, device_names)
125-
126117
def set_timezone(self, tz):
127118
tz_file = self._find_tz_file(tz)
128119
if self.uses_systemd():

0 commit comments

Comments
 (0)