@@ -176,15 +176,18 @@ connect_to_node(
176176 _ ->
177177 rabbit_cli_transport :connect ()
178178 end ,
179- {ClientInfo , Priv1 } = case Ret of
180- {ok , Connection } ->
181- {rabbit_cli_transport :get_client_info (
182- Connection ),
183- Priv #? MODULE {connection = Connection }};
184- {error , _Reason } ->
185- {undefined ,
186- Priv #? MODULE {connection = none }}
187- end ,
179+ Priv1 = case Ret of
180+ {ok , Connection } ->
181+ Priv #? MODULE {connection = Connection };
182+ {error , Reason } ->
183+ ? LOG_DEBUG (
184+ " CLI: failed to establish a connection to a RabbitMQ "
185+ " node: ~0p " ,
186+ [Reason ]),
187+ Priv #? MODULE {connection = none }
188+ end ,
189+ ClientInfo = rabbit_cli_transport :get_client_info (
190+ Priv1 #? MODULE .connection ),
188191 Context1 = Context # rabbit_cli {client = ClientInfo ,
189192 priv = Priv1 },
190193 run_command (Context1 ).
@@ -282,18 +285,31 @@ run_command(
282285 main_loop (Context1 )
283286 end ;
284287run_command (# rabbit_cli {} = Context ) ->
285- % % TODO: If we can't connect to a node, try to parse args locally and run
286- % % the command on this CLI node.
287288 % % FIXME: Load applications first, otherwise module attributes are
288289 % % unavailable.
289- % % FIXME: run_command() relies on rabbit_cli_backend_sup.
290290 maybe
291291 process_flag (trap_exit , true ),
292+ prepare_offline_exec (Context ),
292293 ContextMap = context_to_map (Context ),
293294 {ok , _Backend } ?= rabbit_cli_backend :run_command (ContextMap , self ()),
294295 main_loop (Context )
295296 end .
296297
298+ prepare_offline_exec (_Context ) ->
299+ ? LOG_DEBUG (" CLI: prepare for offline execution" ),
300+ Env = rabbit_env :get_context (),
301+ rabbit_env :context_to_code_path (Env ),
302+ rabbit_env :context_to_app_env_vars (Env ),
303+ PluginsDir = rabbit_plugins :plugins_dir (),
304+ Plugins = rabbit_plugins :plugin_names (
305+ rabbit_plugins :list (PluginsDir , true )),
306+ Apps = [rabbit_common , rabbit | Plugins ],
307+ lists :foreach (
308+ fun (App ) -> _ = application :load (App ) end ,
309+ Apps ),
310+ ? LOG_DEBUG (" CLI: ready for offline execution: ~p " , [application :loaded_applications ()]),
311+ ok .
312+
297313context_to_map (Context ) ->
298314 Fields = [Field || Field <- record_info (fields , rabbit_cli ),
299315 % % We don't need or want to communicate anything that
@@ -311,7 +327,7 @@ record_to_map([], _Record, _Index, Map) ->
311327main_loop (
312328 # rabbit_cli {priv = #? MODULE {connection = Connection ,
313329 backend = Backend ,
314- pager = Pager } = Priv } = Context ) ->
330+ pager = Pager } = Priv } = Context ) ->
315331 ? LOG_DEBUG (" CLI: frontend main loop (pager: ~0p )..." , [Pager ]),
316332 Timeout = case is_port (Pager ) of
317333 false ->
0 commit comments