diff --git a/aif.xsd b/aif.xsd index b2e8b87..5320c07 100644 --- a/aif.xsd +++ b/aif.xsd @@ -1043,7 +1043,8 @@ - + diff --git a/aif/system/__init__.py b/aif/system/__init__.py index 2056488..e91eeb7 100644 --- a/aif/system/__init__.py +++ b/aif/system/__init__.py @@ -1,3 +1,4 @@ from . import locales from . import console from . import users +from . import services diff --git a/aif/system/services.py b/aif/system/services.py new file mode 100644 index 0000000..6cd3f64 --- /dev/null +++ b/aif/system/services.py @@ -0,0 +1,60 @@ +import os +import pathlib +import re +## +import aif.utils + + +_svc_suffixes = ('service', 'socket', 'device', 'mount', 'automount', 'swap', 'target', + 'path', 'timer', 'slice', 'scope') +_svc_re = re.compile(r'\.({0})$'.format('|'.join(_svc_suffixes))) + + +class Service(object): + def __init__(self, service_xml): + self.xml = service_xml + self.slice = None + self.unit_file = None + self.dest_file = None + self.name = service_xml.text.strip() + self.enabled = aif.utils.xmlBool(self.xml.attrib.get('status', 'true')) + p = pathlib.Path(self.name) + suffix = p.suffix.lstrip('.') + if suffix in _svc_suffixes: + self.type = suffix + self.name = _svc_re.sub('', self.name) + else: + self.type = 'service' + s = self.name.split('@', 1) + if len(s) > 1: + self.name = s[0] + self.slice = s[1] + self.unit_file = '{0}@.{1}'.format(self.name, self.type) + self.dest_file = '{0}@{1}.{2}'.format(self.name, self.slice, self.type) + else: + self.unit_file = '{0}.{1}'.format(self.name, self.type) + self.dest_file = self.unit_file + + +class ServiceDB(object): + def __init__(self, services_xml, chroot_base): + self.xml = services_xml + self.chroot_base = chroot_base + self.systemd_sys = os.path.join(self.chroot_base, 'usr', 'lib', 'systemd', 'system') + self.systemd_host = os.path.join(self.chroot_base, 'etc', 'systemd', 'system') + self.services = [] + for service_xml in self.xml.findall('service'): + svc = Service(service_xml) + self.services.append(svc) + + def apply(self): + for svc in self.services: + dest_path = os.path.join(self.systemd_host, svc.dest_file) + src_path = os.path.join(self.systemd_sys, svc.unit_file) + if svc.enabled: + if not os.path.isfile(dest_path): + os.symlink(src_path, dest_path) + else: + if os.path.exists(dest_path): + os.remove(dest_path) + return() diff --git a/examples/aif.xml b/examples/aif.xml index 68c1916..86d74c6 100644 --- a/examples/aif.xml +++ b/examples/aif.xml @@ -194,7 +194,7 @@ - sshd + sshd