Skip to content
Snippets Groups Projects
Commit bdce45ad authored by Thomas Bellman's avatar Thomas Bellman
Browse files

New definition apache::named_vhost.

This definition manages named virtual hosts and their configuration.
parent 6173383e
No related branches found
No related tags found
No related merge requests found
# Copyright (C) 2014 Thomas Bellman.
# Licensed under the GNU LGPL v3+; see the README file for more information.
import "nsc-puppet-utils"
/*
* Configure a named virtual host in Apache httpd.
*
* Parameters:
*
* - urls
* Specifies one or more addresses that this virtual host handles, i.e.
* which 'Listen' directives apply to it, on the format of URLs, in the
* same format as apache::listen does. Listen directives must however
* be configured separately; this definition does not do that for you.
* This parameter can be either a single URL, or a list of URLs.
*
* - servernames
* Which hostnames in URLs this virtual host applies to. Translates
* into 'ServerName' and 'ServerAlias' directives. Defaults to the
* hostnames (before resolving) in the 'urls' parameter if not specified.
*
* - documentroot
* The document root for the virtual host. Optional (Apache will by
* default inherit this setting from the server-global setting).
*
* - config
* Configuration directives to put inside the virtual host declaration.
* A string, or list of strings that will be concatenated with newlines
* between them. This will be surrounded by a '<VirtualHost>' container
* with 'NameVirtualHost' directives, and 'ServerName', 'ServerAlias'
* and optionally 'DocumentRoot' directives inside.
*
* - ensure
* One of 'present' (the default) or 'absent'.
*/
# Note: it might be tempting to let $urls default to "http://${name}/",
# but we do not want to guess the protocol, or imply a preference of
# e.g. http over https.
define apache::named_vhost($urls, $servernames=[],
$documentroot='', $config=undef,
$ensure='present')
{
include apache
case $ensure
{
'present': {
apache::include_file {
"vhost-${name}":
content => template('apache/named_vhost.conf.erb'),
ensure => present;
}
}
'absent': {
apache::include_file {
"vhost-${name}":
ensure => absent;
}
}
default: {
fail("Apache::Vhost[${title}]: Bad parameter ensure: ${ensure}")
}
}
}
<%
server_names = []
server_addrs = []
[@urls].flatten.sort.each { |u|
u =~ /^([a-z]*):\/\/([^:\/]+|\[[^\[\]]+\])(:([0-9]+))?\/?$/
(schema,host,port) = $1,$2,$4
if host == ''
host = @name
end
if host == '*'
addr = host
elsif host =~ /^\[[0-9a-f:]*:[0-9a-f:]*\]$/
addr = host # IPv6 numeric address "[0000:1111::eeee:ffff]"
elsif host =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/
addr = host # IPv4 numeric address "111.222.333.444"
elsif host =~ /^\[(.+)\]$/
# Hostname within brackets "[www.example.com]", not to be resolved
host = addr = $1
else
addr = scope.function_resolve_ipnets([host, 'failerrors'])
end
if port == nil
if schema == 'http'
port = '80'
elsif schema == 'https'
port = '443'
elsif schema == 'ftp'
port = '21'
else
raise(Puppet::Error, "Unknown URL schema and no port: #{u}")
end
end
server_names << host + ':' + port
server_addrs << addr + ':' + port
}
if @servernames != []
server_names = @servernames
end
server_addrs.sort!.uniq!
-%>
<%
## cfg = []
## cfg << 'ServerName ' + server_names[0]
## cfg += server_names[1..-1].collect { |aliasname|
## 'ServerAlias ' + aliasname
## }
## if @documentroot && @documentroot != ''
## cfg << 'DocumentRoot ' + @documentroot
## end
-%>
<% server_addrs.each do |addr| -%>
NameVirtualHost <%= addr %>
<% end -%>
<VirtualHost <%= server_addrs.join(' ') %>>
ServerName <%= server_names[0] %>
<% server_names[1..-1].each do |aliasname| -%>
ServerAlias <%= aliasname %>
<% end -%>
<% if @documentroot && @documentroot != '' -%>
DocumentRoot <%= @documentroot %>
<% end -%>
<%= [@config].flatten.join("\n").gsub(/^./, ' \&') %>
</VirtualHost>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment