Skip to content

Commit

Permalink
Implement 'repo' install method
Browse files Browse the repository at this point in the history
  • Loading branch information
jay7x committed Dec 1, 2024
1 parent 5a4c4c6 commit 9a80cc2
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ fixtures:
archive: https://github.com/voxpupuli/puppet-archive.git
stdlib: https://github.com/puppetlabs/puppetlabs-stdlib.git
systemd: https://github.com/voxpupuli/puppet-systemd.git
apt: https://github.com/puppetlabs/puppetlabs-apt.git
yum: https://github.com/voxpupuli/puppet-yum.git
augeas_core: https://github.com/puppetlabs/puppetlabs-augeas_core.git # required by puppet-yum
54 changes: 48 additions & 6 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

* `caddy::config`: This class handles the Caddy config.
* `caddy::install`: This class handles the Caddy archive.
* `caddy::install::github`: This class handles Caddy installation from the github releases
* `caddy::install::repo`: This class handles Caddy installation from a package repository
* `caddy::install::site`: This class handles Caddy installation from the github releases
* `caddy::service`: This class handles the Caddy service.

### Defined types
Expand Down Expand Up @@ -82,6 +85,10 @@ The following parameters are available in the `caddy` class:
* [`service_name`](#-caddy--service_name)
* [`service_ensure`](#-caddy--service_ensure)
* [`service_enable`](#-caddy--service_enable)
* [`manage_repo`](#-caddy--manage_repo)
* [`distribution_channel`](#-caddy--distribution_channel)
* [`package_name`](#-caddy--package_name)
* [`package_ensure`](#-caddy--package_ensure)

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

Expand All @@ -93,17 +100,20 @@ Default value: `'2.0.0'`

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

Data type: `Optional[Enum['github']]`
Data type: `Optional[Enum['github','repo']]`

Which source is used.
Which source to use for the Caddy installation. See https://caddyserver.com/docs/install.
* `undef` (default) - download from the official Caddy site
* `github` - download from Github releases
* `repo` - install from an OS repository

Default value: `undef`

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

Data type: `Stdlib::Absolutepath`

Directory where the Caddy binary is stored.
Directory where the Caddy binary is stored. Not used when $install_method is 'repo'.

Default value: `'/opt/caddy'`

Expand Down Expand Up @@ -279,26 +289,58 @@ Default value: `true`

Data type: `String[1]`

Customise the name of the system service
Customise the name of the system service.

Default value: `'caddy'`

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

Data type: `Stdlib::Ensure::Service`

Whether the service should be running or stopped
Whether the service should be running or stopped.

Default value: `'running'`

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

Data type: `Boolean`

Whether the service should be enabled or disabled
Whether the service should be enabled or disabled.

Default value: `true`

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

Data type: `Boolean`

Whether the APT/YUM(COPR) repository should be installed. Only relevant when $install_method is 'repo'.

Default value: `true`

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

Data type: `Enum['stable','testing']`

Whether to use stable or testing distribution channel. Only relevant on Debian family and only when $install_method is 'repo'.

Default value: `'stable'`

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

Data type: `String[1]`

Name of the caddy package to use. Only relevant when $install_method is 'repo'.

Default value: `'caddy'`

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

Data type: `String[1]`

Whether to install or remove the caddy package. Only relevant when $install_method is 'repo'.

Default value: `$version`

## Defined types

### <a name="caddy--vhost"></a>`caddy::vhost`
Expand Down
32 changes: 26 additions & 6 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
# Which version is used.
#
# @param install_method
# Which source is used.
# Which source to use for the Caddy installation. See https://caddyserver.com/docs/install.
# * `undef` (default) - download from the official Caddy site
# * `github` - download from Github releases
# * `repo` - install from an OS repository
#
# @param install_path
# Directory where the Caddy binary is stored.
# Directory where the Caddy binary is stored. Not used when $install_method is 'repo'.
#
# @param manage_user
# Whether or not the module should create the user.
Expand Down Expand Up @@ -88,17 +91,29 @@
# Whether or not the module should manage the service.
#
# @param service_name
# Customise the name of the system service
# Customise the name of the system service.
#
# @param service_ensure
# Whether the service should be running or stopped
# Whether the service should be running or stopped.
#
# @param service_enable
# Whether the service should be enabled or disabled
# Whether the service should be enabled or disabled.
#
# @param manage_repo
# Whether the APT/YUM(COPR) repository should be installed. Only relevant when $install_method is 'repo'.
#
# @param distribution_channel
# Whether to use stable or testing distribution channel. Only relevant on Debian family and only when $install_method is 'repo'.
#
# @param package_name
# Name of the caddy package to use. Only relevant when $install_method is 'repo'.
#
# @param package_ensure
# Whether to install or remove the caddy package. Only relevant when $install_method is 'repo'.
#
class caddy (
String[1] $version = '2.0.0',
Optional[Enum['github']] $install_method = undef,
Optional[Enum['github','repo']] $install_method = undef,
Stdlib::Absolutepath $install_path = '/opt/caddy',
Boolean $manage_user = true,
String[1] $caddy_user = 'caddy',
Expand All @@ -124,10 +139,15 @@
String[1] $service_name = 'caddy',
Stdlib::Ensure::Service $service_ensure = 'running',
Boolean $service_enable = true,
Boolean $manage_repo = true,
Enum['stable','testing'] $distribution_channel = 'stable',
String[1] $package_name = 'caddy',
String[1] $package_ensure = $version,
) {
case $caddy_architecture {
'x86_64', 'amd64': { $arch = 'amd64' }
'x86' : { $arch = '386' }
'aarch64' : { $arch = 'arm64' }
default: {
$arch = $caddy_architecture
warning("arch ${arch} may not be supported.")
Expand Down
44 changes: 30 additions & 14 deletions manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
$bin_file = "${caddy::install_path}/caddy"

case $caddy::install_method {
'repo': { contain caddy::install::repo }
'github': {
$caddy_url = 'https://github.com/caddyserver/caddy/releases/download'
$caddy_dl_url = "${caddy_url}/v${caddy::version}/caddy_${caddy::version}_linux_${caddy::arch}.tar.gz"
Expand Down Expand Up @@ -37,6 +38,21 @@
}

$caddy_source = "/var/cache/caddy-${caddy::version}/caddy"

file { $caddy::install_path:
ensure => directory,
owner => $caddy::caddy_user,
group => $caddy::caddy_group,
mode => '0755',
}

file { $bin_file:
ensure => file,
owner => 'root',
group => 'root',
mode => '0755',
source => $caddy_source,
}
}
default: {
$caddy_url = 'https://caddyserver.com/api/download'
Expand All @@ -60,21 +76,21 @@
source => $caddy_dl_url,
replace => false, # Don't download the file on every run
}
}
}

file { $caddy::install_path:
ensure => directory,
owner => $caddy::caddy_user,
group => $caddy::caddy_group,
mode => '0755',
}
file { $caddy::install_path:
ensure => directory,
owner => $caddy::caddy_user,
group => $caddy::caddy_group,
mode => '0755',
}

file { $bin_file:
ensure => file,
owner => 'root',
group => 'root',
mode => '0755',
source => $caddy_source,
file { $bin_file:
ensure => file,
owner => 'root',
group => 'root',
mode => '0755',
source => $caddy_source,
}
}
}
}
44 changes: 44 additions & 0 deletions manifests/install/repo.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# @summary
# This class handles Caddy installation from a package repository
#
# @api private
#
class caddy::install::repo {
assert_private()

case $facts['os']['family'] {
'Debian': {
include apt

if $caddy::manage_repo {
apt::source { 'caddy':
location => "https://dl.cloudsmith.io/public/caddy/${caddy::distribution_channel}/deb/debian",
release => 'any-version',
repos => 'main',
key => {
name => "caddy-${caddy::distribution_channel}-archive-keyring.asc",
source => "https://dl.cloudsmith.io/public/caddy/${caddy::distribution_channel}/gpg.key",
},
before => Package[$caddy::package_name],
}
}
}
'RedHat': {
include yum

if $caddy::manage_repo {
yum::copr { '@caddy/caddy':
ensure => 'enabled',
before => Package[$caddy::package_name],
}
}
}
default: {
fail("OS family ${facts['os']['family']} has no support for 'repo' install method")
}
}

package { $caddy::package_name:
ensure => $caddy::package_ensure.lest || { 'installed' },
}
}
29 changes: 29 additions & 0 deletions spec/acceptance/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,35 @@ class { 'caddy':
end
end

context 'when installing from repo' do
# Debian repo has multiple versions
# RedHat repo has just the latest version at the moment
let(:use_version) do
case shell('/opt/puppetlabs/bin/facter os.family').stdout
when 'Debian'
'2.8.3'
else
latest_release[1..]
end
end

it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<~PUPPET
class { 'caddy':
install_method => 'repo',
version => '#{use_version}',
}
PUPPET
end
end

describe command('caddy version') do
its(:exit_status) { is_expected.to eq 0 }
its(:stdout) { is_expected.to start_with "v#{use_version}" }
end
end

context 'with vhosts' do
it_behaves_like 'an idempotent resource' do
let(:manifest) do
Expand Down
Loading

0 comments on commit 9a80cc2

Please sign in to comment.