summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrent s <bts@square-r00t.net>2019-12-06 22:57:32 -0500
committerbrent s <bts@square-r00t.net>2019-12-06 22:57:32 -0500
commit782ed08a3c92f7aa2784dcf066323eb1c65db4ab (patch)
tree185898188dd7bb789a7d2193ba8e3b24b2730a68
parent9ec1b29160a1cd5701b02abcadcbb51ce8c5f002 (diff)
downloadAIF-NG-782ed08a3c92f7aa2784dcf066323eb1c65db4ab.tar.xz
services done. that was easy!
-rw-r--r--aif.xsd3
-rw-r--r--aif/system/__init__.py1
-rw-r--r--aif/system/services.py60
-rw-r--r--examples/aif.xml2
4 files changed, 64 insertions, 2 deletions
diff --git a/aif.xsd b/aif.xsd
index b2e8b87..5320c07 100644
--- a/aif.xsd
+++ b/aif.xsd
@@ -1043,7 +1043,8 @@
<xs:complexType>
<xs:simpleContent>
<xs:extension base="aif:t_nonempty">
- <xs:attribute name="status" type="xs:boolean" use="required"/>
+ <xs:attribute name="status" type="xs:boolean" use="optional"
+ default="true"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
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 @@
</user>
</users>
<services>
- <service status="1">sshd</service>
+ <service status="true">sshd</service>
</services>
</system>
<pacman>