Skip to content

Commit

Permalink
Merge pull request #951 from jlambert121/modules_1382
Browse files Browse the repository at this point in the history
MODULES-1382: support multiple access log directives
  • Loading branch information
igalic committed Jan 15, 2015
2 parents 9d5ef27 + bb96180 commit 76fd199
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 32 deletions.
56 changes: 28 additions & 28 deletions manifests/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@
$logroot_mode = undef,
$log_level = undef,
$access_log = true,
$access_log_file = undef,
$access_log_pipe = undef,
$access_log_syslog = undef,
$access_log_format = undef,
$access_log_env_var = undef,
$access_log_file = false,
$access_log_pipe = false,
$access_log_syslog = false,
$access_log_format = false,
$access_log_env_var = false,
$access_logs = undef,
$aliases = undef,
$directories = undef,
$error_log = true,
Expand Down Expand Up @@ -269,19 +270,28 @@
# Is apache::mod::shib enabled (or apache::mod['shib2'])
$shibboleth_enabled = defined(Apache::Mod['shib2'])

# Define log file names
if $access_log_file {
$access_log_destination = "${logroot}/${access_log_file}"
} elsif $access_log_pipe {
$access_log_destination = $access_log_pipe
} elsif $access_log_syslog {
$access_log_destination = $access_log_syslog
} else {
if $ssl {
$access_log_destination = "${logroot}/${name}_access_ssl.log"
if $access_log and !$access_logs {
if $access_log_file {
$_logs_dest = "${logroot}/${access_log_file}"
} elsif $access_log_pipe {
$_logs_dest = $access_log_pipe
} elsif $access_log_syslog {
$_logs_dest = $access_log_syslog
} else {
$access_log_destination = "${logroot}/${name}_access.log"
$_logs_dest = undef
}
$_access_logs = [{
'file' => $access_log_file,
'pipe' => $access_log_pipe,
'syslog' => $access_log_syslog,
'format' => $access_log_format,
'env' => $access_log_env_var
}]
} elsif $access_logs {
if !is_array($access_logs) {
fail("Apache::Vhost[${name}]: access_logs must be an array of hashes")
}
$_access_logs = $access_logs
}

if $error_log_file {
Expand All @@ -298,17 +308,6 @@
}
}

# Set access log format
if $access_log_format {
$_access_log_format = "\"${access_log_format}\""
} else {
$_access_log_format = 'combined'
}

if $access_log_env_var {
$_access_log_env_var = "env=${access_log_env_var}"
}

if $ip {
if $port {
$listen_addr_port = "${ip}:${port}"
Expand Down Expand Up @@ -569,7 +568,8 @@
# - $access_log_destination
# - $_access_log_format
# - $_access_log_env_var
if $access_log {
# - $access_logs
if $access_log or $access_logs {
concat::fragment { "${name}-access_log":
target => "${priority_real}-${filename}.conf",
order => 100,
Expand Down
28 changes: 28 additions & 0 deletions spec/acceptance/vhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,34 @@ class { 'apache': }
end
end

describe 'multiple access_logs' do
it 'applies cleanly' do
pp = <<-EOS
class { 'apache': }
host { 'test.server': ip => '127.0.0.1' }
apache::vhost { 'test.server':
docroot => '/tmp',
logroot => '/tmp',
access_logs => [
{'file' => 'log1'},
{'file' => 'log2', 'env' => 'admin' },
{'file' => '/var/tmp/log3', 'format' => '%h %l'},
{'syslog' => 'syslog' }
]
}
EOS
apply_manifest(pp, :catch_failures => true)
end

describe file("#{$vhost_dir}/25-test.server.conf") do
it { is_expected.to be_file }
it { is_expected.to contain 'CustomLog "/tmp/log1" combined' }
it { is_expected.to contain 'CustomLog "/tmp/log2" combined env=admin' }
it { is_expected.to contain 'CustomLog "/var/tmp/log3" "%h %l"' }
it { is_expected.to contain 'CustomLog "syslog" combined' }
end
end

describe 'aliases' do
it 'applies cleanly' do
pp = <<-EOS
Expand Down
67 changes: 67 additions & 0 deletions spec/defines/vhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,63 @@
it { is_expected.to contain_concat__fragment('rspec.example.com-file_footer') }
end
end
describe 'access logs' do
let :facts do
{
:osfamily => 'RedHat',
:operatingsystemrelease => '6',
:concat_basedir => '/dne',
:operatingsystem => 'RedHat',
:id => 'root',
:kernel => 'Linux',
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
}
end
context 'single log file' do
let(:params) do
{
'docroot' => '/rspec/docroot',
'access_log_file' => 'my_log_file',
}
end
it { is_expected.to contain_concat__fragment('rspec.example.com-access_log').with(
:content => /^\s+CustomLog.*my_log_file" combined\s*$/
)}
end
context 'single log file with environment' do
let(:params) do
{
'docroot' => '/rspec/docroot',
'access_log_file' => 'my_log_file',
'access_log_env_var' => 'prod'
}
end
it { is_expected.to contain_concat__fragment('rspec.example.com-access_log').with(
:content => /^\s+CustomLog.*my_log_file" combined\s+env=prod$/
)}
end
context 'multiple log files' do
let(:params) do
{
'docroot' => '/rspec/docroot',
'access_logs' => [
{ 'file' => '/tmp/log1', 'env' => 'dev' },
{ 'file' => 'log2' },
{ 'syslog' => 'syslog', 'format' => '%h %l' }
],
}
end
it { is_expected.to contain_concat__fragment('rspec.example.com-access_log').with(
:content => /^\s+CustomLog "\/tmp\/log1"\s+combined\s+env=dev$/
)}
it { is_expected.to contain_concat__fragment('rspec.example.com-access_log').with(
:content => /^\s+CustomLog "\/var\/log\/httpd\/log2"\s+combined\s*$/
)}
it { is_expected.to contain_concat__fragment('rspec.example.com-access_log').with(
:content => /^\s+CustomLog "syslog" "%h %l"\s*$/
)}
end
end # access logs
describe 'validation' do
context 'bad ensure' do
let :params do
Expand Down Expand Up @@ -620,5 +677,15 @@
let :facts do default_facts end
it { expect { is_expected.to compile }.to raise_error }
end
context 'bad access_logs' do
let :params do
{
'docroot' => '/rspec/docroot',
'access_logs' => '/var/log/somewhere',
}
end
let :facts do default_facts end
it { expect { is_expected.to compile }.to raise_error }
end
end
end
24 changes: 20 additions & 4 deletions templates/vhost/_access_log.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
<% if @access_log and @_access_log_env_var -%>
CustomLog "<%= @access_log_destination %>" <%= @_access_log_format %> <%= @_access_log_env_var %>
<% elsif @access_log -%>
CustomLog "<%= @access_log_destination %>" <%= @_access_log_format %>
<% @_access_logs.each do |log| -%>
<% env ||= "env=#{log['env']}" if log['env'] -%>
<% env ||= '' -%>
<% format ||= "\"#{log['format']}\"" if log['format'] -%>
<% format ||= 'combined' -%>
<% if log['file'] -%>
<% if log['file'].chars.first == '/' -%>
<% destination = "#{log['file']}" -%>
<% else -%>
<% destination = "#{@logroot}/#{log['file']}" -%>
<% end -%>
<% elsif log['syslog'] -%>
<% destination = "syslog" -%>
<% elsif log['pipe'] -%>
<% destination = log['pipe'] -%>
<% else -%>
<% destination ||= "#{@logroot}/#{@name}_access_ssl.log" if @ssl -%>
<% destination ||= "#{@logroot}/#{@name}_access.log" -%>
<% end -%>
CustomLog "<%= destination %>" <%= format %> <%= env %>
<% end -%>

0 comments on commit 76fd199

Please sign in to comment.