Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate plugin compatibility with version ranges #642

Merged
merged 21 commits into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion src/rabbit.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{application, rabbit, %% -*- erlang -*-
[{description, "RabbitMQ"},
{id, "RabbitMQ"},
{vsn, "0.0.0"},
{vsn, "3.7.0"},
Copy link
Member

Choose a reason for hiding this comment

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

This value is updated at build time. @dumbbell do you agree with this default value bump?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, commited accidentally.

{modules, []},
{registered, [rabbit_amqqueue_sup,
rabbit_log,
Expand Down
164 changes: 131 additions & 33 deletions src/rabbit_plugins.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
-export([setup/0, active/0, read_enabled/1, list/1, list/2, dependencies/3]).
-export([ensure/1]).
-export([extract_schemas/1]).
-export([validate_plugins/1, format_invalid_plugins/1]).

%%----------------------------------------------------------------------------
% Export for testing purpose.
-export([version_support/2, validate_plugins/2]).

%%----------------------------------------------------------------------------
-ifdef(use_specs).

-type(plugin_name() :: atom()).
Expand Down Expand Up @@ -97,31 +100,31 @@ extract_schemas(SchemaDir) ->
ok.

extract_schema(#plugin{type = ez, location = Location}, SchemaDir) ->
{ok, Files} = zip:extract(Location,
[memory, {file_filter,
fun(#zip_file{name = Name}) ->
string:str(Name, "priv/schema") > 0
{ok, Files} = zip:extract(Location,
[memory, {file_filter,
fun(#zip_file{name = Name}) ->
string:str(Name, "priv/schema") > 0
end}]),
lists:foreach(
fun({FileName, Content}) ->
ok = file:write_file(filename:join([SchemaDir,
ok = file:write_file(filename:join([SchemaDir,
filename:basename(FileName)]),
Content)
end,
Files),
ok;
extract_schema(#plugin{type = dir, location = Location}, SchemaDir) ->
PluginSchema = filename:join([Location,
"priv",
"schema"]),
"priv",
"schema"]),
case rabbit_file:is_dir(PluginSchema) of
false -> ok;
true ->
PluginSchemaFiles =
true ->
PluginSchemaFiles =
[ filename:join(PluginSchema, FileName)
|| FileName <- rabbit_file:wildcard(".*\\.schema",
|| FileName <- rabbit_file:wildcard(".*\\.schema",
PluginSchema) ],
[ file:copy(SchemaFile, SchemaDir)
[ file:copy(SchemaFile, SchemaDir)
|| SchemaFile <- PluginSchemaFiles ]
end.

Expand All @@ -146,27 +149,31 @@ list(PluginsDir, IncludeRequiredDeps) ->
%% instance.
application:load(rabbit),
{ok, RabbitDeps} = application:get_key(rabbit, applications),
AllPlugins = [plugin_info(PluginsDir, Plug) || Plug <- EZs ++ FreeApps],
{AvailablePlugins, Problems} =
lists:foldl(fun ({error, EZ, Reason}, {Plugins1, Problems1}) ->
{Plugins1, [{EZ, Reason} | Problems1]};
(Plugin = #plugin{name = Name}, {Plugins1, Problems1}) ->
%% Applications RabbitMQ depends on (eg.
%% "rabbit_common") can't be considered
%% plugins, otherwise rabbitmq-plugins would
%% list them and the user may believe he can
%% disable them.
case IncludeRequiredDeps orelse
not lists:member(Name, RabbitDeps) of
true -> {[Plugin|Plugins1], Problems1};
false -> {Plugins1, Problems1}
end
end, {[], []},
[plugin_info(PluginsDir, Plug) || Plug <- EZs ++ FreeApps]),
lists:foldl(
fun ({error, EZ, Reason}, {Plugins1, Problems1}) ->
{Plugins1, [{EZ, Reason} | Problems1]};
(Plugin = #plugin{name = Name},
{Plugins1, Problems1}) ->
%% Applications RabbitMQ depends on (eg.
%% "rabbit_common") can't be considered
%% plugins, otherwise rabbitmq-plugins would
%% list them and the user may believe he can
%% disable them.
case IncludeRequiredDeps orelse
not lists:member(Name, RabbitDeps) of
true -> {[Plugin|Plugins1], Problems1};
false -> {Plugins1, Problems1}
end
end, {[], []},
AllPlugins),
case Problems of
[] -> ok;
_ -> rabbit_log:warning(
"Problem reading some plugins: ~p~n", [Problems])
end,

Plugins = lists:filter(fun(P) -> not plugin_provided_by_otp(P) end,
AvailablePlugins),
ensure_dependencies(Plugins).
Expand Down Expand Up @@ -196,8 +203,9 @@ dependencies(Reverse, Sources, AllPlugins) ->
false -> digraph_utils:reachable(Sources, G);
true -> digraph_utils:reaching(Sources, G)
end,
OrderedDests = digraph_utils:postorder(digraph_utils:subgraph(G, Dests)),
true = digraph:delete(G),
Dests.
OrderedDests.

%% For a few known cases, an externally provided plugin can be trusted.
%% In this special case, it overrides the plugin.
Expand Down Expand Up @@ -245,19 +253,100 @@ prepare_plugins(Enabled) ->
AllPlugins = list(PluginsDistDir),
Wanted = dependencies(false, Enabled, AllPlugins),
WantedPlugins = lookup_plugins(Wanted, AllPlugins),

{ValidPlugins, Problems} = validate_plugins(WantedPlugins),
%TODO: error message formatting
Copy link
Member

Choose a reason for hiding this comment

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

Should we address this?

rabbit_log:warning(format_invalid_plugins(Problems)),
case filelib:ensure_dir(ExpandDir ++ "/") of
ok -> ok;
{error, E2} -> throw({error, {cannot_create_plugins_expand_dir,
[ExpandDir, E2]}})
end,

[prepare_plugin(Plugin, ExpandDir) || Plugin <- WantedPlugins],
[prepare_plugin(Plugin, ExpandDir) || Plugin <- ValidPlugins],

[prepare_dir_plugin(PluginAppDescPath) ||
PluginAppDescPath <- filelib:wildcard(ExpandDir ++ "/*/ebin/*.app")],
Wanted.

format_invalid_plugins(InvalidPlugins) ->
lists:flatten(["Failed to enable some plugins: \r\n"
| [format_invalid_plugin(Plugin)
|| Plugin <- InvalidPlugins]]).

format_invalid_plugin({Name, Errors}) ->
[io_lib:format(" ~p:~n", [Name])
| [format_invalid_plugin_error(Err) || Err <- Errors]].

format_invalid_plugin_error({missing_dependency, Dep}) ->
io_lib:format(" Dependency is missing or invalid: ~p~n", [Dep]);
format_invalid_plugin_error({version_mismatch, {Version, Required}}) ->
io_lib:format(" Broker version is invalid."
" Current version: ~p Required: ~p~n", [Version, Required]);
format_invalid_plugin_error({{version_mismatch, {Version, Required}}, Name}) ->
io_lib:format(" ~p plugin version is invalid."
" Current version: ~p Required: ~p~n",
[Name, Version, Required]);
format_invalid_plugin_error(Err) ->
io_lib:format(" Unknown error ~p~n", [Err]).

validate_plugins(Plugins) ->
application:load(rabbit),
RabbitVersion = RabbitVersion = case application:get_key(rabbit, vsn) of
undefined -> "0.0.0";
{ok, Val} -> Val
end,
validate_plugins(Plugins, RabbitVersion).

validate_plugins(Plugins, RabbitVersion) ->
lists:foldl(
fun(#plugin{name = Name,
rabbitmq_versions = RabbitmqVersions,
plugins_versions = PluginsVersions} = Plugin,
{Plugins0, Errors}) ->
case version_support(RabbitVersion, RabbitmqVersions) of
{error, Err} -> {Plugins0, [{Name, [Err]} | Errors]};
ok ->
case check_plugins_versions(Plugins0, PluginsVersions) of
ok -> {[Plugin | Plugins0], Errors};
{error, Err} -> {Plugins0, [{Name, Err} | Errors]}
end
end
end,
{[],[]},
Plugins).

check_plugins_versions(AllPlugins, RequiredVersions) ->
ExistingVersions = [{Name, Vsn}
|| #plugin{name = Name, version = Vsn} <- AllPlugins],
Problems = lists:foldl(
fun({Name, Versions}, Acc) ->
case proplists:get_value(Name, ExistingVersions) of
undefined -> [{missing_dependency, Name} | Acc];
Version ->
case version_support(Version, Versions) of
{error, Err} -> [{Err, Name} | Acc];
ok -> Acc
end
end
end,
[],
RequiredVersions),
case Problems of
[] -> ok;
_ -> {error, Problems}
end.

version_support(_Version, []) -> ok;
version_support(Version, ExpectedVersions) ->
Copy link
Member

Choose a reason for hiding this comment

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

I'd recommend renaming this to is_version_supported or check_version_compatibility.

case lists:any(fun(ExpectedVersion) ->
rabbit_misc:version_minor_equivalent(ExpectedVersion, Version)
andalso
rabbit_misc:version_compare(ExpectedVersion, Version, lte)
end,
ExpectedVersions) of
true -> ok;
false -> {error, {version_mismatch, {Version, ExpectedVersions}}}
end.

clean_plugins(Plugins) ->
{ok, ExpandDir} = application:get_env(rabbit, plugins_expand_dir),
[clean_plugin(Plugin, ExpandDir) || Plugin <- Plugins].
Expand Down Expand Up @@ -330,8 +419,12 @@ mkplugin(Name, Props, Type, Location) ->
Version = proplists:get_value(vsn, Props, "0"),
Description = proplists:get_value(description, Props, ""),
Dependencies = proplists:get_value(applications, Props, []),
RabbitmqVersions = proplists:get_value(rabbitmq_versions, Props, []),
PluginsVersions = proplists:get_value(plugins_versions, Props, []),
#plugin{name = Name, version = Version, description = Description,
dependencies = Dependencies, location = Location, type = Type}.
dependencies = Dependencies, location = Location, type = Type,
rabbitmq_versions = RabbitmqVersions,
plugins_versions = PluginsVersions}.

read_app_file(EZ) ->
case zip:list_dir(EZ) of
Expand Down Expand Up @@ -366,4 +459,9 @@ plugin_names(Plugins) ->
[Name || #plugin{name = Name} <- Plugins].

lookup_plugins(Names, AllPlugins) ->
[P || P = #plugin{name = Name} <- AllPlugins, lists:member(Name, Names)].
% Preserve order of Names
lists:map(
fun(Name) ->
lists:keyfind(Name, #plugin.name, AllPlugins)
end,
Names).
22 changes: 22 additions & 0 deletions src/rabbit_plugins_main.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ action(enable, Node, ToEnable0, Opts, State = #cli{all = All,
_ -> throw({error_string, fmt_missing(Missing)})
end,
NewEnabled = lists:usort(Enabled ++ ToEnable),
Invalid = validate_plugins(NewEnabled, State),
case Invalid of
[] -> ok;
_ -> throw({error_string,
rabbit_plugins:format_invalid_plugins(Invalid)})
end,
NewImplicit = write_enabled_plugins(NewEnabled, State),
case NewEnabled -- Implicit of
[] -> io:format("Plugin configuration unchanged.~n");
Expand All @@ -115,6 +121,12 @@ action(set, Node, NewEnabled0, Opts, State = #cli{all = All,
[] -> ok;
_ -> throw({error_string, fmt_missing(Missing)})
end,
Invalid = validate_plugins(NewEnabled, State),
case Invalid of
[] -> ok;
_ -> throw({error_string,
rabbit_plugins:format_invalid_plugins(Invalid)})
end,
NewImplicit = write_enabled_plugins(NewEnabled, State),
case NewImplicit of
[] -> io:format("All plugins are now disabled.~n");
Expand Down Expand Up @@ -155,6 +167,16 @@ action(help, _Node, _Args, _Opts, _State) ->

%%----------------------------------------------------------------------------

validate_plugins(Names, #cli{all = All}) ->
Deps = rabbit_plugins:dependencies(false, Names, All),
DepsPlugins = lists:map(
fun(Name) ->
lists:keyfind(Name, #plugin.name, All)
end,
Deps),
{_, Errors} = rabbit_plugins:validate_plugins(DepsPlugins),
Errors.

%% Pretty print a list of plugins.
format_plugins(Node, Pattern, Opts, #cli{all = All,
enabled = Enabled,
Expand Down