25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

New evacuate_kindly command: kick users and prevent login (#4309)

This commit is contained in:
Badlop 2024-11-13 12:23:44 +01:00
parent 9eb8bb6c40
commit 07e20784cb

View File

@ -33,6 +33,7 @@
status/0, stop/0, restart/0, status/0, stop/0, restart/0,
reopen_log/0, rotate_log/0, reopen_log/0, rotate_log/0,
set_loglevel/1, set_loglevel/1,
evacuate_kindly/2,
stop_kindly/2, send_service_message_all_mucs/2, stop_kindly/2, send_service_message_all_mucs/2,
registered_vhosts/0, registered_vhosts/0,
reload_config/0, reload_config/0,
@ -166,9 +167,24 @@ get_commands_spec() ->
"only on log files generated by some modules.", "only on log files generated by some modules.",
module = ?MODULE, function = rotate_log, module = ?MODULE, function = rotate_log,
args = [], result = {res, rescode}}, args = [], result = {res, rescode}},
#ejabberd_commands{name = evacuate_kindly, tags = [server],
desc = "Evacuate kindly all users (kick and prevent login)",
longdesc = "Inform users and rooms, don't allow login, wait, "
"restart the server, and don't allow new logins.\n"
"Provide the delay in seconds, and the "
"announcement quoted, for example: \n"
"`ejabberdctl evacuate_kindly 60 "
"\\\"The server will stop in one minute.\\\"`",
note = "added in 24.xx",
module = ?MODULE, function = evacuate_kindly,
args_desc = ["Seconds to wait", "Announcement to send, with quotes"],
args_example = [60, <<"Server will stop now.">>],
args = [{delay, integer}, {announcement, string}],
result = {res, rescode}},
#ejabberd_commands{name = stop_kindly, tags = [server], #ejabberd_commands{name = stop_kindly, tags = [server],
desc = "Inform users and rooms, wait, and stop the server", desc = "Stop kindly the server (informing users)",
longdesc = "Provide the delay in seconds, and the " longdesc = "Inform users and rooms, wait, and stop the server.\n"
"Provide the delay in seconds, and the "
"announcement quoted, for example: \n" "announcement quoted, for example: \n"
"`ejabberdctl stop_kindly 60 " "`ejabberdctl stop_kindly 60 "
"\\\"The server will stop in one minute.\\\"`", "\\\"The server will stop in one minute.\\\"`",
@ -672,39 +688,49 @@ set_loglevel(LogLevel) ->
%%% Stop Kindly %%% Stop Kindly
%%% %%%
evacuate_kindly(DelaySeconds, AnnouncementTextString) ->
perform_kindly(DelaySeconds, AnnouncementTextString, evacuate).
stop_kindly(DelaySeconds, AnnouncementTextString) -> stop_kindly(DelaySeconds, AnnouncementTextString) ->
Subject = (str:format("Server stop in ~p seconds!", [DelaySeconds])), perform_kindly(DelaySeconds, AnnouncementTextString, stop).
WaitingDesc = (str:format("Waiting ~p seconds", [DelaySeconds])),
perform_kindly(DelaySeconds, AnnouncementTextString, Action) ->
Subject = str:format("Server stop in ~p seconds!", [DelaySeconds]),
WaitingDesc = str:format("Waiting ~p seconds", [DelaySeconds]),
AnnouncementText = list_to_binary(AnnouncementTextString), AnnouncementText = list_to_binary(AnnouncementTextString),
Steps = [ PreSteps =
{"Stopping ejabberd port listeners", [{"Stopping ejabberd port listeners", ejabberd_listener, stop_listeners, []},
ejabberd_listener, stop_listeners, []}, {"Sending announcement to connected users",
{"Sending announcement to connected users", mod_announce,
mod_announce, send_announcement_to_all, send_announcement_to_all,
[ejabberd_config:get_myname(), Subject, AnnouncementText]}, [ejabberd_config:get_myname(), Subject, AnnouncementText]},
{"Sending service message to MUC rooms", {"Sending service message to MUC rooms",
ejabberd_admin, send_service_message_all_mucs, ejabberd_admin,
[Subject, AnnouncementText]}, send_service_message_all_mucs,
{WaitingDesc, timer, sleep, [DelaySeconds * 1000]}, [Subject, AnnouncementText]},
{"Stopping ejabberd", application, stop, [ejabberd]}, {WaitingDesc, timer, sleep, [DelaySeconds * 1000]},
{"Stopping Mnesia", mnesia, stop, []}, {"Stopping ejabberd", application, stop, [ejabberd]}],
{"Stopping Erlang node", init, stop, []} SpecificSteps =
], case Action of
evacuate ->
[{"Starting ejabberd", application, start, [ejabberd]},
{"Stopping ejabberd port listeners", ejabberd_listener, stop_listeners, []}];
stop ->
[{"Stopping Mnesia", mnesia, stop, []}, {"Stopping Erlang node", init, stop, []}]
end,
Steps = PreSteps ++ SpecificSteps,
NumberLast = length(Steps), NumberLast = length(Steps),
TimestampStart = calendar:datetime_to_gregorian_seconds({date(), time()}), TimestampStart = calendar:datetime_to_gregorian_seconds({date(), time()}),
lists:foldl( lists:foldl(fun({Desc, Mod, Func, Args}, NumberThis) ->
fun({Desc, Mod, Func, Args}, NumberThis) -> SecondsDiff =
SecondsDiff = calendar:datetime_to_gregorian_seconds({date(), time()}) - TimestampStart,
calendar:datetime_to_gregorian_seconds({date(), time()}) io:format("[~p/~p ~ps] ~ts... ", [NumberThis, NumberLast, SecondsDiff, Desc]),
- TimestampStart, Result = (catch apply(Mod, Func, Args)),
io:format("[~p/~p ~ps] ~ts... ", io:format("~p~n", [Result]),
[NumberThis, NumberLast, SecondsDiff, Desc]), NumberThis + 1
Result = (catch apply(Mod, Func, Args)), end,
io:format("~p~n", [Result]), 1,
NumberThis+1 Steps),
end,
1,
Steps),
ok. ok.
send_service_message_all_mucs(Subject, AnnouncementText) -> send_service_message_all_mucs(Subject, AnnouncementText) ->