Diff

plugins/mod_admin_adhoc.lua @ 5328:5e15e6700412

mod_admin_adhoc: Implement global module unloading
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 07 Feb 2013 21:14:01 +0100
parent 5327:b0c36b043f00
child 5329:9fffd5fad4b3
line wrap: on
line diff
--- a/plugins/mod_admin_adhoc.lua	Thu Feb 07 00:40:00 2013 +0100
+++ b/plugins/mod_admin_adhoc.lua	Thu Feb 07 21:14:01 2013 +0100
@@ -714,6 +714,68 @@
 	end
 end
 
+local function globally_unload_module_handler(self, data, state)
+	local layout = dataforms_new {
+		title = "Globally unload module";
+		instructions = "Specify a module to unload on all hosts";
+
+		{ name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-unload" };
+		{ name = "module", type = "list-single", required = true, label = "Module to globally unload:"};
+	};
+	if state then
+		if data.action == "cancel" then
+			return { status = "canceled" };
+		end
+
+		local is_global = false;
+		local fields, err = layout:data(data.form);
+		if err then
+			return generate_error_message(err);
+		end
+
+		if modulemanager.is_loaded("*", fields.module) then
+			local ok, err = modulemanager.unload("*", fields.module);
+			if not ok then
+				return { status = "completed", info = 'Global module '..fields.module..' failed to unload: '..err };
+			end
+			is_global = true;
+		end
+
+		local ok_list, err_list = {}, {};
+		for host_name, host in pairs(hosts) do
+			if modulemanager.is_loaded(host_name, fields.module)  then
+				local ok, err = modulemanager.unload(host_name, fields.module);
+				if ok then
+					ok_list[#ok_list + 1] = host_name;
+				else
+					err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
+				end
+			end
+		end
+
+		if #ok_list == 0 and #err_list == 0 then
+			if is_global then
+				return { status = "completed", info = 'Successfully unloaded global module '..fields.module };
+			else
+				return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
+			end
+		end
+
+		local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully unloaded on the hosts:\n"..t_concat(ok_list, "\n")) or "")
+			.. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
+			(#err_list > 0 and ("Failed to unload the module "..fields.module.." on the hosts:\n"..t_concat(err_list, "\n")) or "");
+		return { status = "completed", info = info };
+	else
+		local loaded_modules = array(keys(modulemanager.get_modules("*")));
+		for _, host in pairs(hosts) do
+			loaded_modules:append(array(keys(host.modules)));
+		end
+		loaded_modules = array(keys(set.new(loaded_modules):items())):sort();
+		return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout, values = { module = loaded_modules } } }, "executing";
+	end
+end
+
+
 function activate_host_handler(self, data, state)
 	local layout = dataforms_new {
 		title = "Activate host";
@@ -787,6 +849,7 @@
 local globally_reload_module_desc = adhoc_new("Globally reload module", "http://prosody.im/protocol/modules#global-reload", globally_reload_module_handler, "global_admin");
 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
+local globally_unload_module_desc = adhoc_new("Globally unload module", "http://prosody.im/protocol/modules#global-unload", globally_unload_module_handler, "global_admin");
 local activate_host_desc = adhoc_new("Activate host", "http://prosody.im/protocol/hosts#activate", activate_host_handler, "global_admin");
 local deactivate_host_desc = adhoc_new("Deactivate host", "http://prosody.im/protocol/hosts#deactivate", deactivate_host_handler, "global_admin");
 
@@ -806,5 +869,6 @@
 module:provides("adhoc", globally_reload_module_desc);
 module:provides("adhoc", shut_down_service_desc);
 module:provides("adhoc", unload_modules_desc);
+module:provides("adhoc", globally_unload_module_desc);
 module:provides("adhoc", activate_host_desc);
 module:provides("adhoc", deactivate_host_desc);