summaryrefslogtreecommitdiff
path: root/chip/push
blob: 1b8c1f837834cae19014ef2336be41a22b793d77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3

import argparse
import os
import subprocess
import tempfile
import importlib.util

def shell(*args):
  "Run a shell command and capture the output."
  try:
    proc = subprocess.run(args, check=True, capture_output=True)
  except subprocess.CalledProcessError as e:
    raise RuntimeError(f'{e.cmd} error {e.returncode}: {e.output}')
  return proc.stdout.strip().decode('utf8')

cli = argparse.ArgumentParser(description='deploy a thing')
cli.add_argument('depo', type=str,
                 help='the depo roun to deploy')

args = cli.parse_args()

cwd = os.getcwd()
bild_dir = f"{cwd}/bild"

src = f"{cwd}/depo/{args.depo}"
out = f"{bild_dir}/{args.depo}"


# bild
shell("nix-build", "-A", f"depo.{args.depo}", "--out-link", out)
print("bilt")

# push
shell("nix", "copy", "--to", f"ssh://root@{args.depo}", f"{out}")
print("sent")

# switch
shell("ssh", f"root@{args.depo}", "sudo",
      f"{os.readlink(out)}/bin/switch-to-configuration",
      "switch")
print("switched")

print(f"pushed {args.depo}")