Skip to content

Commit 06c1fe7

Browse files
feat(runagent): list running agent IDs (#789)
Press TAB to complete -m/--module-id option with agent IDs.
1 parent 35cfc02 commit 06c1fe7

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

core/imageroot/etc/profile.d/runagent.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,35 @@ if [ -n "${AGENT_STATE_DIR}" ] && [ -f "${AGENT_STATE_DIR}/agent.env" ]; then
1010
. ${AGENT_STATE_DIR}/agent.env
1111
export PATH
1212
fi
13+
14+
if [ -z "$BASH" ]; then
15+
return
16+
fi
17+
18+
_runagent_wastyped()
19+
{
20+
local xword checkword="$1"
21+
for xword in "${COMP_WORDS[@]}"; do
22+
if [[ "${xword}" == "${checkword}" ]]; then
23+
return 0
24+
fi
25+
done
26+
return 1
27+
}
28+
29+
_runagent_completions()
30+
{
31+
local cword="$2"
32+
local agents=()
33+
34+
if _runagent_wastyped "-l" || _runagent_wastyped "--list-modules"; then
35+
:
36+
elif _runagent_wastyped "-m" || _runagent_wastyped "--module-id" ; then
37+
agents+=($(runagent --list-modules | sort | grep ^"${cword}"))
38+
COMPREPLY+=($(compgen -W "${agents[*]}" -- "${cword}"))
39+
else
40+
COMPREPLY+=($(compgen -W "--list-modules --module-id --current-dir --help" -- "${cword}"))
41+
fi
42+
}
43+
44+
complete -F _runagent_completions runagent

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)