|
5 | 5 | # |
6 | 6 | # === Parameters: |
7 | 7 | # |
| 8 | +# [*ensure*] |
| 9 | +# Intended state of the resource. Accepts either 'present' or 'absent'. |
| 10 | +# Default: 'present'. |
| 11 | +# Will remove certificates for specified domains if set to 'absent'. Will |
| 12 | +# also remove cronjobs and renewal scripts if `manage_cron` is set to 'true'. |
8 | 13 | # [*domains*] |
9 | 14 | # Namevar. An array of domains to include in the CSR. |
10 | 15 | # [*custom_plugin*] |
|
23 | 28 | # `letsencrypt-auto` command. |
24 | 29 | # [*environment*] |
25 | 30 | # An optional array of environment variables (in addition to VENV_PATH). |
26 | | -# [*ensure_cron*] |
27 | | -# Intended state of the cron and helper script resources. Accepts either |
28 | | -# 'present' or 'absent'. Default: 'absent' |
| 31 | +# [*manage_cron*] |
| 32 | +# Boolean indicating whether or not to schedule cron job for renewal. Default: 'false'. |
29 | 33 | # Runs daily but only renews if near expiration, e.g. within 10 days. |
30 | 34 | # [*cron_before_command*] |
31 | 35 | # String representation of a command that should be run before renewal command |
|
40 | 44 | # e.g. 0 or '00' or [0,30]. Default - seeded random minute. |
41 | 45 | # |
42 | 46 | define letsencrypt::certonly ( |
| 47 | + Enum['present','absent'] $ensure = 'present', |
43 | 48 | Array[String[1]] $domains = [$title], |
44 | 49 | Boolean $custom_plugin = false, |
45 | 50 | Letsencrypt::Plugin $plugin = 'standalone', |
|
48 | 53 | Integer[2048] $key_size = $letsencrypt::key_size, |
49 | 54 | Array[String[1]] $additional_args = [], |
50 | 55 | Array[String[1]] $environment = [], |
51 | | - Enum['present','absent'] $ensure_cron = 'absent', |
| 56 | + Boolean $manage_cron = false, |
52 | 57 | Boolean $suppress_cron_output = false, |
53 | 58 | Optional[String[1]] $cron_before_command = undef, |
54 | 59 | Optional[String[1]] $cron_success_command = undef, |
|
62 | 67 | fail("The 'webroot_paths' parameter must be specified when using the 'webroot' plugin") |
63 | 68 | } |
64 | 69 |
|
65 | | - if ($custom_plugin) { |
66 | | - $command_start = "${letsencrypt_command} --text --agree-tos --non-interactive certonly --rsa-key-size ${key_size} " |
| 70 | + if $ensure == 'present' { |
| 71 | + if ($custom_plugin) { |
| 72 | + $command_start = "${letsencrypt_command} --text --agree-tos --non-interactive certonly --rsa-key-size ${key_size} " |
| 73 | + } else { |
| 74 | + $command_start = "${letsencrypt_command} --text --agree-tos --non-interactive certonly --rsa-key-size ${key_size} -a ${plugin} " |
| 75 | + } |
67 | 76 | } else { |
68 | | - $command_start = "${letsencrypt_command} --text --agree-tos --non-interactive certonly --rsa-key-size ${key_size} -a ${plugin} " |
| 77 | + $command_start = "${letsencrypt_command} --text --agree-tos --non-interactive delete " |
69 | 78 | } |
70 | 79 |
|
71 | 80 | case $plugin { |
|
93 | 102 | } |
94 | 103 |
|
95 | 104 | default: { |
96 | | - $_command_domains = join($domains, ' -d ') |
97 | | - $command_domains = "--cert-name ${title} -d ${_command_domains}" |
| 105 | + if $ensure == 'present' { |
| 106 | + $_command_domains = join($domains, ' -d ') |
| 107 | + $command_domains = "--cert-name ${title} -d ${_command_domains}" |
| 108 | + } else { |
| 109 | + $command_domains = "--cert-name ${title}" |
| 110 | + } |
98 | 111 | } |
99 | | - |
100 | 112 | } |
101 | 113 |
|
102 | 114 | if empty($additional_args) { |
|
106 | 118 | $command_end = join(['',] + $additional_args, ' ') |
107 | 119 | } |
108 | 120 |
|
109 | | - $command = "${command_start}${command_domains}${command_end}" |
110 | | - |
111 | 121 | # certbot uses --cert-name to generate the file path |
112 | 122 | $live_path_certname = regsubst($title, '^\*\.', '') |
113 | 123 | $live_path = "${config_dir}/live/${live_path_certname}/cert.pem" |
114 | 124 |
|
115 | 125 | $execution_environment = [ "VENV_PATH=${letsencrypt::venv_path}", ] + $environment |
116 | 126 | $verify_domains = join(unique($domains), ' ') |
| 127 | + |
| 128 | + if $ensure == 'present' { |
| 129 | + $exec_ensure = { 'unless' => "/usr/local/sbin/letsencrypt-domain-validation ${live_path} ${verify_domains}" } |
| 130 | + } else { |
| 131 | + $exec_ensure = { 'onlyif' => "/usr/local/sbin/letsencrypt-domain-validation ${live_path} ${verify_domains}" } |
| 132 | + } |
| 133 | + |
117 | 134 | exec { "letsencrypt certonly ${title}": |
118 | | - command => $command, |
| 135 | + command => "${command_start}${command_domains}${command_end}", |
| 136 | + * => $exec_ensure, |
119 | 137 | path => $facts['path'], |
120 | 138 | environment => $execution_environment, |
121 | | - unless => "/usr/local/sbin/letsencrypt-domain-validation ${live_path} ${verify_domains}", |
122 | 139 | provider => 'shell', |
123 | 140 | require => [ |
124 | 141 | Class['letsencrypt'], |
125 | 142 | File['/usr/local/sbin/letsencrypt-domain-validation'], |
126 | 143 | ], |
127 | 144 | } |
128 | 145 |
|
129 | | - if $ensure_cron == 'present' { |
| 146 | + if $manage_cron { |
130 | 147 | $maincommand = "${command_start}--keep-until-expiring ${command_domains}${command_end}" |
131 | | - $cron_script_ensure = 'file' |
| 148 | + $cron_script_ensure = $ensure ? { 'present' => 'file', default => 'absent' } |
| 149 | + $cron_ensure = $ensure |
132 | 150 |
|
133 | 151 | if $suppress_cron_output { |
134 | 152 | $croncommand = "${maincommand} > /dev/null 2>&1" |
|
145 | 163 | } else { |
146 | 164 | $cron_cmd = $renewcommand |
147 | 165 | } |
148 | | - } else { |
149 | | - $cron_script_ensure = 'absent' |
150 | | - } |
151 | 166 |
|
152 | | - file { "${letsencrypt::cron_scripts_path}/renew-${title}.sh": |
153 | | - ensure => $cron_script_ensure, |
154 | | - mode => '0755', |
155 | | - owner => 'root', |
156 | | - group => $letsencrypt::cron_owner_group, |
157 | | - content => template('letsencrypt/renew-script.sh.erb'), |
158 | | - } |
| 167 | + file { "${letsencrypt::cron_scripts_path}/renew-${title}.sh": |
| 168 | + ensure => $cron_script_ensure, |
| 169 | + mode => '0755', |
| 170 | + owner => 'root', |
| 171 | + group => $letsencrypt::cron_owner_group, |
| 172 | + content => template('letsencrypt/renew-script.sh.erb'), |
| 173 | + } |
159 | 174 |
|
160 | | - cron { "letsencrypt renew cron ${title}": |
161 | | - ensure => $ensure_cron, |
162 | | - command => "\"${letsencrypt::cron_scripts_path}/renew-${title}.sh\"", |
163 | | - user => root, |
164 | | - hour => $cron_hour, |
165 | | - minute => $cron_minute, |
166 | | - monthday => $cron_monthday, |
| 175 | + cron { "letsencrypt renew cron ${title}": |
| 176 | + ensure => $cron_ensure, |
| 177 | + command => "\"${letsencrypt::cron_scripts_path}/renew-${title}.sh\"", |
| 178 | + user => root, |
| 179 | + hour => $cron_hour, |
| 180 | + minute => $cron_minute, |
| 181 | + monthday => $cron_monthday, |
| 182 | + } |
167 | 183 | } |
168 | | - |
169 | 184 | } |
0 commit comments