Blob Blame History Raw
#!/usr/bin/python3
"""
Enable or disable systemd services

Enable or disable systemd units (service, socket, path, etc.)

This stage runs `systemctl enable` for all `enabled_services` items, which may
create symlinks under `/etc/systemd/system`.
After enabling units, it runs `systemctl disable` for all `disabled_services`
items, which will delete _all_ symlinks to the named services.

Uses `systemctl` from the buildhost.
"""


import subprocess
import sys

import osbuild.api


SCHEMA = """
"additionalProperties": false,
"properties": {
  "enabled_services": {
    "type": "array",
    "items": { "type": "string" },
    "description": "Array of systemd unit names to be enabled"
  },
  "disabled_services": {
    "type": "array",
    "items": { "type": "string" },
    "description": "Array of systemd unit names to be enabled"
  },
  "masked_services": {
    "type": "array",
    "items": { "type": "string" },
    "description": "Array of systemd unit names to be masked"
  },
  "default_target": {
    "type": "string",
    "description": "The default target to boot into"
  }
}
"""


def main(tree, options):
    enabled_services = options.get("enabled_services", [])
    disabled_services = options.get("disabled_services", [])
    masked_services = options.get("masked_services", [])
    default_target = options.get("default_target")

    for service in enabled_services:
        subprocess.run(["systemctl", "--root", tree, "enable", service], check=True)

    for service in disabled_services:
        subprocess.run(["systemctl", "--root", tree, "disable", service], check=True)

    for service in masked_services:
        subprocess.run(["systemctl", "--root", tree, "mask", service], check=True)

    if default_target:
        subprocess.run(["systemctl", "--root", tree, "set-default", default_target], check=True)

    return 0


if __name__ == '__main__':
    args = osbuild.api.arguments()
    r = main(args["tree"], args["options"])
    sys.exit(r)