diff options
author | Ben Sima <ben@bsima.me> | 2019-06-14 17:16:45 -0700 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2019-06-16 01:53:35 +0000 |
commit | cfd075cfdd7ed5b98770f1fdab1af3e51030bf5e (patch) | |
tree | 5718b7e95fcf863ac2d10f6bc5439d1eefab0ac9 /chip/push | |
parent | 79a4fc76167f57a5ae65be789980dd2f7ee8b635 (diff) |
chip: refactor push to have a shell function
This handles subprocess errors better and makes the code easier to follow.
Diffstat (limited to 'chip/push')
-rwxr-xr-x | chip/push | 27 |
1 files changed, 15 insertions, 12 deletions
@@ -6,6 +6,14 @@ 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') @@ -20,22 +28,17 @@ out = f"{bild_dir}/{args.depo}" # bild -subprocess.run(["nix-build", "-A", f"depo.{args.depo}", - "--out-link", out]) +shell("nix-build", "-A", f"depo.{args.depo}", "--out-link", out) print("bilt") -# get roun -p = subprocess.run(["chip/roun", args.depo, "-i"], - stdout=subprocess.PIPE) -ip = p.stdout.decode() - # push -subprocess.run(["nix", "copy", "--to", f"ssh://root@{ip}", f"{out}"]) +shell("nix", "copy", "--to", f"ssh://root@{args.depo}", f"{out}") print("sent") # switch -subprocess.run(["ssh", f"root@{ip}", "sudo", - f"{os.readlink(out)}/bin/switch-to-configuration", - "switch"]) +shell("ssh", f"root@{args.depo}", "sudo", + f"{os.readlink(out)}/bin/switch-to-configuration", + "switch") +print("switched") -print(f"pushed {args.depo} -> {ip}") +print(f"pushed {args.depo}") |