Skip to content
This repository was archived by the owner on Nov 18, 2020. It is now read-only.

Handle the move of the enabled_plugins file #407

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions lib/rabbitmq/cli/plugins/commands/directories_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 34 additions & 7 deletions lib/rabbitmq/cli/plugins/plugins_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplication of the logic between rabbitmq-server and this repository makes me think more and more there is something wrong with that file, but at a deeper level. Otherwise the patches solve the immediate issue.

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)
Expand All @@ -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)

Expand Down
49 changes: 43 additions & 6 deletions test/plugins/directories_command_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down