File Coverage

blib/lib/WebDyne/Install/Lighttpd.pm
Criterion Covered Total %
statement 37 107 34.5
branch 0 34 0.0
condition 0 26 0.0
subroutine 13 15 86.6
pod 0 2 0.0
total 50 184 27.1


line stmt bran cond sub pod time code
1             #
2             #
3             # Copyright (C) 2006-2010 Andrew Speer . All rights
4             # reserved.
5             #
6             # This file is part of WebDyne::Install::Lighttpd.
7             #
8             # WebDyne::Install is free software; you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation; either version 2 of the License, or
11             # (at your option) any later version.
12             #
13             # This program is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17             #
18             # You should have received a copy of the GNU General Public License
19             # along with this program; if not, write to the Free Software
20             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21             #
22             #
23             package WebDyne::Install::Lighttpd;
24              
25              
26             # Compiler Pragma
27             #
28 1     1   21496 sub BEGIN { $^W=0 };
29 1     1   10 use strict qw(vars);
  1         2  
  1         27  
30 1     1   4 use vars qw($VERSION);
  1         2  
  1         42  
31 1     1   8 use warnings;
  1         2  
  1         31  
32 1     1   4 no warnings qw(uninitialized);
  1         3  
  1         44  
33              
34              
35             # Webmod Modules
36             #
37 1     1   845 use WebDyne::Base;
  1         31782  
  1         7  
38              
39              
40             # External Modules
41             #
42 1     1   122 use File::Spec;
  1         2  
  1         20  
43 1     1   1230 use Text::Template;
  1         3345  
  1         46  
44 1     1   8 use IO::File;
  1         2  
  1         471  
45              
46              
47             # Base installer
48             #
49 1     1   807 use WebDyne::Install qw(message);
  1         13315  
  1         85  
50              
51              
52             # Constants
53             #
54 1     1   10 use WebDyne::Constant;
  1         3  
  1         559  
55 1     1   6 use WebDyne::Install::Constant;
  1         2  
  1         86  
56 1     1   693 use WebDyne::Install::Lighttpd::Constant;
  1         4  
  1         1610  
57              
58              
59             # Version information in a formate suitable for CPAN etc. Must be
60             # all on one line
61             #
62             $VERSION='1.009';
63              
64              
65             # Debug
66             #
67             0 && debug("%s loaded, version $VERSION", __PACKAGE__);
68              
69              
70             # Uninstall flag
71             #
72             my $Uninstall_fg;
73              
74              
75             # Init done.
76             #
77             1;
78              
79              
80             #------------------------------------------------------------------------------
81              
82              
83             # Uninstall
84             #
85             sub uninstall {
86              
87             # Set flag and call install routine
88             #
89 0     0 0   $Uninstall_fg++;
90 0           shift()->install(@_);
91              
92             }
93              
94              
95             # Install the Lighttpd conf params needed to run the system
96             #
97             sub install {
98              
99              
100             # Get class, other paths
101             #
102 0     0 0   my ($class, $prefix, $installbin)=@_;
103              
104              
105             # Run the base install/uninstall routine to create the cache dir
106             #
107 0 0         unless ($Uninstall_fg) {
108 0 0         WebDyne::Install->install($prefix, $installbin) ||
109             return err();
110             }
111             else {
112 0 0         WebDyne::Install->uninstall($prefix, $installbin) ||
113             return err();
114             }
115              
116              
117             # Get package file name so we can look up in inc
118             #
119 0           (my $class_fn=$class.q(.pm))=~s/::/\//g;
120              
121              
122             # Load constants, get full path for class and constants from INC
123             #
124 0   0       $class_fn=$INC{$class_fn} ||
125             return err("unable to find location for $class in \%INC");
126              
127              
128             # Split
129             #
130 0           my $class_dn=(File::Spec->splitpath($class_fn))[1];
131 0           my @class=split(/\:\:/, $class);
132              
133              
134             # Get webdyne cache dn;
135             #
136 0           my $cache_dn=&WebDyne::Install::cache_dn($prefix);
137              
138              
139             # Get config/constant hash ref
140             #
141 0           my %constant=(
142              
143             %WebDyne::Constant::Constant,
144             %WebDyne::Install::Constant::Constant,
145             %WebDyne::Install::Lighttpd::Constant::Constant,
146             DIR_INSTALLBIN => $installbin,
147             WEBDYNE_CACHE_DN => $cache_dn,
148              
149             );
150 0           my $config_hr=\%constant;
151 0           0 && debug('config_hr %s', Dumper($config_hr));
152              
153              
154             # Get template file name
155             #
156 0           my $template_dn=File::Spec->catdir($class_dn, $class[-1]);
157 0           my $template_fn=File::Spec->catfile(
158             $template_dn, $FILE_WEBDYNE_CONF_TEMPLATE);
159              
160              
161             # Open it as a template
162             #
163 0   0       my $template_or=Text::Template->new(
164              
165             type => 'FILE',
166             source => $template_fn,
167              
168             ) || return err("unable to open template $template_fn, $!");
169              
170              
171             # Fill in with out self ref as a hash
172             #
173 0   0       my $webdyne_conf=$template_or->fill_in(
174              
175             HASH => $config_hr,
176             DELIMITERS => [ '' ],
177              
178             ) || return err("unable to fill in template $template_fn, $Text::Template::ERROR");
179              
180              
181              
182             # Get lighttpd config dir
183             #
184 0   0       my $lighttpd_conf_dn=$DIR_LIGHTTPD_CONF ||
185             return err('unable to determine Lighttpd config directory');
186              
187              
188             # Work out config file name
189             #
190 0           my $webdyne_conf_fn=File::Spec->catfile(
191             $lighttpd_conf_dn, $FILE_WEBDYNE_CONF);
192              
193              
194             # Open, write webdyne config file unless in uninstall
195             #
196 0 0         unless ($Uninstall_fg) {
197 0           message "writing Lighttpd config file '$webdyne_conf_fn'.";
198 0   0       my $webdyne_conf_fh=IO::File->new($webdyne_conf_fn, O_CREAT|O_WRONLY|O_TRUNC) ||
199             return err("unable to open file $webdyne_conf_fn, $!");
200 0           print $webdyne_conf_fh $webdyne_conf;
201 0           $webdyne_conf_fh->close();
202             }
203             else {
204              
205             # In uninstall - get rid of conf file
206             #
207 0 0         if (-f $webdyne_conf_fn) {
208 0 0         unlink($webdyne_conf_fn) && message "remove config file $webdyne_conf_fn";
209             }
210              
211             }
212              
213              
214             # Modify lighttpd config file
215             #
216              
217              
218              
219             # Get Lighttpd config file, append root if not absolute
220             #
221 0   0       my $lighttpd_conf_fn=$FILE_LIGHTTPD_CONF ||
222             return err("unable to determine main server config file");
223 0   0       my $lighttpd_conf_fh=IO::File->new($lighttpd_conf_fn, O_RDONLY) ||
224             return err("unable to open file lighttpd_conf_fn, $!");
225 0           message "Lighttpd config file '$lighttpd_conf_fn'";
226              
227              
228             # Setup delims looking for
229             #
230 0           my ($delim, @delim)=$config_hr->{'FILE_LIGHTTPD_CONF_DELIM'};
231              
232              
233             # Turn into array, search for delims
234             #
235 0           my ($index, @lighttpd_conf);
236 0           while (my $line=<$lighttpd_conf_fh>) {
237 0           push @lighttpd_conf, $line;
238 0 0         push(@delim, $index) if $line=~/\Q$delim\E/;
239 0           $index++;
240             }
241              
242              
243             # Check found right number of delims
244             #
245 0 0 0       if (@delim!=2 and @delim!=0) {
246              
247 0 0         return err(
248             "found %s '$delim' delimiter%s in $lighttpd_conf_fn at line%s %s, expected exactly 2 delimiters",
249             scalar @delim,
250             ($#delim ? 's' : '') x 2,
251             join(',', @delim)
252             );
253              
254             }
255              
256              
257             # Check if delim found, if not, make last line
258             #
259 0 0         unless (@delim) { @delim=($index, $index-1) }
  0            
260 0           $delim[1]++;
261              
262              
263             # Close
264             #
265 0           $lighttpd_conf_fh->close();
266              
267              
268             # Splice the lines between the delimiters out
269             #
270 0           splice(@lighttpd_conf, $delim[0], $delim[1]-$delim[0]);
271              
272              
273             # Clean up end of conf file, remove excess CR's
274             #
275 0           my $lineno=$delim[0]-1;
276 0           for (undef; $lineno > 0; $lineno--) {
277              
278              
279             # We are going backwards through the file, as soon as we
280             # see something we quit
281             #
282 0           my $line=$lighttpd_conf[$lineno];
283 0           chomp($line);
284              
285              
286             # Empty
287             #
288 0 0         if ($line=~/^\s*$/) {
289              
290              
291             # Yes, delete and continue
292             #
293 0           splice(@lighttpd_conf, $lineno, 1);
294              
295             }
296             else {
297              
298              
299             # No, quit after rewinding lineno to last val
300             #
301 0           $lineno++;
302 0           last;
303              
304             }
305             }
306              
307              
308             # Only splice back in if not uninstalling
309             #
310 0 0         unless ($Uninstall_fg) {
311              
312              
313             # Get template we want to include in the config file
314             #
315 0           $template_fn=File::Spec->catfile(
316             $class_dn, $class[-1], $FILE_LIGHTTPD_CONF_TEMPLATE);
317              
318              
319             # Open it as a template
320             #
321 0   0       $template_or=Text::Template->new(
322              
323             type => 'FILE',
324             source => $template_fn,
325              
326             ) || return err("unable to open template $template_fn, $!");
327              
328              
329             # Fill in with out self ref as a hash
330             #
331 0   0       my $lighttpd_conf=$template_or->fill_in(
332              
333             HASH => $config_hr,
334             DELIMITERS => [ '' ],
335              
336             ) || return err("unable to fill in template $template_fn, $Text::Template::ERROR");
337              
338              
339             # Splice in now, but write out at end of block
340             #
341 0           splice(@lighttpd_conf, $lineno, undef, $lighttpd_conf);
342              
343              
344             }
345              
346              
347             # Re-open httpd.conf for write out, unless uninstall and delims not found, which
348             # means nothing was changed.
349             #
350 0 0 0       unless ($Uninstall_fg && ($delim[0] == $delim[1])) {
351 0   0       $lighttpd_conf_fh=IO::File->new($lighttpd_conf_fn, O_TRUNC|O_WRONLY) ||
352             return err("unable to open file $lighttpd_conf_fn, $!");
353 0           print $lighttpd_conf_fh join('', @lighttpd_conf);
354 0           $lighttpd_conf_fh->close();
355 0           message "Lighttpd config file '$lighttpd_conf_fn' updated.";
356             }
357              
358              
359             # Almost done ..
360             #
361 0 0         unless ($Uninstall_fg) {
362              
363             # Chown cache dir unless it is the system temp dir - then don't mess with it
364             #
365 0 0         if ($cache_dn) {
366 0 0         unless ($cache_dn eq File::Spec->tmpdir()) {
367 0           message
368             "Granting Lighttpd ($LIGHTTPD_UNAME.$LIGHTTPD_GNAME) write access to cache directory '$cache_dn'.";
369 0 0         chown($LIGHTTPD_UID, $LIGHTTPD_GID, $cache_dn) ||
370             return err("unable to chown $cache_dn to $LIGHTTPD_UNAME.$LIGHTTPD_GNAME");
371              
372             # Done
373             #
374 0           message "install completed.";
375              
376             }
377             }
378             }
379             else {
380              
381 0           message "uninstall completed";
382              
383             };
384              
385              
386             # Finished
387 0           message;
388 0           return \undef;
389              
390              
391             }
392              
393             __END__