aboutsummaryrefslogtreecommitdiff
path: root/lib/App/Acmeman/Apache/Layout.pm
blob: 5c6d19137c6da418e53524e89a77ec2d945d7eda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package App::Acmeman::Apache::Layout;

use strict;
use warnings;
use Carp;
use File::Basename;
use feature 'state';
use App::Acmeman::Log qw(:all);
use Apache::Defaults;
use Carp;

sub _find_httpd {
    my $httpd;
    foreach my $d (split /:/, $ENV{PATH}) {
	foreach my $n (glob "$d/apachectl $d/httpd") {
	    if (-x $n) {
		return $n;
	    }
	}
    }
}

sub modules {
    my $class = shift;
    my @path = split /::/, $class;
    
    state $loaders //=
	[map { $_->[1] }
	 sort { $a->[0] <=> $b->[0] }
	 map {
	     my ($modname) = $class . '::' . fileparse($_, '.pm');
	     eval {
		 no strict 'refs';
		 if (scalar %{ $modname.'::' }) {
		     ()
		 } else {
		     require $_;
		     my $prio = ${$modname.'::PRIORITY'} // 0;
		     [ $prio, $modname ]
	         }
	     };
	 }
	 map { glob File::Spec->catfile($_, '*.pm') }
	 grep { -d $_ }
	 map { File::Spec->catfile($_, @path) } @INC];
    @$loaders;
}

sub new {
    my ($class, $ap, %args) = @_;

    unless ($ap->isa('Apache::Defaults')) {
    	croak "unrecognized argument";
    }

    my $self = bless { _defaults => $ap }, $class;
    foreach my $kw (qw(layout_name incdir restart_command)) {
	if (defined(my $v = delete $args{$kw})) {
	    $self->{$kw} = $v;
	}
    }

    return $self;
}

sub detect {
    my $class = shift;
    my $server = $class->_find_httpd;

    if (my $name = shift) {
	my $mod = "${class}::$name";
	eval "require $mod";
	croak "undefined Apache layout $name" if ($@);
        my $self = $mod->new(new Apache::Defaults(server => $server), @_);
	if (!$self) {
	    croak "can't use layout $name";
	}
	return $self;
    }
    
    # Autodetect
    debug(3, "detecting Apache configuration layout"
	      .(defined($server) ? " (using httpd binary $server)" : ''));
    my $ap = new Apache::Defaults(server => $server, on_error => 'return');
    if ($ap->status) {
	croak "unable to get Apache defaults: " . $ap->error;
    }

    foreach my $mod ($class->modules) {
	debug(3, "trying layout module $mod");
	my $obj;
	eval { $obj = $mod->new($ap) };
	if ($obj) {
	    return $obj;
	}
	if ($@) {
	    debug(3, "layout module failed: $@");
	}
    }

    return new App::Acmeman::Apache::Layout($ap, layout_name => 'auto');
}

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}) {
	$self->{layout_name} = ref($self);
	$self->{layout_name} =~ s/.*:://;
	$self->{layout_name} =~ s/\.pm$//;
    }
    return $self->{layout_name};
}

sub config_file { shift->apache->server_config }

sub incdir {
    my $self = shift;
    unless ($self->{incdir}) {
	$self->{incdir} = dirname($self->config_file);
    }
    return $self->{incdir};
}

sub server_command { join(' ', shift->apache->server_command) }

sub restart_command {
    my $self = shift;

    unless (exists($self->{restart_command})) {
	if ($self->server_command =~ m{/apachectl$}) {
	    $self->{restart_command} = $self->server_command . ' restart';
	} else {
	    error("no postrenew command defined", prefix => 'warning');
	    $self->{restart_command} = undef
	}
    }
    return $self->{restart_command};
}

sub pre_setup {}
sub post_setup {}

1;

Return to:

Send suggestions and report system problems to the System administrator.