File Coverage

lib/Provision/Unix/Web.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Provision::Unix::Web;
2             {
3             $Provision::Unix::Web::VERSION = '1.07';
4             }
5             # ABSTRACT: provision web hosting accounts
6              
7 1     1   7114 use strict;
  1         4  
  1         64  
8 1     1   9 use warnings;
  1         4  
  1         53  
9              
10 1     1   560 use Params::Validate qw( :all );
  0            
  0            
11              
12             use lib "lib";
13              
14             my ( $prov, $util );
15              
16             sub new {
17             my $class = shift;
18              
19             my %p = validate(
20             @_,
21             { prov => { type => OBJECT },
22             debug => { type => BOOLEAN, optional => 1, default => 1 },
23             fatal => { type => BOOLEAN, optional => 1, default => 1 },
24             }
25             );
26              
27             my $self = {
28             debug => $p{debug},
29             fatal => $p{fatal},
30             };
31             bless( $self, $class );
32              
33             $prov = $p{prov};
34             $prov->audit("loaded Web");
35             $self->{server} = $self->_get_server( debug => $p{debug}, fatal => $p{fatal} )
36             or return;
37              
38             $util = $prov->get_util;
39             return $self;
40             }
41              
42             sub create {
43             my $self = shift;
44             return $self->{server}->create(@_);
45             }
46              
47             sub enable {
48             my $self = shift;
49             return $self->{server}->enable(@_);
50             };
51              
52             sub disable {
53             my $self = shift;
54             return $self->{server}->disable(@_);
55             };
56              
57             sub _get_server {
58              
59             my $self = shift;
60              
61             my %p = validate(
62             @_,
63             { debug => { type => BOOLEAN, optional => 1, default => 1 },
64             fatal => { type => BOOLEAN, optional => 1, default => 1 },
65             }
66             );
67              
68             my $chosen_server = $prov->{config}{Web}{server}
69             or $prov->error( 'missing [Web] server setting in provision.conf',
70             debug => $p{debug},
71             fatal => $p{fatal},
72             );
73              
74             return if ! $chosen_server;
75              
76             if ( $chosen_server eq "apache" ) {
77             require Provision::Unix::Web::Apache;
78             return Provision::Unix::Web::Apache->new(
79             prov => $prov,
80             web => $self,
81             debug => $p{debug},
82             fatal => $p{fatal},
83             );
84             }
85             elsif ( $chosen_server eq "lighttpd" ) {
86             require Provision::Unix::Web::Lighttpd;
87             return Provision::Unix::Web::Lighttpd->new(
88             prov => $prov,
89             web => $self,
90             debug => $p{debug},
91             fatal => $p{fatal},
92             );
93             }
94             else {
95             return $prov->error( "unknown web server. Supported values are lighttpd and apache.",
96             debug => $p{debug},
97             fatal => $p{fatal},
98             );
99             }
100              
101             return;
102              
103             # use Data::Dumper;
104             # print "\n\n";
105             # print Dumper($request);
106             }
107              
108             sub get_vhost_attributes {
109              
110             my $self = shift;
111              
112             my %p = validate(
113             @_,
114             { 'request' => { type => HASHREF, optional => 1 },
115             'prompt' => { type => BOOLEAN, optional => 1, default => 0 },
116             },
117             );
118              
119             my $vals = $p{'request'};
120              
121             if ( $p{'prompt'} ) {
122             $vals->{'vhost'} ||= $util->ask( 'vhost name' );
123             }
124              
125             my $vhost = $vals->{'vhost'}
126             or $prov->error( "vhost is required" );
127              
128             if ( $p{'prompt'} ) {
129             $vals->{'ip'} ||= $util->ask( 'ip', default => '*:80' );
130             $vals->{'serveralias'}
131             ||= $util->ask( 'serveralias', default => "www.$vhost" );
132             }
133              
134             if ( !$vals->{'documentroot'} ) {
135              
136             # calculate a default documentroot
137             my $vdoc_root = $prov->{config}{'Web'}{'vdoc_root'} || "/home";
138             my $vdoc_suffix = $prov->{config}{'Web'}{'vdoc_suffix'} || "html";
139             my $docroot = "$vdoc_root/$vhost/$vdoc_suffix";
140              
141             if ( $p{'prompt'} ) {
142              
143             # prompt with a sensible default
144             $vals->{'documentroot'}
145             = $util->ask( 'documentroot', default => $docroot );
146             }
147             else {
148             $vals->{'documentroot'} = $docroot;
149             }
150             }
151              
152             if ( $p{'prompt'} ) {
153             $vals->{'ssl'} ||= $util->ask( 'ssl' );
154             }
155              
156             if ( $vals->{'ssl'} ) {
157             my $certs = $prov->{config}{'Web'}{'sslcerts'}
158             || "/usr/local/etc/apache2/certs";
159              
160             if ( $p{'prompt'} ) {
161             $vals->{'sslcert'} ||= $util->ask( 'sslcert',
162             default => "$certs/$vhost.crt"
163             );
164             $vals->{'sslkey'} ||= $util->ask( 'sslkey',
165             default => "$certs/$vhost.key"
166             );
167             }
168             }
169              
170             while ( my ( $key, $val ) = each %$vals ) {
171             next if $key eq "debug";
172             next if $key eq "phpmyadmin";
173             next if $key =~ /ssl/;
174             next if $key =~ /custom/;
175             next if $key eq "options";
176             next if $key eq "verbose";
177             next if $key eq "redirect";
178              
179             $util->ask( $key ) if !defined $val;
180             }
181              
182             return $vals;
183             }
184              
185             sub check_apache_setup {
186              
187             my $self = shift;
188             my $conf = $self->{'conf'};
189              
190             my %r;
191              
192             # make sure apache etc dir exists
193             my $dir = $conf->{'apache_dir_etc'};
194             unless ( $dir && -d $dir ) {
195             return {
196             'error_code' => 401,
197             'error_desc' =>
198             'web_check_setup: cannot find Apache\'s conf dir! Please set apache_dir_etc in sysadmin.conf.\n'
199             };
200             }
201              
202             # make sure apache vhost setting exists
203             $dir = $conf->{'apache_dir_vhosts'};
204              
205             #unless ( $dir && (-d $dir || -f $dir) ) # can also be a fnmatch pattern!
206             unless ($dir) {
207             return {
208             'error_code' => 401,
209             'error_desc' =>
210             'web_check_setup: cannot find Apache\'s vhost file/dir! Please set apache_dir_vhosts in sysadmin.conf.\n'
211             };
212             }
213              
214             # all is well
215             return {
216             'error_code' => 200,
217             'error_desc' => 'web_check_setup: all tests pass!\n'
218             };
219             }
220              
221             1;
222              
223             __END__
224              
225             =pod
226              
227             =encoding UTF-8
228              
229             =head1 NAME
230              
231             Provision::Unix::Web - provision web hosting accounts
232              
233             =head1 VERSION
234              
235             version 1.07
236              
237             =head1 SYNOPSIS
238              
239             Provision web hosting accounts.
240              
241             use Provision::Unix::Web;
242              
243             my $foo = Provision::Unix::Web->new();
244             ...
245              
246             =head1 FUNCTIONS
247              
248             =head2 new
249              
250             Creates and returns a new Provision::Unix::Web object.
251              
252             =head1 BUGS
253              
254             Please report any bugs or feature requests to C<bug-unix-provision-web at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Provision-Unix>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
255              
256             =head1 SUPPORT
257              
258             You can find documentation for this module with the perldoc command.
259              
260             perldoc Provision::Unix
261              
262             You can also look for information at:
263              
264             =over 4
265              
266             =item * RT: CPAN's request tracker
267              
268             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Provision-Unix>
269              
270             =item * AnnoCPAN: Annotated CPAN documentation
271              
272             L<http://annocpan.org/dist/Provision-Unix>
273              
274             =item * CPAN Ratings
275              
276             L<http://cpanratings.perl.org/d/Provision-Unix>
277              
278             =item * Search CPAN
279              
280             L<http://search.cpan.org/dist/Provision-Unix>
281              
282             =back
283              
284             =head1 ACKNOWLEDGEMENTS
285              
286             =head1 AUTHOR
287              
288             Matt Simerson <msimerson@cpan.org>
289              
290             =head1 COPYRIGHT AND LICENSE
291              
292             This software is copyright (c) 2014 by The Network People, Inc..
293              
294             This is free software; you can redistribute it and/or modify it under
295             the same terms as the Perl 5 programming language system itself.
296              
297             =cut