Skip to content

Commit

Permalink
Use template for Caddyfile
Browse files Browse the repository at this point in the history
  • Loading branch information
jay7x committed Dec 16, 2024
1 parent 81be2af commit cf6f869
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
6 changes: 3 additions & 3 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,17 @@ Default value: `true`

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

Data type: `Stdlib::Filesource`
Data type: `Optional[Stdlib::Filesource]`

Caddyfile source.

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

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

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

Caddyfile content. Always preferred over caddyfile_source.
Caddyfile content.

Default value: `undef`

Expand Down
5 changes: 0 additions & 5 deletions files/etc/caddy/Caddyfile

This file was deleted.

16 changes: 13 additions & 3 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,24 @@
}

if $caddy::manage_caddyfile {
# caddyfile_content is always preferred over caddyfile_source when set
# Prefer source over content if both are defined
# Fallback to the bundled template if both are unset
$real_source = $caddy::caddyfile_source
$real_content = if $caddy::caddyfile_source { undef } else {
$caddy::caddyfile_content.lest || {
epp('caddy/etc/caddy/caddyfile.epp',
config_dir => $caddy::config_dir,
)
}
}

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,
source => $real_source,
content => $real_content,
}
}
}
4 changes: 2 additions & 2 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
# Caddyfile source.
#
# @param caddyfile_content
# Caddyfile content. Always preferred over caddyfile_source.
# Caddyfile content.
#
# @param config_dir
# Where to store Caddy configs
Expand Down Expand Up @@ -160,7 +160,7 @@
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[Stdlib::Filesource] $caddyfile_source = undef,
Optional[String[1]] $caddyfile_content = undef,
Boolean $purge_config_dir = true,
) {
Expand Down
16 changes: 15 additions & 1 deletion spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
'owner' => 'caddy',
'group' => 'caddy',
'mode' => '0444',
'source' => 'puppet:///modules/caddy/etc/caddy/Caddyfile'
'source' => nil,
'content' => %r{^import /etc/caddy/config/\*\.conf$}
).
that_requires('File[/etc/caddy]')
end
Expand Down Expand Up @@ -337,6 +338,19 @@

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

context 'with both caddyfile_source and caddyfile_content set' do
let(:params) do
{
caddyfile_source: 'http://example.com/Caddyfile',
caddyfile_content: "localhost\nfile_server\n",
}
end

it 'prefers source over content' do
is_expected.to contain_file('/etc/caddy/Caddyfile').with_source('http://example.com/Caddyfile').with_content(nil)
end
end
end
end
end
8 changes: 8 additions & 0 deletions templates/etc/caddy/caddyfile.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%- |
String[1] $config_dir,
| -%>
#
# THIS FILE IS MANAGED BY PUPPET
#

import <%= $config_dir %>/*.conf

0 comments on commit cf6f869

Please sign in to comment.