From cfd075cfdd7ed5b98770f1fdab1af3e51030bf5e Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Fri, 14 Jun 2019 17:16:45 -0700 Subject: chip: refactor push to have a shell function This handles subprocess errors better and makes the code easier to follow. --- chip/push | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/chip/push b/chip/push index 96cc893..1b8c1f8 100755 --- a/chip/push +++ b/chip/push @@ -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}") -- cgit v1.2.3