infra/tasks.py

171 lines
4.4 KiB
Python
Raw Normal View History

2021-10-21 11:09:52 +02:00
#!/usr/bin/env python3
2021-10-24 01:04:22 +02:00
import json
2023-01-16 09:37:38 +10:00
import os
2022-12-31 07:24:17 +01:00
import subprocess
2023-03-12 15:02:16 +10:00
from pathlib import Path
2023-05-13 21:32:00 +10:00
from tempfile import TemporaryDirectory
2023-09-06 17:27:23 +10:00
from typing import Any, List, Union
2022-12-31 07:24:17 +01:00
from deploykit import DeployGroup, DeployHost
from invoke import task
2021-10-21 11:09:52 +02:00
2023-03-12 15:02:16 +10:00
ROOT = Path(__file__).parent.resolve()
os.chdir(ROOT)
2021-10-21 11:09:52 +02:00
2023-03-17 13:36:07 +10:00
@task
def deploy(c: Any, hosts: str) -> None:
"""
Use inv deploy --hosts build01,darwin01
"""
g = DeployGroup(get_hosts(hosts))
2022-01-15 13:38:30 +01:00
2021-10-21 11:09:52 +02:00
def deploy(h: DeployHost) -> None:
2023-07-17 09:37:43 +10:00
if "darwin" in h.host:
# don't use sudo for darwin-rebuild
command = "darwin-rebuild"
2024-03-07 09:16:59 +10:00
target = f"{h.user}@{h.host}"
else:
command = "sudo nixos-rebuild"
2024-03-07 09:16:59 +10:00
target = f"{h.host}"
res = h.run_local(
2024-03-07 09:16:59 +10:00
["nix", "flake", "archive", "--to", f"ssh://{target}", "--json"],
stdout=subprocess.PIPE,
)
data = json.loads(res.stdout)
path = data["path"]
2021-10-21 11:09:52 +02:00
hostname = h.host.replace(".nix-community.org", "")
h.run(
f"{command} switch --option accept-flake-config true --flake {path}#{hostname}"
)
2022-01-15 13:38:30 +01:00
2021-10-21 11:09:52 +02:00
g.run_function(deploy)
2023-10-02 09:14:57 +10:00
@task
def sotp(c: Any, acct: str) -> None:
"""
Get TOTP token from sops
"""
c.run(f"nix develop .#sotp -c sotp {acct}")
@task
def update_agenix_files(c: Any) -> None:
"""
Update all agenix secrets
"""
os.chdir("secrets")
c.run("agenix --rekey", pty=True)
2022-10-25 09:55:14 +02:00
@task
2023-09-06 17:27:23 +10:00
def update_sops_files(c: Any) -> None:
2022-10-25 09:55:14 +02:00
"""
Update all sops yaml files according to .sops.yaml rules
2022-10-25 09:55:14 +02:00
"""
c.run("shopt -s globstar && sops updatekeys --yes **/secrets.yaml")
2022-10-25 09:55:14 +02:00
2022-12-31 07:24:17 +01:00
2022-12-30 20:51:58 +01:00
@task
2023-09-06 17:27:23 +10:00
def print_keys(c: Any, flake_attr: str) -> None:
2022-12-30 20:51:58 +01:00
"""
2023-05-13 21:32:00 +10:00
Decrypt host private key, print ssh and age public keys. Use inv print-keys --flake-attr build01
2022-12-30 20:51:58 +01:00
"""
2023-05-13 21:32:00 +10:00
with TemporaryDirectory() as tmpdir:
decrypt_host_key(flake_attr, tmpdir)
key = f"{tmpdir}/etc/ssh/ssh_host_ed25519_key"
pubkey = subprocess.run(
["ssh-keygen", "-y", "-f", f"{key}"],
stdout=subprocess.PIPE,
text=True,
check=True,
)
print("###### Public keys ######")
print(pubkey.stdout)
print("###### Age keys ######")
subprocess.run(
["ssh-to-age"],
input=pubkey.stdout,
check=True,
text=True,
)
2022-12-30 20:51:58 +01:00
2022-10-25 09:55:14 +02:00
@task
def docs(c: Any) -> None:
"""
Serve docs (mkdoc serve)
"""
c.run("nix develop .#mkdocs -c mkdocs serve")
2024-06-01 08:22:31 +10:00
@task
def docs_linkcheck(c: Any) -> None:
"""
Run docs online linkchecker
"""
c.run("nix run .#docs-linkcheck.online")
2021-10-24 01:04:22 +02:00
def get_hosts(hosts: str) -> List[DeployHost]:
2024-10-16 22:22:16 +10:00
deploy_hosts = []
for host in hosts.split(","):
if host.startswith("darwin"):
deploy_hosts.append(
DeployHost(f"{host}.nix-community.org", user="customer")
)
else:
deploy_hosts.append(DeployHost(f"{host}.nix-community.org"))
2024-10-16 22:22:16 +10:00
return deploy_hosts
2021-10-21 11:09:52 +02:00
2023-09-06 17:27:23 +10:00
def decrypt_host_key(flake_attr: str, tmpdir: str) -> None:
def opener(path: str, flags: int) -> Union[str, int]:
2023-05-13 21:32:00 +10:00
return os.open(path, flags, 0o400)
t = Path(tmpdir)
t.mkdir(parents=True, exist_ok=True)
t.chmod(0o755)
host_key = t / "etc/ssh/ssh_host_ed25519_key"
host_key.parent.mkdir(parents=True, exist_ok=True)
with open(host_key, "w", opener=opener) as fh:
subprocess.run(
[
"sops",
"--extract",
2024-06-30 13:20:54 +10:00
f'["ssh_host_ed25519_key"]["{flake_attr}"]',
2023-05-13 21:32:00 +10:00
"--decrypt",
2024-06-30 13:20:54 +10:00
f"{ROOT}/secrets.yaml",
2023-05-13 21:32:00 +10:00
],
check=True,
stdout=fh,
)
@task
2023-09-06 17:27:23 +10:00
def install(c: Any, flake_attr: str, hostname: str) -> None:
"""
2023-05-13 21:32:00 +10:00
Decrypt host private key, install with nixos-anywhere. Use inv install --flake-attr build01 --hostname build01.nix-community.org
"""
2023-05-13 21:32:00 +10:00
ask = input(f"Install {hostname} with {flake_attr}? [y/N] ")
if ask != "y":
return
with TemporaryDirectory() as tmpdir:
decrypt_host_key(flake_attr, tmpdir)
flags = "--build-on-remote --debug --option accept-flake-config true"
2023-05-13 21:32:00 +10:00
c.run(
f"nix run --inputs-from . nixpkgs#nixos-anywhere -- {hostname} --extra-files {tmpdir} --flake .#{flake_attr} {flags}",
2023-05-13 21:32:00 +10:00
echo=True,
)
2021-10-21 11:09:52 +02:00
@task
def cleanup_gcroots(c: Any, hosts: str) -> None:
2021-10-21 11:09:52 +02:00
g = DeployGroup(get_hosts(hosts))
g.run("sudo find /nix/var/nix/gcroots/auto -type s -delete")