From e38598c70d5d5cbd837049ec9d0b0e33b8bc4e9c Mon Sep 17 00:00:00 2001 From: Yury Bushmelev Date: Sat, 14 Dec 2024 16:28:16 +0800 Subject: [PATCH] fix: Use caddyfile template over source --- REFERENCE.md | 6 +++--- files/etc/caddy/Caddyfile | 5 ----- manifests/config.pp | 16 +++++++++++++--- manifests/init.pp | 4 ++-- spec/classes/init_spec.rb | 16 +++++++++++++++- templates/etc/caddy/caddyfile.epp | 8 ++++++++ 6 files changed, 41 insertions(+), 14 deletions(-) delete mode 100644 files/etc/caddy/Caddyfile create mode 100644 templates/etc/caddy/caddyfile.epp diff --git a/REFERENCE.md b/REFERENCE.md index 28c6ec7..ee101b9 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -359,17 +359,17 @@ Default value: `true` ##### `caddyfile_source` -Data type: `Stdlib::Filesource` +Data type: `Optional[Stdlib::Filesource]` Caddyfile source. -Default value: `'puppet:///modules/caddy/etc/caddy/Caddyfile'` +Default value: `undef` ##### `caddyfile_content` Data type: `Optional[String[1]]` -Caddyfile content. Always preferred over caddyfile_source. +Caddyfile content. Default value: `undef` diff --git a/files/etc/caddy/Caddyfile b/files/etc/caddy/Caddyfile deleted file mode 100644 index 4a1f549..0000000 --- a/files/etc/caddy/Caddyfile +++ /dev/null @@ -1,5 +0,0 @@ -# -# THIS FILE IS MANAGED BY PUPPET -# - -import config/*.conf diff --git a/manifests/config.pp b/manifests/config.pp index 0edbc04..8fe685f 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -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, } } } diff --git a/manifests/init.pp b/manifests/init.pp index 1ee1ef1..ec49d5f 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -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 @@ -163,7 +163,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, Hash[String[1], Caddy::VirtualHost] $vhosts = {}, diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index ee4c0fd..08b054e 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -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 @@ -338,6 +339,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 + context 'with vhosts set' do let(:params) do { diff --git a/templates/etc/caddy/caddyfile.epp b/templates/etc/caddy/caddyfile.epp new file mode 100644 index 0000000..b65848f --- /dev/null +++ b/templates/etc/caddy/caddyfile.epp @@ -0,0 +1,8 @@ +<%- | + String[1] $config_dir, +| -%> +# +# THIS FILE IS MANAGED BY PUPPET +# + +import <%= $config_dir %>/*.conf