Skip to content

Commit

Permalink
Merge pull request #118 from jay7x/vhosts
Browse files Browse the repository at this point in the history
Implement vhost_dir and vhost_enable_dir support
  • Loading branch information
jay7x authored Dec 21, 2024
2 parents 4080c26 + 453c762 commit b21e2f7
Show file tree
Hide file tree
Showing 13 changed files with 638 additions and 31 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,56 @@ caddy::vhost { 'example2':
}
```

Use `conf.d` + `sites-available` + `sites-enabled` layout with `config_files` and `vhosts` parameters set:

```puppet
class { 'caddy':
config_dir => '/etc/caddy/conf.d',
vhost_dir => '/etc/caddy/sites-available',
vhost_enable_dir => '/etc/caddy/sites-enabled',
config_files => {
admin_port_2020 => {
content => "{\n admin localhost:2020\n}\n",
},
},
vhosts => {
port_3000 => {
content => "http://localhost:3000 {\n respond \\"port 3000\\"\n}\n",
},
port_3001 => {
ensure => 'disabled',
content => "http://localhost:3001 {\n respond \\"port 3001\\"\n}\n",
}
}
}
```

Same as above but configured in Hiera:

```yaml
caddy::config_dir: /etc/caddy/conf.d
caddy::vhost_dir: /etc/caddy/sites-available
caddy::vhost_enable_dir: /etc/caddy/sites-enabled
caddy::config_files:
admin_port_2020:
content: |
{
admin localhost:2020
}
caddy::vhosts:
port_3000:
content: |
http://localhost:3000 {
respond "port 3000"
}
port_3001:
ensure: disabled
content: |
http://localhost:3001 {
respond "port 3001"
}
```
## Reference
The [reference][1] documentation of this module is generated using [puppetlabs/puppetlabs-strings][2].
Expand Down
169 changes: 158 additions & 11 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

### Defined types

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

### Data types

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

## Classes
Expand Down Expand Up @@ -96,6 +98,11 @@ The following parameters are available in the `caddy` class:
* [`caddyfile_content`](#-caddy--caddyfile_content)
* [`config_dir`](#-caddy--config_dir)
* [`purge_config_dir`](#-caddy--purge_config_dir)
* [`config_files`](#-caddy--config_files)
* [`vhost_dir`](#-caddy--vhost_dir)
* [`purge_vhost_dir`](#-caddy--purge_vhost_dir)
* [`vhost_enable_dir`](#-caddy--vhost_enable_dir)
* [`purge_vhost_enable_dir`](#-caddy--purge_vhost_enable_dir)
* [`vhosts`](#-caddy--vhosts)

##### <a name="-caddy--version"></a>`version`
Expand Down Expand Up @@ -389,19 +396,136 @@ Whether to purge Caddy config directory.

Default value: `true`

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

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

Hash of config files to create.

Default value: `{}`

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

Data type: `Stdlib::Absolutepath`

Where to store Caddy available virtual host configs. Set this to
/etc/caddy/vhost.d if you'd prefer to keep virtual hosts separated from
configs.
Set this to /etc/caddy/sites-available to simulate nginx/apache behavior
(see vhost_enable_dir also).

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

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

Data type: `Boolean`

Whether to purge Caddy available virtual host directory.

Default value: `$purge_config_dir`

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

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

Where to load Caddy virtual host configs from. Set this parameter to /etc/caddy/sites-enabled
to simulate nginx/apache behavior.

Default value: `undef`

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

Data type: `Boolean`

Whether to purge Caddy enabled virtual host directory.

Default value: `$purge_vhost_dir`

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

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

List of virtual hosts to create.
Hash of virtual hosts to create.

Default value: `{}`

## Defined types

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

This defined type handles a Caddy config file

#### Examples

##### Configure Caddy logging

```puppet
caddy::configfile { 'subdomain-log':
source => 'puppet:///modules/caddy/etc/caddy/config/logging.conf',
}
```

##### Same as above but using content

```puppet
$log_config = @(SUBDOMAIN_LOG)
(subdomain-log) {
log {
hostnames {args[0]}
output file /var/log/caddy/{args[0]}.log
}
}
| SUBDOMAIN_LOG
caddy::configfile { 'subdomain-log':
content => $log_config,
}
```

#### Parameters

The following parameters are available in the `caddy::configfile` defined type:

* [`ensure`](#-caddy--configfile--ensure)
* [`source`](#-caddy--configfile--source)
* [`content`](#-caddy--configfile--content)
* [`config_dir`](#-caddy--configfile--config_dir)

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

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

Make the config file either present or absent.

Default value: `'present'`

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

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

Source (path) for the caddy config file.

Default value: `undef`

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

Data type: `Optional[String]`

String with the caddy config file.

Default value: `undef`

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

Data type: `Stdlib::Absolutepath`

Where to store the config file.

Default value: `$caddy::config_dir`

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

This defined type handles the Caddy virtual hosts.
This defined type handles a Caddy virtual host

#### Examples

Expand Down Expand Up @@ -429,41 +553,64 @@ The following parameters are available in the `caddy::vhost` defined type:
* [`source`](#-caddy--vhost--source)
* [`content`](#-caddy--vhost--content)
* [`config_dir`](#-caddy--vhost--config_dir)
* [`enable_dir`](#-caddy--vhost--enable_dir)

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

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

Make the vhost either present or absent
Make the vhost either present (same as disabled), enabled, disabled or absent.

Default value: `'present'`
Default value: `'enabled'`

##### <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
Where to store the vhost config file.

Default value: `$caddy::config_dir`
Default value: `$caddy::vhost_dir`

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

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

Directory to symlink the vhost config file into (sites-enabled e.g.) if any.

Default value: `$caddy::vhost_enable_dir`

## Data types

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

Caddy config file type

Alias of

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

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

Caddy virtual host type
Expand All @@ -472,7 +619,7 @@ Alias of

```puppet
Struct[{
ensure => Optional[Enum['absent', 'present']],
ensure => Optional[Enum['present','enabled','disabled','absent']],
source => Optional[Stdlib::Filesource],
content => Optional[String[1]],
}]
Expand Down
28 changes: 26 additions & 2 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,42 @@
;
$caddy::config_dir:
purge => $caddy::purge_config_dir,
recurse => true,
recurse => if $caddy::purge_config_dir { true } else { undef },
;
}

# Manage vhost_dir if not the same as config dir
unless $caddy::vhost_dir == $caddy::config_dir {
file { $caddy::vhost_dir:
ensure => directory,
owner => $caddy::caddy_user,
group => $caddy::caddy_group,
mode => '0755',
purge => $caddy::purge_vhost_dir,
recurse => if $caddy::purge_vhost_dir { true } else { undef },
}
}

# Manage vhost_enable_dir if defined
if $caddy::vhost_enable_dir {
file { $caddy::vhost_enable_dir:
ensure => directory,
owner => $caddy::caddy_user,
group => $caddy::caddy_group,
mode => '0755',
purge => $caddy::purge_vhost_enable_dir,
recurse => if $caddy::purge_vhost_enable_dir { true } else { undef },
}
}

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,
include_dirs => unique([$caddy::config_dir] + [$caddy::vhost_enable_dir.lest || { $caddy::vhost_dir }])
)
}
}
Expand Down
Loading

0 comments on commit b21e2f7

Please sign in to comment.