[services-systemd] Refactor to repeat less code

- The three steps of modifying services in the target
   system do basically the same thing, so factor out
   the loops and logging into a systemctl() function.
 - Log to warning() instead of just debugging, on failure.
This commit is contained in:
Adriaan de Groot 2018-06-27 05:07:18 -04:00
parent 0e314447ec
commit 0520fc3b7e

View File

@ -23,6 +23,36 @@
import libcalamares import libcalamares
def systemctl(targets, command, suffix):
"""
For each entry in @p targets, run "systemctl <command> <thing>",
where <thing> is the entry's name plus the given @p suffix.
A dot is added between name and suffix.
Returns a failure message, or None if this was successful.
Services that are not mandatory have their failures suppressed
silently.
"""
for svc in targets:
ec = libcalamares.utils.target_env_call(
['systemctl', command, "{}.{}".format(svc['name'], suffix)]
)
if ec != 0:
if svc['mandatory']:
return ("Cannot {} systemd {} {}".format(command, suffix, svc['name']),
"systemctl {} call in chroot returned error code {}".format(command, ec)
)
else:
libcalamares.utils.warning(
"Cannot {} systemd {} {}".format(command, suffix, svc['name'])
)
libcalamares.utils.warning(
"systemctl {} call in chroot returned error code {}".format(command, ec)
)
return None
def run(): def run():
""" """
Setup systemd services Setup systemd services
@ -36,66 +66,17 @@ def run():
# that support that, see: # that support that, see:
# http://0pointer.de/blog/projects/changing-roots.html # http://0pointer.de/blog/projects/changing-roots.html
# enable services r = systemctl(services, "enable", "service")
for svc in services: if r is not None:
ec = libcalamares.utils.target_env_call( return r
['systemctl', 'enable', '{}.service'.format(svc['name'])]
)
if ec != 0: r = systemctl(targets, "enable", "target")
if svc['mandatory']: if r is not None:
return ("Cannot enable systemd service {}".format(svc['name']), return r
"systemctl enable call in chroot returned error code "
"{}".format(ec)
)
else:
libcalamares.utils.debug(
"Cannot enable systemd service {}".format(svc['name'])
)
libcalamares.utils.debug(
"systemctl enable call in chroot returned error code "
"{}".format(ec)
)
# enable targets r = systemctl(disable, "disable", "service")
for tgt in targets: if r is not None:
ec = libcalamares.utils.target_env_call( return r
['systemctl', 'enable', '{}.target'.format(tgt['name'])]
)
if ec != 0:
if tgt['mandatory']:
return ("Cannot enable systemd target {}".format(tgt['name']),
"systemctl enable call in chroot returned error code"
"{}".format(ec)
)
else:
libcalamares.utils.debug(
"Cannot enable systemd target {}".format(tgt['name'])
)
libcalamares.utils.debug(
"systemctl enable call in chroot returned error code "
"{}".format(ec)
)
for dbl in disable:
ec = libcalamares.utils.target_env_call(
['systemctl', 'disable', '{}.service'.format(dbl['name'])]
)
if ec != 0:
if dbl['mandatory']:
return ("Cannot disable systemd service"
"{}".format(dbl['name']),
"systemctl disable call in chroot returned error code"
"{}".format(ec))
else:
libcalamares.utils.debug(
"Cannot disable systemd service {}".format(dbl['name'])
)
libcalamares.utils.debug(
"systemctl disable call in chroot returned error code "
"{}".format(ec)
)
# This could have just been return r
return None return None