From 81be2affd99f010a98a6f56eb454518e5cc4f435 Mon Sep 17 00:00:00 2001 From: Yury Bushmelev Date: Sat, 14 Dec 2024 14:09:59 +0800 Subject: [PATCH] Allow to override Caddy config directory --- REFERENCE.md | 27 +++++++++++++++++++++++++++ manifests/config.pp | 4 ++-- manifests/init.pp | 8 ++++++++ manifests/vhost.pp | 19 +++++++++++++++---- spec/acceptance/init_spec.rb | 2 +- spec/defines/vhost_spec.rb | 33 ++++++++++++++++++++++++--------- 6 files changed, 77 insertions(+), 16 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index bd77abe..37bb655 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -90,6 +90,8 @@ 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_dir`](#-caddy--config_dir) +* [`purge_config_dir`](#-caddy--purge_config_dir) ##### `version` @@ -366,6 +368,22 @@ Caddyfile content. Always preferred over caddyfile_source. Default value: `undef` +##### `config_dir` + +Data type: `Stdlib::Absolutepath` + +Where to store Caddy configs + +Default value: `'/etc/caddy/config'` + +##### `purge_config_dir` + +Data type: `Boolean` + +Whether to purge Caddy config directory. + +Default value: `true` + ## Defined types ### `caddy::vhost` @@ -397,6 +415,7 @@ The following parameters are available in the `caddy::vhost` defined type: * [`ensure`](#-caddy--vhost--ensure) * [`source`](#-caddy--vhost--source) * [`content`](#-caddy--vhost--content) +* [`config_dir`](#-caddy--vhost--config_dir) ##### `ensure` @@ -422,3 +441,11 @@ String with the caddy vhost configuration Default value: `undef` +##### `config_dir` + +Data type: `Stdlib::Absolutepath` + +Where to store the vhost config file + +Default value: `$caddy::config_dir` + diff --git a/manifests/config.pp b/manifests/config.pp index 70553e2..0edbc04 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -22,8 +22,8 @@ owner => 'root', group => 'root', ; - ['/etc/caddy/config']: - purge => true, + $caddy::config_dir: + purge => $caddy::purge_config_dir, recurse => true, ; } diff --git a/manifests/init.pp b/manifests/init.pp index a0b33d9..9b8973b 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -120,6 +120,12 @@ # @param caddyfile_content # Caddyfile content. Always preferred over caddyfile_source. # +# @param config_dir +# Where to store Caddy configs +# +# @param purge_config_dir +# Whether to purge Caddy config directory. +# class caddy ( String[1] $version = '2.0.0', Optional[Enum['github','repo']] $install_method = undef, @@ -132,6 +138,7 @@ Stdlib::Absolutepath $caddy_log_dir = '/var/log/caddy', Stdlib::Absolutepath $caddy_home = '/var/lib/caddy', Stdlib::Absolutepath $caddy_ssl_dir = '/etc/ssl/caddy', + Stdlib::Absolutepath $config_dir = '/etc/caddy/config', Enum['personal', 'commercial'] $caddy_license = 'personal', Enum['on','off'] $caddy_telemetry = 'off', String[1] $caddy_features = 'http.git,http.filter,http.ipfilter', @@ -155,6 +162,7 @@ Boolean $manage_caddyfile = true, Stdlib::Filesource $caddyfile_source = 'puppet:///modules/caddy/etc/caddy/Caddyfile', Optional[String[1]] $caddyfile_content = undef, + Boolean $purge_config_dir = true, ) { case $caddy_architecture { 'x86_64', 'amd64': { $arch = 'amd64' } diff --git a/manifests/vhost.pp b/manifests/vhost.pp index 6deaf53..4e5cef7 100644 --- a/manifests/vhost.pp +++ b/manifests/vhost.pp @@ -2,11 +2,16 @@ # # @param ensure # Make the vhost either present or absent +# # @param source # Source (path) for the caddy vhost configuration +# # @param content # String with the caddy vhost configuration # +# @param config_dir +# Where to store the vhost config file +# # @example Configure virtual host, based on source # caddy::vhost { 'example1': # source => 'puppet:///modules/caddy/etc/caddy/config/example1.conf', @@ -18,12 +23,18 @@ # } # define caddy::vhost ( - Enum['present','absent'] $ensure = 'present', - Optional[Stdlib::Filesource] $source = undef, - Optional[String] $content = undef, + Enum['present','absent'] $ensure = 'present', + Optional[Stdlib::Filesource] $source = undef, + Optional[String] $content = undef, + Stdlib::Absolutepath $config_dir = $caddy::config_dir, ) { include caddy - file { "/etc/caddy/config/${title}.conf": + + if ($ensure == 'present') and !($source or $content) { + fail('Either $source or $content must be specified when $ensure is "present"') + } + + file { "${config_dir}/${title}.conf": ensure => stdlib::ensure($ensure, 'file'), content => $content, source => $source, diff --git a/spec/acceptance/init_spec.rb b/spec/acceptance/init_spec.rb index 3282d62..46f9677 100644 --- a/spec/acceptance/init_spec.rb +++ b/spec/acceptance/init_spec.rb @@ -93,7 +93,7 @@ class { 'caddy': } caddy::vhost {'example1': - source => 'puppet:///modules/caddy/etc/caddy/config/example1.conf', + content => "localhost:3000 {\n respond \\'example1\\'\n}\n" } caddy::vhost {'example2': diff --git a/spec/defines/vhost_spec.rb b/spec/defines/vhost_spec.rb index da3eae5..0cae95e 100644 --- a/spec/defines/vhost_spec.rb +++ b/spec/defines/vhost_spec.rb @@ -8,13 +8,14 @@ let(:facts) do facts end + let(:pre_condition) { 'class { "caddy": config_dir => "/etc/caddy/config" }' } + let(:title) { 'example' } context 'with source' do - let(:title) { 'example1' } let(:params) { { source: 'puppet:///modules/caddy/etc/caddy/config/example1.conf' } } it do - expect(subject).to contain_file('/etc/caddy/config/example1.conf').with( + is_expected.to contain_file('/etc/caddy/config/example.conf').with( 'ensure' => 'file', 'source' => 'puppet:///modules/caddy/etc/caddy/config/example1.conf', 'mode' => '0444', @@ -22,14 +23,31 @@ 'notify' => 'Class[Caddy::Service]' ) end + + context 'with config_dir set' do + let(:params) { super().merge(config_dir: '/etc/caddy/conf.d') } + + it { is_expected.to contain_file('/etc/caddy/conf.d/example.conf') } + end + + context 'with ensure => absent' do + let(:params) { super().merge(ensure: 'absent') } + + it { is_expected.to contain_file('/etc/caddy/config/example.conf').with_ensure('absent') } + end + + context 'with custom title' do + let(:title) { 'test' } + + it { is_expected.to contain_file('/etc/caddy/config/test.conf') } + end end context 'with content' do - let(:title) { 'example2' } let(:params) { { content: 'localhost:2015' } } it do - expect(subject).to contain_file('/etc/caddy/config/example2.conf').with( + is_expected.to contain_file('/etc/caddy/config/example.conf').with( 'ensure' => 'file', 'content' => 'localhost:2015', 'mode' => '0444', @@ -39,11 +57,8 @@ end end - context 'with ensure => absent' do - let(:title) { 'example3' } - let(:params) { { ensure: 'absent' } } - - it { expect(subject).to contain_file('/etc/caddy/config/example3.conf').with_ensure('absent') } + context 'without source & content' do + it { is_expected.to compile.and_raise_error(%r{Either \$source or \$content must be specified}) } end end end