1616import string
1717import urllib .parse
1818from io import StringIO
19+ from typing import Any , Mapping
1920
2021from cloudinit import importer
2122from cloudinit import log as logging
2223from cloudinit import net
24+ from cloudinit .net import activators
2325from cloudinit .net import eni
2426from cloudinit .net import network_state
2527from cloudinit .net import renderers
28+ from cloudinit .net .network_state import parse_net_config_data
2629from cloudinit import persistence
2730from cloudinit import ssh_util
2831from 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 ):
0 commit comments