From 0e6d015d4ae6c6f3f07879fa86392d1379ac2769 Mon Sep 17 00:00:00 2001 From: Yury Bushmelev Date: Sat, 14 Dec 2024 12:39:59 +0800 Subject: [PATCH] Allow to override Caddyfile content --- REFERENCE.md | 27 +++++++++++++++++++++++++++ manifests/config.pp | 20 ++++++++++++-------- manifests/init.pp | 12 ++++++++++++ spec/classes/init_spec.rb | 18 ++++++++++++++++++ 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index fb5cc41..71a9022 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -87,6 +87,9 @@ The following parameters are available in the `caddy` class: * [`repo_settings`](#-caddy--repo_settings) * [`package_name`](#-caddy--package_name) * [`package_ensure`](#-caddy--package_ensure) +* [`manage_caddyfile`](#-caddy--manage_caddyfile) +* [`caddyfile_source`](#-caddy--caddyfile_source) +* [`caddyfile_content`](#-caddy--caddyfile_content) ##### `version` @@ -339,6 +342,30 @@ Whether to install or remove the caddy package. Only relevant when $install_meth Default value: `$version` +##### `manage_caddyfile` + +Data type: `Boolean` + +Whether to manage Caddyfile. + +Default value: `true` + +##### `caddyfile_source` + +Data type: `Optional[Stdlib::Filesource]` + +Caddyfile source. + +Default value: `'puppet:///modules/caddy/etc/caddy/Caddyfile'` + +##### `caddyfile_content` + +Data type: `Optional[String[1]]` + +Caddyfile content. Always preferred over caddyfile_source. + +Default value: `undef` + ## Defined types ### `caddy::vhost` diff --git a/manifests/config.pp b/manifests/config.pp index d4d3eb9..70553e2 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -22,17 +22,21 @@ owner => 'root', group => 'root', ; - - ['/etc/caddy/Caddyfile']: - ensure => file, - mode => '0444', - source => 'puppet:///modules/caddy/etc/caddy/Caddyfile', - require => File['/etc/caddy'], - ; - ['/etc/caddy/config']: purge => true, recurse => true, ; } + + if $caddy::manage_caddyfile { + # caddyfile_content is always preferred over caddyfile_source when set + 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, + } + } } diff --git a/manifests/init.pp b/manifests/init.pp index 7147b8a..f5008dc 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -111,6 +111,15 @@ # @param package_ensure # Whether to install or remove the caddy package. Only relevant when $install_method is 'repo'. # +# @param manage_caddyfile +# Whether to manage Caddyfile. +# +# @param caddyfile_source +# Caddyfile source. +# +# @param caddyfile_content +# Caddyfile content. Always preferred over caddyfile_source. +# class caddy ( String[1] $version = '2.0.0', Optional[Enum['github','repo']] $install_method = undef, @@ -143,6 +152,9 @@ Hash[String[1],Any] $repo_settings = {}, String[1] $package_name = 'caddy', String[1] $package_ensure = $version, + Boolean $manage_caddyfile = true, + Optional[Stdlib::Filesource] $caddyfile_source = 'puppet:///modules/caddy/etc/caddy/Caddyfile', + Optional[String[1]] $caddyfile_content = undef, ) { case $caddy_architecture { 'x86_64', 'amd64': { $arch = 'amd64' } diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index e7fbcc1..48de283 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -319,6 +319,24 @@ it { is_expected.to contain_service('caddy').with_enable(false) } end + + context 'with manage_caddyfile => false' do + let(:params) { { manage_caddyfile: false } } + + it { is_expected.not_to contain_file('/etc/caddy/Caddyfile') } + end + + context 'with caddyfile_source set' do + let(:params) { { caddyfile_source: 'http://example.com/Caddyfile' } } + + it { is_expected.to contain_file('/etc/caddy/Caddyfile').with_source('http://example.com/Caddyfile').with_content(nil) } + end + + context 'with caddyfile_content set' do + let(:params) { { caddyfile_content: "localhost\nfile_server\n" } } + + it { is_expected.to contain_file('/etc/caddy/Caddyfile').with_source(nil).with_content("localhost\nfile_server\n") } + end end end end