17 |
use File::Slurp; |
use File::Slurp; |
18 |
#use JSON; |
#use JSON; |
19 |
use IO::Socket::INET; |
use IO::Socket::INET; |
|
use Module::Refresh; |
|
20 |
|
|
21 |
our $pids; |
our $pids; |
22 |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
37 |
|
|
38 |
use html; |
use html; |
39 |
our $static_pids; |
our $static_pids; |
40 |
|
use progress_bar; |
41 |
|
|
42 |
sub static { |
sub static { |
43 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
64 |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n"; |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n"; |
65 |
|
|
66 |
open(my $fh, $full); |
open(my $fh, $full); |
|
print "static $path $type $size\n"; |
|
67 |
|
|
68 |
my $block = 8192; |
my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it) |
69 |
my $buff; |
my $buff; |
70 |
my $pos = 0; |
my $pos = 0; |
71 |
|
|
72 |
|
warn "static $path $type $size block: $block\n"; |
73 |
|
|
74 |
|
progress_bar::start; |
75 |
|
|
76 |
while( my $len = read $fh, $buff, $block ) { |
while( my $len = read $fh, $buff, $block ) { |
77 |
print $client $buff; |
print $client $buff; |
78 |
|
$client->flush; |
79 |
$pos += $len; |
$pos += $len; |
80 |
printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size; |
progress_bar::tick( $path, $pos, $size ); |
81 |
} |
} |
82 |
close($fh); |
close($fh); |
83 |
close($client); |
close($client); |
84 |
|
|
85 |
print "$path $pos == $size OK\n"; |
print STDERR "\n"; |
86 |
|
|
87 |
|
warn "exit static child"; |
88 |
|
|
89 |
exit; |
exit(0); |
90 |
} |
} |
91 |
|
|
92 |
use boolean; |
use boolean; |
100 |
my $daemon = shift; |
my $daemon = shift; |
101 |
my $pid = $pids->{$daemon}; |
my $pid = $pids->{$daemon}; |
102 |
|
|
103 |
warn "start_stop $daemon $pid pids: ",dump( $pids ); |
warn "start_stop $daemon $pid\n"; |
104 |
|
|
105 |
if ( $pid =~ m{^\d+$} ) { |
if ( $pid =~ m{^\d+$} ) { |
106 |
warn "kill 9 $pid"; |
my $pstree = `pstree -p $pid`; |
107 |
kill 9, $pid; |
my @pids = $pstree =~ m{\((\d+)\)}g; |
108 |
|
warn "pstree $pstree pids ",dump( @pids ); |
109 |
|
kill 1, $_ foreach reverse @pids; |
110 |
$pids->{$daemon} = 'stopped'; |
$pids->{$daemon} = 'stopped'; |
111 |
return qq|$daemon pid $pid stopped|; |
return qq|$daemon pid $pid stopped|; |
112 |
} else { |
} else { |
113 |
if ( $pid = fork ) { |
if ( $pid = fork ) { |
114 |
# parent |
# parent |
115 |
$pids->{$daemon} = $pid; |
$pids->{$daemon} = $pid; |
116 |
warn "forked $daemon $pid"; |
warn "forked $daemon $pid\n"; |
117 |
return qq|$daemon pid $pid started|; |
return qq|$daemon pid $pid started|; |
118 |
} elsif ( defined $pid ) { |
} elsif ( defined $pid ) { |
119 |
# child |
# child |
120 |
my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')'; |
my $invoke = 'start'; |
121 |
|
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
122 |
|
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
123 |
warn "eval $eval"; |
warn "eval $eval"; |
124 |
eval $eval; |
eval $eval; |
125 |
warn "can't start $daemon: $@" if $@; |
warn "can't start $daemon: $@" if $@; |
136 |
sub get_request { |
sub get_request { |
137 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
138 |
|
|
139 |
warn "get_request $client $path ",dump( $param ); |
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
140 |
|
|
141 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
142 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
153 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
154 |
); |
); |
155 |
|
|
156 |
my $debug_proc; |
my $debug_proc = ''; |
157 |
|
|
158 |
warn 'pids: ', dump( $pids ); |
warn 'pids: ', dump( $pids ) if $debug; |
159 |
foreach my $name ( keys %$pids ) { |
foreach my $name ( sort keys %$pids ) { |
160 |
my $pid = $pids->{$name} || next; |
my $pid = $pids->{$name} || next; |
161 |
|
|
162 |
my $html = qq|<a href=/$name>$pid</a>|; |
my $html = qq|<a href=/start_stop/$name>$pid</a>|; |
163 |
|
|
164 |
if ( $debug ) { |
my $proc = "/proc/$pid/status"; |
|
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
|
165 |
|
|
166 |
my $proc = "/proc/$pid/status"; |
if ( -e $proc ) { |
167 |
$debug_proc |
if ( $debug ) { |
168 |
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
169 |
. read_file($proc) |
|
170 |
. qq|</pre>| |
$debug_proc |
171 |
if -e $proc; |
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
172 |
|
. read_file($proc) |
173 |
|
. qq|</pre>| |
174 |
|
; |
175 |
|
} |
176 |
|
|
177 |
|
if ( $name->can('start_fork') ) { |
178 |
|
$html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork; |
179 |
|
} |
180 |
|
|
181 |
|
if ( $name->can('actions') ) { |
182 |
|
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
183 |
|
} |
184 |
} |
} |
185 |
|
|
186 |
push @rows, ( $name => $html ); |
push @rows, ( $name => $html ); |
187 |
} |
} |
188 |
|
|
189 |
warn 'static_pids: ', dump( $static_pids ); |
my $below_table = ''; |
190 |
|
|
191 |
|
warn 'static_pids: ', dump( $static_pids ) if $debug; |
192 |
foreach my $pid ( keys %$static_pids ) { |
foreach my $pid ( keys %$static_pids ) { |
193 |
my $path = $static_pids->{$pid}; |
my $path = $static_pids->{$pid}; |
194 |
if ( -d "/proc/$pid" ) { |
if ( -d "/proc/$pid" ) { |
195 |
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
196 |
|
} elsif ( $param->{clean_completed_downloads} ) { |
197 |
|
delete $static_pids->{$pid} |
198 |
} else { |
} else { |
199 |
push @rows, ( $path => "$pid competed" ); |
push @rows, ( $path => "$pid competed" ); |
200 |
|
$below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|; |
201 |
} |
} |
202 |
} |
} |
203 |
|
|
204 |
print $client $ok |
print $client $ok |
205 |
, html::table( 2, @rows ) |
, html::table( 2, @rows ) |
206 |
|
, $below_table |
207 |
, html::tabs( log::mac_changes ) |
, html::tabs( log::mac_changes ) |
208 |
, $debug_proc |
, $debug_proc |
209 |
; |
; |
213 |
warn $@ if $@; |
warn $@ if $@; |
214 |
print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|; |
print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|; |
215 |
server::debug( $debug ) if $1 eq 'debug'; |
server::debug( $debug ) if $1 eq 'debug'; |
216 |
} elsif ( $path =~ m{^/(screen|kvm)} ) { |
} elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes |
217 |
print $client $redirect, start_stop($1); |
print $client $redirect, start_stop($1); |
218 |
|
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
219 |
|
$1->$2(); |
220 |
|
print $client $redirect; |
221 |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
222 |
print $client $redirect; |
print $client $redirect; |
223 |
kill 9, $1 && warn "killed $1"; |
kill 1, $1 || kill 9, $2 && warn "killed $1"; |
224 |
} elsif ( $path eq '/exit' ) { |
} elsif ( $path eq '/exit' ) { |
225 |
# DESTROY; |
# DESTROY; |
226 |
exit 0; |
exit 0; |
256 |
start_stop 'screen'; |
start_stop 'screen'; |
257 |
start_stop 'kvm'; |
start_stop 'kvm'; |
258 |
|
|
259 |
while (my $client = $server->accept()) { |
while (1) { |
260 |
$client->autoflush(1); |
my $client = $server->accept() || next; # ALARM trickle us |
261 |
my $request = <$client>; |
my $request = <$client>; |
262 |
|
|
263 |
warn "request $request\n" if $debug; |
warn "request $request\n" if $debug; |
264 |
|
|
|
Module::Refresh->refresh; |
|
|
|
|
265 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
266 |
my $path = $1; |
my $path = $1; |
267 |
my $param; |
my $param; |
272 |
} |
} |
273 |
warn "param: ",dump( $param ) if $debug; |
warn "param: ",dump( $param ) if $debug; |
274 |
} |
} |
|
warn "path $path param: ",dump( $param ); |
|
275 |
get_request $client, $path, $param; |
get_request $client, $path, $param; |
276 |
} else { |
} else { |
277 |
print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request"; |
print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request"; |