annotate mod_adhoc_cmd_modules/mod_adhoc_cmd_modules.lua @ 205:a6361a1fda5e

mod_adhoc_cmd_modules: Add "Reload module" command
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 09 Jul 2010 22:29:26 +0200
parents 4927b9737bb7
children d3498f115fcd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
203
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
1 -- Copyright (C) 2009-2010 Florian Zeitz
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
2 --
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
3 -- This file is MIT/X11 licensed. Please see the
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
4 -- COPYING file in the source package for more information.
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
5 --
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
6
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
7 local _G = _G;
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
8
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
9 local prosody = _G.prosody;
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
10 local hosts = prosody.hosts;
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
11
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
12 require "util.iterators";
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
13 local dataforms_new = require "util.dataforms".new;
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
14 local array = require "util.array";
205
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
15 local modulemanager = require "modulemanager";
203
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
16 local adhoc_new = module:require "adhoc".new;
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
17
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
18 function list_modules_handler(self, data, state)
205
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
19 local result = dataforms_new {
203
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
20 title = "List of loaded modules";
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
21
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
22 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
23 { name = "modules", type = "text-multi", label = "The following modules are loaded:" };
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
24 };
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
25
205
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
26 local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n");
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
27
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
28 return { status = "completed", result = { layout = result; data = { modules = modules } } };
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
29 end
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
30
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
31 -- TODO: Allow reloading multiple modules (depends on list-multi
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
32 function reload_modules_handler(self, data, state)
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
33 local modules = {};
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
34 local layout = dataforms_new {
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
35 title = "Reload module";
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
36 instructions = "Select the module to be reloaded";
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
37
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
38 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
39 { name = "module", type = "list-single", value = modules, label = "Module to be reloaded:"};
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
40 };
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
41 if state then
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
42 if data.action == "cancel" then
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
43 return { status = "canceled" };
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
44 end
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
45 fields = layout:data(data.form);
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
46 local ok, err = modulemanager.reload(data.to, fields.module);
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
47 if ok then
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
48 return { status = "completed", info = 'Module "'..fields.module..'" successfully reloaded on host "'..data.to..'".' };
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
49 else
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
50 return { status = "completed", error = 'Failed to reload module "'..fields.module..'" on host "'..data.to..
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
51 '". Error was: "'..tostring(err)..'"' };
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
52 end
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
53 else
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
54 local modules2 = array.collect(keys(hosts[data.to].modules)):sort();
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
55 for i, val in ipairs(modules2) do
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
56 modules[i] = val;
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
57 end
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
58 return { status = "executing", form = layout }, "executing";
203
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
59 end
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
60 end
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
61
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
62 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
205
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
63 local reload_modules_desc = adhoc_new("Reload module", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
203
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
64
4927b9737bb7 mod_adhoc_cmd_modules: Initial version. Provides a "List loaded modules" cmd for now
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
65 module:add_item("adhoc", list_modules_desc);
205
a6361a1fda5e mod_adhoc_cmd_modules: Add "Reload module" command
Florian Zeitz <florob@babelmonkeys.de>
parents: 203
diff changeset
66 module:add_item("adhoc", reload_modules_desc);