Skip to content

Commit

Permalink
Refactor to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
ghoneycutt committed Apr 28, 2020
1 parent 7636fa1 commit c18ac74
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 157 deletions.
46 changes: 0 additions & 46 deletions manifests/config.pp

This file was deleted.

117 changes: 111 additions & 6 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
# @param systemd_no_new_privileges
# Whether the process and all its children can gain new privileges through execve().
#
# @param caddy_url
# URL for downloading Caddy.
#
class caddy (
Stdlib::Absolutepath $install_path = '/usr/local/bin',
String[1] $caddy_user = 'caddy',
Expand All @@ -94,7 +97,11 @@
Optional[String[1]] $systemd_capability_bounding_set = undef,
Optional[String[1]] $systemd_ambient_capabilities = undef,
Optional[Boolean] $systemd_no_new_privileges = undef,
Stdlib::HTTPUrl $caddy_url = 'https://caddyserver.com/download/linux',
) {

include file_capability

case $caddy_architecture {
'x86_64', 'amd64': { $arch = 'amd64'}
'x86' : { $arch = '386' }
Expand All @@ -104,6 +111,9 @@
}
}

$caddy_dl_url = "${caddy_url}/${arch}?plugins=${caddy_features}&license=${caddy_license}&telemetry=${caddy_telemetry}"
$caddy_dl_dir = "${caddy_tmp_dir}/caddy_linux_${$arch}_custom.tar.gz"

group { $caddy_group:
ensure => present,
system => true,
Expand All @@ -117,11 +127,106 @@
home => $caddy_home,
}

contain caddy::install
contain caddy::config
contain caddy::service
archive { $caddy_dl_dir:
ensure => present,
extract => true,
extract_path => $install_path,
source => $caddy_dl_url,
username => $caddy_account_id,
password => $caddy_api_key,
user => 'root',
group => 'root',
creates => "${install_path}/caddy",
cleanup => true,
notify => File_capability["${install_path}/caddy"],
}

file_capability { "${install_path}/caddy":
ensure => present,
capability => 'cap_net_bind_service=ep',
require => Archive[$caddy_dl_dir],
}

file { $caddy_home:
ensure => directory,
owner => $caddy_user,
group => $caddy_group,
mode => '0755',
require => Archive[$caddy_dl_dir],
notify => Service['caddy'],
}

file { $caddy_ssl_dir:
ensure => directory,
owner => $caddy_user,
group => $caddy_group,
mode => '0755',
require => Archive[$caddy_dl_dir],
notify => Service['caddy'],
}

file { $caddy_log_dir:
ensure => directory,
owner => $caddy_user,
group => $caddy_group,
mode => '0755',
require => Archive[$caddy_dl_dir],
notify => Service['caddy'],
}

file { '/etc/caddy':
ensure => directory,
owner => 'root',
group => 'root',
mode => '0755',
require => Archive[$caddy_dl_dir],
notify => Service['caddy'],
}

file { '/etc/caddy/Caddyfile':
ensure => file,
owner => $caddy_user,
group => $caddy_group,
mode => '0444',
source => 'puppet:///modules/caddy/etc/caddy/Caddyfile',
require => Archive[$caddy_dl_dir],
notify => Service['caddy'],
}

Class['caddy::install']
-> Class['caddy::config']
~> Class['caddy::service']
file { '/etc/caddy/config':
ensure => directory,
purge => true,
recurse => true,
owner => $caddy_user,
group => $caddy_group,
mode => '0755',
require => Archive[$caddy_dl_dir],
notify => Service['caddy'],
}

case $facts['service_provider'] {
default: {
fail("service provider ${$facts['service_provider']} is not supported.")
}
'systemd': {
systemd::unit_file { 'caddy.service':
content => template('caddy/etc/systemd/system/caddy.service.erb'),
notify => Service['caddy'],
}
}
'redhat': {
file { '/etc/init.d/caddy':
ensure => file,
mode => '0755',
owner => 'root',
group => 'root',
content => template('caddy/etc/init.d/caddy.erb'),
}
}
}

service { 'caddy':
ensure => running,
enable => true,
}
}
43 changes: 0 additions & 43 deletions manifests/install.pp

This file was deleted.

50 changes: 0 additions & 50 deletions manifests/service.pp

This file was deleted.

7 changes: 5 additions & 2 deletions manifests/vhost.pp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
Optional[String] $content = undef,
) {

include caddy

file { "/etc/caddy/config/${title}.conf":
ensure => file,
content => $content,
source => $source,
owner => $caddy::caddy_user,
group => $caddy::caddy_group,
mode => '0444',
require => Class['caddy::config'],
notify => Class['caddy::service'],
notify => Service['caddy'],
}
}
6 changes: 1 addition & 5 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
context 'with defaults for all parameters' do
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_class('caddy') }
it { is_expected.to contain_class('caddy::install').that_comes_before('Class[caddy::config]') }
it { is_expected.to contain_class('caddy::config').that_notifies('Class[caddy::service]') }
it { is_expected.to contain_class('caddy::service') }
it do
is_expected.to contain_group(caddy_group).with(
'ensure' => 'present',
Expand Down Expand Up @@ -103,8 +100,7 @@
'owner' => caddy_user,
'group' => caddy_group,
'mode' => '0444',
'source' => 'puppet:///modules/caddy/etc/caddy/Caddyfile',
'require' => 'File[/etc/caddy]'
'source' => 'puppet:///modules/caddy/etc/caddy/Caddyfile'
)
end
it do
Expand Down
17 changes: 12 additions & 5 deletions spec/defines/vhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
facts
end

let(:pre_condition) { 'include caddy' }
# rubocop:disable Style/ConditionalAssignment
if facts[:os]['family'] == 'Debian'
owner_group = 'www-data'
else
owner_group = 'caddy'
end

context 'with source' do
let(:title) { 'example1' }
Expand All @@ -21,9 +26,10 @@
is_expected.to contain_file('/etc/caddy/config/example1.conf').with(
'ensure' => 'file',
'source' => 'puppet:///modules/caddy/etc/caddy/config/example1.conf',
'owner' => owner_group,
'group' => owner_group,
'mode' => '0444',
'require' => 'Class[Caddy::Config]',
'notify' => 'Class[Caddy::Service]'
'notify' => 'Service[caddy]'
)
end
end
Expand All @@ -40,9 +46,10 @@
is_expected.to contain_file('/etc/caddy/config/example2.conf').with(
'ensure' => 'file',
'content' => 'localhost:2015',
'owner' => owner_group,
'group' => owner_group,
'mode' => '0444',
'require' => 'Class[Caddy::Config]',
'notify' => 'Class[Caddy::Service]'
'notify' => 'Service[caddy]'
)
end
end
Expand Down

0 comments on commit c18ac74

Please sign in to comment.