|
|
15862b |
From 1f57e26816d8148e0c77ff7573457b8d2599bf8b Mon Sep 17 00:00:00 2001
|
|
|
15862b |
From: Peter Lemenkov <lemenkov@redhat.com>
|
|
|
15862b |
Date: Wed, 5 Apr 2017 19:12:26 +0200
|
|
|
15862b |
Subject: [PATCH] Backup and restore policies
|
|
|
15862b |
|
|
|
15862b |
- Backup and restore policies.
|
|
|
15862b |
- Simplify code
|
|
|
15862b |
|
|
|
15862b |
Signed-off-by: Peter Lemenkov <lemenkov@redhat.com>
|
|
|
15862b |
---
|
|
|
15862b |
heartbeat/rabbitmq-cluster | 123 ++++++++++++++++++++++++---------------------
|
|
|
15862b |
1 file changed, 66 insertions(+), 57 deletions(-)
|
|
|
15862b |
|
|
|
15862b |
diff --git a/heartbeat/rabbitmq-cluster b/heartbeat/rabbitmq-cluster
|
|
|
15862b |
index 6a17590..b5060b1 100755
|
|
|
15862b |
--- a/heartbeat/rabbitmq-cluster
|
|
|
15862b |
+++ b/heartbeat/rabbitmq-cluster
|
|
|
15862b |
@@ -388,83 +388,92 @@ rmq_start() {
|
|
|
15862b |
return $OCF_ERR_GENERIC
|
|
|
15862b |
fi
|
|
|
15862b |
|
|
|
15862b |
- # Restore users and users' permissions (if any)
|
|
|
15862b |
+ # Restore users, user permissions, and policies (if any)
|
|
|
15862b |
BaseDataDir=`dirname $RMQ_DATA_DIR`
|
|
|
15862b |
- if [ -f $BaseDataDir/users.erl ] ; then
|
|
|
15862b |
- rabbitmqctl eval "
|
|
|
15862b |
- %% Run only if Mnesia is ready.
|
|
|
15862b |
- lists:any(fun({mnesia,_,_}) -> true; ({_,_,_}) -> false end, application:which_applications()) andalso
|
|
|
15862b |
- begin
|
|
|
15862b |
- [WildPattern] = ets:select(mnesia_gvar, [ { {{rabbit_user, wild_pattern}, '\\\$1'}, [], ['\\\$1'] } ]),
|
|
|
15862b |
-
|
|
|
15862b |
- %% Read users first
|
|
|
15862b |
- {ok, [Users]} = file:consult(\"$BaseDataDir/users.erl\"),
|
|
|
15862b |
-
|
|
|
15862b |
- Upgrade = fun
|
|
|
15862b |
- ({internal_user, A, B, C}) -> {internal_user, A, B, C, rabbit_password_hashing_md5};
|
|
|
15862b |
- ({internal_user, A, B, C, D}) -> {internal_user, A, B, C, D}
|
|
|
15862b |
- end,
|
|
|
15862b |
+ rabbitmqctl eval "
|
|
|
15862b |
+ %% Run only if Mnesia is ready.
|
|
|
15862b |
+ lists:any(fun({mnesia,_,_}) -> true; ({_,_,_}) -> false end, application:which_applications()) andalso
|
|
|
15862b |
+ begin
|
|
|
15862b |
+ Restore = fun(Table, PostprocessFun, Filename) ->
|
|
|
15862b |
+ case file:consult(Filename) of
|
|
|
15862b |
+ {error, _} ->
|
|
|
15862b |
+ ok;
|
|
|
15862b |
+ {ok, [Result]) ->
|
|
|
15862b |
+ lists:foreach(fun(X) -> mnesia:dirty_write(Table, PostprocessFun(X)) end, Result),
|
|
|
15862b |
+ file:delete(Filename)
|
|
|
15862b |
+ end
|
|
|
15862b |
+ end,
|
|
|
15862b |
|
|
|
15862b |
- Downgrade = fun
|
|
|
15862b |
- ({internal_user, A, B, C}) -> {internal_user, A, B, C};
|
|
|
15862b |
- ({internal_user, A, B, C, rabbit_password_hashing_md5}) -> {internal_user, A, B, C};
|
|
|
15862b |
- %% Incompatible scheme, so we will loose user's password ('B' value) during conversion.
|
|
|
15862b |
- %% Unfortunately, this case will require manual intervention - user have to run:
|
|
|
15862b |
- %% rabbitmqctl change_password <somenewpassword>
|
|
|
15862b |
- ({internal_user, A, B, C, _}) -> {internal_user, A, B, C}
|
|
|
15862b |
- end,
|
|
|
15862b |
+ %% Restore users
|
|
|
15862b |
|
|
|
15862b |
- case WildPattern of
|
|
|
15862b |
- %% Version < 3.6.0
|
|
|
15862b |
- {internal_user,'_','_','_'} ->
|
|
|
15862b |
- lists:foreach(fun(X) -> mnesia:dirty_write(rabbit_user, Downgrade(X)) end, Users);
|
|
|
15862b |
- %% Version >= 3.6.0
|
|
|
15862b |
- {internal_user,'_','_','_','_'} ->
|
|
|
15862b |
- lists:foreach(fun(X) -> mnesia:dirty_write(rabbit_user, Upgrade(X)) end, Users)
|
|
|
15862b |
- end,
|
|
|
15862b |
+ Upgrade = fun
|
|
|
15862b |
+ ({internal_user, A, B, C}) -> {internal_user, A, B, C, rabbit_password_hashing_md5};
|
|
|
15862b |
+ ({internal_user, A, B, C, D}) -> {internal_user, A, B, C, D}
|
|
|
15862b |
+ end,
|
|
|
15862b |
|
|
|
15862b |
- ok = file:delete(\"$BaseDataDir/users.erl\")
|
|
|
15862b |
- end.
|
|
|
15862b |
- "
|
|
|
15862b |
- fi
|
|
|
15862b |
- if [ -f $BaseDataDir/users_perms.erl ] ; then
|
|
|
15862b |
- rabbitmqctl eval "
|
|
|
15862b |
- %% Run only if Mnesia is ready.
|
|
|
15862b |
- lists:any(fun({mnesia,_,_}) -> true; ({_,_,_}) -> false end, application:which_applications()) andalso
|
|
|
15862b |
- begin
|
|
|
15862b |
- {ok, [UsersPerms]} = file:consult(\"$BaseDataDir/users_perms.erl\"),
|
|
|
15862b |
- lists:foreach(fun(X) -> mnesia:dirty_write(rabbit_user_permission, X) end, UsersPerms),
|
|
|
15862b |
-
|
|
|
15862b |
- ok = file:delete(\"$BaseDataDir/users_perms.erl\")
|
|
|
15862b |
- end.
|
|
|
15862b |
- "
|
|
|
15862b |
- fi
|
|
|
15862b |
+ Downgrade = fun
|
|
|
15862b |
+ ({internal_user, A, B, C}) -> {internal_user, A, B, C};
|
|
|
15862b |
+ ({internal_user, A, B, C, rabbit_password_hashing_md5}) -> {internal_user, A, B, C};
|
|
|
15862b |
+ %% Incompatible scheme, so we will loose user's password ('B' value) during conversion.
|
|
|
15862b |
+ %% Unfortunately, this case will require manual intervention - user have to run:
|
|
|
15862b |
+ %% rabbitmqctl change_password <somenewpassword>
|
|
|
15862b |
+ ({internal_user, A, B, C, _}) -> {internal_user, A, B, C}
|
|
|
15862b |
+ end,
|
|
|
15862b |
+
|
|
|
15862b |
+ %% Check db scheme first
|
|
|
15862b |
+ [WildPattern] = ets:select(mnesia_gvar, [ { {{rabbit_user, wild_pattern}, '\\\$1'}, [], ['\\\$1'] } ]),
|
|
|
15862b |
+ case WildPattern of
|
|
|
15862b |
+ %% Version < 3.6.0
|
|
|
15862b |
+ {internal_user,'_','_','_'} ->
|
|
|
15862b |
+ Restore(rabbit_user, Downgrade, \"$BaseDataDir/users.erl\");
|
|
|
15862b |
+ %% Version >= 3.6.0
|
|
|
15862b |
+ {internal_user,'_','_','_','_'} ->
|
|
|
15862b |
+ Restore(rabbit_user, Upgrade, \"$BaseDataDir/users.erl\")
|
|
|
15862b |
+ end,
|
|
|
15862b |
|
|
|
15862b |
+ NoOp = fun(X) -> X end,
|
|
|
15862b |
+
|
|
|
15862b |
+ %% Restore user permissions
|
|
|
15862b |
+ Restore(rabbit_user_permission, NoOp, \"$BaseDataDir/users_perms.erl\"),
|
|
|
15862b |
+
|
|
|
15862b |
+ %% Restore policies
|
|
|
15862b |
+ Restore(rabbit_runtime_parameters, NoOp, \"$BaseDataDir/policies.erl\")
|
|
|
15862b |
+ end.
|
|
|
15862b |
+ "
|
|
|
15862b |
return $OCF_SUCCESS
|
|
|
15862b |
}
|
|
|
15862b |
|
|
|
15862b |
rmq_stop() {
|
|
|
15862b |
- # Backup users and users' permissions
|
|
|
15862b |
+ # Backup users, user permissions, and policies
|
|
|
15862b |
BaseDataDir=`dirname $RMQ_DATA_DIR`
|
|
|
15862b |
rabbitmqctl eval "
|
|
|
15862b |
%% Run only if Mnesia is still available.
|
|
|
15862b |
lists:any(fun({mnesia,_,_}) -> true; ({_,_,_}) -> false end, application:which_applications()) andalso
|
|
|
15862b |
begin
|
|
|
15862b |
- [WildPattern] = ets:select(mnesia_gvar, [ { {{rabbit_user, wild_pattern}, '\\\$1'}, [], ['\\\$1'] } ]),
|
|
|
15862b |
+ Backup = fun(Table, SelectPattern, Filter, Filename) ->
|
|
|
15862b |
+ Result = case catch mnesia:dirty_select(Table, [{SelectPattern, [Filter], ['\\\$_']}]) of
|
|
|
15862b |
+ {'EXIT', _} -> [];
|
|
|
15862b |
+ Any -> Any
|
|
|
15862b |
+ end,
|
|
|
15862b |
+ Result /= [] andalso file:write_file(Filename, io_lib:fwrite(\"~p.~n\", [RuntimeParams]))
|
|
|
15862b |
+ end,
|
|
|
15862b |
|
|
|
15862b |
- Users = case WildPattern of
|
|
|
15862b |
+ %% Backup users
|
|
|
15862b |
+ %% Check db scheme first
|
|
|
15862b |
+ [WildPattern] = ets:select(mnesia_gvar, [ { {{rabbit_user, wild_pattern}, '\\\$1'}, [], ['\\\$1'] } ]),
|
|
|
15862b |
+ UsersSelectPattern = case WildPattern of
|
|
|
15862b |
%% Version < 3.6.0
|
|
|
15862b |
- {internal_user,'_','_','_'} ->
|
|
|
15862b |
- mnesia:dirty_select(rabbit_user, [{ {internal_user, '\\\$1', '_', '_'}, [{'/=', '\\\$1', <<\"guest\">>}], ['\\\$_'] } ]);
|
|
|
15862b |
+ {internal_user,'_','_','_'} -> {internal_user, '\\\$1', '_', '_'};
|
|
|
15862b |
%% Version >= 3.6.0
|
|
|
15862b |
- {internal_user,'_','_','_','_'} ->
|
|
|
15862b |
- mnesia:dirty_select(rabbit_user, [{ {internal_user, '\\\$1', '_', '_', '_'}, [{'/=', '\\\$1', <<\"guest\">>}], ['\\\$_'] } ])
|
|
|
15862b |
+ {internal_user,'_','_','_','_'} -> {internal_user, '\\\$1', '_', '_', '_'}
|
|
|
15862b |
end,
|
|
|
15862b |
+ Backup(rabbit_user, UsersSelectPattern, {'/=', '\\\$1', <<\"guest\">>}, \"$BaseDataDir/users.erl\"),
|
|
|
15862b |
|
|
|
15862b |
- Users /= [] andalso file:write_file(\"$BaseDataDir/users.erl\", io_lib:fwrite(\"~p.~n\", [Users])),
|
|
|
15862b |
+ %% Backup user permissions
|
|
|
15862b |
+ Backup(rabbit_user_permission, {'\\\$1', {'\\\$2', '\\\$3','\\\$4'}, '\\\$5'}, {'/=', '\\\$3', <<\"guest\">>}, \"$BaseDataDir/users_perms.erl\"),
|
|
|
15862b |
|
|
|
15862b |
- UsersPerms = mnesia:dirty_select(rabbit_user_permission, [{{'\\\$1', {'\\\$2', '\\\$3','\\\$4'}, '\\\$5'}, [{'/=', '\\\$3', <<\"guest\">>}], ['\\\$_']}]),
|
|
|
15862b |
- UsersPerms /= [] andalso file:write_file(\"$BaseDataDir/users_perms.erl\", io_lib:fwrite(\"~p.~n\", [UsersPerms]))
|
|
|
15862b |
+ %% Backup policies
|
|
|
15862b |
+ Backup(rabbit_runtime_parameters, {runtime_parameters, {'_', '\\\$1', '_'}, '_'}, {'==', '\\\$1', <<\"policy\">>}, \"$BaseDataDir/policies.erl\")
|
|
|
15862b |
end.
|
|
|
15862b |
"
|
|
|
15862b |
|