Skip to content

Commit

Permalink
Reintegrate voxpupuli#331 (upstream exports/collections)
Browse files Browse the repository at this point in the history
- resurrecting voxpupuli#331 from @rainopik
- update (concat/stdlib) dependency version requirements based on current usage
- fix upstream tests to work with rainopik's changes in what was voxpupuli#331
  • Loading branch information
rabbitt authored and janorn committed Jun 24, 2014
1 parent 1f43c13 commit fac3235
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Modulefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ summary 'Puppet NGINX management module'
description 'This module can be used for basic NGINX Management'
project_page 'http://github.com/jfryman/puppet-nginx'

dependency 'puppetlabs/stdlib', '>= 0.1.6'
dependency 'puppetlabs/apt', '>= 1.0.0'
dependency 'puppetlabs/concat', '>= 1.0.0'
dependency 'puppetlabs/stdlib', '>= 3.0.0'
dependency 'puppetlabs/apt', '>= 1.0.0'
dependency 'puppetlabs/concat', '>= 1.1.0'
6 changes: 3 additions & 3 deletions Puppetfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
forge 'http://forge.puppetlabs.com'

mod 'puppetlabs/stdlib', '>=0.1.6'
mod 'puppetlabs/apt', '>=1.0.0'
mod 'puppetlabs/concat', '>=1.0.0'
mod 'puppetlabs/stdlib', '>= 3.0.0'
mod 'puppetlabs/apt', '>= 1.0.0'
mod 'puppetlabs/concat', '>= 1.1.0'
6 changes: 3 additions & 3 deletions Puppetfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ FORGE
specs:
puppetlabs/apt (1.2.0)
puppetlabs/stdlib (>= 2.2.1)
puppetlabs/concat (1.0.0)
puppetlabs/concat (1.1.0)
puppetlabs/stdlib (4.1.0)

DEPENDENCIES
puppetlabs/apt (>= 1.0.0)
puppetlabs/concat (>= 1.0.0)
puppetlabs/stdlib (>= 0.1.6)
puppetlabs/concat (>= 1.1.0)
puppetlabs/stdlib (>= 3.0.0)

40 changes: 34 additions & 6 deletions manifests/resource/upstream.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# Parameters:
# [*members*] - Array of member URIs for NGINX to connect to. Must follow valid NGINX syntax.
# If omitted, individual members should be defined with nginx::resource::upstream::member
# [*ensure*] - Enables or disables the specified location (present|absent)
# [*upstream_cfg_prepend*] - It expects a hash with custom directives to put before anything else inside upstream
# [*upstream_fail_timeout*] - Set the fail_timeout for the upstream. Default is 10 seconds - As that is what Nginx does normally.
Expand Down Expand Up @@ -38,31 +39,58 @@
# upstream_cfg_prepend => $my_config,
# }
define nginx::resource::upstream (
$members,
$members = undef,
$ensure = 'present',
$upstream_cfg_prepend = undef,
$upstream_fail_timeout = '10s',
) {

validate_array($members)
if $members != undef {
validate_array($members)
}
validate_re($ensure, '^(present|absent)$',
"${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
if ($upstream_cfg_prepend != undef) {
validate_hash($upstream_cfg_prepend)
}

File {
Concat {
owner => 'root',
group => 'root',
mode => '0644',
}

file { "${nginx::config::conf_dir}/conf.d/${name}-upstream.conf":
concat { "${nginx::config::conf_dir}/conf.d/${name}-upstream.conf":
ensure => $ensure ? {
'absent' => absent,
default => 'file',
'file' => present,
default => present,
},
content => template('nginx/conf.d/upstream.erb'),
notify => Class['nginx::service'],
}

# Uses: $name, $upstream_cfg_prepend
concat::fragment { "${name}_upstream_header":
target => "${nginx::config::conf_dir}/conf.d/${name}-upstream.conf",
order => 10,
content => template('nginx/conf.d/upstream_header.erb'),
}

if $members != undef {
# Uses: $members, $upstream_fail_timeout
concat::fragment { "${name}_upstream_members":
target => "${nginx::config::conf_dir}/conf.d/${name}-upstream.conf",
order => 50,
content => template('nginx/conf.d/upstream_members.erb'),
}
} else {
# Collect exported members:
Nginx::Resource::Upstream::Member <<| upstream == $name |>>
}

concat::fragment { "${name}_upstream_footer":
target => "${nginx::config::conf_dir}/conf.d/${name}-upstream.conf",
order => 90,
content => "}\n",
}
}
48 changes: 48 additions & 0 deletions manifests/resource/upstream/member.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Define: nginx::resources::upstream::member
#
# Creates an upstream member inside the upstream block. Export this resource
# in all upstream member servers and collect them on the NGINX server.
#
#
# Requirements:
# Requires storeconfigs on the Puppet Master to export and collect resources
#
#
# Parameters:
# [*upstream*] - The name of the upstream resource
# [*server*] - Hostname or IP of the upstream member server
# [*port*] - Port of the listening service on the upstream member
# [*upstream_fail_timeout*] - Set the fail_timeout for the upstream. Default is 10 seconds
#
#
# Examples:
#
# Exporting the resource on a upstream member server:
#
# @@nginx::resource::upstream::member { $::fqdn:
# upstream => 'proxypass',
# server => $::ipaddress,
# port => '3000',
# }
#
#
# Collecting the resource on the NGINX server:
#
# nginx::resource::upstream { 'proxypass':
# ensure => present,
# }
#
define nginx::resource::upstream::member (
$upstream,
$server,
$port = '80',
$upstream_fail_timeout = '10s',
) {

# Uses: $server, $port, $upstream_fail_timeout
concat::fragment { "${upstream}_upstream_member_${name}":
target => "${nginx::config::conf_dir}/conf.d/${upstream}-upstream.conf",
order => 40,
content => template('nginx/conf.d/upstream_member.erb'),
}
}
83 changes: 57 additions & 26 deletions spec/defines/resource_upstream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,79 @@
:members => ['test'],
}
end
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'debian',
}
end

let :pre_condition do
[
'include ::nginx::params',
'include ::nginx::config',
]
end

let :default_facts do
{
:osfamily => 'RedHat',
:operatingsystem => 'CentOS',
:concat_basedir => '/var/lib/puppet/concat'
}
end

let :pre_condition do
[
'include ::nginx::params',
'include ::nginx::config',
]
end

describe 'os-independent items' do
let :facts do default_facts end

describe 'basic assumptions' do
let :params do default_params end

it { should contain_file("/etc/nginx/conf.d/#{title}-upstream.conf").with(
it { should contain_class("nginx::params") }
it { should contain_class('concat::setup') }
it { should contain_file("/etc/nginx/conf.d/#{title}-upstream.conf") }
it { should contain_concat__fragment("#{title}_upstream_header").with_content(/upstream #{title}/) }

it {
should contain_concat__fragment("#{title}_upstream_header").with(
{
'owner' => 'root',
'group' => 'root',
'mode' => '0644',
'ensure' => 'file',
'content' => /upstream #{title}/,
'target' => "/etc/nginx/conf.d/#{title}-upstream.conf",
'order' => 10,
}
)}

it {
should contain_concat__fragment("#{title}_upstream_members").with(
{
'target' => "/etc/nginx/conf.d/#{title}-upstream.conf",
'order' => 50,
}
)}

it {
should contain_concat__fragment("#{title}_upstream_footer").with(
{
'target' => "/etc/nginx/conf.d/#{title}-upstream.conf",
'order' => 90,
}).with_content("}\n")
}
end

describe "upstream.conf template content" do
let :facts do default_facts end
[
{
:title => 'should contain ordered prepended directives',
:attr => 'upstream_cfg_prepend',
:title => 'should contain ordered prepended directives',
:attr => 'upstream_cfg_prepend',
:fragment => 'header',
:value => {
'test3' => 'test value 3',
'test6' => {'subkey1' => ['subvalue1', 'subvalue2']},
'test1' => 'test value 1',
'test2' => 'test value 2',
'test4' => ['test value 1', 'test value 2'],
'test5' => {'subkey1' => 'subvalue1'},
'test6' => {'subkey1' => ['subvalue1', 'subvalue2']},
'test4' => ['test value 1', 'test value 2'],
},
:match => [
' test1 test value 1;',
Expand All @@ -64,14 +96,11 @@
],
},
{
:title => 'should set server',
:attr => 'members',
:value => [
'test3',
'test1',
'test2',
],
:match => [
:title => 'should set server',
:attr => 'members',
:fragment => 'members',
:value => %W( test3 test1 test2 ),
:match => [
' server test3 fail_timeout=10s;',
' server test1 fail_timeout=10s;',
' server test2 fail_timeout=10s;',
Expand All @@ -82,10 +111,12 @@
let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end

it { should contain_file("/etc/nginx/conf.d/#{title}-upstream.conf").with_mode('0644') }
it { should contain_concat__fragment("#{title}_upstream_#{param[:fragment]}") }
it param[:title] do
verify_contents(subject, "/etc/nginx/conf.d/#{title}-upstream.conf", Array(param[:match]))
lines = subject.resource('concat::fragment', "#{title}_upstream_#{param[:fragment]}").send(:parameters)[:content].split("\n")
(lines & Array(param[:match])).should == Array(param[:match])
Array(param[:notmatch]).each do |item|
should contain_file("/etc/nginx/conf.d/#{title}-upstream.conf").without_content(item)
should contain_concat__fragment("#{title}_upstream_#{param[:fragment]}").without_content(item)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ upstream <%= @name %> {
<% end -%>
<% end -%>
<% end -%><% end -%>
<% @members.each do |i| %>
server <%= i %> fail_timeout=<%= @upstream_fail_timeout %>;<% end %>
}
1 change: 1 addition & 0 deletions templates/conf.d/upstream_member.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server <%= @server %>:<%= @port %> fail_timeout=<%= @upstream_fail_timeout %>;
2 changes: 2 additions & 0 deletions templates/conf.d/upstream_members.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<% @members.each do |i| %>
server <%= i %> fail_timeout=<%= @upstream_fail_timeout %>;<% end %>

0 comments on commit fac3235

Please sign in to comment.