| 1 | # | ||
| 2 | # | ||
| 3 | # Copyright (C) 2006-2010 Andrew Speer <andrew@webdyne.org>. 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 | ▶ | sub BEGIN { $^W=0 }; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 1 | ▶ | use strict qw(vars); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 1 | ▶ | no warnings qw(uninitialized); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 33 | |||
| 34 | |||
| 35 | # Webmod Modules | ||
| 36 | # | ||
| 37 | 0 | ▶ | use WebDyne::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 38 | |||
| 39 | |||
| 40 | # External Modules | ||
| 41 | # | ||
| 42 | use File::Spec; | ||
| 43 | use Text::Template; | ||
| 44 | use IO::File; | ||
| 45 | |||
| 46 | |||
| 47 | # Base installer | ||
| 48 | # | ||
| 49 | use WebDyne::Install qw(message); | ||
| 50 | |||
| 51 | |||
| 52 | # Constants | ||
| 53 | # | ||
| 54 | use WebDyne::Constant; | ||
| 55 | use WebDyne::Install::Constant; | ||
| 56 | use WebDyne::Install::Lighttpd::Constant; | ||
| 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 | $Uninstall_fg++; | ||
| 90 | 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 | my ($class, $prefix, $installbin)=@_; | ||
| 103 | |||
| 104 | |||
| 105 | # Run the base install/uninstall routine to create the cache dir | ||
| 106 | # | ||
| 107 | unless ($Uninstall_fg) { | ||
| 108 | WebDyne::Install->install($prefix, $installbin) || | ||
| 109 | return err(); | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | 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 | (my $class_fn=$class.q(.pm))=~s/::/\//g; | ||
| 120 | |||
| 121 | |||
| 122 | # Load constants, get full path for class and constants from INC | ||
| 123 | # | ||
| 124 | $class_fn=$INC{$class_fn} || | ||
| 125 | return err("unable to find location for $class in \%INC"); | ||
| 126 | |||
| 127 | |||
| 128 | # Split | ||
| 129 | # | ||
| 130 | my $class_dn=(File::Spec->splitpath($class_fn))[1]; | ||
| 131 | my @class=split(/\:\:/, $class); | ||
| 132 | |||
| 133 | |||
| 134 | # Get webdyne cache dn; | ||
| 135 | # | ||
| 136 | my $cache_dn=&WebDyne::Install::cache_dn($prefix); | ||
| 137 | |||
| 138 | |||
| 139 | # Get config/constant hash ref | ||
| 140 | # | ||
| 141 | 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 | my $config_hr=\%constant; | ||
| 151 | 0 && debug('config_hr %s', Dumper($config_hr)); | ||
| 152 | |||
| 153 | |||
| 154 | # Get template file name | ||
| 155 | # | ||
| 156 | my $template_dn=File::Spec->catdir($class_dn, $class[-1]); | ||
| 157 | my $template_fn=File::Spec->catfile( | ||
| 158 | $template_dn, $FILE_WEBDYNE_CONF_TEMPLATE); | ||
| 159 | |||
| 160 | |||
| 161 | # Open it as a template | ||
| 162 | # | ||
| 163 | 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 | 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 | 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 | 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 | unless ($Uninstall_fg) { | ||
| 197 | message "writing Lighttpd config file '$webdyne_conf_fn'."; | ||
| 198 | 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 | print $webdyne_conf_fh $webdyne_conf; | ||
| 201 | $webdyne_conf_fh->close(); | ||
| 202 | } | ||
| 203 | else { | ||
| 204 | |||
| 205 | # In uninstall - get rid of conf file | ||
| 206 | # | ||
| 207 | if (-f $webdyne_conf_fn) { | ||
| 208 | 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 | my $lighttpd_conf_fn=$FILE_LIGHTTPD_CONF || | ||
| 222 | return err("unable to determine main server config file"); | ||
| 223 | my $lighttpd_conf_fh=IO::File->new($lighttpd_conf_fn, O_RDONLY) || | ||
| 224 | return err("unable to open file lighttpd_conf_fn, $!"); | ||
| 225 | message "Lighttpd config file '$lighttpd_conf_fn'"; | ||
| 226 | |||
| 227 | |||
| 228 | # Setup delims looking for | ||
| 229 | # | ||
| 230 | my ($delim, @delim)=$config_hr->{'FILE_LIGHTTPD_CONF_DELIM'}; | ||
| 231 | |||
| 232 | |||
| 233 | # Turn into array, search for delims | ||
| 234 | # | ||
| 235 | my ($index, @lighttpd_conf); | ||
| 236 | while (my $line=<$lighttpd_conf_fh>) { | ||
| 237 | push @lighttpd_conf, $line; | ||
| 238 | push(@delim, $index) if $line=~/\Q$delim\E/; | ||
| 239 | $index++; | ||
| 240 | } | ||
| 241 | |||
| 242 | |||
| 243 | # Check found right number of delims | ||
| 244 | # | ||
| 245 | if (@delim!=2 and @delim!=0) { | ||
| 246 | |||
| 247 | 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 | unless (@delim) { @delim=($index, $index-1) } | ||
| 260 | $delim[1]++; | ||
| 261 | |||
| 262 | |||
| 263 | # Close | ||
| 264 | # | ||
| 265 | $lighttpd_conf_fh->close(); | ||
| 266 | |||
| 267 | |||
| 268 | # Splice the lines between the delimiters out | ||
| 269 | # | ||
| 270 | splice(@lighttpd_conf, $delim[0], $delim[1]-$delim[0]); | ||
| 271 | |||
| 272 | |||
| 273 | # Clean up end of conf file, remove excess CR's | ||
| 274 | # | ||
| 275 | my $lineno=$delim[0]-1; | ||
| 276 | 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 | my $line=$lighttpd_conf[$lineno]; | ||
| 283 | chomp($line); | ||
| 284 | |||
| 285 | |||
| 286 | # Empty | ||
| 287 | # | ||
| 288 | if ($line=~/^\s*$/) { | ||
| 289 | |||
| 290 | |||
| 291 | # Yes, delete and continue | ||
| 292 | # | ||
| 293 | splice(@lighttpd_conf, $lineno, 1); | ||
| 294 | |||
| 295 | } | ||
| 296 | else { | ||
| 297 | |||
| 298 | |||
| 299 | # No, quit after rewinding lineno to last val | ||
| 300 | # | ||
| 301 | $lineno++; | ||
| 302 | last; | ||
| 303 | |||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | |||
| 308 | # Only splice back in if not uninstalling | ||
| 309 | # | ||
| 310 | unless ($Uninstall_fg) { | ||
| 311 | |||
| 312 | |||
| 313 | # Get template we want to include in the config file | ||
| 314 | # | ||
| 315 | $template_fn=File::Spec->catfile( | ||
| 316 | $class_dn, $class[-1], $FILE_LIGHTTPD_CONF_TEMPLATE); | ||
| 317 | |||
| 318 | |||
| 319 | # Open it as a template | ||
| 320 | # | ||
| 321 | $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 | 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 | 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 | unless ($Uninstall_fg && ($delim[0] == $delim[1])) { | ||
| 351 | $lighttpd_conf_fh=IO::File->new($lighttpd_conf_fn, O_TRUNC|O_WRONLY) || | ||
| 352 | return err("unable to open file $lighttpd_conf_fn, $!"); | ||
| 353 | print $lighttpd_conf_fh join('', @lighttpd_conf); | ||
| 354 | $lighttpd_conf_fh->close(); | ||
| 355 | message "Lighttpd config file '$lighttpd_conf_fn' updated."; | ||
| 356 | } | ||
| 357 | |||
| 358 | |||
| 359 | # Almost done .. | ||
| 360 | # | ||
| 361 | unless ($Uninstall_fg) { | ||
| 362 | |||
| 363 | # Chown cache dir unless it is the system temp dir - then don't mess with it | ||
| 364 | # | ||
| 365 | if ($cache_dn) { | ||
| 366 | unless ($cache_dn eq File::Spec->tmpdir()) { | ||
| 367 | message | ||
| 368 | "Granting Lighttpd ($LIGHTTPD_UNAME.$LIGHTTPD_GNAME) write access to cache directory '$cache_dn'."; | ||
| 369 | 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 | message "install completed."; | ||
| 375 | |||
| 376 | } | ||
| 377 | } | ||
| 378 | } | ||
| 379 | else { | ||
| 380 | |||
| 381 | message "uninstall completed"; | ||
| 382 | |||
| 383 | }; | ||
| 384 | |||
| 385 | |||
| 386 | # Finished | ||
| 387 | message; | ||
| 388 | return \undef; | ||
| 389 | |||
| 390 | |||
| 391 | } | ||
| 392 | |||
| 393 | __END__ | ||