25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-26 16:26:24 +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,
reopen_log/0, rotate_log/0,
set_loglevel/1,
evacuate_kindly/2,
stop_kindly/2, send_service_message_all_mucs/2,
registered_vhosts/0,
reload_config/0,
@ -166,9 +167,24 @@ get_commands_spec() ->
"only on log files generated by some modules.",
module = ?MODULE, function = rotate_log,
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],
desc = "Inform users and rooms, wait, and stop the server",
longdesc = "Provide the delay in seconds, and the "
desc = "Stop kindly the server (informing users)",
longdesc = "Inform users and rooms, wait, and stop the server.\n"
"Provide the delay in seconds, and the "
"announcement quoted, for example: \n"
"`ejabberdctl stop_kindly 60 "
"\\\"The server will stop in one minute.\\\"`",
@ -672,39 +688,49 @@ set_loglevel(LogLevel) ->
%%% Stop Kindly
%%%
evacuate_kindly(DelaySeconds, AnnouncementTextString) ->
perform_kindly(DelaySeconds, AnnouncementTextString, evacuate).
stop_kindly(DelaySeconds, AnnouncementTextString) ->
Subject = (str:format("Server stop in ~p seconds!", [DelaySeconds])),
WaitingDesc = (str:format("Waiting ~p seconds", [DelaySeconds])),
perform_kindly(DelaySeconds, AnnouncementTextString, stop).
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),
Steps = [
{"Stopping ejabberd port listeners",
ejabberd_listener, stop_listeners, []},
{"Sending announcement to connected users",
mod_announce, send_announcement_to_all,
[ejabberd_config:get_myname(), Subject, AnnouncementText]},
{"Sending service message to MUC rooms",
ejabberd_admin, send_service_message_all_mucs,
[Subject, AnnouncementText]},
{WaitingDesc, timer, sleep, [DelaySeconds * 1000]},
{"Stopping ejabberd", application, stop, [ejabberd]},
{"Stopping Mnesia", mnesia, stop, []},
{"Stopping Erlang node", init, stop, []}
],
PreSteps =
[{"Stopping ejabberd port listeners", ejabberd_listener, stop_listeners, []},
{"Sending announcement to connected users",
mod_announce,
send_announcement_to_all,
[ejabberd_config:get_myname(), Subject, AnnouncementText]},
{"Sending service message to MUC rooms",
ejabberd_admin,
send_service_message_all_mucs,
[Subject, AnnouncementText]},
{WaitingDesc, timer, sleep, [DelaySeconds * 1000]},
{"Stopping ejabberd", application, stop, [ejabberd]}],
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),
TimestampStart = calendar:datetime_to_gregorian_seconds({date(), time()}),
lists:foldl(
fun({Desc, Mod, Func, Args}, NumberThis) ->
SecondsDiff =
calendar:datetime_to_gregorian_seconds({date(), time()})
- TimestampStart,
io:format("[~p/~p ~ps] ~ts... ",
[NumberThis, NumberLast, SecondsDiff, Desc]),
Result = (catch apply(Mod, Func, Args)),
io:format("~p~n", [Result]),
NumberThis+1
end,
1,
Steps),
lists:foldl(fun({Desc, Mod, Func, Args}, NumberThis) ->
SecondsDiff =
calendar:datetime_to_gregorian_seconds({date(), time()}) - TimestampStart,
io:format("[~p/~p ~ps] ~ts... ", [NumberThis, NumberLast, SecondsDiff, Desc]),
Result = (catch apply(Mod, Func, Args)),
io:format("~p~n", [Result]),
NumberThis + 1
end,
1,
Steps),
ok.
send_service_message_all_mucs(Subject, AnnouncementText) ->