Commit 6257101c authored by Nigel Kukard's avatar Nigel Kukard
Browse files

Added quiet support

parent 50e281ef
Pipeline #781 failed with stages
in 18 seconds
...@@ -87,6 +87,7 @@ BEGIN { ...@@ -87,6 +87,7 @@ BEGIN {
decode_base64 decode_base64
encode_base64 encode_base64
); );
use POSIX qw(floor ceil);
use Sys::Hostname; use Sys::Hostname;
} }
...@@ -410,7 +411,32 @@ sub logger ...@@ -410,7 +411,32 @@ sub logger
my ($self,$level,$arg1,@args) = @_; 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; return;
} }
...@@ -454,6 +480,11 @@ sub _init ...@@ -454,6 +480,11 @@ sub _init
$self->{'only_vhosts'} = $opts->{'only_vhosts'}; $self->{'only_vhosts'} = $opts->{'only_vhosts'};
$self->{'use_dns'} = $opts->{'use_dns'}; $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; return $self;
} }
...@@ -512,8 +543,8 @@ sub _webserverCheckCertificates ...@@ -512,8 +543,8 @@ sub _webserverCheckCertificates
my $remainingTime = $parsedCert->{'notafter'}->epoch() - $now->epoch(); my $remainingTime = $parsedCert->{'notafter'}->epoch() - $now->epoch();
my $daysToExpire = $remainingTime / 86400; my $daysToExpire = $remainingTime / 86400;
# Check remaining time # Check remaining time
if ($daysToExpire < 30) { if ($daysToExpire < 15) {
$self->logger("WARNING","WEBSERVER: - Certificate for vhost '%s' expires in under 30 days, adding to regeneration list",$vhostName); $self->logger("WARNING","WEBSERVER: - Certificate for vhost '%s' expires in under 15 days, adding to regeneration list",$vhostName);
next; next;
} }
...@@ -534,8 +565,8 @@ sub _webserverCheckCertificates ...@@ -534,8 +565,8 @@ sub _webserverCheckCertificates
next if (@missingDomains); next if (@missingDomains);
# Output a notice if the cert will be re-applied for in the next 3 days # Output a notice if the cert will be re-applied for in the next 3 days
if ($daysToExpire < 34) { if ($daysToExpire < 30) {
$self->logger("NOTICE","WEBSERVER: - Certificate for vhost '%s' expires soon, it will be renewed in %s days",$vhostName,$daysToExpire); $self->logger("NOTICE","WEBSERVER: - Certificate for vhost '%s' expires soon, it will be renewed in %s days",$vhostName,ceil($daysToExpire - 15));
} }
SKIP: SKIP:
...@@ -1754,11 +1785,7 @@ use Getopt::Long; ...@@ -1754,11 +1785,7 @@ use Getopt::Long;
my $NAME = "AWIT-CertMaster"; my $NAME = "AWIT-CertMaster";
our $VERSION = "1.1.2"; our $VERSION = "1.1.4";
print(STDERR "$NAME v$VERSION - Copyright (c) 2016-2017, AllWorldIT\n\n");
...@@ -1792,6 +1819,12 @@ C<awit-certmaster> provides the below commandline options... ...@@ -1792,6 +1819,12 @@ C<awit-certmaster> provides the below commandline options...
=cut =cut
=head2 --quiet
Operate in quiet mode and only output logs if there was an warning or worse error.
=cut
=head1 CERT OPTIONS =head1 CERT OPTIONS
...@@ -1843,6 +1876,7 @@ GetOptions(\%optctl, ...@@ -1843,6 +1876,7 @@ GetOptions(\%optctl,
"version", "version",
"nginx", "nginx",
"apache", "apache",
"quiet",
"check-only", "check-only",
"force=s@", "force=s@",
...@@ -1852,6 +1886,13 @@ GetOptions(\%optctl, ...@@ -1852,6 +1886,13 @@ GetOptions(\%optctl,
"use-dns=s@", "use-dns=s@",
) or exit 1; ) or exit 1;
# Display copyright
if (!defined($optctl{'quiet'})) {
print(STDERR "$NAME v$VERSION - Copyright (c) 2016-2017, AllWorldIT\n\n");
}
# Check for help # Check for help
if (defined($optctl{'help'})) { if (defined($optctl{'help'})) {
displayHelp(); displayHelp();
...@@ -1873,6 +1914,7 @@ my $cm = AWIT::CertMaster::LetsEncrypt->new({ ...@@ -1873,6 +1914,7 @@ my $cm = AWIT::CertMaster::LetsEncrypt->new({
'live' => $optctl{'live'}, 'live' => $optctl{'live'},
'only_vhosts' => $optctl{'only'}, 'only_vhosts' => $optctl{'only'},
'use_dns' => $optctl{'use-dns'}, 'use_dns' => $optctl{'use-dns'},
'quiet' => $optctl{'quiet'},
}); });
...@@ -1887,7 +1929,7 @@ if ($optctl{'nginx'}) { ...@@ -1887,7 +1929,7 @@ if ($optctl{'nginx'}) {
exit 1; exit 1;
} }
$cm->logger_end();
exit 0; exit 0;
...@@ -1913,6 +1955,7 @@ sub displayHelp ...@@ -1913,6 +1955,7 @@ sub displayHelp
--version Display version. --version Display version.
--nginx Process Nginx vhosts. --nginx Process Nginx vhosts.
--apache Process Apache vhosts. --apache Process Apache vhosts.
--quiet Only output if warnings are detected.
Certificate Functions: 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