about summary refs log tree commit diff
path: root/users/wpcarro/tools
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-11-19T02·08-0800
committerclbot <clbot@tvl.fyi>2022-11-19T02·11+0000
commit932c3b31bbb16b5d52d34fd65c51d2bd59f94762 (patch)
treed00aa584b9b2d9cd4e3d429208b9744c06a0b222 /users/wpcarro/tools
parent36f4547fb1c873a57d5f8e9342c2ae90b596d14a (diff)
feat(wpcarro/tools): Support systemd-shell r/5290
Drop into a new shell environment with the same variables defined in a systemd
unit file (for debugging purposes).

Change-Id: Iaf513809b524f3f3e845b512450da71694bb7c7f
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7308
Autosubmit: wpcarro <wpcarro@gmail.com>
Reviewed-by: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/wpcarro/tools')
-rw-r--r--users/wpcarro/tools/systemd-shell/default.nix8
-rw-r--r--users/wpcarro/tools/systemd-shell/setup.py9
-rw-r--r--users/wpcarro/tools/systemd-shell/systemd-shell36
3 files changed, 53 insertions, 0 deletions
diff --git a/users/wpcarro/tools/systemd-shell/default.nix b/users/wpcarro/tools/systemd-shell/default.nix
new file mode 100644
index 000000000000..eace76b7087b
--- /dev/null
+++ b/users/wpcarro/tools/systemd-shell/default.nix
@@ -0,0 +1,8 @@
+{ pkgs, ... }:
+
+pkgs.python310Packages.buildPythonApplication {
+  pname = "systemd-shell";
+  version = "0.0.1";
+  src = ./.;
+  doCheck = false;
+}
diff --git a/users/wpcarro/tools/systemd-shell/setup.py b/users/wpcarro/tools/systemd-shell/setup.py
new file mode 100644
index 000000000000..f45e058e672e
--- /dev/null
+++ b/users/wpcarro/tools/systemd-shell/setup.py
@@ -0,0 +1,9 @@
+from setuptools import setup
+
+setup(
+    name="systemd-shell",
+    version="0.0.1",
+    author="William Carroll",
+    author_email="wpcarro@gmail.com",
+    scripts=["systemd-shell"],
+)
diff --git a/users/wpcarro/tools/systemd-shell/systemd-shell b/users/wpcarro/tools/systemd-shell/systemd-shell
new file mode 100644
index 000000000000..646d59143afd
--- /dev/null
+++ b/users/wpcarro/tools/systemd-shell/systemd-shell
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+# Drop into a new shell environment with the same variables defined in a systemd
+# unit file (for debugging purposes).
+#
+# USAGE:
+#   $ systemd-shell -u buildkite-agent-foundation-1.service
+
+import argparse
+import os
+
+def parse_env(entry):
+    x = entry[12:].split("=", 1)
+    return x[0].removeprefix("\"").removesuffix("\""), x[1].removeprefix("\"").removesuffix("\"")
+
+def run(unit):
+  envfile = []
+  print("--- Environment ---")
+  for line in open(f"/etc/systemd/system/{unit}").readlines():
+      if line.startswith("Environment="):
+          key, val = parse_env(line[:-1])
+          print(f"export {key}={val}")
+          envfile.append(f"{key}={val}")
+      else:
+          continue
+  print()
+
+  env = " ".join(envfile)
+  print("--- Command ---")
+  os.system(f"/usr/bin/env {env} /bin/sh")
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser()
+    parser.add_argument("-u", "--unit", type=str, required=True)
+    args = parser.parse_args()
+    run(args.unit)