Blob Blame History Raw
#!/usr/bin/python3
"""
Set system hostname

Sets system hostname.

Deletes /etc/hostname if present, then runs `systemd-firstboot` from the
buildhost with `--hostname={hostname}`, which checks the validity of the
hostname and writes it to /etc/hostname.
"""


import os
import subprocess
import sys

import osbuild.api


SCHEMA = """
"additionalProperties": false,
"required": ["hostname"],
"properties": {
  "hostname": {
    "type": "string",
    "description": "hostname for the target system"
  }
}
"""


def main(tree, options):
    hostname = options["hostname"]
    try:
        os.remove(f"{tree}/etc/hostname")
        print("/etc/hostname already exists. Replacing.")
    except FileNotFoundError:
        pass

    subprocess.run(["systemd-firstboot", f"--root={tree}", f"--hostname={hostname}"], check=True)

    return 0


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