Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runagent): list running agent IDs #789

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion core/imageroot/usr/local/bin/runagent
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import sys
import argparse
import agent
import pwd
import subprocess

argp = argparse.ArgumentParser(description="Run COMMAND in the agent environment of MODULE_ID")
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")
argx = argp.add_mutually_exclusive_group()
argx.add_argument("-l", "--list-modules", action="store_true", help="List running modules")
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")
argp.add_argument("-c", "--current-dir", action="store_true", help="Run COMMAND in current directory, instead of changing directory to AGENT_STATE_DIR")
argp.add_argument('COMMAND', nargs='?', help="Command to run in the agent environment")
argp.add_argument('ARGS', nargs=argparse.REMAINDER, help="Additional arguments for COMMAND")
Expand All @@ -40,6 +43,23 @@ def read_env(file_path):
env = agent.read_envfile(file_path)
os.environ.update(env)

if args.list_modules:
for line in subprocess.check_output(['pgrep', '-x', 'agent', '-a'], text=True).split("\n"):
if not line:
break
fields = line.split()
if len(fields) < 3:
break
if fields[1] != '/usr/local/bin/agent':
continue
if fields[2] == '--agentid=cluster':
print("cluster")
elif fields[2].startswith('--agentid=module/'):
print(fields[2].removeprefix('--agentid=module/'))
elif fields[2].startswith('--agentid=node/'):
print("node")
sys.exit(0)

mid = args.module_id

if os.geteuid() != 0:
Expand Down
Loading