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

Commit e78dd26

Browse files
committed
Handle the move of the enabled_plugins file
We look for the enabled_plugins file at the deprecated location, and at the new location. If it exists only at the deprecated location, we attempt to use that file. If the deprecated location is read-only, we allow the cli command to fail (or not) as it always used to. Part of the changes intended to address rabbitmq/rabbitmq-server#2234
1 parent f6eaae2 commit e78dd26

File tree

3 files changed

+82
-15
lines changed

3 files changed

+82
-15
lines changed

lib/rabbitmq/cli/plugins/commands/directories_command.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,14 @@ defmodule RabbitMQ.CLI.Plugins.Commands.DirectoriesCommand do
7171
end
7272

7373
def run([], %{offline: true} = opts) do
74-
do_run(fn key ->
75-
Config.get_option(key, opts)
74+
do_run(fn
75+
:enabled_plugins_file ->
76+
case PluginHelpers.enabled_plugins_file(opts) do
77+
{:ok, plugins_file} -> plugins_file
78+
_ -> nil
79+
end
80+
key ->
81+
Config.get_option(key, opts)
7682
end)
7783
end
7884

lib/rabbitmq/cli/plugins/plugins_helpers.ex

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,44 @@ defmodule RabbitMQ.CLI.Plugins.Helpers do
7070
end
7171
end
7272

73+
@spec enabled_plugins_file(map()) :: {:ok, charlist()} | {:error, any()}
7374
def enabled_plugins_file(opts) do
7475
case Config.get_option(:enabled_plugins_file, opts) do
75-
nil -> {:error, :no_plugins_file}
76-
file -> {:ok, file}
76+
nil ->
77+
case :os.type do
78+
{:unix, _} ->
79+
sys_prefix = System.get_env("SYS_PREFIX", "/")
80+
config_base_dir = Path.join([sys_prefix, "etc", "rabbitmq"])
81+
data_dir = Path.join([sys_prefix, "var", "lib", "rabbitmq"])
82+
legacy_location = Path.absname(Path.join([config_base_dir, "enabled_plugins"]))
83+
modern_location = Path.absname(Path.join([data_dir, "enabled_plugins"]))
84+
case {File.exists?(modern_location), File.exists?(legacy_location)} do
85+
{false, true} ->
86+
{:ok, legacy_location}
87+
_ ->
88+
{:ok, modern_location}
89+
end
90+
{:win32, _} ->
91+
rabbitmq_base = case System.fetch_env("RABBITMQ_BASE") do
92+
{:ok, Value} ->
93+
Value
94+
_ ->
95+
app_data = System.get_env("APPDATA")
96+
Path.join([app_data, "RabbitMQ"])
97+
end
98+
{:ok, Path.join([rabbitmq_base, "enabled_plugins"])}
99+
end
100+
file ->
101+
{:ok, file}
77102
end
78103
end
79104

105+
@spec enabled_plugins_file(any(), map()) :: {:ok, charlist()} | {:error, any()}
80106
def enabled_plugins_file(_, opts) do
81107
enabled_plugins_file(opts)
82108
end
83109

110+
@spec set_enabled_plugins([atom()], map()) :: {:ok, Enumerable.t()} | {:error, any()}
84111
def set_enabled_plugins(plugins, opts) do
85112
plugin_atoms = :lists.usort(for plugin <- plugins, do: to_atom(plugin))
86113
require_rabbit_and_plugins(opts)
@@ -89,11 +116,11 @@ defmodule RabbitMQ.CLI.Plugins.Helpers do
89116
end
90117

91118
@spec update_enabled_plugins(
92-
[atom()],
93-
:online | :offline | :best_effort,
94-
node(),
95-
map()
96-
) :: map() | {:error, any()}
119+
[atom()],
120+
:online | :offline | :best_effort,
121+
node(),
122+
map()
123+
) :: map() | {:error, any()}
97124
def update_enabled_plugins(enabled_plugins, mode, node_name, opts) do
98125
{:ok, plugins_file} = enabled_plugins_file(opts)
99126

test/plugins/directories_command_test.exs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ defmodule DirectoriesCommandTest do
2323
RabbitMQ.CLI.Core.Distribution.start()
2424
node = get_rabbit_hostname()
2525

26-
{:ok, plugins_file} = :rabbit_misc.rpc_call(node,
27-
:application, :get_env,
28-
[:rabbit, :enabled_plugins_file])
2926
{:ok, plugins_dir} = :rabbit_misc.rpc_call(node,
3027
:application, :get_env,
3128
[:rabbit, :plugins_dir])
@@ -36,7 +33,7 @@ defmodule DirectoriesCommandTest do
3633
rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit])
3734

3835
{:ok, opts: %{
39-
plugins_file: plugins_file,
36+
plugins_file: nil,
4037
plugins_dir: plugins_dir,
4138
plugins_expand_dir: plugins_expand_dir,
4239
rabbitmq_home: rabbitmq_home,
@@ -100,12 +97,49 @@ defmodule DirectoriesCommandTest do
10097
assert @command.validate_execution_environment([], opts) == :ok
10198
end
10299

103-
104100
test "run: when --online is used, lists plugin directories", context do
105101
opts = Map.merge(context[:opts], %{online: true})
102+
103+
{:ok, plugins_file} = :rabbit_misc.rpc_call(Map.get(opts, :node),
104+
:application, :get_env,
105+
[:rabbit, :enabled_plugins_file])
106+
107+
dirs = %{plugins_dir: to_string(Map.get(opts, :plugins_dir)),
108+
plugins_expand_dir: to_string(Map.get(opts, :plugins_expand_dir)),
109+
enabled_plugins_file: to_string(plugins_file)}
110+
111+
assert @command.run([], opts) == {:ok, dirs}
112+
end
113+
114+
test "run: when --offline is used, checks for enabled_plugins in the data dir, falling back to the config dir", context do
115+
sys_prefix = Path.join([System.tmp_dir(), "DirectoriesCommandTest"])
116+
117+
System.put_env("SYS_PREFIX", sys_prefix)
118+
119+
config_dir = Path.join([sys_prefix, "etc", "rabbitmq"])
120+
legacy_file = Path.join([config_dir, "enabled_plugins"])
121+
122+
data_dir = Path.join([sys_prefix, "var", "lib", "rabbitmq"])
123+
modern_file = Path.join([data_dir, "enabled_plugins"])
124+
125+
File.rm(legacy_file)
126+
File.rm(modern_file)
127+
128+
opts = Map.merge(context[:opts], %{offline: true})
129+
106130
dirs = %{plugins_dir: to_string(Map.get(opts, :plugins_dir)),
107131
plugins_expand_dir: to_string(Map.get(opts, :plugins_expand_dir)),
108-
enabled_plugins_file: to_string(Map.get(opts, :plugins_file))}
132+
enabled_plugins_file: modern_file}
133+
134+
assert @command.run([], opts) == {:ok, dirs}
135+
136+
:ok = File.mkdir_p(config_dir)
137+
:ok = File.touch(legacy_file)
138+
139+
assert @command.run([], opts) == {:ok, Map.merge(dirs, %{enabled_plugins_file: legacy_file})}
140+
141+
:ok = File.mkdir_p(data_dir)
142+
:ok = File.touch(modern_file)
109143

110144
assert @command.run([], opts) == {:ok, dirs}
111145
end

0 commit comments

Comments
 (0)