Skip to content

Commit

Permalink
Merge pull request voxpupuli#1278 from syseleven/1187_recycle_locations
Browse files Browse the repository at this point in the history
Allow multiple servers per location
  • Loading branch information
bastelfreak authored Dec 11, 2018
2 parents df9f4af + b97c44e commit 97b98fa
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 36 deletions.
80 changes: 44 additions & 36 deletions manifests/resource/location.pp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
# (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 a server or list of servers that include this location
# [*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.
# [*location_satisfy*] - Allows access if all (all) or at least one (any) of the auth modules allow access.
# [*location_allow*] - Array: Locations to allow connections from.
# [*location_deny*] - Array: Locations to deny connections from.
# [*www_root*] - Specifies the location on disk for files to be
Expand Down Expand Up @@ -123,6 +122,14 @@
# server => 'test2.local',
# }
#
# Use one location in multiple servers
# 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 @@ -161,10 +168,10 @@
# }

define nginx::resource::location (
Enum['present', 'absent'] $ensure = present,
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,58 +265,59 @@
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 (
$ensure == present and
if (
$ensure == 'present' and
$fastcgi != undef and
!defined(File[$fastcgi_params]) and
$fastcgi_params == "${nginx::conf_dir}/fastcgi.conf"
) {
) {
file { $fastcgi_params:
ensure => present,
ensure => 'present',
mode => '0644',
content => template('nginx/server/fastcgi.conf.erb'),
}
}

if $ensure == present and $uwsgi != undef and !defined(File[$uwsgi_params]) and $uwsgi_params == "${nginx::conf_dir}/uwsgi_params" {
if $ensure == 'present' and $uwsgi != undef and !defined(File[$uwsgi_params]) and $uwsgi_params == "${nginx::conf_dir}/uwsgi_params" {
file { $uwsgi_params:
ensure => present,
ensure => 'present',
mode => '0644',
content => template('nginx/server/uwsgi_params.erb'),
}
}

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
67 changes: 67 additions & 0 deletions spec/acceptance/nginx_location_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'spec_helper_acceptance'

describe 'nginx::resource::location define:' do
it 'runs successfully' do
pp = "
class { 'nginx': }
nginx::resource::server { 'www.puppetlabs.com':
ensure => present,
www_root => '/var/www/www.puppetlabs.com',
}
nginx::resource::server { 'stage.puppetlabs.com':
ensure => present,
www_root => '/var/www/stage.puppetlabs.com',
}
nginx::resource::location { 'static-production':
ensure => present,
server => 'www.puppetlabs.com',
location => '/media',
www_root => '/var/www/staticfiles/production',
}
nginx::resource::location { 'static-stage':
ensure => present,
server => 'stage.puppetlabs.com',
location => '/media',
www_root => '/var/www/staticfiles/stage',
}
nginx::resource::location { 'letsencrypt':
ensure => present,
server => ['www.puppetlabs.com', 'stage.puppetlabs.com'],
location => '/.well-known/acme-challenge/',
www_root => '/var/www/letsencrypt',
}
"
apply_manifest(pp, catch_failures: true)
end

describe file('/etc/nginx/sites-available/www.puppetlabs.com.conf') do
it { is_expected.to be_file }
it { is_expected.to contain '# MANAGED BY PUPPET' }
it { is_expected.to contain ' root /var/www/www.puppetlabs.com;' }
it { is_expected.to contain ' location /media {' }
it { is_expected.to contain ' root /var/www/staticfiles/production;' }
it { is_expected.not_to contain ' root /var/www/staticfiles/stage;' }
it { is_expected.to contain ' location /.well-known/acme-challenge/ {' }
it { is_expected.to contain ' root /var/www/letsencrypt;' }
end
describe file('/etc/nginx/sites-available/stage.puppetlabs.com.conf') do
it { is_expected.to be_file }
it { is_expected.to contain '# MANAGED BY PUPPET' }
it { is_expected.to contain ' root /var/www/stage.puppetlabs.com;' }
it { is_expected.to contain ' location /media {' }
it { is_expected.to contain ' root /var/www/staticfiles/stage;' }
it { is_expected.not_to contain ' root /var/www/staticfiles/production;' }
it { is_expected.to contain ' location /.well-known/acme-challenge/ {' }
it { is_expected.to contain ' root /var/www/letsencrypt;' }
end

describe service('nginx') do
it { is_expected.to be_running }
it { is_expected.to be_enabled }
end

describe port(80) do
it { is_expected.to be_listening }
end
end
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: %w[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 97b98fa

Please sign in to comment.