Changeset

2391:85d04dd87f14

mod_reload_components: add new module and README file.
author Camilo <camilo@camilo.fm>
date Mon, 21 Nov 2016 16:18:32 -0200
parents 2390:28fbe960adcf
children 2392:d1e975c24545
files mod_reload_components/README.markdown mod_reload_components/mod_reload_components.lua
diffstat 2 files changed, 131 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_reload_components/README.markdown	Mon Nov 21 16:18:32 2016 -0200
@@ -0,0 +1,91 @@
+Introduction
+============
+
+This module allows to load/unload external components after they have
+been added/removed to a configuration file. It is necessary to explicitly
+initiate a reload on Prosody either via prosodyctl reload or config:reload().
+
+Example 1:
+--------
+If Prosody has started with this configuration file:
+
+``` {.lua}
+VirtualHost "example.com"
+    authentication = "internal_plain"
+
+Component "a.example.com"
+    component_secret = "a"
+
+Component "b.example.com"
+    component_secret = "b"
+```
+
+And the file has changed manually or dynamically to:
+
+``` {.lua}
+VirtualHost "example.com"
+    authentication = "internal_plain"
+
+Component "a.example.com"
+    component_secret = "a"
+
+Component "c.example.com"
+    component_secret = "c"
+```
+
+Then, the following actions will occur if this module is loaded:
+
+1. The component c.example.com will be loaded and start bouncing for
+authentication.
+2. The component b.example.com will be unloaded and deactivated. The
+connection with it will not be closed, but no further actions will be
+executed on Prosody.
+
+Example 2:
+--------
+
+If Prosody has started with this configuration file:
+
+``` {.lua}
+VirtualHost "example.com"
+    authentication = "internal_plain"
+
+Component "a.example.com"
+    component_secret = "a"
+```
+
+And the file has changed manually or dynamically to:
+
+``` {.lua}
+VirtualHost "example.com"
+    authentication = "internal_plain"
+
+Component "a.example.com"
+    component_secret = "a"
+
+VirtualHost "newexample.com"
+        authentication = "internal_plain"
+
+Component "a.newexample.com"
+    component_secret = "a"
+```
+
+Then, the following actions will occur if this module is loaded:
+
+1. The component a.newexample.com will be loaded and start bouncing for
+authentication. Note that its respective VirtualHost is not loaded. Bad
+things may happen.
+
+Usage
+=====
+
+Copy the module folder into your Prosody modules directory. Place the
+module between your enabled modules either into the global or a vhost
+section.
+
+No configuration directives are needed
+
+Info
+====
+
+-   0.9, works
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_reload_components/mod_reload_components.lua	Mon Nov 21 16:18:32 2016 -0200
@@ -0,0 +1,40 @@
+module:set_global();
+
+local configmanager = require "core.configmanager";
+local hostmanager = require"core.hostmanager";
+
+local function reload_components()
+
+        --- Check if host configuration is a component
+        --- @param h hostname
+        local function config_is_component(h)
+            return h ~= nil and configmanager.get(h, "component_module") ~= nil; -- If a host has a component module defined within it, then it is a component
+        end;
+
+        --- Check if host / component configuration is active
+        --- @param h hostname / component name
+        local function component_is_new(h)
+                return h ~= "*" and not hosts[h]; -- If a host is not defined in hosts and it is not global, then it is new
+        end
+
+        --- Search for new components that are not activated
+        for h, c in pairs(configmanager.getconfig()) do
+            if config_is_component(h) and component_is_new(h) then
+                module:log ("debug", "Loading new component %s", h );
+                hostmanager.activate(h, c);
+            end
+        end
+
+        --- Search for active components that are not enabled in the configmanager anymore
+        local enabled = {}
+        for h in pairs(configmanager.getconfig()) do
+            enabled[h] = true; -- Set true if it is defined in the configuration file
+        end
+        for h, c in pairs(hosts) do
+            if not enabled[h] then -- Deactivate if not present in the configuration file
+                hostmanager.deactivate(h,c);
+            end
+        end
+end
+
+module:hook("config-reloaded", reload_components);