Skip to content

Commit

Permalink
Allow multiple servers per location
Browse files Browse the repository at this point in the history
By changing the data type, you can now specify multiple servers for
a location.

Fixes voxpupuliGH-1187
  • Loading branch information
SaschaDoering committed Nov 21, 2018
1 parent ae2af80 commit 0c99478
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 30 deletions.
68 changes: 38 additions & 30 deletions manifests/resource/location.pp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
# (present|absent)
# [*internal*] - Indicates whether or not this location can be
# used for internal requests only. Default: false
# [*server*] - Defines the default server for this location
# entry to include with
# [*server*] - Defines all server for this location entry to include with
# [*location*] - Specifies the URI associated with this location
# entry
# [*location_satisfy*] - Allows access if all (all) or at least one (any) of the auth modules allow access.
Expand Down Expand Up @@ -123,6 +122,14 @@
# server => 'test2.local',
# }
#
# Use one location in multiple serrvers
# nginx::resource::location { 'test2.local-bob':
# ensure => present,
# www_root => '/var/www/bob',
# location => '/bob',
# server => ['test1.local','test2.local'],
# }
#
# Custom config example to limit location on localhost,
# create a hash with any extra custom config you want.
# $my_config = {
Expand Down Expand Up @@ -164,7 +171,7 @@
Enum['present', 'absent'] $ensure = present,
Boolean $internal = false,
String $location = $name,
String $server = undef,
Variant[String[1],Array[String[1],1]] $server = undef,
Optional[String] $www_root = undef,
Optional[String] $autoindex = undef,
Array $index_files = [
Expand Down Expand Up @@ -258,24 +265,15 @@
warning('The $fastcgi_script parameter is deprecated; please use $fastcgi_param instead to define custom fastcgi_params!')
}

$server_sanitized = regsubst($server, ' ', '_', 'G')
if $nginx::confd_only {
$server_dir = "${nginx::conf_dir}/conf.d"
} else {
$server_dir = "${nginx::conf_dir}/sites-available"
}

$config_file = "${server_dir}/${server_sanitized}.conf"

# Only try to manage these files if they're the default one (as you presumably
# usually don't want the default template if you're using a custom file.

if (
if (
$ensure == present and
$fastcgi != undef and
!defined(File[$fastcgi_params]) and
$fastcgi_params == "${nginx::conf_dir}/fastcgi.conf"
) {
) {
file { $fastcgi_params:
ensure => present,
mode => '0644',
Expand All @@ -291,25 +289,35 @@
}
}

if $ensure == present {
## Create stubs for server File Fragment Pattern
$location_md5 = md5($location)
if ($ssl_only != true) {
concat::fragment { "${server_sanitized}-${priority}-${location_md5}":
target => $config_file,
content => template('nginx/server/location.erb'),
order => $priority,
}
any2array($server).each |$s| {
$server_sanitized = regsubst($s, ' ', '_', 'G')
if $nginx::confd_only {
$server_dir = "${nginx::conf_dir}/conf.d"
} else {
$server_dir = "${nginx::conf_dir}/sites-available"
}

## Only create SSL Specific locations if $ssl is true.
if ($ssl == true or $ssl_only == true) {
$ssl_priority = $priority + 300
$config_file = "${server_dir}/${server_sanitized}.conf"
if $ensure == present {
## Create stubs for server File Fragment Pattern
$location_md5 = md5($location)
if ($ssl_only != true) {
concat::fragment { "${server_sanitized}-${priority}-${location_md5}":
target => $config_file,
content => template('nginx/server/location.erb'),
order => $priority,
}
}

## Only create SSL Specific locations if $ssl is true.
if ($ssl == true or $ssl_only == true) {
$ssl_priority = $priority + 300

concat::fragment { "${server_sanitized}-${ssl_priority}-${location_md5}-ssl":
target => $config_file,
content => template('nginx/server/location.erb'),
order => $ssl_priority,
concat::fragment { "${server_sanitized}-${ssl_priority}-${location_md5}-ssl":
target => $config_file,
content => template('nginx/server/location.erb'),
order => $ssl_priority,
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions spec/defines/resource_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@
it { is_expected.not_to contain_file('/etc/nginx/rspec-test_htpasswd') }
end

describe 'server/location configuration files' do
context 'when we have one location and one server' do
let(:params) { { location: 'my_location', proxy: 'proxy_value', server: 'server1' } }

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) }
it { is_expected.not_to contain_concat__fragment('server2-500-' + Digest::MD5.hexdigest(params[:location].to_s)) }
end
context 'when we have one location and two server' do
let(:params) { { location: 'my_location', proxy: 'proxy_value', server: ['server1', 'server2'] } }

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) }
it { is_expected.to contain_concat__fragment('server2-500-' + Digest::MD5.hexdigest(params[:location].to_s)) }
end
end

describe 'server/location_header template content' do
[
{
Expand Down

0 comments on commit 0c99478

Please sign in to comment.