Skip to content

Commit

Permalink
Merge pull request #32 from danzilio/cron_success_cmd
Browse files Browse the repository at this point in the history
Closes #18: merging @mheistermann's changes
  • Loading branch information
danzilio committed Apr 28, 2016
2 parents 80f8d25 + 3fd4940 commit 547a4ea
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased][unreleased]
- Ability to run commands after a successful cronjob-based renewal with the `cron_success_command` parameter.

## [1.0.0] - 2016-02-22
## Added
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ group :test do
gem 'puppetlabs_spec_helper'
gem 'metadata-json-lint'
unless ENV['PUPPET_VERSION'] == '~> 3.4.0'
gem 'puppet-strings', git: 'git://github.com/puppetlabs/puppetlabs-strings.git'
gem 'puppet-strings'
end
gem 'puppet-lint-absolute_classname-check'
gem 'puppet-lint-alias-check'
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ letsencrypt::certonly { 'foo':
}
```

To automatically renew a certificate, you can pass the `manage_cron` parameter.
You can optionally add a shell command to be run on success using the `cron_success_command` parameter.

```puppet
letsencrypt::certonly { 'foo':
domains => ['foo.example.com', 'bar.example.com'],
manage_cron => true,
cron_success_command => '/bin/systemctl reload nginx.service',
}
```

## Development

1. Fork it
Expand Down
25 changes: 17 additions & 8 deletions manifests/certonly.pp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@
# [*manage_cron*]
# Boolean indicating whether or not to schedule cron job for renewal.
# Runs daily but only renews if near expiration, e.g. within 10 days.
# [*cron_success_command*]
# String representation of a command that should be run if the renewal command
# succeeds.
#
define letsencrypt::certonly (
$domains = [$title],
$plugin = 'standalone',
$webroot_paths = undef,
$letsencrypt_command = $letsencrypt::command,
$additional_args = undef,
$environment = [],
$manage_cron = false,
$domains = [$title],
$plugin = 'standalone',
$webroot_paths = undef,
$letsencrypt_command = $letsencrypt::command,
$additional_args = undef,
$environment = [],
$manage_cron = false,
$cron_success_command = undef,
) {
validate_array($domains)
validate_re($plugin, ['^apache$', '^standalone$', '^webroot$'])
Expand Down Expand Up @@ -66,10 +70,15 @@

if $manage_cron {
$renewcommand = "${command_start}--keep-until-expiring ${command_domains}${command_end}"
if $cron_success_command {
$cron_cmd = "${renewcommand} && (${cron_success_command})"
} else {
$cron_cmd = $renewcommand
}
$cron_hour = fqdn_rand(24, $title) # 0 - 23, seed is title plus fqdn
$cron_minute = fqdn_rand(60, $title ) # 0 - 59, seed is title plus fqdn
cron { "letsencrypt renew cron ${title}":
command => $renewcommand,
command => $cron_cmd,
environment => concat([ $venv_path_var ], $environment),
user => root,
hour => $cron_hour,
Expand Down
8 changes: 8 additions & 0 deletions spec/defines/letsencrypt_certonly_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@
it { is_expected.to contain_cron('letsencrypt renew cron foo.example.com').with_command 'letsencrypt --agree-tos certonly -a apache --keep-until-expiring -d foo.example.com' }
end

context 'with custom plugin and manage cron and cron_success_command' do
let(:title) { 'foo.example.com' }
let(:params) { { plugin: 'apache',
manage_cron: true,
cron_success_command: "echo success" } }
it { is_expected.to contain_cron('letsencrypt renew cron foo.example.com').with_command 'letsencrypt --agree-tos certonly -a apache --keep-until-expiring -d foo.example.com && (echo success)' }
end

context 'with invalid plugin' do
let(:title) { 'foo.example.com' }
let(:params) { { plugin: 'bad' } }
Expand Down

3 comments on commit 547a4ea

@santinoncs
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

When executing the renew command, and getting

Certificate not yet due for renewal; no action taken.

unfortunately, the "cron_success_command" is run.

This is because the status exit code of the renewal is 0.

Is this the expected behaviour?

I am restarting the apache service although the certs are already valids.

Thanks a lot.

@domcleal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd perhaps be better to update the module to configure letsencrypt renew and pass the command to --renew-hook so it's only called when required. I'd suggest filing an issue instead of commenting on a previous commit though.

@danzilio
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @santinoncs for this report and @domcleal for the suggestion. This is most certainly not expected behavior. This module is definitely in need of some love; I'll take a look at implementing this behavior with letsencrypt renew.

Please sign in to comment.