diff options
author | sshedi <53473811+sshedi@users.noreply.github.com> | 2021-06-29 00:12:34 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-28 13:42:34 -0500 |
commit | b5aecbe9512fa546255cc93b178b4081342fc247 (patch) | |
tree | e7300b313984dfdde9799294d8f2a02f276e19c0 /cloudinit | |
parent | 35aa9db6f8e2ba05d366776c0e8d97f52217e930 (diff) | |
download | vyos-cloud-init-b5aecbe9512fa546255cc93b178b4081342fc247.tar.gz vyos-cloud-init-b5aecbe9512fa546255cc93b178b4081342fc247.zip |
Removed distro specific network code from Photon (#929)
Minor fixes in networkd renderer & fixed corresponding tests
Removed datasource_list for Photon from cloud.cfg.tmpl & added a comment
in cloud.cfg.tmpl about not to use multiline array for datasource_list.
Signed-off-by: Shreenidhi Shedi <sshedi@vmware.com>
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/distros/photon.py | 217 | ||||
-rw-r--r-- | cloudinit/net/networkd.py | 51 |
2 files changed, 32 insertions, 236 deletions
diff --git a/cloudinit/distros/photon.py b/cloudinit/distros/photon.py index 8b78f98f..45125be7 100644 --- a/cloudinit/distros/photon.py +++ b/cloudinit/distros/photon.py @@ -10,10 +10,8 @@ from cloudinit import subp from cloudinit import distros from cloudinit import helpers from cloudinit import log as logging -from cloudinit.distros import net_util from cloudinit.settings import PER_INSTANCE from cloudinit.distros import rhel_util as rhutil -from cloudinit.net.network_state import mask_to_net_prefix from cloudinit.distros.parsers.hostname import HostnameConf LOG = logging.getLogger(__name__) @@ -79,9 +77,6 @@ class Distro(distros.Distro): self.package_command('install', pkgs=pkglist) def _write_network_config(self, netconfig): - if isinstance(netconfig, str): - self._write_network_(netconfig) - return return self._supported_write_network_config(netconfig) def _bring_up_interfaces(self, device_names): @@ -141,215 +136,3 @@ class Distro(distros.Distro): def update_package_sources(self): self._runner.run('update-sources', self.package_command, ['makecache'], freq=PER_INSTANCE) - - def _generate_resolv_conf(self): - resolv_conf_fn = self.resolve_conf_fn - resolv_templ_fn = 'systemd.resolved.conf' - - return resolv_conf_fn, resolv_templ_fn - - def _write_network_(self, settings): - entries = net_util.translate_network(settings) - LOG.debug('Translated ubuntu style network settings %s into %s', - settings, entries) - route_entries = [] - route_entries = translate_routes(settings) - dev_names = entries.keys() - nameservers = [] - searchdomains = [] - # Format for systemd - for (dev, info) in entries.items(): - if 'dns-nameservers' in info: - nameservers.extend(info['dns-nameservers']) - if 'dns-search' in info: - searchdomains.extend(info['dns-search']) - if dev == 'lo': - continue - - net_fn = self.network_conf_dir + '10-cloud-init-' + dev - net_fn += '.network' - dhcp_enabled = 'no' - if info.get('bootproto') == 'dhcp': - if (settings.find('inet dhcp') >= 0 and - settings.find('inet6 dhcp') >= 0): - dhcp_enabled = 'yes' - else: - if info.get('inet6') is True: - dhcp_enabled = 'ipv6' - else: - dhcp_enabled = 'ipv4' - - net_cfg = { - 'Name': dev, - 'DHCP': dhcp_enabled, - } - - if info.get('hwaddress'): - net_cfg['MACAddress'] = info.get('hwaddress') - if info.get('address'): - net_cfg['Address'] = '%s' % (info.get('address')) - if info.get('netmask'): - net_cfg['Address'] += '/%s' % ( - mask_to_net_prefix(info.get('netmask'))) - if info.get('gateway'): - net_cfg['Gateway'] = info.get('gateway') - if info.get('dns-nameservers'): - net_cfg['DNS'] = str( - tuple(info.get('dns-nameservers'))).replace(',', '') - if info.get('dns-search'): - net_cfg['Domains'] = str( - tuple(info.get('dns-search'))).replace(',', '') - route_entry = [] - if dev in route_entries: - route_entry = route_entries[dev] - route_index = 0 - found = True - while found: - route_name = 'routes.' + str(route_index) - if route_name in route_entries[dev]: - val = str(tuple(route_entries[dev][route_name])) - val = val.replace(',', '') - if val: - net_cfg[route_name] = val - else: - found = False - route_index += 1 - - if info.get('auto'): - self._write_interface_file(net_fn, net_cfg, route_entry) - - resolve_data = [] - new_resolve_data = [] - with open(self.resolve_conf_fn, 'r') as rf: - resolve_data = rf.readlines() - LOG.debug('Old Resolve Data\n') - LOG.debug('%s', resolve_data) - for item in resolve_data: - if ((nameservers and ('DNS=' in item)) or - (searchdomains and ('Domains=' in item))): - continue - else: - new_resolve_data.append(item) - - new_resolve_data = new_resolve_data + \ - convert_resolv_conf(nameservers, searchdomains) - LOG.debug('New resolve data\n') - LOG.debug('%s', new_resolve_data) - if nameservers or searchdomains: - util.write_file(self.resolve_conf_fn, ''.join(new_resolve_data)) - - return dev_names - - def _write_interface_file(self, net_fn, net_cfg, route_entry): - if not net_cfg['Name']: - return - content = '[Match]\n' - content += 'Name=%s\n' % (net_cfg['Name']) - if 'MACAddress' in net_cfg: - content += 'MACAddress=%s\n' % (net_cfg['MACAddress']) - content += '[Network]\n' - - if 'DHCP' in net_cfg and net_cfg['DHCP'] in {'yes', 'ipv4', 'ipv6'}: - content += 'DHCP=%s\n' % (net_cfg['DHCP']) - else: - if 'Address' in net_cfg: - content += 'Address=%s\n' % (net_cfg['Address']) - if 'Gateway' in net_cfg: - content += 'Gateway=%s\n' % (net_cfg['Gateway']) - if 'DHCP' in net_cfg and net_cfg['DHCP'] == 'no': - content += 'DHCP=%s\n' % (net_cfg['DHCP']) - - route_index = 0 - found = True - if route_entry: - while found: - route_name = 'routes.' + str(route_index) - if route_name in route_entry: - content += '[Route]\n' - if len(route_entry[route_name]) != 2: - continue - content += 'Gateway=%s\n' % ( - route_entry[route_name][0]) - content += 'Destination=%s\n' % ( - route_entry[route_name][1]) - else: - found = False - route_index += 1 - - util.write_file(net_fn, content) - - -def convert_resolv_conf(nameservers, searchdomains): - ''' Returns a string formatted for resolv.conf ''' - result = [] - if nameservers: - nslist = 'DNS=' - for ns in nameservers: - nslist = nslist + '%s ' % ns - nslist = nslist + '\n' - result.append(str(nslist)) - if searchdomains: - sdlist = 'Domains=' - for sd in searchdomains: - sdlist = sdlist + '%s ' % sd - sdlist = sdlist + '\n' - result.append(str(sdlist)) - return result - - -def translate_routes(settings): - entries = [] - for line in settings.splitlines(): - line = line.strip() - if not line or line.startswith('#'): - continue - split_up = line.split(None, 1) - if len(split_up) <= 1: - continue - entries.append(split_up) - consume = {} - ifaces = [] - for (cmd, args) in entries: - if cmd == 'iface': - if consume: - ifaces.append(consume) - consume = {} - consume[cmd] = args - else: - consume[cmd] = args - - absorb = False - for (cmd, args) in consume.items(): - if cmd == 'iface': - absorb = True - if absorb: - ifaces.append(consume) - out_ifaces = {} - for info in ifaces: - if 'iface' not in info: - continue - iface_details = info['iface'].split(None) - dev_name = None - if len(iface_details) >= 1: - dev = iface_details[0].strip().lower() - if dev: - dev_name = dev - if not dev_name: - continue - route_info = {} - route_index = 0 - found = True - while found: - route_name = 'routes.' + str(route_index) - if route_name in info: - val = info[route_name].split() - if val: - route_info[route_name] = val - else: - found = False - route_index += 1 - if dev_name in out_ifaces: - out_ifaces[dev_name].update(route_info) - else: - out_ifaces[dev_name] = route_info - return out_ifaces diff --git a/cloudinit/net/networkd.py b/cloudinit/net/networkd.py index 71f87995..2dffce59 100644 --- a/cloudinit/net/networkd.py +++ b/cloudinit/net/networkd.py @@ -35,6 +35,8 @@ class CfgParser: for k in self.conf_dict.keys(): if k == sec: self.conf_dict[k].append(key+'='+str(val)) + # remove duplicates from list + self.conf_dict[k] = list(dict.fromkeys(self.conf_dict[k])) self.conf_dict[k].sort() def get_final_conf(self): @@ -103,19 +105,27 @@ class Renderer(renderer.Renderer): def parse_routes(self, conf, cfg): sec = 'Route' + route_cfg_map = { + 'gateway': 'Gateway', + 'network': 'Destination', + 'metric': 'Metric', + } + + # prefix is derived using netmask by network_state + prefix = '' + if 'prefix' in conf: + prefix = '/' + str(conf['prefix']) + for k, v in conf.items(): - if k == 'gateway': - cfg.update_section(sec, 'Gateway', v) - elif k == 'network': - tmp = v - if 'prefix' in conf: - tmp += '/' + str(conf['prefix']) - cfg.update_section(sec, 'Destination', tmp) - elif k == 'metric': - cfg.update_section(sec, 'Metric', v) + if k not in route_cfg_map: + continue + if k == 'network': + v += prefix + cfg.update_section(sec, route_cfg_map[k], v) def parse_subnets(self, iface, cfg): dhcp = 'no' + sec = 'Network' for e in iface.get('subnets', []): t = e['type'] if t == 'dhcp4' or t == 'dhcp': @@ -131,21 +141,24 @@ class Renderer(renderer.Renderer): if 'routes' in e and e['routes']: for i in e['routes']: self.parse_routes(i, cfg) - elif 'address' in e: + if 'address' in e: + subnet_cfg_map = { + 'address': 'Address', + 'gateway': 'Gateway', + 'dns_nameservers': 'DNS', + 'dns_search': 'Domains', + } for k, v in e.items(): if k == 'address': - tmp = v if 'prefix' in e: - tmp += '/' + str(e['prefix']) - cfg.update_section('Address', 'Address', tmp) + v += '/' + str(e['prefix']) + cfg.update_section('Address', subnet_cfg_map[k], v) elif k == 'gateway': - cfg.update_section('Route', 'Gateway', v) - elif k == 'dns_nameservers': - cfg.update_section('Network', 'DNS', ' '.join(v)) - elif k == 'dns_search': - cfg.update_section('Network', 'Domains', ' '.join(v)) + cfg.update_section('Route', subnet_cfg_map[k], v) + elif k == 'dns_nameservers' or k == 'dns_search': + cfg.update_section(sec, subnet_cfg_map[k], ' '.join(v)) - cfg.update_section('Network', 'DHCP', dhcp) + cfg.update_section(sec, 'DHCP', dhcp) # This is to accommodate extra keys present in VMware config def dhcp_domain(self, d, cfg): |