diff --git a/REFERENCE.md b/REFERENCE.md index 6552a68..7393ca3 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -96,6 +96,7 @@ 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_file_extension`](#-caddy--config_file_extension) * [`config_dir`](#-caddy--config_dir) * [`purge_config_dir`](#-caddy--purge_config_dir) * [`config_enable_dir`](#-caddy--config_enable_dir) @@ -382,6 +383,14 @@ Caddyfile content. Default value: `undef` +##### `config_file_extension` + +Data type: `Variant[Enum[''], Pattern[/^\./]]` + +Default extension for config and virtual host files (must include leading `.`) + +Default value: `'.conf'` + ##### `config_dir` Data type: `Stdlib::Absolutepath` @@ -512,6 +521,7 @@ The following parameters are available in the `caddy::configfile` defined type: * [`content`](#-caddy--configfile--content) * [`config_dir`](#-caddy--configfile--config_dir) * [`enable_dir`](#-caddy--configfile--enable_dir) +* [`file_extension`](#-caddy--configfile--file_extension) ##### `ensure` @@ -553,6 +563,14 @@ Directory to symlink the config config file into (conf-enabled e.g.) if any. Default value: `$caddy::config_enable_dir` +##### `file_extension` + +Data type: `Variant[Enum[''], Pattern[/^\./]]` + +Default extension for the config file (must include leading `.`) + +Default value: `$caddy::config_file_extension` + ### `caddy::vhost` This defined type handles a Caddy virtual host @@ -584,6 +602,7 @@ The following parameters are available in the `caddy::vhost` defined type: * [`content`](#-caddy--vhost--content) * [`config_dir`](#-caddy--vhost--config_dir) * [`enable_dir`](#-caddy--vhost--enable_dir) +* [`file_extension`](#-caddy--vhost--file_extension) ##### `ensure` @@ -625,6 +644,14 @@ Directory to symlink the vhost config file into (sites-enabled e.g.) if any. Default value: `$caddy::vhost_enable_dir` +##### `file_extension` + +Data type: `Variant[Enum[''], Pattern[/^\./]]` + +Default extension for the vhost config file (must include leading `.`) + +Default value: `$caddy::config_file_extension` + ## Data types ### `Caddy::Config` diff --git a/manifests/config.pp b/manifests/config.pp index 1ae1649..7f88a14 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -72,7 +72,8 @@ $config_dir = $caddy::config_enable_dir.lest || { $caddy::config_dir } $vhost_dir = $caddy::vhost_enable_dir.lest || { $caddy::vhost_dir } epp('caddy/etc/caddy/caddyfile.epp', - include_dirs => unique([$config_dir, $vhost_dir]) + include_dirs => unique([$config_dir, $vhost_dir]), + file_extension => $caddy::config_file_extension, ) } } diff --git a/manifests/configfile.pp b/manifests/configfile.pp index 895d4a7..a9048af 100644 --- a/manifests/configfile.pp +++ b/manifests/configfile.pp @@ -15,6 +15,9 @@ # @param enable_dir # Directory to symlink the config config file into (conf-enabled e.g.) if any. # +# @param file_extension +# Default extension for the config file (must include leading `.`) +# # @example Configure Caddy logging # caddy::configfile { 'subdomain-log': # source => 'puppet:///modules/caddy/etc/caddy/config/logging.conf', @@ -40,6 +43,7 @@ Optional[String] $content = undef, Stdlib::Absolutepath $config_dir = $caddy::config_dir, Optional[Stdlib::Absolutepath] $enable_dir = $caddy::config_enable_dir, + Variant[Enum[''], Pattern[/^\./]] $file_extension = $caddy::config_file_extension, ) { include caddy @@ -52,7 +56,9 @@ default => 'file', } - file { "${config_dir}/${title}.conf": + $filename = "${title}${file_extension}" + + file { "${config_dir}/${filename}": ensure => $file_ensure, content => $content, source => $source, @@ -67,9 +73,9 @@ default => 'absent', } - file { "${enable_dir}/${title}.conf": + file { "${enable_dir}/${filename}": ensure => $symlink_ensure, - target => "${config_dir}/${title}.conf", + target => "${config_dir}/${filename}", require => Class['caddy::config'], notify => Class['caddy::service'], } diff --git a/manifests/init.pp b/manifests/init.pp index 2b092f2..29d429f 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -120,6 +120,9 @@ # @param caddyfile_content # Caddyfile content. # +# @param config_file_extension +# Default extension for config and virtual host files (must include leading `.`) +# # @param config_dir # Where to store Caddy configs. # Set this to /etc/caddy/conf-available to simulate nginx/apache behavior @@ -201,6 +204,7 @@ Boolean $purge_config_enable_dir = $purge_config_dir, Boolean $purge_vhost_dir = $purge_config_dir, Boolean $purge_vhost_enable_dir = $purge_vhost_dir, + Variant[Enum[''], Pattern[/^\./]] $config_file_extension = '.conf', Hash[String[1], Caddy::Config] $config_files = {}, Hash[String[1], Caddy::VirtualHost] $vhosts = {}, ) { diff --git a/manifests/vhost.pp b/manifests/vhost.pp index eaae231..aeec63b 100644 --- a/manifests/vhost.pp +++ b/manifests/vhost.pp @@ -15,6 +15,9 @@ # @param enable_dir # Directory to symlink the vhost config file into (sites-enabled e.g.) if any. # +# @param file_extension +# Default extension for the vhost config file (must include leading `.`) +# # @example Configure virtual host, based on source # caddy::vhost { 'example1': # source => 'puppet:///modules/caddy/etc/caddy/config/example1.conf', @@ -31,6 +34,7 @@ Optional[String] $content = undef, Stdlib::Absolutepath $config_dir = $caddy::vhost_dir, Optional[Stdlib::Absolutepath] $enable_dir = $caddy::vhost_enable_dir, + Variant[Enum[''], Pattern[/^\./]] $file_extension = $caddy::config_file_extension, ) { include caddy @@ -43,7 +47,9 @@ default => 'file', } - file { "${config_dir}/${title}.conf": + $filename = "${title}${file_extension}" + + file { "${config_dir}/${filename}": ensure => $file_ensure, content => $content, source => $source, @@ -58,9 +64,9 @@ default => 'absent', } - file { "${enable_dir}/${title}.conf": + file { "${enable_dir}/${filename}": ensure => $symlink_ensure, - target => "${config_dir}/${title}.conf", + target => "${config_dir}/${filename}", require => Class['caddy::config'], notify => Class['caddy::service'], } diff --git a/spec/acceptance/init_spec.rb b/spec/acceptance/init_spec.rb index 72bee9c..45c7884 100644 --- a/spec/acceptance/init_spec.rb +++ b/spec/acceptance/init_spec.rb @@ -113,6 +113,7 @@ class { 'caddy': config_enable_dir => '/etc/caddy/conf-enabled', vhost_dir => '/etc/caddy/sites-available', vhost_enable_dir => '/etc/caddy/sites-enabled', + config_file_extension => '.caddyfile', config_files => { admin_2020 => { content => "{\n admin localhost:2020\n}\n", diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index 4dfeafd..9eab336 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -511,6 +511,18 @@ end end + context 'with config_file_extension set' do + let(:params) { { config_file_extension: '.caddyfile' } } + + it { is_expected.to contain_file('/etc/caddy/Caddyfile').with_content(%r{^import /etc/caddy/config/\*\.caddyfile$}) } + end + + context 'with config_file_extension set to an empty string' do + let(:params) { { config_file_extension: '' } } + + it { is_expected.to contain_file('/etc/caddy/Caddyfile').with_content(%r{^import /etc/caddy/config/\*$}) } + end + context 'with configs set' do let(:params) do { diff --git a/spec/defines/configfile_spec.rb b/spec/defines/configfile_spec.rb index 5da4133..c9764c2 100644 --- a/spec/defines/configfile_spec.rb +++ b/spec/defines/configfile_spec.rb @@ -64,6 +64,12 @@ it { is_expected.to contain_file('/etc/caddy/config/example.conf').with_ensure('absent') } end + context 'with file_extension set' do + let(:params) { super().merge(file_extension: '.caddyfile') } + + it { is_expected.to contain_file('/etc/caddy/config/example.caddyfile').with_ensure('file') } + end + context 'with custom title' do let(:title) { 'test' } diff --git a/spec/defines/vhost_spec.rb b/spec/defines/vhost_spec.rb index 254cc8d..ce2b948 100644 --- a/spec/defines/vhost_spec.rb +++ b/spec/defines/vhost_spec.rb @@ -64,6 +64,12 @@ it { is_expected.to contain_file('/etc/caddy/config/example.conf').with_ensure('absent') } end + context 'with file_extension set' do + let(:params) { super().merge(file_extension: '.caddyfile') } + + it { is_expected.to contain_file('/etc/caddy/config/example.caddyfile').with_ensure('file') } + end + context 'with custom title' do let(:title) { 'test' } diff --git a/templates/etc/caddy/caddyfile.epp b/templates/etc/caddy/caddyfile.epp index 2d45d26..d79adb8 100644 --- a/templates/etc/caddy/caddyfile.epp +++ b/templates/etc/caddy/caddyfile.epp @@ -1,10 +1,11 @@ <%- | Array[String[1]] $include_dirs, + String $file_extension, | -%> # # THIS FILE IS MANAGED BY PUPPET # <%- $include_dirs.each |$dir| { -%> -import <%= $dir %>/*.conf +import <%= $dir %>/*<%= $file_extension %> <%- } -%>