diff --git a/lib/rabbitmq/cli/plugins/commands/directories_command.ex b/lib/rabbitmq/cli/plugins/commands/directories_command.ex index aa5f1058..b4f8714c 100644 --- a/lib/rabbitmq/cli/plugins/commands/directories_command.ex +++ b/lib/rabbitmq/cli/plugins/commands/directories_command.ex @@ -71,8 +71,14 @@ defmodule RabbitMQ.CLI.Plugins.Commands.DirectoriesCommand do end def run([], %{offline: true} = opts) do - do_run(fn key -> - Config.get_option(key, opts) + do_run(fn + :enabled_plugins_file -> + case PluginHelpers.enabled_plugins_file(opts) do + {:ok, plugins_file} -> plugins_file + _ -> nil + end + key -> + Config.get_option(key, opts) end) end diff --git a/lib/rabbitmq/cli/plugins/plugins_helpers.ex b/lib/rabbitmq/cli/plugins/plugins_helpers.ex index c423268a..ca712282 100644 --- a/lib/rabbitmq/cli/plugins/plugins_helpers.ex +++ b/lib/rabbitmq/cli/plugins/plugins_helpers.ex @@ -70,17 +70,44 @@ defmodule RabbitMQ.CLI.Plugins.Helpers do end end + @spec enabled_plugins_file(map()) :: {:ok, charlist()} | {:error, any()} def enabled_plugins_file(opts) do case Config.get_option(:enabled_plugins_file, opts) do - nil -> {:error, :no_plugins_file} - file -> {:ok, file} + nil -> + case :os.type do + {:unix, _} -> + sys_prefix = System.get_env("SYS_PREFIX", "/") + config_base_dir = Path.join([sys_prefix, "etc", "rabbitmq"]) + data_dir = Path.join([sys_prefix, "var", "lib", "rabbitmq"]) + legacy_location = Path.absname(Path.join([config_base_dir, "enabled_plugins"])) + modern_location = Path.absname(Path.join([data_dir, "enabled_plugins"])) + case {File.exists?(modern_location), File.exists?(legacy_location)} do + {false, true} -> + {:ok, legacy_location} + _ -> + {:ok, modern_location} + end + {:win32, _} -> + rabbitmq_base = case System.fetch_env("RABBITMQ_BASE") do + {:ok, Value} -> + Value + _ -> + app_data = System.get_env("APPDATA") + Path.join([app_data, "RabbitMQ"]) + end + {:ok, Path.join([rabbitmq_base, "enabled_plugins"])} + end + file -> + {:ok, file} end end + @spec enabled_plugins_file(any(), map()) :: {:ok, charlist()} | {:error, any()} def enabled_plugins_file(_, opts) do enabled_plugins_file(opts) end + @spec set_enabled_plugins([atom()], map()) :: {:ok, Enumerable.t()} | {:error, any()} def set_enabled_plugins(plugins, opts) do plugin_atoms = :lists.usort(for plugin <- plugins, do: to_atom(plugin)) require_rabbit_and_plugins(opts) @@ -89,11 +116,11 @@ defmodule RabbitMQ.CLI.Plugins.Helpers do end @spec update_enabled_plugins( - [atom()], - :online | :offline | :best_effort, - node(), - map() - ) :: map() | {:error, any()} + [atom()], + :online | :offline | :best_effort, + node(), + map() + ) :: map() | {:error, any()} def update_enabled_plugins(enabled_plugins, mode, node_name, opts) do {:ok, plugins_file} = enabled_plugins_file(opts) diff --git a/test/plugins/directories_command_test.exs b/test/plugins/directories_command_test.exs index 9c431b67..6cf2d1e0 100644 --- a/test/plugins/directories_command_test.exs +++ b/test/plugins/directories_command_test.exs @@ -23,9 +23,6 @@ defmodule DirectoriesCommandTest do RabbitMQ.CLI.Core.Distribution.start() node = get_rabbit_hostname() - {:ok, plugins_file} = :rabbit_misc.rpc_call(node, - :application, :get_env, - [:rabbit, :enabled_plugins_file]) {:ok, plugins_dir} = :rabbit_misc.rpc_call(node, :application, :get_env, [:rabbit, :plugins_dir]) @@ -36,7 +33,7 @@ defmodule DirectoriesCommandTest do rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit]) {:ok, opts: %{ - plugins_file: plugins_file, + plugins_file: nil, plugins_dir: plugins_dir, plugins_expand_dir: plugins_expand_dir, rabbitmq_home: rabbitmq_home, @@ -100,12 +97,52 @@ defmodule DirectoriesCommandTest do assert @command.validate_execution_environment([], opts) == :ok end - test "run: when --online is used, lists plugin directories", context do opts = Map.merge(context[:opts], %{online: true}) + + {:ok, plugins_file} = :rabbit_misc.rpc_call(Map.get(opts, :node), + :application, :get_env, + [:rabbit, :enabled_plugins_file]) + dirs = %{plugins_dir: to_string(Map.get(opts, :plugins_dir)), plugins_expand_dir: to_string(Map.get(opts, :plugins_expand_dir)), - enabled_plugins_file: to_string(Map.get(opts, :plugins_file))} + enabled_plugins_file: to_string(plugins_file)} + + assert @command.run([], opts) == {:ok, dirs} + end + + test "run: when --offline is used, checks for enabled_plugins in the data dir, falling back to the config dir", context do + sys_prefix = Path.join([System.tmp_dir(), "DirectoriesCommandTest"]) + + System.put_env("SYS_PREFIX", sys_prefix) + + config_dir = Path.join([sys_prefix, "etc", "rabbitmq"]) + legacy_file = Path.join([config_dir, "enabled_plugins"]) + + data_dir = Path.join([sys_prefix, "var", "lib", "rabbitmq"]) + modern_file = Path.join([data_dir, "enabled_plugins"]) + + File.rm(legacy_file) + File.rm(modern_file) + + refute File.exists?(legacy_file) + refute File.exists?(modern_file) + + opts = Map.merge(context[:opts], %{offline: true}) + + dirs = %{plugins_dir: to_string(Map.get(opts, :plugins_dir)), + plugins_expand_dir: to_string(Map.get(opts, :plugins_expand_dir)), + enabled_plugins_file: modern_file} + + assert @command.run([], opts) == {:ok, dirs} + + :ok = File.mkdir_p(config_dir) + :ok = File.touch(legacy_file) + + assert @command.run([], opts) == {:ok, Map.merge(dirs, %{enabled_plugins_file: legacy_file})} + + :ok = File.mkdir_p(data_dir) + :ok = File.touch(modern_file) assert @command.run([], opts) == {:ok, dirs} end