Skip to content

Commit 1b90e9c

Browse files
authored
Merge pull request #2521 from dodevops/feature/mod_cache_disk
Added cache_disk
2 parents c26ca5d + 8b998bb commit 1b90e9c

File tree

10 files changed

+466
-65
lines changed

10 files changed

+466
-65
lines changed

manifests/default_mods.pp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@
6969
'FreeBSD': {
7070
include apache::mod::actions
7171
include apache::mod::authn_core
72-
include apache::mod::cache
73-
include apache::mod::disk_cache
7472
include apache::mod::filter
7573
include apache::mod::headers
7674
include apache::mod::info

manifests/mod/cache.pp

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,58 @@
11
# @summary
22
# Installs `mod_cache`
3-
#
3+
#
4+
# @param cache_ignore_headers
5+
# Specifies HTTP header(s) that should not be stored in the cache.
6+
#
7+
# @param cache_default_expire
8+
# The default duration to cache a document when no expiry date is specified.
9+
#
10+
# @param cache_max_expire
11+
# The maximum time in seconds to cache a document
12+
#
13+
# @param cache_ignore_no_lastmod
14+
# Ignore the fact that a response has no Last Modified header.
15+
#
16+
# @param cache_header
17+
# Add an X-Cache header to the response.
18+
#
19+
# @param cache_lock
20+
# Enable the thundering herd lock.
21+
#
22+
# @param cache_ignore_cache_control
23+
# Ignore request to not serve cached content to client
24+
#
425
# @see https://httpd.apache.org/docs/current/mod/mod_cache.html for additional documentation.
526
#
6-
class apache::mod::cache {
7-
::apache::mod { 'cache': }
27+
class apache::mod::cache (
28+
Array[String[1]] $cache_ignore_headers = [],
29+
Optional[Integer] $cache_default_expire = undef,
30+
Optional[Integer] $cache_max_expire = undef,
31+
Optional[Apache::OnOff] $cache_ignore_no_lastmod = undef,
32+
Optional[Apache::OnOff] $cache_header = undef,
33+
Optional[Apache::OnOff] $cache_lock = undef,
34+
Optional[Apache::OnOff] $cache_ignore_cache_control = undef,
35+
) {
36+
include apache
37+
apache::mod { 'cache': }
38+
39+
$_configuration_file_name = 'cache.conf'
40+
41+
file { $_configuration_file_name:
42+
ensure => file,
43+
path => "${apache::mod_dir}/${_configuration_file_name}",
44+
mode => $apache::file_mode,
45+
content => epp('apache/mod/cache.conf.epp', {
46+
cache_ignore_headers => $cache_ignore_headers,
47+
cache_default_expire => $cache_default_expire,
48+
cache_max_expire => $cache_max_expire,
49+
cache_ignore_no_lastmod => $cache_ignore_no_lastmod,
50+
cache_header => $cache_header,
51+
cache_lock => $cache_lock,
52+
cache_ignore_cache_control => $cache_ignore_cache_control,
53+
}),
54+
require => Exec["mkdir ${apache::mod_dir}"],
55+
before => File[$apache::mod_dir],
56+
notify => Class['apache::service'],
57+
}
858
}

manifests/mod/cache_disk.pp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# @summary
2+
# Installs and configures `mod_cache_disk`.
3+
#
4+
# @description
5+
# This will install an configure the proper module depending on the used apache version, so
6+
# - mod_cache_disk for apache version >= 2.4
7+
# - mod_disk_cache for older apache versions
8+
#
9+
# @param cache_root
10+
# Defines the name of the directory on the disk to contain cache files.
11+
# Default depends on the Apache version and operating system:
12+
# - Debian: /var/cache/apache2/mod_cache_disk
13+
# - FreeBSD: /var/cache/mod_cache_disk
14+
# - Red Hat: /var/cache/httpd/proxy
15+
#
16+
# @param cache_enable
17+
# Defines an array of directories to cache, the default is none
18+
#
19+
# @param cache_dir_length
20+
# The number of characters in subdirectory names
21+
#
22+
# @param cache_dir_levels
23+
# The number of levels of subdirectories in the cache.
24+
#
25+
# @param cache_max_filesize
26+
# The maximum size (in bytes) of a document to be placed in the cache
27+
#
28+
# @param cache_ignore_headers
29+
# DEPRECATED Ignore request to not serve cached content to client (included for compatibility reasons to support disk_cache)
30+
#
31+
# @param configuration_file_name
32+
# DEPRECATED Name of module configuration file (used for the compatibility layer for disk_cache)
33+
#
34+
# @see https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html
35+
#
36+
class apache::mod::cache_disk (
37+
Optional[Stdlib::Absolutepath] $cache_root = undef,
38+
Array[String] $cache_enable = [],
39+
Optional[Integer] $cache_dir_length = undef,
40+
Optional[Integer] $cache_dir_levels = undef,
41+
Optional[Integer] $cache_max_filesize = undef,
42+
Optional[String] $cache_ignore_headers = undef,
43+
Optional[String] $configuration_file_name = undef,
44+
) {
45+
include apache
46+
47+
if $cache_ignore_headers {
48+
deprecation(
49+
'apache::mod::cache_disk',
50+
'The parameter cache_ignore_headers is deprecated. Please use apache::mod::cache::cache_ignore_headers instead.'
51+
)
52+
}
53+
54+
$_cache_root = $cache_root ? {
55+
undef => $facts['os']['family'] ? {
56+
'debian' => '/var/cache/apache2/mod_cache_disk',
57+
'redhat' => '/var/cache/httpd/proxy',
58+
'freebsd' => '/var/cache/mod_cache_disk',
59+
},
60+
default => $cache_root,
61+
}
62+
$_configuration_file_name = pick($configuration_file_name, 'cache_disk.conf')
63+
$_class_name = 'apache::mod::cache_disk'
64+
65+
apache::mod { 'cache_disk': }
66+
67+
Class['apache::mod::cache'] -> Class[$_class_name]
68+
69+
file { $_configuration_file_name:
70+
ensure => file,
71+
path => "${apache::mod_dir}/${_configuration_file_name}",
72+
mode => $apache::file_mode,
73+
content => epp('apache/mod/cache_disk.conf.epp', {
74+
cache_root => $_cache_root,
75+
cache_enable => $cache_enable,
76+
cache_dir_length => $cache_dir_length,
77+
cache_dir_levels => $cache_dir_levels,
78+
cache_max_filesize => $cache_max_filesize,
79+
cache_ignore_headers => $cache_ignore_headers,
80+
}),
81+
require => Exec["mkdir ${apache::mod_dir}"],
82+
before => File[$apache::mod_dir],
83+
notify => Class['apache::service'],
84+
}
85+
}

manifests/mod/disk_cache.pp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# @summary
22
# Installs and configures `mod_disk_cache`.
3-
#
3+
#
44
# @param cache_root
55
# Defines the name of the directory on the disk to contain cache files.
66
# Default depends on the Apache version and operating system:
77
# - Debian: /var/cache/apache2/mod_cache_disk
88
# - FreeBSD: /var/cache/mod_cache_disk
9-
# - Red Hat: /var/cache/httpd/proxy
109
#
1110
# @param cache_ignore_headers
1211
# Specifies HTTP header(s) that should not be stored in the cache.
@@ -17,44 +16,24 @@
1716
# You can then control this behaviour in individual vhosts by explicitly defining CacheEnable.
1817
#
1918
# @note
20-
# On Apache 2.4, mod_cache_disk installed.
19+
# Apache 2.2, mod_disk_cache installed. On Apache 2.4, mod_cache_disk installed.
20+
# This class is deprecated, use mode_cache_disk instead
2121
#
22-
# @see https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html for additional documentation.
22+
# @see https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html for additional documentation on version 2.4.
2323
#
2424
class apache::mod::disk_cache (
2525
Optional[Stdlib::Absolutepath] $cache_root = undef,
2626
Optional[String] $cache_ignore_headers = undef,
2727
Boolean $default_cache_enable = true,
2828
) {
29-
include apache
30-
if $cache_root {
31-
$_cache_root = $cache_root
32-
} else {
33-
$_cache_root = $facts['os']['family'] ? {
34-
'Debian' => '/var/cache/apache2/mod_cache_disk',
35-
'RedHat' => '/var/cache/httpd/proxy',
36-
'FreeBSD' => '/var/cache/mod_cache_disk',
37-
}
38-
}
39-
40-
apache::mod { 'cache_disk': }
41-
42-
Class['apache::mod::cache'] -> Class['apache::mod::disk_cache']
43-
44-
$parameters = {
45-
'default_cache_enable' => $default_cache_enable,
46-
'_cache_root' => $_cache_root,
47-
'cache_ignore_headers' => $cache_ignore_headers,
48-
}
29+
deprecation('apache::mod::disk_cache', 'This class is deprecated; please use apache::mod::cache_disk')
4930

50-
# Template uses $_cache_root
51-
file { 'disk_cache.conf':
52-
ensure => file,
53-
path => "${apache::mod_dir}/disk_cache.conf",
54-
mode => $apache::file_mode,
55-
content => epp('apache/mod/disk_cache.conf.epp', $parameters),
56-
require => Exec["mkdir ${apache::mod_dir}"],
57-
before => File[$apache::mod_dir],
58-
notify => Class['apache::service'],
31+
class { 'apache::mod::cache_disk':
32+
cache_root => $cache_root,
33+
cache_enable => ['/'],
34+
cache_ignore_headers => $cache_ignore_headers,
35+
cache_dir_length => 1,
36+
cache_dir_levels => 2,
37+
configuration_file_name => 'cache_disk.conf'
5938
}
6039
}

spec/classes/mod/cache_disk_spec.rb

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'apache::mod::cache_disk', type: :class do
6+
context 'on a Debian OS' do
7+
include_examples 'Debian 11'
8+
9+
let(:params) do
10+
{
11+
cache_enable: ['/'],
12+
}
13+
end
14+
15+
let :pre_condition do
16+
'class{ "apache":
17+
default_mods => ["cache"],
18+
mod_dir => "/tmp/junk",
19+
}'
20+
end
21+
22+
it { is_expected.to compile }
23+
it { is_expected.to contain_class('apache::mod::cache_disk') }
24+
it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') }
25+
it { is_expected.to contain_apache__mod('cache_disk') }
26+
27+
default_config = %r{CacheEnable disk /\nCacheRoot "/var/cache/apache2/mod_cache_disk"}
28+
29+
it { is_expected.to contain_file('cache_disk.conf').with(content: default_config) }
30+
31+
describe 'with multiple cache_enable parameters' do
32+
let(:params) do
33+
{
34+
cache_enable: ['/', '/something'],
35+
}
36+
end
37+
38+
it {
39+
expect(subject).to contain_file('cache_disk.conf')
40+
.with(content: %r{CacheEnable disk /\nCacheEnable disk /something\nCacheRoot "/var/cache/apache2/mod_cache_disk"})
41+
}
42+
end
43+
44+
describe 'with cache_dir_length' do
45+
let(:params) do
46+
{
47+
cache_dir_length: 2,
48+
cache_enable: ['/'],
49+
}
50+
end
51+
52+
it {
53+
expect(subject).to contain_file('cache_disk.conf')
54+
.with(content: %r{#{default_config}\nCacheDirLength 2})
55+
}
56+
end
57+
58+
describe 'with cache_dir_levels' do
59+
let(:params) do
60+
{
61+
cache_dir_levels: 2,
62+
cache_enable: ['/'],
63+
}
64+
end
65+
66+
it {
67+
expect(subject).to contain_file('cache_disk.conf')
68+
.with(content: %r{#{default_config}\nCacheDirLevels 2})
69+
}
70+
end
71+
end
72+
73+
context 'on a RedHat 8-based OS' do
74+
include_examples 'RedHat 8'
75+
76+
let(:params) do
77+
{
78+
cache_enable: ['/'],
79+
}
80+
end
81+
82+
let :pre_condition do
83+
'class{ "apache":
84+
default_mods => ["cache"],
85+
mod_dir => "/tmp/junk",
86+
}'
87+
end
88+
89+
it { is_expected.to compile }
90+
91+
it {
92+
expect(subject).to contain_file('cache_disk.conf')
93+
.with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/httpd/proxy"})
94+
}
95+
end
96+
97+
context 'on a FreeBSD OS' do
98+
include_examples 'FreeBSD 10'
99+
100+
let(:params) do
101+
{
102+
cache_enable: ['/'],
103+
}
104+
end
105+
106+
let :pre_condition do
107+
'class{ "apache":
108+
default_mods => ["cache"],
109+
mod_dir => "/tmp/junk",
110+
}'
111+
end
112+
113+
it { is_expected.to compile }
114+
115+
it {
116+
expect(subject).to contain_file('cache_disk.conf')
117+
.with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/mod_cache_disk"})
118+
}
119+
end
120+
end

0 commit comments

Comments
 (0)