Skip to content

Commit

Permalink
Allow multiple ports per vhost
Browse files Browse the repository at this point in the history
  • Loading branch information
tjikkun committed Dec 27, 2016
1 parent 28e9d41 commit fa815e7
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ apache::vhost { 'ip.example.com':
}
```

You can configure multiple ports per virtual host by using an array of ports for the [`port`][] parameter:

``` puppet
apache::vhost { 'ip.example.com':
ip => ['127.0.0.1'],
port => ['80','8080']
docroot => '/var/www/ip',
}
```

To configure a virtual host with [aliased servers][], refer to the aliases using the [`serveraliases`][] parameter:

``` puppet
Expand Down
9 changes: 5 additions & 4 deletions manifests/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,11 @@


if $ip {
$_ip = enclose_ipv6($ip)
$_ip = any2array(enclose_ipv6($ip))
if $port {
$listen_addr_port = suffix(any2array($_ip),":${port}")
$nvh_addr_port = suffix(any2array($_ip),":${port}")
$_port = any2array($port)
$listen_addr_port = split(inline_template("<%= @_ip.product(@_port).map {|x| x.join(':') }.join(',')%>"), ',')
$nvh_addr_port = split(inline_template("<%= @_ip.product(@_port).map {|x| x.join(':') }.join(',')%>"), ',')
} else {
$listen_addr_port = undef
$nvh_addr_port = $_ip
Expand All @@ -460,7 +461,7 @@
} else {
if $port {
$listen_addr_port = $port
$nvh_addr_port = "${vhost_name}:${port}"
$nvh_addr_port = prefix(any2array($port),"${vhost_name}:")
} else {
$listen_addr_port = undef
$nvh_addr_port = $name
Expand Down
129 changes: 129 additions & 0 deletions spec/acceptance/vhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,135 @@ class { 'apache':
end
end

context 'new vhost with multiple ports on 1 IP address' do
it 'should configure one apache vhost with 2 ports' do
pp = <<-EOS
class { 'apache':
default_vhost => false,
}
apache::vhost { 'example.com':
port => '80','8080'
ip => '127.0.0.1',
ip_based => true,
docroot => '/var/www/html',
}
host { 'host1.example.com': ip => '127.0.0.1', }
file { '/var/www/html/index.html':
ensure => file,
content => "Hello from vhost\\n",
}
EOS
apply_manifest(pp, :catch_failures => true)
end

describe service($service_name) do
if (fact('operatingsystem') == 'Debian' && fact('operatingsystemmajrelease') == '8')
pending 'Should be enabled - Bug 760616 on Debian 8'
else
it { should be_enabled }
end
it { is_expected.to be_running }
end

describe file("#{$vhost_dir}/25-example.com.conf") do
it { is_expected.to contain '<VirtualHost 127.0.0.1:80 127.0.0.1:8080>' }
it { is_expected.to contain "ServerName example.com" }
end

describe file($ports_file) do
it { is_expected.to be_file }
it { is_expected.to contain 'Listen 127.0.0.1:80' }
it { is_expected.to contain 'Listen 127.0.0.1:8080' }
it { is_expected.not_to contain 'NameVirtualHost 127.0.0.1:80' }
it { is_expected.not_to contain 'NameVirtualHost 127.0.0.1:8080' }
end

it 'should answer to host1.example.com port 80' do
shell("/usr/bin/curl host1.example.com:80", {:acceptable_exit_codes => 0}) do |r|
expect(r.stdout).to eq("Hello from vhost\n")
end
end

it 'should answer to host1.example.com port 8080' do
shell("/usr/bin/curl host1.example.com:8080", {:acceptable_exit_codes => 0}) do |r|
expect(r.stdout).to eq("Hello from vhost\n")
end
end
end

context 'new vhost with multiple IP addresses on multiple ports' do
it 'should configure one apache vhost with 2 ip addresses and 2 ports' do
pp = <<-EOS
class { 'apache':
default_vhost => false,
}
apache::vhost { 'example.com':
port => ['80', '8080'],
ip => ['127.0.0.1','127.0.0.2'],
ip_based => true,
docroot => '/var/www/html',
}
host { 'host1.example.com': ip => '127.0.0.1', }
host { 'host2.example.com': ip => '127.0.0.2', }
file { '/var/www/html/index.html':
ensure => file,
content => "Hello from vhost\\n",
}
EOS
apply_manifest(pp, :catch_failures => true)
end

describe service($service_name) do
if (fact('operatingsystem') == 'Debian' && fact('operatingsystemmajrelease') == '8')
pending 'Should be enabled - Bug 760616 on Debian 8'
else
it { should be_enabled }
end
it { is_expected.to be_running }
end

describe file("#{$vhost_dir}/25-example.com.conf") do
it { is_expected.to contain '<VirtualHost 127.0.0.1:80 127.0.0.1:8080 127.0.0.2:80 127.0.0.2:8080>' }
it { is_expected.to contain "ServerName example.com" }
end

describe file($ports_file) do
it { is_expected.to be_file }
it { is_expected.to contain 'Listen 127.0.0.1:80' }
it { is_expected.to contain 'Listen 127.0.0.1:8080' }
it { is_expected.to contain 'Listen 127.0.0.2:80' }
it { is_expected.to contain 'Listen 127.0.0.2:8080' }
it { is_expected.not_to contain 'NameVirtualHost 127.0.0.1:80' }
it { is_expected.not_to contain 'NameVirtualHost 127.0.0.1:8080' }
it { is_expected.not_to contain 'NameVirtualHost 127.0.0.2:80' }
it { is_expected.not_to contain 'NameVirtualHost 127.0.0.2:8080' }
end

it 'should answer to host1.example.com port 80' do
shell("/usr/bin/curl host1.example.com:80", {:acceptable_exit_codes => 0}) do |r|
expect(r.stdout).to eq("Hello from vhost\n")
end
end

it 'should answer to host1.example.com port 81' do
shell("/usr/bin/curl host1.example.com:81", {:acceptable_exit_codes => 0}) do |r|
expect(r.stdout).to eq("Hello from vhost\n")
end
end

it 'should answer to host2.example.com port 80' do
shell("/usr/bin/curl host2.example.com:80", {:acceptable_exit_codes => 0}) do |r|
expect(r.stdout).to eq("Hello from vhost\n")
end
end

it 'should answer to host2.example.com port 81' do
shell("/usr/bin/curl host2.example.com:81", {:acceptable_exit_codes => 0}) do |r|
expect(r.stdout).to eq("Hello from vhost\n")
end
end
end

context 'new vhost with IPv6 address on port 80', :ipv6 do
it 'should configure one apache vhost with an ipv6 address' do
pp = <<-EOS
Expand Down
74 changes: 74 additions & 0 deletions spec/defines/vhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,80 @@
it { is_expected.to_not contain_concat__fragment('NameVirtualHost [::1]:80') }
end

context 'vhost with multiple ports' do
let :params do
{
'port' => ['80', '8080'],
'ip' => '127.0.0.1',
'ip_based' => true,
'servername' => 'example.com',
'docroot' => '/var/www/html',
'add_listen' => true,
'ensure' => 'present'
}
end
let :facts do
{
:osfamily => 'RedHat',
:operatingsystemrelease => '7',
:concat_basedir => '/dne',
:operatingsystem => 'RedHat',
:id => 'root',
:kernel => 'Linux',
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
:kernelversion => '3.6.2',
:is_pe => false,
}
end

it { is_expected.to compile }
it { is_expected.to contain_concat__fragment('rspec.example.com-apache-header').with(
:content => /[.\/m]*<VirtualHost 127.0.0.1:80 127.0.0.1:8080>[.\/m]*$/ ) }
it { is_expected.to contain_concat__fragment('Listen 127.0.0.1:80') }
it { is_expected.to contain_concat__fragment('Listen 127.0.0.1:8080') }
it { is_expected.to_not contain_concat__fragment('NameVirtualHost 127.0.0.1:80') }
it { is_expected.to_not contain_concat__fragment('NameVirtualHost 127.0.0.1:8080') }
end

context 'vhost with multiple ip addresses, multiple ports' do
let :params do
{
'port' => ['80', '8080'],
'ip' => ['127.0.0.1','::1'],
'ip_based' => true,
'servername' => 'example.com',
'docroot' => '/var/www/html',
'add_listen' => true,
'ensure' => 'present'
}
end
let :facts do
{
:osfamily => 'RedHat',
:operatingsystemrelease => '7',
:concat_basedir => '/dne',
:operatingsystem => 'RedHat',
:id => 'root',
:kernel => 'Linux',
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
:kernelversion => '3.6.2',
:is_pe => false,
}
end

it { is_expected.to compile }
it { is_expected.to contain_concat__fragment('rspec.example.com-apache-header').with(
:content => /[.\/m]*<VirtualHost 127.0.0.1:80 127.0.0.1:8080 \[::1\]:80 \[::1\]:8080>[.\/m]*$/ ) }
it { is_expected.to contain_concat__fragment('Listen 127.0.0.1:80') }
it { is_expected.to contain_concat__fragment('Listen 127.0.0.1:8080') }
it { is_expected.to contain_concat__fragment('Listen [::1]:80') }
it { is_expected.to contain_concat__fragment('Listen [::1]:8080') }
it { is_expected.to_not contain_concat__fragment('NameVirtualHost 127.0.0.1:80') }
it { is_expected.to_not contain_concat__fragment('NameVirtualHost 127.0.0.1:8080') }
it { is_expected.to_not contain_concat__fragment('NameVirtualHost [::1]:80') }
it { is_expected.to_not contain_concat__fragment('NameVirtualHost [::1]:8080') }
end

context 'vhost with ipv6 address' do
let :params do
{
Expand Down

0 comments on commit fa815e7

Please sign in to comment.