Annotate

mod_persisthosts/mod_persisthosts.lua @ 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
child 2447:366fadb5c6e5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2446
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- mod_persisthosts
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 module:set_global();
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local set = require"util.set";
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local stat = require"lfs".attributes;
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local resolve_relative_path = require"core.configmanager".resolve_relative_path;
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local vhost_path = module:get_option_string("persisthosts_path", "conf.d");
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local path_pattern = resolve_relative_path(prosody.paths.config, vhost_path) .. "/%s.cfg.lua";
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local original = set.new();
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 original:include(prosody.hosts);
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 module:hook("host-activated", function(host)
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 if not original:contains(host) then
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local path = path_pattern:format(host);
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 if not stat(path) then
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local fh, err = io.open(path, "w");
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 if fh then
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 fh:write(("VirtualHost%q\n"):format(host));
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 fh:close();
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 module:log("info", "Config file for host '%s' created", host);
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 else
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 module:log("error", "Could not open '%s' for writing: %s", path, err or "duno");
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 end
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 else
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 module:log("debug", "File '%s' existed already", path);
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 else
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 module:log("debug", "VirtualHost '%s' existed already", host);
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 end
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 end);
c563f4d64302 mod_persisthosts: Module that dynamically creates stub configuration files for dynamically activated hosts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33