Changeset

2446:c563f4d64302

mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
author Kim Alvefur <zash@zash.se>
date Wed, 18 Jan 2017 01:38:17 +0100
parents 2445:e822900c87d4
children 2447:366fadb5c6e5
files mod_persisthosts/README.markdown mod_persisthosts/mod_persisthosts.lua
diffstat 2 files changed, 47 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_persisthosts/README.markdown	Wed Jan 18 01:38:17 2017 +0100
@@ -0,0 +1,14 @@
+Introduction
+============
+
+This module creates stub configuration files for newly activated hosts.
+
+Configuration
+=============
+
+A single option exists, `persisthosts_path`, which is the path where new
+stub configuration files are created. It defaults to `"conf.d"`, and is
+treated as relative to the configuration directiory [^1] unless set to
+an absolute path.
+
+[^1]: usually \`/etc/prosody on \*nix systems
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_persisthosts/mod_persisthosts.lua	Wed Jan 18 01:38:17 2017 +0100
@@ -0,0 +1,33 @@
+-- mod_persisthosts
+module:set_global();
+
+local set = require"util.set";
+local stat = require"lfs".attributes;
+local resolve_relative_path = require"core.configmanager".resolve_relative_path;
+
+local vhost_path = module:get_option_string("persisthosts_path", "conf.d");
+local path_pattern = resolve_relative_path(prosody.paths.config, vhost_path) .. "/%s.cfg.lua";
+
+local original = set.new();
+original:include(prosody.hosts);
+
+module:hook("host-activated", function(host)
+	if not original:contains(host) then
+		local path = path_pattern:format(host);
+		if not stat(path) then
+			local fh, err = io.open(path, "w");
+			if fh then
+				fh:write(("VirtualHost%q\n"):format(host));
+				fh:close();
+				module:log("info", "Config file for host '%s' created", host);
+			else
+				module:log("error", "Could not open '%s' for writing: %s", path, err or "duno");
+			end
+		else
+			module:log("debug", "File '%s' existed already", path);
+		end
+	else
+		module:log("debug", "VirtualHost '%s' existed already", host);
+	end
+end);
+