[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.
main
Adriaan de Groot 7 years ago
parent 0e314447ec
commit 0520fc3b7e

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

Loading…
Cancel
Save