diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2020-01-01 20:24:46 +0100 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2020-01-02 00:08:21 +0100 |
commit | c59e5b99ae6c4f3e75284e1549ae72fcee0e2790 (patch) | |
tree | 39946747a67a3379d3745b1b3eac0f1b9424d103 | |
parent | 2a7586e63eb06a1509e76e885080db090b00beab (diff) | |
download | acmeman-c59e5b99ae6c4f3e75284e1549ae72fcee0e2790.tar.gz acmeman-c59e5b99ae6c4f3e75284e1549ae72fcee0e2790.tar.bz2 |
Use external modules to support different Apache configuration layouts.
* lib/App/Acmeman.pm: Minor change.
* lib/App/Acmeman/Apache/Layout.pm: Rewrite. Use external modules
to support different layouts.
* lib/App/Acmeman/Apache/Layout/debian.pm: Definition of Debian layout.
* lib/App/Acmeman/Apache/Layout/rh.pm: Definition of Red Hat layout.
* lib/App/Acmeman/Apache/Layout/slackware.pm: Definition of Slackware
layout.
* lib/App/Acmeman/Apache/Layout/suse.pm: Definition of Suse layout
-rw-r--r-- | lib/App/Acmeman.pm | 3 | ||||
-rw-r--r-- | lib/App/Acmeman/Apache/Layout.pm | 186 | ||||
-rw-r--r-- | lib/App/Acmeman/Apache/Layout/debian.pm | 40 | ||||
-rw-r--r-- | lib/App/Acmeman/Apache/Layout/rh.pm | 21 | ||||
-rw-r--r-- | lib/App/Acmeman/Apache/Layout/slackware.pm | 24 | ||||
-rw-r--r-- | lib/App/Acmeman/Apache/Layout/suse.pm | 22 | ||||
-rw-r--r-- | lib/App/Acmeman/Source/Apache.pm | 4 |
7 files changed, 221 insertions, 79 deletions
diff --git a/lib/App/Acmeman.pm b/lib/App/Acmeman.pm index 01191ab..02231bf 100644 --- a/lib/App/Acmeman.pm +++ b/lib/App/Acmeman.pm | |||
@@ -333,7 +333,8 @@ sub renew { | |||
333 | local $ENV{ACMEMAN_ALT_NAMES} = | 333 | local $ENV{ACMEMAN_ALT_NAMES} = |
334 | join(' ', map { ($_->alt) } @renewed); | 334 | join(' ', map { ($_->alt) } @renewed); |
335 | if ($self->cf->is_set(qw(core postrenew))) { | 335 | if ($self->cf->is_set(qw(core postrenew))) { |
336 | foreach my $cmd ($self->cf->get(qw(core postrenew))) { | 336 | foreach my $cmd (grep { defined($_) } |
337 | $self->cf->get(qw(core postrenew))) { | ||
337 | $self->runcmd($cmd); | 338 | $self->runcmd($cmd); |
338 | } | 339 | } |
339 | } else { | 340 | } else { |
diff --git a/lib/App/Acmeman/Apache/Layout.pm b/lib/App/Acmeman/Apache/Layout.pm index 7cc8399..39e40d5 100644 --- a/lib/App/Acmeman/Apache/Layout.pm +++ b/lib/App/Acmeman/Apache/Layout.pm | |||
@@ -4,112 +4,148 @@ use strict; | |||
4 | use warnings; | 4 | use warnings; |
5 | use Carp; | 5 | use Carp; |
6 | use File::Basename; | 6 | use File::Basename; |
7 | use feature 'state'; | ||
8 | use App::Acmeman::Log qw(:all); | ||
7 | 9 | ||
8 | require Exporter; | 10 | sub _find_httpd { |
9 | our @ISA = qw(Exporter); | 11 | my $httpd; |
10 | 12 | foreach my $d (split /:/, $ENV{PATH}) { | |
11 | my %apache_layout_tab = ( | 13 | foreach my $n (glob "$d/apachectl $d/httpd") { |
12 | slackware => { | 14 | if (-x $n) { |
13 | _test => sub { -d '/etc/httpd/extra' }, | 15 | return $n; |
14 | _config_file => '/etc/httpd/httpd.conf', | ||
15 | _incdir => '/etc/httpd/extra', | ||
16 | _restart => '/etc/rc.d/rc.httpd restart' | ||
17 | }, | ||
18 | debian => { | ||
19 | _config_file => '/etc/apache2/apache2.conf', | ||
20 | _incdir => sub { | ||
21 | for my $dir ('/etc/apache2/conf-available', | ||
22 | '/etc/apache2/conf.d') { | ||
23 | return $dir if -d $dir; | ||
24 | } | ||
25 | carp 'none of the expected configuration directories found; falling back to /etc/apache2'; | ||
26 | return '/etc/apache2'; | ||
27 | }, | ||
28 | _restart => '/usr/sbin/service apache2 restart', | ||
29 | _post_setup => sub { | ||
30 | my ($filename) = @_; | ||
31 | my $dir = dirname($filename); | ||
32 | my $name = basename($filename); | ||
33 | if ($dir eq '/etc/apache2/conf-available') { | ||
34 | chdir('/etc/apache2/conf-enabled'); | ||
35 | symlink "../conf-available/$name", $name; | ||
36 | } | 16 | } |
37 | } | 17 | } |
38 | }, | ||
39 | rh => { | ||
40 | _config_file => '/etc/httpd/conf/httpd.conf', | ||
41 | _incdir => '/etc/httpd/conf.d', | ||
42 | _restart => '/usr/sbin/service httpd restart' | ||
43 | }, | ||
44 | suse => { | ||
45 | _config_file => '/etc/apache2/httpd.conf', | ||
46 | _test => sub { ! -f '/etc/apache2/apache2.conf' }, | ||
47 | _incdir => '/etc/apache2/conf.d', | ||
48 | _restart => '/usr/sbin/service httpd restart' | ||
49 | # or systemctl restart apache2.service | ||
50 | } | 18 | } |
51 | ); | 19 | } |
20 | |||
21 | sub modules { | ||
22 | my $class = shift; | ||
23 | my @path = split /::/, $class; | ||
24 | |||
25 | state $loaders //= | ||
26 | [map { $_->[1] } | ||
27 | sort { $a->[0] <=> $b->[0] } | ||
28 | map { | ||
29 | my ($modname) = $class . '::' . fileparse($_, '.pm'); | ||
30 | eval { | ||
31 | no strict 'refs'; | ||
32 | if (scalar %{ $modname.'::' }) { | ||
33 | die "INCLUDED $modname"; | ||
34 | }; | ||
35 | require $_; | ||
36 | my $prio = ${$modname.'::PRIORITY'} // 0; | ||
37 | [ $prio, $modname ] | ||
38 | }; | ||
39 | } | ||
40 | map { glob File::Spec->catfile($_, '*.pm') } | ||
41 | grep { -d $_ } | ||
42 | map { File::Spec->catfile($_, @path) } @INC]; | ||
43 | @$loaders; | ||
44 | } | ||
52 | 45 | ||
53 | # new(NAME) | 46 | # new(NAME) |
54 | # new() | 47 | # new() |
55 | sub new { | 48 | sub new { |
56 | my $class = shift; | 49 | my $class = shift; |
57 | my $self = bless { }, $class; | ||
58 | my $name; | ||
59 | 50 | ||
60 | if (@_ == 0) { | 51 | if (@_ == 0) { |
61 | # Autodetect | 52 | # Autodetect |
62 | while (my ($n, $layout) = each %apache_layout_tab) { | 53 | my $ap = new Apache::Defaults(server => $class->_find_httpd, |
63 | if (-f $layout->{_config_file}) { | 54 | on_error => 'return'); |
64 | if (exists($layout->{_test}) && !&{$layout->{_test}}) { | 55 | if ($ap->status) { |
65 | next; | 56 | croak "unable to get Apache defaults: " . $ap->error; |
66 | } | 57 | } |
67 | $name = $n; | 58 | |
68 | last; | 59 | foreach my $mod ($class->modules) { |
60 | my $obj; | ||
61 | eval { | ||
62 | $obj = $mod->new($ap); | ||
63 | }; | ||
64 | if ($obj) { | ||
65 | return $obj; | ||
69 | } | 66 | } |
67 | # print "A $mod: $@\n"; | ||
68 | } | ||
69 | |||
70 | if ($ap) { | ||
71 | return new App::Acmeman::Apache::Layout::auto($ap); | ||
70 | } | 72 | } |
71 | croak "unrecognized Apache layout" unless defined $name; | 73 | |
72 | } elsif (@_ == 1) { | 74 | croak "unrecognized Apache layout"; |
73 | $name = shift; | ||
74 | } | 75 | } |
75 | 76 | ||
76 | if (exists($apache_layout_tab{$name})) { | 77 | my ($name, %args) = @_; |
77 | @{$self}{keys %{$apache_layout_tab{$name}}} = | 78 | if (ref($name) eq '') { |
78 | values %{$apache_layout_tab{$name}}; | 79 | my $mod = "$class::$name"; |
79 | } else { | 80 | my $obj; |
80 | croak "undefined Apache layout $name"; | 81 | eval { |
82 | require $mod; | ||
83 | }; | ||
84 | croak "undefined Apache layout $name" if ($@); | ||
85 | return $mod->new; | ||
86 | } | ||
87 | |||
88 | unless ($name->isa('Apache::Defaults')) { | ||
89 | croak "unrecognized argument"; | ||
90 | } | ||
91 | |||
92 | my $self = bless { _defaults => $name }, $class; | ||
93 | foreach my $kw (qw(layout_name incdir restart_command)) { | ||
94 | if (defined(my $v = delete $args{$kw})) { | ||
95 | $self->{$kw} = $v; | ||
96 | } | ||
81 | } | 97 | } |
82 | 98 | ||
83 | $self->{_layout_name} = $name; | ||
84 | |||
85 | return $self; | 99 | return $self; |
86 | } | 100 | } |
101 | |||
102 | sub apache { shift->{_defaults} } | ||
87 | 103 | ||
88 | sub name { | 104 | sub name { |
89 | my $self = shift; | 105 | my $self = shift; |
90 | return $self->{_layout_name}; | 106 | unless ($self->{layout_name}) { |
107 | $self->{layout_name} = ref($self); | ||
108 | $self->{layout_name} =~ s/.*:://; | ||
109 | $self->{layout_name} =~ s/\.pm$//; | ||
110 | } | ||
111 | return $self->{layout_name}; | ||
91 | } | 112 | } |
92 | 113 | ||
93 | sub config_file { | 114 | sub config_file { shift->apache->server_config } |
115 | |||
116 | sub incdir { | ||
94 | my $self = shift; | 117 | my $self = shift; |
95 | return $self->{_config_file}; | 118 | unless ($self->{incdir}) { |
119 | $self->{incdir} = dirname($self->config_file); | ||
120 | } | ||
121 | return $self->{incdir}; | ||
96 | } | 122 | } |
97 | 123 | ||
124 | sub server_command { join(' ', shift->apache->server_command) } | ||
125 | |||
98 | sub restart_command { | 126 | sub restart_command { |
99 | my $self = shift; | 127 | my $self = shift; |
100 | return $self->{_restart}; | ||
101 | } | ||
102 | 128 | ||
103 | sub incdir { | 129 | unless (exists($self->{restart_command})) |