Commit 36d517e3 authored by Nigel Kukard's avatar Nigel Kukard

Added quiet support

parent 40f4549a
......@@ -87,6 +87,7 @@ BEGIN {
decode_base64
encode_base64
);
use POSIX qw(floor ceil);
use Sys::Hostname;
}
......@@ -410,7 +411,32 @@ sub logger
my ($self,$level,$arg1,@args) = @_;
printf(STDERR '%-7s: '.$arg1."\n",$level,@args);
# Create the log line
my $logLine = sprintf('%-7s: '.$arg1."\n",$level,@args);
# If we're being quiet save it
if ($self->{'quiet'}) {
push(@{$self->{'_logs'}},$logLine);
} else {
# If not output to console
print(STDERR $logLine);
}
return;
}
# Function to output logs of warning or higher if we were quiet
sub logger_end
{
my $self = shift;
# If we're being quiet here is where we should output the logs if there were warnings
if ($self->{'quiet'} && grep({/^WARNING/},@{$self->{'_logs'}})) {
print(STDERR @{$self->{'_logs'}});
}
return;
}
......@@ -454,6 +480,11 @@ sub _init
$self->{'only_vhosts'} = $opts->{'only_vhosts'};
$self->{'use_dns'} = $opts->{'use_dns'};
# Only output to console if we have a warning or higher
$self->{'quiet'} = $opts->{'quiet'};
# Initialize our log array, just incase we're operating quietly
$self->{'_logs'} = [];
return $self;
}
......@@ -512,8 +543,8 @@ sub _webserverCheckCertificates
my $remainingTime = $parsedCert->{'notafter'}->epoch() - $now->epoch();
my $daysToExpire = $remainingTime / 86400;
# Check remaining time
if ($daysToExpire < 30) {
$self->logger("WARNING","WEBSERVER: - Certificate for vhost '%s' expires in under 30 days, adding to regeneration list",$vhostName);
if ($daysToExpire < 15) {
$self->logger("WARNING","WEBSERVER: - Certificate for vhost '%s' expires in under 15 days, adding to regeneration list",$vhostName);
next;
}
......@@ -534,8 +565,8 @@ sub _webserverCheckCertificates
next if (@missingDomains);
# Output a notice if the cert will be re-applied for in the next 3 days
if ($daysToExpire < 34) {
$self->logger("NOTICE","WEBSERVER: - Certificate for vhost '%s' expires soon, it will be renewed in %s days",$vhostName,$daysToExpire);
if ($daysToExpire < 30) {
$self->logger("NOTICE","WEBSERVER: - Certificate for vhost '%s' expires soon, it will be renewed in %s days",$vhostName,ceil($daysToExpire - 15));
}
SKIP:
......@@ -1754,11 +1785,7 @@ use Getopt::Long;
my $NAME = "AWIT-CertMaster";
our $VERSION = "1.1.2";
print(STDERR "$NAME v$VERSION - Copyright (c) 2016-2017, AllWorldIT\n\n");
our $VERSION = "1.1.4";
......@@ -1792,6 +1819,12 @@ C<awit-certmaster> provides the below commandline options...
=cut
=head2 --quiet
Operate in quiet mode and only output logs if there was an warning or worse error.
=cut
=head1 CERT OPTIONS
......@@ -1843,6 +1876,7 @@ GetOptions(\%optctl,
"version",
"nginx",
"apache",
"quiet",
"check-only",
"force=s@",
......@@ -1852,6 +1886,13 @@ GetOptions(\%optctl,
"use-dns=s@",
) or exit 1;
# Display copyright
if (!defined($optctl{'quiet'})) {
print(STDERR "$NAME v$VERSION - Copyright (c) 2016-2017, AllWorldIT\n\n");
}
# Check for help
if (defined($optctl{'help'})) {
displayHelp();
......@@ -1873,6 +1914,7 @@ my $cm = AWIT::CertMaster::LetsEncrypt->new({
'live' => $optctl{'live'},
'only_vhosts' => $optctl{'only'},
'use_dns' => $optctl{'use-dns'},
'quiet' => $optctl{'quiet'},
});
......@@ -1887,7 +1929,7 @@ if ($optctl{'nginx'}) {
exit 1;
}
$cm->logger_end();
exit 0;
......@@ -1913,6 +1955,7 @@ sub displayHelp
--version Display version.
--nginx Process Nginx vhosts.
--apache Process Apache vhosts.
--quiet Only output if warnings are detected.
Certificate Functions:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment