Skip to content

Commit 3896460

Browse files
feat(runagent): list running agent IDs
1 parent fea1a2f commit 3896460

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

core/imageroot/usr/local/bin/runagent

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ import sys
2525
import argparse
2626
import agent
2727
import pwd
28+
import subprocess
2829

2930
argp = argparse.ArgumentParser(description="Run COMMAND in the agent environment of MODULE_ID")
30-
argp.add_argument("-m", "--module-id", help="MODULE_ID, a module identifier (e.g. \"module1\", \"node\"). Default is \"cluster\". Only root can use this flag", default="cluster")
31+
argx = argp.add_mutually_exclusive_group()
32+
argx.add_argument("-l", "--list-modules", action="store_true", help="List running modules")
33+
argx.add_argument("-m", "--module-id", help="MODULE_ID, a module identifier (e.g. \"module1\", \"node\"). Default is \"cluster\". Only root can use this flag", default="cluster")
3134
argp.add_argument("-c", "--current-dir", action="store_true", help="Run COMMAND in current directory, instead of changing directory to AGENT_STATE_DIR")
3235
argp.add_argument('COMMAND', nargs='?', help="Command to run in the agent environment")
3336
argp.add_argument('ARGS', nargs=argparse.REMAINDER, help="Additional arguments for COMMAND")
@@ -40,6 +43,23 @@ def read_env(file_path):
4043
env = agent.read_envfile(file_path)
4144
os.environ.update(env)
4245

46+
if args.list_modules:
47+
for line in subprocess.check_output(['pgrep', '-x', 'agent', '-a'], text=True).split("\n"):
48+
if not line:
49+
break
50+
fields = line.split()
51+
if len(fields) < 3:
52+
break
53+
if fields[1] != '/usr/local/bin/agent':
54+
continue
55+
if fields[2] == '--agentid=cluster':
56+
print("cluster")
57+
elif fields[2].startswith('--agentid=module/'):
58+
print(fields[2].removeprefix('--agentid=module/'))
59+
elif fields[2].startswith('--agentid=node/'):
60+
print("node")
61+
sys.exit(0)
62+
4363
mid = args.module_id
4464

4565
if os.geteuid() != 0:

0 commit comments

Comments
 (0)