Skip to content

Commit

Permalink
Allow to override Caddyfile content
Browse files Browse the repository at this point in the history
  • Loading branch information
jay7x committed Dec 14, 2024
1 parent 1299b73 commit fe090ae
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
27 changes: 27 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ The following parameters are available in the `caddy` class:
* [`repo_settings`](#-caddy--repo_settings)
* [`package_name`](#-caddy--package_name)
* [`package_ensure`](#-caddy--package_ensure)
* [`manage_caddyfile`](#-caddy--manage_caddyfile)
* [`caddyfile_source`](#-caddy--caddyfile_source)
* [`caddyfile_content`](#-caddy--caddyfile_content)

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

Expand Down Expand Up @@ -339,6 +342,30 @@ Whether to install or remove the caddy package. Only relevant when $install_meth

Default value: `$version`

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

Data type: `Boolean`

Whether to manage Caddyfile.

Default value: `true`

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

Data type: `Stdlib::Filesource`

Caddyfile source.

Default value: `'puppet:///modules/caddy/etc/caddy/Caddyfile'`

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

Data type: `Optional[String[1]]`

Caddyfile content. Always preferred over caddyfile_source.

Default value: `undef`

## Defined types

### <a name="caddy--vhost"></a>`caddy::vhost`
Expand Down
20 changes: 12 additions & 8 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@
owner => 'root',
group => 'root',
;

['/etc/caddy/Caddyfile']:
ensure => file,
mode => '0444',
source => 'puppet:///modules/caddy/etc/caddy/Caddyfile',
require => File['/etc/caddy'],
;

['/etc/caddy/config']:
purge => true,
recurse => true,
;
}

if $caddy::manage_caddyfile {
# caddyfile_content is always preferred over caddyfile_source when set
file { '/etc/caddy/Caddyfile':
ensure => file,
mode => '0444',
owner => $caddy::caddy_user,
group => $caddy::caddy_group,
source => if $caddy::caddyfile_content { undef } else { $caddy::caddyfile_source },
content => $caddy::caddyfile_content,
}
}
}
12 changes: 12 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@
# @param package_ensure
# Whether to install or remove the caddy package. Only relevant when $install_method is 'repo'.
#
# @param manage_caddyfile
# Whether to manage Caddyfile.
#
# @param caddyfile_source
# Caddyfile source.
#
# @param caddyfile_content
# Caddyfile content. Always preferred over caddyfile_source.
#
class caddy (
String[1] $version = '2.0.0',
Optional[Enum['github','repo']] $install_method = undef,
Expand Down Expand Up @@ -143,6 +152,9 @@
Hash[String[1],Any] $repo_settings = {},
String[1] $package_name = 'caddy',
String[1] $package_ensure = $version,
Boolean $manage_caddyfile = true,
Stdlib::Filesource $caddyfile_source = 'puppet:///modules/caddy/etc/caddy/Caddyfile',
Optional[String[1]] $caddyfile_content = undef,
) {
case $caddy_architecture {
'x86_64', 'amd64': { $arch = 'amd64' }
Expand Down
18 changes: 18 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,24 @@

it { is_expected.to contain_service('caddy').with_enable(false) }
end

context 'with manage_caddyfile => false' do
let(:params) { { manage_caddyfile: false } }

it { is_expected.not_to contain_file('/etc/caddy/Caddyfile') }
end

context 'with caddyfile_source set' do
let(:params) { { caddyfile_source: 'http://example.com/Caddyfile' } }

it { is_expected.to contain_file('/etc/caddy/Caddyfile').with_source('http://example.com/Caddyfile').with_content(nil) }
end

context 'with caddyfile_content set' do
let(:params) { { caddyfile_content: "localhost\nfile_server\n" } }

it { is_expected.to contain_file('/etc/caddy/Caddyfile').with_source(nil).with_content("localhost\nfile_server\n") }
end
end
end
end

0 comments on commit fe090ae

Please sign in to comment.