Skip to content

Commit

Permalink
Merge pull request #116 from jay7x/manage_configs
Browse files Browse the repository at this point in the history
Improve caddy config files management
  • Loading branch information
jay7x authored Dec 17, 2024
2 parents a48900d + 1ded83c commit 4080c26
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 38 deletions.
96 changes: 94 additions & 2 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

* [`caddy::vhost`](#caddy--vhost): This defined type handles the Caddy virtual hosts.

### Data types

* [`Caddy::VirtualHost`](#Caddy--VirtualHost): Caddy virtual host type

## Classes

### <a name="caddy"></a>`caddy`
Expand Down Expand Up @@ -87,6 +91,12 @@ 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)
* [`config_dir`](#-caddy--config_dir)
* [`purge_config_dir`](#-caddy--purge_config_dir)
* [`vhosts`](#-caddy--vhosts)

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

Expand Down Expand Up @@ -339,6 +349,54 @@ Whether to install or remove the caddy package. Only relevant when $install_meth

Default value: `$version`

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

Data type: `Boolean`

Whether to manage Caddyfile.

Default value: `true`

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

Data type: `Optional[Stdlib::Filesource]`

Caddyfile source.

Default value: `undef`

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

Data type: `Optional[String[1]]`

Caddyfile content.

Default value: `undef`

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

Data type: `Stdlib::Absolutepath`

Where to store Caddy configs

Default value: `'/etc/caddy/config'`

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

Data type: `Boolean`

Whether to purge Caddy config directory.

Default value: `true`

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

Data type: `Hash[String[1], Caddy::VirtualHost]`

List of virtual hosts to create.

Default value: `{}`

## Defined types

### <a name="caddy--vhost"></a>`caddy::vhost`
Expand Down Expand Up @@ -367,22 +425,56 @@ caddy::vhost { 'example2:

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)

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

Data type: `Enum['present','absent']`

Make the vhost either present or absent

Default value: `'present'`

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

Data type: `Optional[Stdlib::Filesource]`

source (path) for the caddy vhost configuration
Source (path) for the caddy vhost configuration

Default value: `undef`

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

Data type: `Optional[String]`

string with the caddy vhost configuration
String with the caddy vhost configuration

Default value: `undef`

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

Data type: `Stdlib::Absolutepath`

Where to store the vhost config file

Default value: `$caddy::config_dir`

## Data types

### <a name="Caddy--VirtualHost"></a>`Caddy::VirtualHost`

Caddy virtual host type

Alias of

```puppet
Struct[{
ensure => Optional[Enum['absent', 'present']],
source => Optional[Stdlib::Filesource],
content => Optional[String[1]],
}]
```

5 changes: 0 additions & 5 deletions files/etc/caddy/Caddyfile

This file was deleted.

32 changes: 23 additions & 9 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,31 @@
owner => 'root',
group => 'root',
;
$caddy::config_dir:
purge => $caddy::purge_config_dir,
recurse => true,
;
}

if $caddy::manage_caddyfile {
# 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,
)
}
}

['/etc/caddy/Caddyfile']:
file { '/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,
;
owner => $caddy::caddy_user,
group => $caddy::caddy_group,
source => $real_source,
content => $real_content,
}
}
}
30 changes: 30 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@
# @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.
#
# @param config_dir
# Where to store Caddy configs
#
# @param purge_config_dir
# Whether to purge Caddy config directory.
#
# @param vhosts
# List of virtual hosts to create.
#
class caddy (
String[1] $version = '2.0.0',
Optional[Enum['github','repo']] $install_method = undef,
Expand All @@ -123,6 +141,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',
Expand All @@ -143,6 +162,11 @@
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 = undef,
Optional[String[1]] $caddyfile_content = undef,
Boolean $purge_config_dir = true,
Hash[String[1], Caddy::VirtualHost] $vhosts = {},
) {
case $caddy_architecture {
'x86_64', 'amd64': { $arch = 'amd64' }
Expand Down Expand Up @@ -179,6 +203,12 @@
contain caddy::config
contain caddy::service

$vhosts.each |String[1] $name, Caddy::VirtualHost $vhost| {
caddy::vhost { $name:
* => $vhost,
}
}

Class['caddy::install']
-> Class['caddy::config']
~> Class['caddy::service']
Expand Down
28 changes: 22 additions & 6 deletions manifests/vhost.pp
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# @summary This defined type handles the Caddy virtual hosts.
#
# @param source source (path) for the caddy vhost configuration
# @param content string with the caddy vhost configuration
# @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':
Expand All @@ -14,12 +23,19 @@
# }
#
define caddy::vhost (
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":
ensure => file,

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,
mode => '0444',
Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
56 changes: 55 additions & 1 deletion spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -319,6 +320,59 @@

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

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
{
vhosts: {
'h1.example.com': {
source: 'http://example.com/test-example-com.conf',
},
'h2.example.com': {
content: "localhost:1234{\n file_server\n}\n",
},
'h3.example.com': {
ensure: 'absent',
}
}
}
end

it { is_expected.to contain_file('/etc/caddy/config/h1.example.com.conf').with_source('http://example.com/test-example-com.conf') }
it { is_expected.to contain_file('/etc/caddy/config/h2.example.com.conf').with_content("localhost:1234{\n file_server\n}\n") }
it { is_expected.to contain_file('/etc/caddy/config/h3.example.com.conf').with_ensure('absent') }
end
end
end
end
Loading

0 comments on commit 4080c26

Please sign in to comment.