tvl-depot/users/wpcarro/tools/systemd-shell/systemd-shell
William Carroll 932c3b31bb feat(wpcarro/tools): Support systemd-shell
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
2022-11-19 02:11:37 +00:00

36 lines
1,004 B
Python

#!/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)