Skip to content

Commit 8ba3bbe

Browse files
committedJan 8, 2012
Refactor Nginx.pm to remove common code for testing port privileges and building base command paths. Add a reload command for nginx.
1 parent 46180af commit 8ba3bbe

File tree

2 files changed

+81
-14
lines changed

2 files changed

+81
-14
lines changed
 

‎wre/lib/WRE/Nginx.pm

+70-12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ WRE::Service
2626
=cut
2727

2828

29+
#-------------------------------------------------------------------
30+
31+
=head base ()
32+
33+
Get's the base command line invocation for this service and make sure that the user has permission
34+
to start services.
35+
36+
/path/to/wre/nginx -c __NGINX_CONFIG__
37+
38+
=cut
39+
40+
sub base {
41+
my $self = shift;
42+
my $wreConfig = $self->wreConfig;
43+
my $host = WRE::Host->new(wreConfig=>$wreConfig);
44+
unless ($wreConfig->get("nginx/port") > 1024 || $host->isPrivilegedUser) {
45+
croak "You are not an administrator on this machine so you cannot start services with ports 1-1024.";
46+
}
47+
return $wreConfig->getRoot("/prereqs/sbin/nginx")." -c ".$wreConfig->getRoot("/etc/nginx.conf");
48+
}
49+
2950
#-------------------------------------------------------------------
3051

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

6889
#-------------------------------------------------------------------
6990

91+
=head2 reload ( )
92+
93+
Makes nginx reload its configuration files without fully shutting down.
94+
95+
Returns a 1 if the start was successful, or a 0 if it was not.
96+
97+
Note: The process that runs this command must be either root or the user specified in the WRE config file.
98+
99+
=cut
100+
101+
sub reload {
102+
my $self = shift;
103+
my $cmd = $self->base . ' -s reload';
104+
my $count = 0;
105+
my $success = 0;
106+
`$cmd`; # catch command line output
107+
while ($count < 10 && !$success) {
108+
sleep(1);
109+
eval {$success = $self->ping};
110+
$count++;
111+
}
112+
return $success;
113+
}
114+
115+
#-------------------------------------------------------------------
116+
70117
=head2 start ( )
71118
72119
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
77124

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

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

168+
#-------------------------------------------------------------------
169+
170+
=head2 test ( )
171+
172+
Have nginx
173+
174+
Returns a 1 if the start was successful, or a 0 if it was not.
175+
176+
Note: The process that runs this command must be either root or the user specified in the WRE config file.
177+
178+
=cut
179+
180+
sub reload {
181+
my $self = shift;
182+
my $cmd = $self->base . ' -t';
183+
my $out = `$cmd`; # catch command line output
184+
print $out;
185+
return 1;
186+
}
187+
188+
131189
1;

‎wre/sbin/wreservice.pl

+11-2
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@
2222
$|=1; # turn off buffering
2323

2424
my ($quiet, $help, $verbose) = "";
25-
my (@start, @stop, @restart, @status) = ();
25+
my (@start, @stop, @restart, @status, @reload) = ();
2626

2727
GetOptions(
2828
"help" => \$help,
2929
"start|begin=s{1,4}" => \@start,
3030
"stop|end|shutdown=s{1,4}" => \@stop,
3131
"restart|cycle=s{1,4}" => \@restart,
32+
"reload=s{1,4}" => \@reload,
3233
"status|ping=s{1,4}" => \@status,
3334
"verbose" => \$verbose,
3435
"quiet" => \$quiet,
3536
);
3637

37-
if ($help || !(scalar(@start) || scalar(@stop) || scalar(@restart) || scalar(@status))) {
38+
if ($help || !(scalar(@start) || scalar(@stop) || scalar(@restart) || scalar(@status) || scalar(@reload))) {
3839
print <<STOP;
3940
Usage: $0 --[action] [service] [service] [service]
4041
@@ -61,6 +62,8 @@
6162
6263
--restart Stops and then starts a service again.
6364
65+
--reload Makes a service reload its configuration files, only works for Nginx.
66+
6467
--shutdown An alias for --stop.
6568
6669
--start Puts a service online.
@@ -124,6 +127,12 @@
124127
}
125128
}
126129

130+
if (scalar(@reload)) {
131+
if (grep /^nginx|modproxy|all|web$/, @reload) {
132+
printSuccess(sub{WRE::Nginx->new(wreConfig=>$config)->Reload}, "Reload nginx configs");
133+
}
134+
}
135+
127136
if (scalar(@status)) {
128137
if (grep /^starman|modperl|all|web$/, @status) {
129138
printSuccess(sub{WRE::Starman->new(wreConfig=>$config)->ping}, "Ping starman");

0 commit comments

Comments
 (0)
Please sign in to comment.