Skip to content

Added cache_disk #2521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions manifests/default_mods.pp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@
'FreeBSD': {
include apache::mod::actions
include apache::mod::authn_core
include apache::mod::cache
include apache::mod::disk_cache
include apache::mod::filter
include apache::mod::headers
include apache::mod::info
Expand Down
56 changes: 53 additions & 3 deletions manifests/mod/cache.pp
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
# @summary
# Installs `mod_cache`
#
#
# @param cache_ignore_headers
# Specifies HTTP header(s) that should not be stored in the cache.
#
# @param cache_default_expire
# The default duration to cache a document when no expiry date is specified.
#
# @param cache_max_expire
# The maximum time in seconds to cache a document
#
# @param cache_ignore_no_lastmod
# Ignore the fact that a response has no Last Modified header.
#
# @param cache_header
# Add an X-Cache header to the response.
#
# @param cache_lock
# Enable the thundering herd lock.
#
# @param cache_ignore_cache_control
# Ignore request to not serve cached content to client
#
# @see https://httpd.apache.org/docs/current/mod/mod_cache.html for additional documentation.
#
class apache::mod::cache {
::apache::mod { 'cache': }
class apache::mod::cache (
Array[String[1]] $cache_ignore_headers = [],
Optional[Integer] $cache_default_expire = undef,
Optional[Integer] $cache_max_expire = undef,
Optional[Apache::OnOff] $cache_ignore_no_lastmod = undef,
Optional[Apache::OnOff] $cache_header = undef,
Optional[Apache::OnOff] $cache_lock = undef,
Optional[Apache::OnOff] $cache_ignore_cache_control = undef,
) {
include apache
apache::mod { 'cache': }

$_configuration_file_name = 'cache.conf'

file { $_configuration_file_name:
ensure => file,
path => "${apache::mod_dir}/${_configuration_file_name}",
mode => $apache::file_mode,
content => epp('apache/mod/cache.conf.epp', {
cache_ignore_headers => $cache_ignore_headers,
cache_default_expire => $cache_default_expire,
cache_max_expire => $cache_max_expire,
cache_ignore_no_lastmod => $cache_ignore_no_lastmod,
cache_header => $cache_header,
cache_lock => $cache_lock,
cache_ignore_cache_control => $cache_ignore_cache_control,
}),
require => Exec["mkdir ${apache::mod_dir}"],
before => File[$apache::mod_dir],
notify => Class['apache::service'],
}
}
85 changes: 85 additions & 0 deletions manifests/mod/cache_disk.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# @summary
# Installs and configures `mod_cache_disk`.
#
# @description
# This will install an configure the proper module depending on the used apache version, so
# - mod_cache_disk for apache version >= 2.4
# - mod_disk_cache for older apache versions
#
# @param cache_root
# Defines the name of the directory on the disk to contain cache files.
# Default depends on the Apache version and operating system:
# - Debian: /var/cache/apache2/mod_cache_disk
# - FreeBSD: /var/cache/mod_cache_disk
# - Red Hat: /var/cache/httpd/proxy
#
# @param cache_enable
# Defines an array of directories to cache, the default is none
#
# @param cache_dir_length
# The number of characters in subdirectory names
#
# @param cache_dir_levels
# The number of levels of subdirectories in the cache.
#
# @param cache_max_filesize
# The maximum size (in bytes) of a document to be placed in the cache
#
# @param cache_ignore_headers
# DEPRECATED Ignore request to not serve cached content to client (included for compatibility reasons to support disk_cache)
#
# @param configuration_file_name
# DEPRECATED Name of module configuration file (used for the compatibility layer for disk_cache)
#
# @see https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html
#
class apache::mod::cache_disk (
Optional[Stdlib::Absolutepath] $cache_root = undef,
Array[String] $cache_enable = [],
Optional[Integer] $cache_dir_length = undef,
Optional[Integer] $cache_dir_levels = undef,
Optional[Integer] $cache_max_filesize = undef,
Optional[String] $cache_ignore_headers = undef,
Optional[String] $configuration_file_name = undef,
) {
include apache

if $cache_ignore_headers {
deprecation(
'apache::mod::cache_disk',
'The parameter cache_ignore_headers is deprecated. Please use apache::mod::cache::cache_ignore_headers instead.'
)
}

$_cache_root = $cache_root ? {
undef => $facts['os']['family'] ? {
'debian' => '/var/cache/apache2/mod_cache_disk',
'redhat' => '/var/cache/httpd/proxy',
'freebsd' => '/var/cache/mod_cache_disk',
},
default => $cache_root,
}
$_configuration_file_name = pick($configuration_file_name, 'cache_disk.conf')
$_class_name = 'apache::mod::cache_disk'

apache::mod { 'cache_disk': }

Class['apache::mod::cache'] -> Class[$_class_name]

file { $_configuration_file_name:
ensure => file,
path => "${apache::mod_dir}/${_configuration_file_name}",
mode => $apache::file_mode,
content => epp('apache/mod/cache_disk.conf.epp', {
cache_root => $_cache_root,
cache_enable => $cache_enable,
cache_dir_length => $cache_dir_length,
cache_dir_levels => $cache_dir_levels,
cache_max_filesize => $cache_max_filesize,
cache_ignore_headers => $cache_ignore_headers,
}),
require => Exec["mkdir ${apache::mod_dir}"],
before => File[$apache::mod_dir],
notify => Class['apache::service'],
}
}
45 changes: 12 additions & 33 deletions manifests/mod/disk_cache.pp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# @summary
# Installs and configures `mod_disk_cache`.
#
#
# @param cache_root
# Defines the name of the directory on the disk to contain cache files.
# Default depends on the Apache version and operating system:
# - Debian: /var/cache/apache2/mod_cache_disk
# - FreeBSD: /var/cache/mod_cache_disk
# - Red Hat: /var/cache/httpd/proxy
#
# @param cache_ignore_headers
# Specifies HTTP header(s) that should not be stored in the cache.
Expand All @@ -17,44 +16,24 @@
# You can then control this behaviour in individual vhosts by explicitly defining CacheEnable.
#
# @note
# On Apache 2.4, mod_cache_disk installed.
# Apache 2.2, mod_disk_cache installed. On Apache 2.4, mod_cache_disk installed.
# This class is deprecated, use mode_cache_disk instead
#
# @see https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html for additional documentation.
# @see https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html for additional documentation on version 2.4.
#
class apache::mod::disk_cache (
Optional[Stdlib::Absolutepath] $cache_root = undef,
Optional[String] $cache_ignore_headers = undef,
Boolean $default_cache_enable = true,
) {
include apache
if $cache_root {
$_cache_root = $cache_root
} else {
$_cache_root = $facts['os']['family'] ? {
'Debian' => '/var/cache/apache2/mod_cache_disk',
'RedHat' => '/var/cache/httpd/proxy',
'FreeBSD' => '/var/cache/mod_cache_disk',
}
}

apache::mod { 'cache_disk': }

Class['apache::mod::cache'] -> Class['apache::mod::disk_cache']

$parameters = {
'default_cache_enable' => $default_cache_enable,
'_cache_root' => $_cache_root,
'cache_ignore_headers' => $cache_ignore_headers,
}
deprecation('apache::mod::disk_cache', 'This class is deprecated; please use apache::mod::cache_disk')

# Template uses $_cache_root
file { 'disk_cache.conf':
ensure => file,
path => "${apache::mod_dir}/disk_cache.conf",
mode => $apache::file_mode,
content => epp('apache/mod/disk_cache.conf.epp', $parameters),
require => Exec["mkdir ${apache::mod_dir}"],
before => File[$apache::mod_dir],
notify => Class['apache::service'],
class { 'apache::mod::cache_disk':
cache_root => $cache_root,
cache_enable => ['/'],
cache_ignore_headers => $cache_ignore_headers,
cache_dir_length => 1,
cache_dir_levels => 2,
configuration_file_name => 'cache_disk.conf'
}
}
120 changes: 120 additions & 0 deletions spec/classes/mod/cache_disk_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'apache::mod::cache_disk', type: :class do
context 'on a Debian OS' do
include_examples 'Debian 11'

let(:params) do
{
cache_enable: ['/'],
}
end

let :pre_condition do
'class{ "apache":
default_mods => ["cache"],
mod_dir => "/tmp/junk",
}'
end

it { is_expected.to compile }
it { is_expected.to contain_class('apache::mod::cache_disk') }
it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') }
it { is_expected.to contain_apache__mod('cache_disk') }

default_config = %r{CacheEnable disk /\nCacheRoot "/var/cache/apache2/mod_cache_disk"}

it { is_expected.to contain_file('cache_disk.conf').with(content: default_config) }

describe 'with multiple cache_enable parameters' do
let(:params) do
{
cache_enable: ['/', '/something'],
}
end

it {
expect(subject).to contain_file('cache_disk.conf')
.with(content: %r{CacheEnable disk /\nCacheEnable disk /something\nCacheRoot "/var/cache/apache2/mod_cache_disk"})
}
end

describe 'with cache_dir_length' do
let(:params) do
{
cache_dir_length: 2,
cache_enable: ['/'],
}
end

it {
expect(subject).to contain_file('cache_disk.conf')
.with(content: %r{#{default_config}\nCacheDirLength 2})
}
end

describe 'with cache_dir_levels' do
let(:params) do
{
cache_dir_levels: 2,
cache_enable: ['/'],
}
end

it {
expect(subject).to contain_file('cache_disk.conf')
.with(content: %r{#{default_config}\nCacheDirLevels 2})
}
end
end

context 'on a RedHat 8-based OS' do
include_examples 'RedHat 8'

let(:params) do
{
cache_enable: ['/'],
}
end

let :pre_condition do
'class{ "apache":
default_mods => ["cache"],
mod_dir => "/tmp/junk",
}'
end

it { is_expected.to compile }

it {
expect(subject).to contain_file('cache_disk.conf')
.with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/httpd/proxy"})
}
end

context 'on a FreeBSD OS' do
include_examples 'FreeBSD 10'

let(:params) do
{
cache_enable: ['/'],
}
end

let :pre_condition do
'class{ "apache":
default_mods => ["cache"],
mod_dir => "/tmp/junk",
}'
end

it { is_expected.to compile }

it {
expect(subject).to contain_file('cache_disk.conf')
.with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/mod_cache_disk"})
}
end
end
Loading