Port infra-rebuild to Python

Do this for a nicer developer experience in a safer language, which has
nice libraries available to e.g. build command line interfaces (e.g.
click).

Set minimum Python version to 3.10 to support match statements.
This commit is contained in:
June 2024-06-06 00:05:55 +02:00
parent 500438636a
commit 05c45fe5e3
Signed by: june
SSH key fingerprint: SHA256:o9EAq4Y9N9K0pBQeBTqhSDrND5E7oB+60ZNx0U1yPe0
10 changed files with 207 additions and 216 deletions

View file

@ -1,3 +0,0 @@
#!/usr/bin/bash
echo "$(tput bold)$(tput setaf 9)$@$(tput sgr0)"

View file

@ -1,3 +0,0 @@
#!/usr/bin/bash
echo "$(tput bold)$(tput setaf 12)$@$(tput sgr0)"

View file

@ -1,3 +0,0 @@
#!/usr/bin/bash
echo "$(tput bold)$(tput setaf 11)$@$(tput sgr0)"

View file

@ -1,39 +0,0 @@
#!/usr/bin/env bash
REAL_BASE_DIR=$( dirname $( realpath "$0" ) )
if [ $# -lt 2 ] || [ $# -gt 2 ]; then
$REAL_BASE_DIR/helper/msg_error.sh "\
Error: Incorrect amount of arguments given.
You need to provide exactly two arguments. The first one being the operation you want to run and the second one being the host or hosts (comma separated) you want to run the operation for."
exit 1
fi
OPERATION="$1"
HOSTS="$2"
case $OPERATION in
build|build-vm|build-vm-with-bootloader|switch|boot|test|reboot)
;;
*)
$REAL_BASE_DIR/helper/msg_error.sh "\
Error: No valid operation given.
The operation must be one of:
build, build-vm, build-vm-with-bootloader, switch, boot, test, reboot."
exit 1
;;
esac
set -e
for HOST in ${HOSTS//,/ }; do
case $OPERATION in
build|build-vm|build-vm-with-bootloader)
env OPERATION="$OPERATION" HOST="$HOST" $REAL_BASE_DIR/operations/local.sh
;;
switch|boot|test|reboot)
env OPERATION="$OPERATION" HOST="$HOST" $REAL_BASE_DIR/operations/remote.sh
;;
esac
done

View file

@ -1,45 +0,0 @@
#!/usr/bin/env bash
REAL_BASE_DIR=$( dirname $( realpath "$0" ) )
# Wrapper for nixos-rebuild operations, which act locally.
# Takes the following arguments supplied as environment variables.
# HOST: The host as defined in nixosConfigurations.
# OPERATION: The nixos-rebuild operation to execute. Can be one of:
# build, build-vm, build-vm-with-bootloader
# All operations are as defined in the nixos-rebuild man page.
if [ -z $HOST ]; then
$REAL_BASE_DIR/../helper/msg_error.sh "\
Internal Error: No host given.
A host needs to be provided via the HOST environment variable."
exit 1
fi
if [ -z $OPERATION ]; then
$REAL_BASE_DIR/../helper/msg_error.sh "\
Internal Error: No operation given.
An operation needs to be provided via the OPERATION environment variable."
exit 1
fi
case $OPERATION in
build|build-vm|build-vm-with-bootloader)
;;
*)
$REAL_BASE_DIR/../helper/msg_error.sh "\
Internal Error: No valid operation given.
The operation provided via the OPERATION environment variable needs to be one of:
build, build-vm, build-vm-with-bootloader."
exit 1
;;
esac
set -e
$REAL_BASE_DIR/../helper/msg_info.sh "\
Running nixos-rebuild $OPERATION for $HOST..."
nixos-rebuild "$OPERATION" --flake ".#$HOST"

View file

@ -1,115 +0,0 @@
#!/usr/bin/env bash
REAL_BASE_DIR=$( dirname $( realpath "$0" ) )
# Wrapper for nixos-rebuild operations, which act remotely.
# Gets the necessary configuration to do so.
# Takes the following arguments supplied as environment variables.
# HOST: The host as defined in nixosConfigurations and the
# deployment_configuration.json.
# OPERATION: The nixos-rebuild operation to execute. Can be one of:
# switch, boot, test, reboot
# All operations are as defined in the nixos-rebuild man page except
# for reboot, which runs boot, but then also reboots the host.
if [ -z $HOST ]; then
$REAL_BASE_DIR/../helper/msg_error.sh "\
Internal Error: No host given.
A host needs to be provided via the HOST environment variable."
exit 1
fi
if [ -z $OPERATION ]; then
$REAL_BASE_DIR/../helper/msg_error.sh "\
Internal Error: No operation given.
An operation needs to be provided via the OPERATION environment variable."
exit 1
fi
ACTUAL_OPERATION=""
case $OPERATION in
switch|boot|test)
ACTUAL_OPERATION="$OPERATION"
;;
reboot)
ACTUAL_OPERATION="boot"
;;
*)
$REAL_BASE_DIR/../helper/msg_error.sh "\
Internal Error: No valid operation given.
The operation provided via the OPERTION environment variable needs to be one of:
switch, boot, test, reboot."
exit 1
;;
esac
TARGET_HOSTNAME=""
TARGET_USER=""
TARGET_PORT=""
DEPLOYMENT_CONFIGURATION_EXISTS=true
if ! [ -f deployment_configuration.json ]; then
$REAL_BASE_DIR/../helper/msg_warning.sh "\
Warning: No deployment_configuration.json exists and therefore it can't be used to retrieve configuration values."
DEPLOYMENT_CONFIGURATION_EXISTS=false
fi
if $DEPLOYMENT_CONFIGURATION_EXISTS && ! cat deployment_configuration.json | jq . >/dev/null 2>&1; then
$REAL_BASE_DIR/../helper/msg_warning.sh "\
Warning: jq can't parse the deployment_configuration.json and therefore it can't be used to retrieve configuration values."
fi
if $DEPLOYMENT_CONFIGURATION_EXISTS && CONFIG_TARGET_HOSTNAME="$(cat deployment_configuration.json | jq -re .hosts.\"$HOST\".targetHostname 2>/dev/null)"; then
TARGET_HOSTNAME=$CONFIG_TARGET_HOSTNAME
elif NIX_CONFIG_FQDN="$(nix eval --raw .\#nixosConfigurations.$HOST.config.networking.fqdn 2>/dev/null)"; then
TARGET_HOSTNAME=$NIX_CONFIG_FQDN
else
$REAL_BASE_DIR/../helper/msg_error.sh "\
Error: Couldn't determine target hostname for $HOST.
You either need to set targetHostname for this host in the deployment_configuration.json or have an FQDN available in the NixOS configuration of this host, which then gets used for the target hostname."
exit 1
fi
if $DEPLOYMENT_CONFIGURATION_EXISTS && CONFIG_TARGET_USER="$(cat deployment_configuration.json | jq -re .hosts.\"$HOST\".targetUser 2>/dev/null)"; then
TARGET_USER=$CONFIG_TARGET_USER
elif $DEPLOYMENT_CONFIGURATION_EXISTS && CONFIG_DEFAULT_TARGET_USER="$(cat deployment_configuration.json | jq -re '.default.targetUser' 2>/dev/null)"; then
TARGET_USER=$CONFIG_DEFAULT_TARGET_USER
fi
if $DEPLOYMENT_CONFIGURATION_EXISTS && CONFIG_TARGET_PORT="$(cat deployment_configuration.json | jq -re .hosts.\"$HOST\".targetPort 2>/dev/null)"; then
TARGET_PORT=$CONFIG_TARGET_PORT
elif $DEPLOYMENT_CONFIGURATION_EXISTS && CONFIG_DEFAULT_TARGET_PORT="$(cat deployment_configuration.json | jq -re '.default.targetPort' 2>/dev/null)"; then
TARGET_PORT=$CONFIG_DEFAULT_TARGET_PORT
fi
TARGET_HOST="$TARGET_HOSTNAME"
if [ -n "$TARGET_USER" ]; then
TARGET_HOST="$TARGET_USER@$TARGET_HOST"
fi
SSHOPTS=""
if [ -n "$TARGET_PORT" ]; then
SSHOPTS="-o Port=$TARGET_PORT"
fi
set -e
if [ -n "$TARGET_PORT" ]; then
$REAL_BASE_DIR/../helper/msg_info.sh "\
Running nixos-rebuild $ACTUAL_OPERATION for $HOST on $TARGET_HOST:$TARGET_PORT..."
else
$REAL_BASE_DIR/../helper/msg_info.sh "\
Running nixos-rebuild $ACTUAL_OPERATION for $HOST on $TARGET_HOST..."
fi
env NIX_SSHOPTS="$SSHOPTS" nixos-rebuild "$ACTUAL_OPERATION" --flake ".#$HOST" --target-host "$TARGET_HOST" --use-substitutes --use-remote-sudo
if [ "$OPERATION" = "reboot" ]; then
$REAL_BASE_DIR/../helper/msg_info.sh "\
Rebooting $TARGET_HOSTNAME..."
ssh $SSH_OPTS "$TARGET_HOST" sudo systemctl reboot
fi

View file

@ -7,7 +7,7 @@ name = "infra-rebuild"
dynamic = ["version"]
description = "A simple NixOS deployment tool using nixos-rebuild internally, but trying to make infrastructure deployment more convenient."
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.10"
keywords = ["nix", "NixOS"]
authors = [
{ name = "June", email = "june@jsts.xyz" },
@ -16,9 +16,6 @@ classifiers = [
"License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: Implementation :: CPython",
@ -57,7 +54,7 @@ cov = [
]
[[tool.hatch.envs.all.matrix]]
python = ["3.7", "3.8", "3.9", "3.10", "3.11"]
python = ["3.10", "3.11"]
[tool.hatch.envs.lint]
detached = true
@ -83,12 +80,12 @@ all = [
]
[tool.black]
target-version = ["py37"]
target-version = ["py310"]
line-length = 120
skip-string-normalization = true
[tool.ruff]
target-version = "py37"
target-version = "py310"
line-length = 120
select = [
"A",

View file

@ -1,9 +1,52 @@
import click
from infra_rebuild import operations
from infra_rebuild.__about__ import __version__
@click.group(context_settings={"help_option_names": ["-h", "--help"]}, invoke_without_command=True)
@click.version_option(version=__version__, prog_name="infra-rebuild")
def infra_rebuild():
click.echo("Hello world!")
pass
@infra_rebuild.command()
@click.argument("hosts")
def build(hosts):
operations.run("build", hosts)
@infra_rebuild.command()
@click.argument("hosts")
def build_vm(hosts):
operations.run("build-vm", hosts)
@infra_rebuild.command()
@click.argument("hosts")
def build_vm_with_bootloader(hosts):
operations.run("build-vm-with-bootloader", hosts)
@infra_rebuild.command()
@click.argument("hosts")
def switch(hosts):
operations.run("switch", hosts)
@infra_rebuild.command()
@click.argument("hosts")
def boot(hosts):
operations.run("boot", hosts)
@infra_rebuild.command()
@click.argument("hosts")
def test(hosts):
operations.run("test", hosts)
@infra_rebuild.command()
@click.argument("hosts")
def reboot(hosts):
operations.run("reboot", hosts)

View file

@ -0,0 +1,13 @@
import click
def info(message):
click.echo(click.style(message, bold=True, fg="bright_blue"))
def warning(message):
click.echo(click.style(message, bold=True, fg="bright_yellow"), err=True)
def error(message):
click.echo(click.style(message, bold=True, fg="bright_red"), err=True)

View file

@ -0,0 +1,146 @@
import json
import os
import subprocess
import sys
from infra_rebuild import msg
def run(operation, hosts):
act_remotely = False
reboot = False
match operation:
case "build" | "build-vm" | "build-vm-with-bootloader":
actual_operation = operation
case "switch" | "boot" | "test":
act_remotely = True
actual_operation = operation
case "reboot":
act_remotely = True
actual_operation = "boot"
reboot = True
case _:
msg.error("Internal Error: The given operation isn't valid.")
sys.exit(1)
for host_string in hosts.split(","):
host = host_string.strip()
if not host:
msg.warning("Skipping empty string provided for host.")
continue
if act_remotely:
remote(actual_operation, host, reboot)
else:
local(actual_operation, host)
def local(operation, host):
msg.info(f"Running nixos-rebuild {operation} for {host}...")
subprocess.run(
["nixos-rebuild", operation, "--flake", f".#{host}"], # noqa: S607, S603 - can't know what the users system looks like, maybe do some kind of validation for the given host in the future?
check=False
) # fmt: skip
def remote(operation, host, reboot=False): # noqa: FBT002 - having reboot as a Boolean positional argument is fine I think # fmt: skip
config = None
try:
with open("deployment_configuration.json") as config_file:
try:
config = json.load(config_file)
except json.JSONDecodeError:
msg.warning(
"The deployment_configuration.json is not a valid JSON document and therefore it can't be used to retrieve configuration values." # noqa: E501
)
except FileNotFoundError:
msg.warning(
"No deployment_configuration.json exists and therefore it can't be used to retrieve configuration values."
)
except OSError:
msg.warning(
"Couldn't open deployment_configuration.json and therefore it can't be used to retrieve configuration values." # noqa: E501
)
target_hostname = None
if config:
try:
target_hostname = config["hosts"][host]["targetHostname"]
except KeyError:
pass
if not target_hostname:
nix_config_fqdn = subprocess.run(
["nix", "eval", "--raw", f".#nixosConfigurations.{host}.config.networking.fqdn"], # noqa: S607, S603 - can't know what the users system looks like, maybe do some kind of validation for host in the future?
capture_output=True,
text=True,
check=False,
) # fmt: skip
if nix_config_fqdn.returncode == 0 and nix_config_fqdn.stdout:
target_hostname = nix_config_fqdn.stdout
if not target_hostname:
msg.error(f"Couldn't determine target hostname for {host}.")
msg.error(
"You either need to set targetHostname for this host in the deployment_configuration.json or have an FQDN available in the NixOS configuration of this host, which then gets used for the target hostname." # noqa: E501
)
msg.error(f"Skipping {host}.")
return
target_user = None
if config:
try:
target_user = config["hosts"][host]["targetUser"]
except KeyError:
pass
if config and not target_user:
try:
target_user = config["default"]["targetUser"]
except KeyError:
pass
target_port = None
if config:
try:
target_port = config["hosts"][host]["targetPort"]
except KeyError:
pass
if config and not target_port:
try:
target_port = config["default"]["targetPort"]
except KeyError:
pass
target_host = target_hostname
if target_user:
target_host = f"{target_user}@{target_host}"
ssh_opts = None
if target_port:
ssh_opts = f"-o Port={target_port}"
nixos_rebuild_env = os.environ.copy()
if ssh_opts:
nixos_rebuild_env["NIX_SSHOPTS"] = ssh_opts
if target_port:
msg.info(f"Running nixos-rebuild {operation} for {host} on {target_host}:{target_port}...")
else:
msg.info(f"Running nixos-rebuild {operation} for {host} on {target_host}...")
nixos_rebuild = subprocess.run(
["nixos-rebuild", operation, "--flake", f".#{host}", "--target-host", target_host, "--use-substitutes", "--use-remote-sudo"], # noqa: S607, S603, E501 - can't know what the users system looks like, maybe do some kind of validation for the given host in the future?, not breaking up the command makes it more readable
env=nixos_rebuild_env,
check=False,
) # fmt: skip
if reboot and nixos_rebuild.returncode == 0:
msg.info(f"Rebooting {target_hostname}...")
if ssh_opts:
subprocess.run(
["ssh", ssh_opts, target_host, "sudo", "systemctl", "reboot"], # noqa: S607, S603 - can't know what the users system looks like, maybe do some kind of validation for the given host in the future?
check=False
) # fmt: skip
else:
subprocess.run(
["ssh", target_host, "sudo", "systemctl", "reboot"], # noqa: S607, S603 - can't know what the users system looks like, maybe do some kind of validation for the given host in the future?
check=False,
) # fmt: skip