aif-ng/aif/network/__init__.py

73 lines
2.5 KiB
Python
Raw Permalink Normal View History

2019-12-28 02:20:50 -05:00
import logging
import os
##
2019-12-28 02:20:50 -05:00
from lxml import etree
##
2019-11-30 01:05:20 -05:00
from . import _common
from . import netctl
from . import networkd
from . import networkmanager
# No longer necessary:
# try:
# from . import _common
# except ImportError:
# pass # GI isn't supported, so we don't even use a fallback.
2019-11-10 05:39:33 -05:00
# http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html
# https://www.youtube.com/watch?v=ZUX9Fx8Rwzg
# https://www.youtube.com/watch?v=lBQgMGPxqNo
# https://github.com/facebookincubator/pystemd has some unit/service examples
2019-11-30 01:05:20 -05:00
# try:
# from . import networkd
# except ImportError:
# from . import networkd_fallback as networkd
2019-12-28 02:20:50 -05:00
_logger = logging.getLogger(__name__)
class Net(object):
def __init__(self, chroot_base, network_xml):
self.xml = network_xml
2019-12-28 02:20:50 -05:00
# We don't bother logging the entirety of network_xml here because we do it in the respective networks
self.chroot_base = chroot_base
self.hostname = self.xml.attrib['hostname'].strip()
2019-12-28 02:20:50 -05:00
_logger.info('Hostname: {0}'.format(self.hostname))
self.provider = self.xml.attrib.get('provider', 'networkd').strip()
if self.provider == 'netctl':
self.provider = netctl
elif self.provider == 'nm':
self.provider = networkmanager
elif self.provider == 'networkd':
self.provider = networkd
else:
_logger.error('Unable to determine which network provider to use based on configuration.')
raise RuntimeError('Could not determine provider')
self.connections = []
self._initConns()
def _initConns(self):
for e in self.xml.xpath('ethernet|wireless'):
conn = None
if e.tag == 'ethernet':
conn = self.provider.Ethernet(e)
elif e.tag == 'wireless':
conn = self.provider.Wireless(e)
self.connections.append(conn)
2019-12-28 02:20:50 -05:00
_logger.info('Added connection of type {0}.'.format(type(conn).__name__))
def apply(self, chroot_base):
cfg = os.path.join(chroot_base, 'etc', 'hostname')
with open(cfg, 'w') as fh:
fh.write('{0}\n'.format(self.hostname))
os.chown(cfg, 0, 0)
os.chmod(cfg, 0o0644)
_logger.info('Wrote: {0}'.format(cfg))
for iface in self.connections:
for src, dest in iface.services.items():
realdest = os.path.join(chroot_base, dest)
os.symlink(src, realdest)
iface.writeConf(chroot_base)
return(None)