From 967317a3d5c0ced836d8b6217288cd04819ddc36 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Thu, 19 Apr 2018 15:41:35 +0300 Subject: Provide a base class for source classes. * lib/App/Acmeman/Apache/Layout.pm: Remove debug. * lib/App/Acmeman/Source.pm: New file. * lib/App/Acmeman/Source/Apache.pm: Inherit from App::Acmeman::Source. * lib/App/Acmeman/Source/Null.pm: Likewise. * lib/App/Acmeman/Source/File.pm: New file. --- lib/App/Acmeman/Apache/Layout.pm | 7 ---- lib/App/Acmeman/Source.pm | 59 +++++++++++++++++++++++++++++ lib/App/Acmeman/Source/Apache.pm | 81 ++++++++++++---------------------------- lib/App/Acmeman/Source/File.pm | 49 ++++++++++++++++++++++++ lib/App/Acmeman/Source/Null.pm | 6 +-- 5 files changed, 134 insertions(+), 68 deletions(-) create mode 100644 lib/App/Acmeman/Source.pm create mode 100644 lib/App/Acmeman/Source/File.pm diff --git a/lib/App/Acmeman/Apache/Layout.pm b/lib/App/Acmeman/Apache/Layout.pm index 44d5e3d..5b763a2 100644 --- a/lib/App/Acmeman/Apache/Layout.pm +++ b/lib/App/Acmeman/Apache/Layout.pm @@ -64,7 +64,6 @@ sub new { if (exists($layout->{_test}) && !&{$layout->{_test}}) { next; } - debug(2, "assuming Apache layout \"$n\""); $name = $n; last; } @@ -86,12 +85,6 @@ sub new { return $self; } -sub debug { - if (defined(&::debug)) { - ::debug(@_); - } -} - sub name { my $self = shift; return $self->{_layout_name}; diff --git a/lib/App/Acmeman/Source.pm b/lib/App/Acmeman/Source.pm new file mode 100644 index 0000000..391daca --- /dev/null +++ b/lib/App/Acmeman/Source.pm @@ -0,0 +1,59 @@ +package App::Acmeman::Source; + +use strict; +use warnings; +use Carp; + +sub debug { + my $self = shift; + if (defined(&::debug)) { + ::debug(@_); + } +} + +sub error { + my $self = shift; + if (exists($self->{_cfg})) { + $self->{_cfg}->error(@_); + } else { + carp @_; + } +} + +sub set { + my $self = shift; + croak "improper use of the set method" + unless exists $self->{_cfg}; + return $self->{_cfg}->set(@_); +} + +sub get { + my $self = shift; + croak "improper use of the get method" + unless exists $self->{_cfg}; + return $self->{_cfg}->get(@_); +} + +sub define_domain { + my $self = shift; + my $cn = shift || croak "domain name must be given"; + $self->set('domain', $cn, 'files', 'default'); +} + +sub define_alias { + my $self = shift; + my $cn = shift || croak "domain name must be given"; + foreach my $alias (@_) { + $self->set('domain', $cn, 'alt', $alias); + } +} + +sub configure { + my ($self, $config) = @_; + $self->{_cfg} = $config; + return $self->scan(); +} + +sub setup { 1 } + +1; diff --git a/lib/App/Acmeman/Source/Apache.pm b/lib/App/Acmeman/Source/Apache.pm index 1e75044..c84aea6 100644 --- a/lib/App/Acmeman/Source/Apache.pm +++ b/lib/App/Acmeman/Source/Apache.pm @@ -7,50 +7,21 @@ use feature 'state'; use File::Path qw(make_path); use File::Spec; use IPC::Open3; - -require App::Acmeman::Apache::Layout; -our @ISA = qw(App::Acmeman::Apache::Layout); +use App::Acmeman::Apache::Layout; +use parent 'App::Acmeman::Source'; sub new { my $class = shift; - my $self = $class->SUPER::new(@_); - return $self; -} - -sub debug { - if (defined(&::debug)) { - ::debug(@_); - } -} - -sub configure { - my ($self, $config) = @_; - $config->set(qw(core postrenew), $self->restart_command); - $self->{_cfg} = $config; - return $self->examine_http_config($self->config_file); -} - -sub set { - my $self = shift; - croak "improper use of the set method" - unless exists $self->{_cfg}; - return $self->{_cfg}->set(@_); + bless { _layout => new App::Acmeman::Apache::Layout(@_) }, $class; } -sub get { - my $self = shift; - croak "improper use of the get method" - unless exists $self->{_cfg}; - return $self->{_cfg}->get(@_); -} +sub layout { shift->{_layout} } -sub error { - my $self = shift; - if (exists($self->{_cfg})) { - $self->{_cfg}->error(@_); - } else { - carp @_; - } +sub scan { + my ($self) = @_; + $self->debug(2, 'assuming Apache layout "'.$self->layout->name.'"'); + $self->set(qw(core postrenew), $self->layout->restart_command); + return $self->examine_http_config($self->layout->config_file); } sub dequote { @@ -73,7 +44,7 @@ sub examine_http_config { state $state = STATE_INITIAL; - debug(3, "reading apache configuration file \"$file\""); + $self->debug(3, "reading apache configuration file \"$file\""); if (open(my $fd, '<', $file)) { my $server_name; my @server_aliases; @@ -86,7 +57,7 @@ sub examine_http_config { s/^\s+//; next if /^(#.*)?$/; if (/^include(optional)?\s+(.+?)\s*$/i) { - #debug(3, "$file:$line: state $state: Include".($1||'')." $2"); + #$self->debug(3, "$file:$line: state $state: Include".($1||'')." $2"); $self->http_include($self->dequote($2), defined($1)); next; } @@ -114,18 +85,14 @@ sub examine_http_config { if (@server_aliases) { if ($state == STATE_USE_CHALLENGE) { my $cn = shift @server_aliases; - $self->set('domain', $cn, 'files', 'apache'); - foreach my $name (@server_aliases) { - $self->set('domain', $cn, 'alt', $name); - } - debug(3, "$file:$line: will handle ". - join(',', $cn, @server_aliases)); + $self->define_domain($cn); + $self->define_alias($cn, @server_aliases); + $self->debug(3, "$file:$line: will handle ". + join(',', $cn, @server_aliases)); } elsif ($state == STATE_USE_REFERENCE) { - $self->set('domain', $reference, 'files', 'apache'); - foreach my $name (@server_aliases) { - $self->set('domain', $reference, - 'alt', $name); - } + $self->set('domain', $reference, + 'files', 'apache'); + $self->define_alias($reference, @server_aliases); } } $state = STATE_INITIAL; @@ -160,7 +127,7 @@ sub examine_http_config { my $dir = $self->dequote($1); $dir =~ s{/.well-known/acme-challenge$}{}; $self->set(qw(core rootdir), $dir); - debug(3, "ACME challenge root dir: $dir"); + $self->debug(3, "ACME challenge root dir: $dir"); } } elsif ($state == STATE_MACRO_SSL) { if (m{^catfile($pattern, '*') if -d $pattern; foreach my $file (glob $pattern) { if ($optional && ! -e $file) { - debug(1, "optional include file \"$file\" doesn't exist"); + $self->debug(1, "optional include file \"$file\" doesn't exist"); next; } $self->examine_http_config($file); @@ -230,7 +197,7 @@ sub mkpath { sub setup { my ($self, %args) = @_; - my $filename = $self->incdir() . "/httpd-letsencrypt.conf"; + my $filename = $self->layout->incdir() . "/httpd-letsencrypt.conf"; if (-e $filename) { if ($args{force}) { ::error("the file \"$filename\" already exists", @@ -242,9 +209,9 @@ sub setup { } } my $www_root = $self->get(qw(core rootdir)); - debug(2, "writing $filename"); + $self->debug(2, "writing $filename"); unless ($args{dry_run}) { - unless ($self->mkpath($self->incdir())) { + unless ($self->mkpath($self->layout->incdir())) { return 0; } open(my $fd, '>', $filename) @@ -318,7 +285,7 @@ sub probe { } }; close $fd; - last unless ($@) + last unless ($@) } close $nullin; close $nullout; diff --git a/lib/App/Acmeman/Source/File.pm b/lib/App/Acmeman/Source/File.pm new file mode 100644 index 0000000..561c279 --- /dev/null +++ b/lib/App/Acmeman/Source/File.pm @@ -0,0 +1,49 @@ +package App::Acmeman::Source::File; + +use strict; +use warnings; +use Carp; +use File::Spec; +use parent 'App::Acmeman::Source'; + +sub new { + my $class = shift; + my $pattern = shift || croak "file name or globbing pattern must be given"; + my $ignore = '^\.|~$|\.bak$|^#.*#$'; + unless ($pattern =~ m{[][*?]}) { + $pattern =~ s{/$}{}; + $pattern = File::Spec->catfile($pattern, '*'); + } + bless { pattern => $pattern, ignore => $ignore }, $class; +} + +sub scan { + my ($self) = @_; + $self->debug(1, "initializing file list from $self->{pattern}"); + my $err = 0; + foreach my $file (glob $self->{pattern}) { + next if $file =~ m{$self->{ignore}}; + $err |= $self->load($file); + } + return $err == 0; +} + +sub load { + my ($self, $file) = @_; + $self->debug(1, "reading $file"); + open(my $fh, '<', $file) + or do { + $self->error("can't open $file: $!"); + return 0; + }; + chomp(my @lines = <$fh>); + close $fh; + my $cn = shift @lines; + $self->define_domain($cn); + $self->define_alias($cn, @lines); + return 1; +} + +1; + + diff --git a/lib/App/Acmeman/Source/Null.pm b/lib/App/Acmeman/Source/Null.pm index 5b30e0c..d119419 100644 --- a/lib/App/Acmeman/Source/Null.pm +++ b/lib/App/Acmeman/Source/Null.pm @@ -1,10 +1,8 @@ package App::Acmeman::Source::Null; - use strict; use warnings; -use Carp; +use parent 'App::Acmeman::Source'; -sub configure { 1; } -sub setup { 1; } +sub scan { 1; } 1; -- cgit v1.2.1