Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to change default config files extension #120

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -382,6 +383,14 @@ Caddyfile content.

Default value: `undef`

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

Data type: `Variant[Enum[''], Pattern[/^\./]]`

Default extension for config and virtual host files (must include leading `.`)

Default value: `'.conf'`

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

Data type: `Stdlib::Absolutepath`
Expand Down Expand Up @@ -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)

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

Expand Down Expand Up @@ -553,6 +563,14 @@ Directory to symlink the config config file into (conf-enabled e.g.) if any.

Default value: `$caddy::config_enable_dir`

##### <a name="-caddy--configfile--file_extension"></a>`file_extension`

Data type: `Variant[Enum[''], Pattern[/^\./]]`

Default extension for the config file (must include leading `.`)

Default value: `$caddy::config_file_extension`

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

This defined type handles a Caddy virtual host
Expand Down Expand Up @@ -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)

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

Expand Down Expand Up @@ -625,6 +644,14 @@ Directory to symlink the vhost config file into (sites-enabled e.g.) if any.

Default value: `$caddy::vhost_enable_dir`

##### <a name="-caddy--vhost--file_extension"></a>`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

### <a name="Caddy--Config"></a>`Caddy::Config`
Expand Down
3 changes: 2 additions & 1 deletion manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
Expand Down
12 changes: 9 additions & 3 deletions manifests/configfile.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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

Expand All @@ -52,7 +56,9 @@
default => 'file',
}

file { "${config_dir}/${title}.conf":
$filename = "${title}${file_extension}"
jay7x marked this conversation as resolved.
Show resolved Hide resolved

file { "${config_dir}/${filename}":
ensure => $file_ensure,
content => $content,
source => $source,
Expand All @@ -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'],
}
Expand Down
4 changes: 4 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {},
) {
Expand Down
12 changes: 9 additions & 3 deletions manifests/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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

Expand All @@ -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,
Expand All @@ -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'],
}
Expand Down
1 change: 1 addition & 0 deletions spec/acceptance/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
6 changes: 6 additions & 0 deletions spec/defines/configfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }

Expand Down
6 changes: 6 additions & 0 deletions spec/defines/vhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }

Expand Down
3 changes: 2 additions & 1 deletion templates/etc/caddy/caddyfile.epp
Original file line number Diff line number Diff line change
@@ -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 %>
<%- } -%>
Loading