Skip to content
Permalink
Browse files

Refactor Nginx.pm to remove common code for testing port privileges a…

…nd building base command paths. Add a reload command for nginx.
  • Loading branch information...
perlDreamer committed Jan 8, 2012
1 parent 46180af commit 8ba3bbe2f3964f8413d153014681f92e99d477ad
Showing with 81 additions and 14 deletions.
  1. +70 −12 wre/lib/WRE/Nginx.pm
  2. +11 −2 wre/sbin/wreservice.pl
@@ -26,6 +26,27 @@ WRE::Service
=cut


#-------------------------------------------------------------------

=head base ()
Get's the base command line invocation for this service and make sure that the user has permission
to start services.
/path/to/wre/nginx -c __NGINX_CONFIG__
=cut

sub base {
my $self = shift;
my $wreConfig = $self->wreConfig;
my $host = WRE::Host->new(wreConfig=>$wreConfig);
unless ($wreConfig->get("nginx/port") > 1024 || $host->isPrivilegedUser) {
croak "You are not an administrator on this machine so you cannot start services with ports 1-1024.";
}
return $wreConfig->getRoot("/prereqs/sbin/nginx")." -c ".$wreConfig->getRoot("/etc/nginx.conf");
}

#-------------------------------------------------------------------

=head getName ()
@@ -67,6 +88,32 @@ sub ping {

#-------------------------------------------------------------------

=head2 reload ( )
Makes nginx reload its configuration files without fully shutting down.
Returns a 1 if the start was successful, or a 0 if it was not.
Note: The process that runs this command must be either root or the user specified in the WRE config file.
=cut

sub reload {
my $self = shift;
my $cmd = $self->base . ' -s reload';
my $count = 0;
my $success = 0;
`$cmd`; # catch command line output
while ($count < 10 && !$success) {
sleep(1);
eval {$success = $self->ping};
$count++;
}
return $success;
}

#-------------------------------------------------------------------

=head2 start ( )
Returns a 1 if the start was successful, or a 0 if it was not.
@@ -77,12 +124,7 @@ Note: The process that runs this command must be either root or the user specifi

sub start {
my $self = shift;
my $wreConfig = $self->wreConfig;
my $host = WRE::Host->new(wreConfig=>$wreConfig);
unless ($wreConfig->get("nginx/port") > 1024 || $host->isPrivilegedUser) {
croak "You are not an administrator on this machine so you cannot start services with ports 1-1024.";
}
my $cmd = $wreConfig->getRoot("/prereqs/sbin/nginx")." -c ".$wreConfig->getRoot("/etc/nginx.conf");
my $cmd = $self->base;
my $count = 0;
my $success = 0;
`$cmd`; # catch command line output
@@ -109,12 +151,7 @@ Note: The process that runs this command must be either root or the user specifi

sub stop {
my $self = shift;
my $wreConfig = $self->wreConfig;
my $host = WRE::Host->new(wreConfig=>$wreConfig);
unless ($wreConfig->get("nginx/port") > 1024 || $host->isPrivilegedUser) {
croak "You are not an administrator on this machine so you cannot stop services with ports 1-1024.";
}
my $cmd = $wreConfig->getRoot("/prereqs/sbin/nginx")." -c ".$wreConfig->getRoot("/etc/nginx.conf")." -s stop";
my $cmd = $self->base . " -s stop";
`$cmd`; # catch command line output
my $count = 0;
my $success = 0;
@@ -128,4 +165,25 @@ sub stop {
return $success;
}

#-------------------------------------------------------------------

=head2 test ( )
Have nginx
Returns a 1 if the start was successful, or a 0 if it was not.
Note: The process that runs this command must be either root or the user specified in the WRE config file.
=cut

sub reload {
my $self = shift;
my $cmd = $self->base . ' -t';
my $out = `$cmd`; # catch command line output
print $out;
return 1;
}


1;
@@ -22,19 +22,20 @@
$|=1; # turn off buffering

my ($quiet, $help, $verbose) = "";
my (@start, @stop, @restart, @status) = ();
my (@start, @stop, @restart, @status, @reload) = ();

GetOptions(
"help" => \$help,
"start|begin=s{1,4}" => \@start,
"stop|end|shutdown=s{1,4}" => \@stop,
"restart|cycle=s{1,4}" => \@restart,
"reload=s{1,4}" => \@reload,
"status|ping=s{1,4}" => \@status,
"verbose" => \$verbose,
"quiet" => \$quiet,
);

if ($help || !(scalar(@start) || scalar(@stop) || scalar(@restart) || scalar(@status))) {
if ($help || !(scalar(@start) || scalar(@stop) || scalar(@restart) || scalar(@status) || scalar(@reload))) {
print <<STOP;
Usage: $0 --[action] [service] [service] [service]
@@ -61,6 +62,8 @@
--restart Stops and then starts a service again.
--reload Makes a service reload its configuration files, only works for Nginx.
--shutdown An alias for --stop.
--start Puts a service online.
@@ -124,6 +127,12 @@
}
}

if (scalar(@reload)) {
if (grep /^nginx|modproxy|all|web$/, @reload) {
printSuccess(sub{WRE::Nginx->new(wreConfig=>$config)->Reload}, "Reload nginx configs");
}
}

if (scalar(@status)) {
if (grep /^starman|modperl|all|web$/, @status) {
printSuccess(sub{WRE::Starman->new(wreConfig=>$config)->ping}, "Ping starman");

0 comments on commit 8ba3bbe

Please sign in to comment.
You can’t perform that action at this time.