Skip to content

Commit 9f3a977

Browse files
committed
wip
1 parent 9caca8e commit 9f3a977

File tree

3 files changed

+122
-19
lines changed

3 files changed

+122
-19
lines changed

deps/rabbit/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ ebin/$(PROJECT).app:: $(CLI_SCRIPTS)
158158

159159
$(CLI_SCRIPTS): ebin/rabbit_cli.beam
160160
$(gen_verbose) echo '#!/usr/bin/env escript' > "$@"
161-
$(verbose) echo '%%! -start_epmd false' >> "$@"
161+
$(verbose) echo '%%! +sbtu +A1 -start_epmd false' >> "$@"
162162
$(verbose) cat $^ >> "$@"
163163
$(verbose) chmod a+x "$@"
164164

deps/rabbit/src/rabbit_cli.erl

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,29 @@ main(Args) ->
1515

1616
run_ci(Args) ->
1717
maybe
18-
{ok, _RemainingArgs, Nodename} ?= determine_rabbitmq_nodename(Args),
18+
io:format("Args = ~p~n", [Args]),
19+
{ok, RemainingArgs, Nodename} ?= lookup_rabbitmq_nodename(Args),
20+
io:format("Args = ~p~n", [RemainingArgs]),
1921
io:format("Nodename = ~p~n", [Nodename]),
20-
{ok, CommandMap} ?= determine_command_map(Nodename),
21-
io:format("CommandMap= ~p~n", [CommandMap]),
22+
{ok, _} ?= net_kernel:start(undefined, #{name_domain => shortnames}),
23+
true ?= net_kernel:connect_node(Nodename),
24+
io:format("This node = ~p~n", [node()]),
25+
%{ok, CommandMap} ?= lookup_command_map(Nodename),
26+
%io:format("CommandMap= ~p~n", [CommandMap]),
27+
Ret ?= run_command(Nodename, RemainingArgs),
28+
io:format("Ret = ~p~n", [Ret]),
2229
ok
2330
end.
2431

25-
determine_rabbitmq_nodename(Args) ->
32+
lookup_rabbitmq_nodename(Args) ->
2633
case get_rabbitmq_nodename_from_args(Args) of
2734
{ok, Args1} ->
28-
GuessedNodename = guess_rabbitmq_nodename(),
29-
{ok, Args1, GuessedNodename};
30-
{ok, Args1, Nodename} ->
31-
{ok, Args1, Nodename};
35+
GuessedNodename0 = guess_rabbitmq_nodename(),
36+
GuessedNodename1 = complete_nodename(GuessedNodename0),
37+
{ok, Args1, GuessedNodename1};
38+
{ok, Args1, Nodename0} ->
39+
Nodename1 = complete_nodename(Nodename0),
40+
{ok, Args1, Nodename1};
3241
{error, _} = Error ->
3342
Error
3443
end.
@@ -74,15 +83,65 @@ guess_rabbitmq_nodename() ->
7483
"rabbit"
7584
end.
7685

77-
determine_command_map(Nodename) ->
78-
case is_node_local(Nodename) of
79-
true ->
80-
%% File or RPC.
81-
{ok, #{}};
82-
false ->
83-
%% RPC.
84-
{ok, #{}}
86+
complete_nodename(Nodename) ->
87+
case re:run(Nodename, "@", [{capture, none}]) of
88+
nomatch ->
89+
{ok, ThisHost} = inet:gethostname(),
90+
list_to_atom(Nodename ++ "@" ++ ThisHost);
91+
match ->
92+
list_to_atom(Nodename)
8593
end.
8694

87-
is_node_local(Nodename) ->
88-
Nodename =:= node().
95+
%lookup_command_map(Nodename) ->
96+
% %% Order of operations:
97+
% %% 1. refresh the cached copy:
98+
% %% a. is the node running?
99+
% %% yes -> query its uptime
100+
% %% no -> local or remote?
101+
% %% local -> list beam files + file size + last modified date
102+
% %% remote -> no refresh possible
103+
% %% b. compare uptime to cache date
104+
% %% or
105+
% %% compare files list to cached files list
106+
% %% c. if refresh needed, query command map or (local only) extract it from files list
107+
% %%
108+
% %% 2. use cached command map; error out if none
109+
% %%
110+
% %% Or:
111+
% %% 0. node extracts the command map on startup and stores it persistent_term
112+
% %% 1. query node for command map with a very short timeout + store result locally
113+
% %% 2. read local copy
114+
% %% + we skip the query if `--help` or completion and if there is a local copy
115+
% case is_node_local(Nodename) of
116+
% true ->
117+
% %% Generated local command map.
118+
% lookup_local_command_map(),
119+
% {ok, #{}};
120+
% false ->
121+
% %% Cache or RPC.
122+
% {ok, #{}}
123+
% end.
124+
%
125+
%is_node_local(Nodename) ->
126+
% case re:run(Nodename, "@(.+)", [{capture, all_but_first, list}]) of
127+
% {match, ["localhost"]} ->
128+
% true;
129+
% {match, [HostPart]} ->
130+
% ThisHost = inet:gethostname(),
131+
% HostPart =:= ThisHost;
132+
% nomatch ->
133+
% true
134+
% end.
135+
%
136+
%lookup_local_command_map() ->
137+
% ScriptDir = filename:dirname(escript:script_name()),
138+
% io:format("Script = ~p~n", [ScriptDir]),
139+
% ok.
140+
141+
run_command(Nodename, Args) ->
142+
try
143+
erpc:call(Nodename, rabbit_cli_commands, run_command, [Args])
144+
catch
145+
error:{erpc, Reason} ->
146+
{error, Reason}
147+
end.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
-module(rabbit_cli_commands).
2+
3+
-include_lib("kernel/include/logger.hrl").
4+
5+
-export([commands/0, run_command/1]).
6+
-export([list_queues/1]).
7+
8+
commands() ->
9+
#{
10+
arguments =>
11+
[
12+
#{name => verbose,
13+
short => $v,
14+
action => count,
15+
help =>
16+
"Be verbose; can be specified multiple times to increase "
17+
"verbosity"}
18+
],
19+
commands =>
20+
#{
21+
"list" =>
22+
#{commands =>
23+
#{"queues" =>
24+
#{handler => {?MODULE, list_queues}}
25+
}
26+
}
27+
}
28+
}.
29+
30+
run_command(Args) ->
31+
Definition = commands(),
32+
case argparse:parse(Args, Definition, #{}) of
33+
{ok, ArgMap, CmdPath, #{handler := {Mod, Fun}} = Command} ->
34+
?LOG_ALERT("ArgMap=~0p~nCmdPath=~0p~nCommand=~0p", [ArgMap, CmdPath, Command]),
35+
Mod:Fun(ArgMap),
36+
ok;
37+
{error, Reason} ->
38+
?LOG_ALERT("Reason=~0p", [Reason]),
39+
ok
40+
end.
41+
42+
list_queues(ArgMap) ->
43+
?LOG_ALERT("Listing queues, ~0p", [ArgMap]),
44+
ok.

0 commit comments

Comments
 (0)