Skip to content

Commit

Permalink
Allow to override Caddy config directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jay7x committed Dec 14, 2024
1 parent 40f2d1d commit e2b5aa8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 16 deletions.
27 changes: 27 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ The following parameters are available in the `caddy` class:
* [`manage_caddyfile`](#-caddy--manage_caddyfile)
* [`caddyfile_source`](#-caddy--caddyfile_source)
* [`caddyfile_content`](#-caddy--caddyfile_content)
* [`config_dir`](#-caddy--config_dir)
* [`purge_config_dir`](#-caddy--purge_config_dir)

##### <a name="-caddy--version"></a>`version`

Expand Down Expand Up @@ -366,6 +368,22 @@ Caddyfile content. Always preferred over caddyfile_source.

Default value: `undef`

##### <a name="-caddy--config_dir"></a>`config_dir`

Data type: `Stdlib::Absolutepath`

Where to store Caddy configs

Default value: `'/etc/caddy/config'`

##### <a name="-caddy--purge_config_dir"></a>`purge_config_dir`

Data type: `Boolean`

Whether to purge Caddy config directory.

Default value: `true`

## Defined types

### <a name="caddy--vhost"></a>`caddy::vhost`
Expand Down Expand Up @@ -397,6 +415,7 @@ The following parameters are available in the `caddy::vhost` defined type:
* [`ensure`](#-caddy--vhost--ensure)
* [`source`](#-caddy--vhost--source)
* [`content`](#-caddy--vhost--content)
* [`config_dir`](#-caddy--vhost--config_dir)

##### <a name="-caddy--vhost--ensure"></a>`ensure`

Expand All @@ -422,3 +441,11 @@ String with the caddy vhost configuration

Default value: `undef`

##### <a name="-caddy--vhost--config_dir"></a>`config_dir`

Data type: `Stdlib::Absolutepath`

Where to store the vhost config file

Default value: `$caddy::config_dir`

4 changes: 2 additions & 2 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
owner => 'root',
group => 'root',
;
['/etc/caddy/config']:
purge => true,
$caddy::config_dir:
purge => $caddy::purge_config_dir,
recurse => true,
;
}
Expand Down
8 changes: 8 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
# @param caddyfile_content
# Caddyfile content. Always preferred over caddyfile_source.
#
# @param config_dir
# Where to store Caddy configs
#
# @param purge_config_dir
# Whether to purge Caddy config directory.
#
class caddy (
String[1] $version = '2.0.0',
Optional[Enum['github','repo']] $install_method = undef,
Expand All @@ -132,6 +138,7 @@
Stdlib::Absolutepath $caddy_log_dir = '/var/log/caddy',
Stdlib::Absolutepath $caddy_home = '/var/lib/caddy',
Stdlib::Absolutepath $caddy_ssl_dir = '/etc/ssl/caddy',
Stdlib::Absolutepath $config_dir = '/etc/caddy/config',
Enum['personal', 'commercial'] $caddy_license = 'personal',
Enum['on','off'] $caddy_telemetry = 'off',
String[1] $caddy_features = 'http.git,http.filter,http.ipfilter',
Expand All @@ -155,6 +162,7 @@
Boolean $manage_caddyfile = true,
Stdlib::Filesource $caddyfile_source = 'puppet:///modules/caddy/etc/caddy/Caddyfile',
Optional[String[1]] $caddyfile_content = undef,
Boolean $purge_config_dir = true,
) {
case $caddy_architecture {
'x86_64', 'amd64': { $arch = 'amd64' }
Expand Down
19 changes: 15 additions & 4 deletions manifests/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
#
# @param ensure
# Make the vhost either present or absent
#
# @param source
# Source (path) for the caddy vhost configuration
#
# @param content
# String with the caddy vhost configuration
#
# @param config_dir
# Where to store the vhost config file
#
# @example Configure virtual host, based on source
# caddy::vhost { 'example1':
# source => 'puppet:///modules/caddy/etc/caddy/config/example1.conf',
Expand All @@ -18,12 +23,18 @@
# }
#
define caddy::vhost (
Enum['present','absent'] $ensure = 'present',
Optional[Stdlib::Filesource] $source = undef,
Optional[String] $content = undef,
Enum['present','absent'] $ensure = 'present',
Optional[Stdlib::Filesource] $source = undef,
Optional[String] $content = undef,
Stdlib::Absolutepath $config_dir = $caddy::config_dir,
) {
include caddy
file { "/etc/caddy/config/${title}.conf":

unless $source or $content {
fail('Either $source or $content must be specified')
}

file { "${config_dir}/${title}.conf":
ensure => stdlib::ensure($ensure, 'file'),
content => $content,
source => $source,
Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class { 'caddy':
}
caddy::vhost {'example1':
source => 'puppet:///modules/caddy/etc/caddy/config/example1.conf',
content => "localhost:3000 {\n respond \\'example1\\'\n}\n"
}
caddy::vhost {'example2':
Expand Down
33 changes: 24 additions & 9 deletions spec/defines/vhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,46 @@
let(:facts) do
facts
end
let(:pre_condition) { 'class { "caddy": config_dir => "/etc/caddy/config" }' }
let(:title) { 'example' }

context 'with source' do
let(:title) { 'example1' }
let(:params) { { source: 'puppet:///modules/caddy/etc/caddy/config/example1.conf' } }

it do
expect(subject).to contain_file('/etc/caddy/config/example1.conf').with(
is_expected.to contain_file('/etc/caddy/config/example.conf').with(
'ensure' => 'file',
'source' => 'puppet:///modules/caddy/etc/caddy/config/example1.conf',
'mode' => '0444',
'require' => 'Class[Caddy::Config]',
'notify' => 'Class[Caddy::Service]'
)
end

context 'with config_dir set' do
let(:params) { super().merge(config_dir: '/etc/caddy/conf.d') }

it { is_expected.to contain_file('/etc/caddy/conf.d/example.conf') }
end

context 'with ensure => absent' do
let(:params) { super().merge(ensure: 'absent') }

it { is_expected.to contain_file('/etc/caddy/config/example.conf').with_ensure('absent') }
end

context 'with custom title' do
let(:title) { 'test' }

it { is_expected.to contain_file('/etc/caddy/config/test.conf') }
end
end

context 'with content' do
let(:title) { 'example2' }
let(:params) { { content: 'localhost:2015' } }

it do
expect(subject).to contain_file('/etc/caddy/config/example2.conf').with(
is_expected.to contain_file('/etc/caddy/config/example.conf').with(
'ensure' => 'file',
'content' => 'localhost:2015',
'mode' => '0444',
Expand All @@ -39,11 +57,8 @@
end
end

context 'with ensure => absent' do
let(:title) { 'example3' }
let(:params) { { ensure: 'absent' } }

it { expect(subject).to contain_file('/etc/caddy/config/example3.conf').with_ensure('absent') }
context 'without source & content' do
it { is_expected.to compile.and_raise_error(%r{Either \$source or \$content must be specified}) }
end
end
end
Expand Down

0 comments on commit e2b5aa8

Please sign in to comment.