#!/usr/bin/perl # # restart httpd server with our correct settings # # use strict; use warnings; use Sys::Hostname; use Data::Dump qw/ pp /; use File::Path; use Template; use FindBin; use File::Slurp; use Getopt::Long; use Pod::Usage; my $tmpl = "$FindBin::Bin/blog_httpd_conf.tt"; # check hostname and choose config files, paths, etc. # or allow for manual override my %def = ( 'ira' => { root => '/opt/webdocs/blog.peknet.com', apache => '/usr/sbin', conf => 'blog_httpd.conf', appname => 'PodBlog', ip => '10.0.0.63', port => '80' }, ); my %c = %def; my $host = hostname(); $host =~ s,\..*,,g; $host = 'test' if $ENV{PERL_TEST}; my ($verbose, $debug, $dump, $test); GetOptions( help => sub { pod2usage(1) }, man => sub { pod2usage(-exitstatus => 1, -verbose => 2) }, verbose => \$verbose, dump => \$dump, debug => \$debug, 'apache=s' => \$c{$host}->{apache}, 'appdir=s' => \$c{$host}->{root}, 'httpdconf=s' => \$c{$host}->{conf}, test => \$test, ) || pod2usage(1); if ($dump) { print pp(\%c); exit; } $debug and select(STDERR); $debug and $verbose = 1; if (!exists $def{$host} && !$test) { die "hostname '$host' doesn't match " . join(' or ', keys %def) . "\n"; } else { $debug and print "$host: "; } my $apache = $c{$host}->{apache}; my $root = $c{$host}->{root}; my $conf = $c{$host}->{conf}; my $action = shift @ARGV || 'restart'; mkpath(["$root/server_root/logs", "$root/server_root/run"], 1); mktmpl($c{$host}, $tmpl, "$root/server_root/$conf"); my $cmd = "$apache/httpd -d $root/server_root -f $root/server_root/$conf -k $action"; $debug and print $cmd, "\n"; exit if $test; if (!system($cmd)) { $verbose and print("$action ok\n"); } else { print("$action failed: $!\n"); } sub mktmpl { my ($config, $tmpl, $path) = @_; my $stash = {}; $stash->{header} = <{backend_ip} = $config->{ip} || ''; $stash->{backend_port} = $config->{port} || ''; $stash->{now} = scalar localtime; $stash->{appdir} = $config->{root}; $stash->{appname} = $config->{appname}; my $t = Template->new(ABSOLUTE => 1) or die $Template::ERROR; my $conf = ''; $t->process($tmpl, $stash, \$conf) or die $t->error; write_file($path, $conf); } __END__ =pod =head1 NAME httpd.pl - stop/start/restart Catalyst backend mod_perl server =head1 OPTIONS =over =item --help Print this POD. =item --man Print this POD as man page. =item --debug Print lots of verbage on stderr. =item --verbose Print some verbase on stdout. =item --dump Print config. =item --apache I Set alternate path to httpd program. See --dump. =item --appdir I Set alternate path to application root directory. See --dump. =item --httpdconf I Set alternate path to write "blog_httpd.conf" file. =back =head1 DESCRIPTION This script manages the backend Catalyst mod_perl server and writes correct httpd.conf file based on hostname. See OPTIONS for ways to control output. Takes one argument: start, stop or restart (same as B). The default is C. Example: sudo /opt/webdocs/blog.peknet.com/server_root/httpd.pl will restart the server with all defaults. =head1 AUTHOR Peter Karman This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Based on example from Bill Moseley. Thanks Bill! =cut