about summary refs log tree commit diff
path: root/users/wpcarro/tools/systemd-shell/systemd-shell
blob: 646d59143afd48ffc64101a18a430f7c07a7c6dd (plain) (blame)
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
#!/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)