diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2020-01-03 13:49:24 +0100 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2020-01-03 14:26:52 +0100 |
commit | ec2c3da9a865bd51c8766d5c25f9339738a40073 (patch) | |
tree | 30a801c06a46ff310b26df9ca64464638d7878cf | |
parent | 9f9564bdb882d38a35d164336cb8f0d0da7eb251 (diff) | |
download | acmeman-ec2c3da9a865bd51c8766d5c25f9339738a40073.tar.gz acmeman-ec2c3da9a865bd51c8766d5c25f9339738a40073.tar.bz2 |
Improve apache setup procedure
* lib/App/Acmeman/Apache/Layout.pm (apache_modules): New method.
(pre_setup): New method.
* lib/App/Acmeman/Apache/Layout/debian.pm (post_setup): Enable
mod_macro if necessary.
* lib/App/Acmeman/Source/Apache.pm (setup): Suggest enabling
mod_macro only unless it is already enabled.
Suggest including httpd-letsencrypt.conf unless the macro
LetsEncryptSSL is already defined.
-rw-r--r-- | lib/App/Acmeman/Apache/Layout.pm | 40 | ||||
-rw-r--r-- | lib/App/Acmeman/Apache/Layout/debian.pm | 5 | ||||
-rw-r--r-- | lib/App/Acmeman/Source/Apache.pm | 42 |
3 files changed, 81 insertions, 6 deletions
diff --git a/lib/App/Acmeman/Apache/Layout.pm b/lib/App/Acmeman/Apache/Layout.pm index 7b9ec9d..1f2027c 100644 --- a/lib/App/Acmeman/Apache/Layout.pm +++ b/lib/App/Acmeman/Apache/Layout.pm @@ -106,6 +106,43 @@ sub new { sub apache { shift->{_defaults} } +sub apache_modules { + my $self = shift; + + if (@_ == 1 && !defined $_[0]) { + delete $self->{apache_modules}; + shift; + } + unless ($self->{apache_modules}) { + if (open(my $fd, '-|', + $self->server_command, '-t', '-D', 'DUMP_MODULES')) { + while (<$fd>) { + chomp; + if (/^\s+(\w+)_module\s+\((static|shared)\)$/) { + $self->{apache_modules}{$1} = $2; + } + } + close $fd; + } else { + croak "can't run ".$self->server_command.": $!"; + } + } + my %ret; + if (@_) { + foreach my $m (@_) { + $ret{$m} = $self->{apache_modules}{$m} + if exists $self->{apache_modules}{$m}; + } + } else { + %ret = %{$self->{apache_modules}}; + } + if (wantarray) { + return %ret; + } else { + return keys %ret; + } +} + sub name { my $self = shift; unless ($self->{layout_name}) { @@ -142,10 +179,11 @@ sub restart_command { return $self->{restart_command}; } +sub pre_setup {} sub post_setup {} package App::Acmeman::Apache::Layout::auto; -use parent 'App::Acmeman::Apache::Layout'; +my @ISA = qw(App::Acmeman::Apache::Layout); use App::Acmeman::Log qw(:all); sub new { diff --git a/lib/App/Acmeman/Apache/Layout/debian.pm b/lib/App/Acmeman/Apache/Layout/debian.pm index f123892..c740925 100644 --- a/lib/App/Acmeman/Apache/Layout/debian.pm +++ b/lib/App/Acmeman/Apache/Layout/debian.pm @@ -31,10 +31,13 @@ sub post_setup { my $dir = dirname($filename); my $name = basename($filename); + unless ($self->apache_modules('macro')) { + system("a2enmod macro"); + $self->apache_modules(undef); + } if ($dir eq '/etc/apache2/conf-available') { chdir('/etc/apache2/conf-enabled'); symlink "../conf-available/$name", $name; - system("a2enmod macro"); } } diff --git a/lib/App/Acmeman/Source/Apache.pm b/lib/App/Acmeman/Source/Apache.pm index 8b7eda1..29fe24a 100644 --- a/lib/App/Acmeman/Source/Apache.pm +++ b/lib/App/Acmeman/Source/Apache.pm @@ -176,7 +176,9 @@ sub setup { return 0; } } - + + $self->layout->pre_setup; + open(my $fd, '>', $filename) or croak "can't open \"$filename\" for writing: $!"; print $fd <<EOT; @@ -215,15 +217,47 @@ sub setup { EOT ; close $fd; + error("created file \"$filename\"", prefix => 'note'); $self->layout->post_setup($filename); } - error("created file \"$filename\"", prefix => 'note'); - error("please, enable mod_macro and make sure your Apache configuration includes this file", - prefix => 'note'); + my $pfx = "please,"; + unless ($self->is_letsencryptssl_defined) { + error("please, make sure your Apache configuration includes this file", + prefix => 'note'); + $pfx = "and"; + } + unless ($self->layout->apache_modules('macro')) { + error("$pfx enable mod_macro", + prefix => 'note'); + } return 1; } +# Check if LetsEncryptSSL macro is defined +sub is_letsencryptssl_defined { + my ($self) = @_; + my $app = new Apache::Config::Preproc( + $self->layout->config_file, + -expand => [ 'compact', + { 'include' => [ server_root => $self->server_root ] }, + { 'macro' => [ + 'keep' => [ qw(LetsEncryptChallenge + LetsEncryptReference + LetsEncryptSSL) + ] + ] + } + ]); + if ($app) { + return grep { $_->value =~ m{^(?i)letsencryptssl\s+} } + $app->section(-name => "macro"); + } else { + debug(1, $Apache::Admin::Config::ERROR); + return undef + } +} + 1; |