| 1 | package App::Acmeman::Apache::Layout; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | use File::Basename; | ||
| 7 | use feature 'state'; | ||
| 8 | use App::Acmeman::Log qw(:all); | ||
| 9 | use Apache::Defaults; | ||
| 10 | use Carp; | ||
| 11 | |||
| 12 | sub _find_httpd { | ||
| 13 | my $httpd; | ||
| 14 | foreach my $d (split /:/, $ENV{PATH}) { | ||
| 15 | foreach my $n (glob "$d/apachectl $d/httpd") { | ||
| 16 | if (-x $n) { | ||
| 17 | return $n; | ||
| 18 | } | ||
| 19 | } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | sub modules { | ||
| 24 | my $class = shift; | ||
| 25 | my @path = split /::/, $class; | ||
| 26 | |||
| 27 | state $loaders //= | ||
| 28 | [map { $_->[1] } | ||
| 29 | sort { $a->[0] <=> $b->[0] } | ||
| 30 | map { | ||
| 31 | my ($modname) = $class . '::' . fileparse($_, '.pm'); | ||
| 32 | eval { | ||
| 33 | no strict 'refs'; | ||
| 34 | if (scalar %{ $modname.'::' }) { | ||
| 35 | () | ||
| 36 | } else { | ||
| 37 | require $_; | ||
| 38 | my $prio = ${$modname.'::PRIORITY'} // 0; | ||
| 39 | [ $prio, $modname ] | ||
| 40 | } | ||
| 41 | }; | ||
| 42 | } | ||
| 43 | map { glob File::Spec->catfile($_, '*.pm') } | ||
| 44 | grep { -d $_ } | ||
| 45 | map { File::Spec->catfile($_, @path) } @INC]; | ||
| 46 | @$loaders; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my ($class, $ap, %args) = @_; | ||
| 51 | |||
| 52 | unless ($ap->isa('Apache::Defaults')) { | ||
| 53 | croak "unrecognized argument"; | ||
| 54 | } | ||
| 55 | |||
| 56 | my $self = bless { _defaults => $ap }, $class; | ||
| 57 | foreach my $kw (qw(layout_name incdir restart_command)) { | ||
| 58 | if (defined(my $v = delete $args{$kw})) { | ||
| 59 | $self->{$kw} = $v; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub detect { | ||
| 67 | my $class = shift; | ||
| 68 | my $server = $class->_find_httpd; | ||
| 69 | |||
| 70 | if (my $name = shift) { | ||
| 71 | my $mod = "${class}::$name"; | ||
| 72 | eval "require $mod"; | ||
| 73 | croak "undefined Apache layout $name" if ($@); | ||
| 74 | my $self = $mod->new(new Apache::Defaults(server => $server), @_); | ||
| 75 | if (!$self) { | ||
| 76 | croak "can't use layout $name"; | ||
| 77 | } | ||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | # Autodetect | ||
| 82 | debug(3, "detecting Apache configuration layout" | ||
| 83 | .(defined($server) ? " (using httpd binary $server)" : '')); | ||
| 84 | my $ap = new Apache::Defaults(server => $server, on_error => 'return'); | ||
| 85 | if ($ap->status) { | ||
| 86 | croak "unable to get Apache defaults: " . $ap->error; | ||
| 87 | } | ||
| 88 | |||
| 89 | foreach my $mod ($class->modules) { | ||
| 90 | debug(3, "trying layout module $mod"); | ||
| 91 | if (my $obj = eval { $mod->new($ap) }) { | ||
| 92 | return $obj; | ||
| 93 | } | ||
| 94 | if ($@) { | ||
| 95 | debug(3, "layout module failed: $@"); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | return new App::Acmeman::Apache::Layout($ap, layout_name => 'auto'); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub apache { shift->{_defaults} } | ||
| 103 | |||
| 104 | sub apache_modules { | ||
| 105 | my $self = shift; | ||
| 106 | |||
| 107 | if (@_ == 1 && !defined $_[0]) { | ||
| 108 | delete $self->{apache_modules}; | ||
| 109 | shift; | ||
| 110 | } | ||
| 111 | unless ($self->{apache_modules}) { | ||
| 112 | if (open(my $fd, '-|', | ||
| 113 | $self->server_command, '-t', '-D', 'DUMP_MODULES')) { | ||
| 114 | while (<$fd>) { | ||
| 115 | chomp; | ||
| 116 | if (/^\s+(\w+)_module\s+\((static|shared)\)$/) { | ||
| 117 | $self->{apache_modules}{$1} = $2; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | close $fd; | ||
| 121 | } else { | ||
| 122 | croak "can't run ".$self->server_command.": $!"; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | my %ret; | ||
| 126 | if (@_) { | ||
| 127 | foreach my $m (@_) { | ||
| 128 | $ret{$m} = $self->{apache_modules}{$m} | ||
| 129 | if exists $self->{apache_modules}{$m}; | ||
| 130 | } | ||
| 131 | } else { | ||
| 132 | %ret = %{$self->{apache_modules}}; | ||
| 133 | } | ||
| 134 | if (wantarray) { | ||
| 135 | return %ret; | ||
| 136 | } else { | ||
| 137 | return keys %ret; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | sub name { | ||
| 142 | my $self = shift; | ||
| 143 | unless ($self->{layout_name}) { | ||
| 144 | $self->{layout_name} = ref($self); | ||
| 145 | $self->{layout_name} =~ s/.*:://; | ||
| 146 | $self->{layout_name} =~ s/\.pm$//; | ||
| 147 | } | ||
| 148 | return $self->{layout_name}; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub config_file { shift->apache->server_config } | ||
| 152 | |||
| 153 | sub incdir { | ||
| 154 | my $self = shift; | ||
| 155 | unless ($self->{incdir}) { | ||
| 156 | $self->{incdir} = dirname($self->config_file); | ||
| 157 | } | ||
| 158 | return $self->{incdir}; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub server_command { join(' ', shift->apache->server_command) } | ||
| 162 | |||
| 163 | sub restart_command { | ||
| 164 | my $self = shift; | ||
| 165 | |||
| 166 | unless (exists($self->{restart_command})) { | ||
| 167 | if ($self->server_command =~ m{/apachectl$}) { | ||
| 168 | $self->{restart_command} = $self->server_command . ' restart'; | ||
| 169 | } else { | ||
| 170 | error("no postrenew command defined", prefix => 'warning'); | ||
| 171 | $self->{restart_command} = undef | ||
| 172 | } | ||
| 173 | } | ||
| 174 | return $self->{restart_command}; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub pre_setup {} | ||
| 178 | sub post_setup {} | ||
| 179 | |||
| 180 | 1; |