#!/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 json
import os
import subprocess
import sys
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 = json.load(sys.stdin)
r = main(args["tree"], args["options"])
sys.exit(r)