Blob Blame History Raw
#!/usr/bin/python3
"""
Configure the resolver

This stage configures the resolver(3) via the resolv.conf(5)
configure file. See the man page for more details. Currently
only a subset of options are supported. If no options are
specified but the stage is included it will create an empty
`/etc/resolv.conf` file.
"""


import contextlib
import os
import sys

import osbuild.api


SCHEMA = """
"additionalProperties": false,
"properties": {
  "nameserver": {
    "description": "Array of IP addresses the resolver should query",
    "type": "array",
    "items": { "type": "string" }
  },
  "search": {
    "description": "Array of ",
    "type": "array",
    "items": { "type": "string" }
  }
}
"""


def main(tree, options):
    nameserver = options.get("nameserver", [])
    search = options.get("search", [])

    filepath = "etc/resolv.conf"
    fullpath = os.path.join(tree, filepath)

    with contextlib.suppress(FileNotFoundError):
        os.remove(fullpath)
        print(f"Replacing existing file: /{filepath}")

    data = ["# This file was created by osbuild"]

    if search:
        data += [
            "search " + " ".join(search)
        ]

    for ns in nameserver:
        data += ["nameserver " + str(ns)]

    os.makedirs(os.path.join(tree, "etc"), exist_ok=True)
    with open(fullpath, "w") as f:
        f.write("\n".join(data) + "\n")

    return 0


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