17 |
use File::Slurp; |
use File::Slurp; |
18 |
#use JSON; |
#use JSON; |
19 |
use IO::Socket::INET; |
use IO::Socket::INET; |
20 |
use Module::Refresh; |
|
21 |
|
our $pids; |
22 |
|
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
23 |
|
|
24 |
|
sub DESTROY { |
25 |
|
warn "pids ",dump( $pids ); |
26 |
|
foreach ( values %$pids ) { |
27 |
|
warn "kill $_"; |
28 |
|
kill 1,$_ || kill 9, $_; |
29 |
|
} |
30 |
|
} |
31 |
|
|
32 |
our $port = 7777; |
our $port = 7777; |
|
our $debug = 1; |
|
33 |
|
|
34 |
use server; |
use server; |
35 |
|
our $debug = server::debug; |
36 |
our $url = "http://$server::ip:$port"; |
our $url = "http://$server::ip:$port"; |
37 |
|
|
38 |
use html; |
use html; |
39 |
|
our $static_pids; |
40 |
|
use progress_bar; |
41 |
|
|
42 |
sub static { |
sub static { |
43 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
44 |
|
|
45 |
$path = "tftp/$path"; |
my $full = "$server::base_dir/tftp/$path"; |
46 |
|
|
47 |
if ( ! -e $path || -d $path ) { |
return if ! -f $full; |
48 |
print $client "HTTP/1.0 404 $path not found\r\n"; |
|
49 |
return; |
if ( my $pid = fork ) { |
50 |
|
# parent |
51 |
|
close($client); |
52 |
|
print "http static child $pid\n"; |
53 |
|
$static_pids->{$pid} = $path; |
54 |
|
return 1; |
55 |
} |
} |
56 |
|
|
57 |
my $type = 'text/plain'; |
my $type = 'application/octet-stream'; |
58 |
$type = 'text/html' if $path =~ m{\.htm}; |
$type = 'text/html' if $path =~ m{\.htm}; |
59 |
$type = 'application/javascript' if $path =~ m{\.js}; |
$type = 'application/javascript' if $path =~ m{\.js}; |
60 |
|
$type = 'text/plain' if $path =~ m{\.txt}; |
61 |
|
|
62 |
|
my $size = -s $full || return; |
63 |
|
|
64 |
|
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); |
67 |
|
|
68 |
|
my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it) |
69 |
|
my $buff; |
70 |
|
my $pos = 0; |
71 |
|
|
72 |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n"; |
warn "static $path $type $size block: $block\n"; |
73 |
open(my $html, $path); |
|
74 |
while(<$html>) { |
progress_bar::start; |
75 |
print $client $_; |
|
76 |
|
while( my $len = read $fh, $buff, $block ) { |
77 |
|
print $client $buff; |
78 |
|
$client->flush; |
79 |
|
$pos += $len; |
80 |
|
progress_bar::tick( $path, $pos, $size ); |
81 |
} |
} |
82 |
close($html); |
close($fh); |
83 |
|
close($client); |
84 |
|
|
85 |
return $path; |
print STDERR "\n"; |
|
} |
|
86 |
|
|
87 |
my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"; |
warn "exit static child"; |
88 |
|
|
89 |
|
exit(0); |
90 |
|
} |
91 |
|
|
92 |
use boolean; |
use boolean; |
93 |
|
|
94 |
use screen; |
use screen; |
95 |
use kvm; |
use kvm; |
|
our $pids; |
|
96 |
|
|
97 |
$SIG{CHLD} = 'IGNORE'; |
$SIG{CHLD} = 'IGNORE'; |
98 |
|
|
100 |
my $daemon = shift; |
my $daemon = shift; |
101 |
my $pid = $pids->{$daemon}; |
my $pid = $pids->{$daemon}; |
102 |
|
|
103 |
warn "start_stop $daemon pids: ",dump( $pids ); |
warn "start_stop $daemon $pid\n"; |
104 |
|
|
105 |
if ( $pid ) { |
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 |
delete $pids->{$daemon}; |
warn "pstree $pstree pids ",dump( @pids ); |
109 |
|
kill 1, $_ foreach reverse @pids; |
110 |
|
$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'; |
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 $@; |
130 |
} |
} |
131 |
} |
} |
132 |
|
|
133 |
|
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|; |
134 |
|
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
135 |
|
|
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 ); |
server->refresh; |
140 |
|
|
141 |
|
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
142 |
|
|
143 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
144 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
148 |
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
149 |
$pids->{screen} ? qq|start| : qq|start screen first|; |
$pids->{screen} ? qq|start| : qq|start screen first|; |
150 |
|
|
151 |
print $client $ok, |
my @rows = ( |
|
html::table( 2, |
|
|
'pid', html::tt( $$ ), |
|
152 |
'ip', html::tt( $server::ip ), |
'ip', html::tt( $server::ip ), |
153 |
'netmask', html::tt( $server::netmask ), |
'netmask', html::tt( $server::netmask ), |
154 |
|
|
155 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
|
'screen', qq|<a href=/screen>$screen</a>|, |
|
|
'kvm', qq|<a href=/kvm>$kvm</a>|, |
|
156 |
); |
); |
157 |
|
|
158 |
|
my $debug_proc = ''; |
159 |
|
|
160 |
|
warn 'pids: ', dump( $pids ) if $debug; |
161 |
|
foreach my $name ( sort keys %$pids ) { |
162 |
|
my $pid = $pids->{$name} || next; |
163 |
|
|
164 |
|
my $html = qq|<a href=/start_stop/$name>$pid</a>|; |
165 |
|
|
166 |
|
my $proc = "/proc/$pid/status"; |
167 |
|
|
168 |
|
if ( -e $proc ) { |
169 |
|
if ( $debug ) { |
170 |
|
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
171 |
|
|
172 |
|
$debug_proc |
173 |
|
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
174 |
|
. read_file($proc) |
175 |
|
. qq|</pre>| |
176 |
|
; |
177 |
|
} |
178 |
|
|
179 |
|
if ( $name->can('start_fork') ) { |
180 |
|
$html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork; |
181 |
|
} |
182 |
|
|
183 |
|
if ( $name->can('actions') ) { |
184 |
|
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
|
push @rows, ( $name => $html ); |
189 |
|
} |
190 |
|
|
191 |
|
my $below_table = ''; |
192 |
|
|
193 |
|
warn 'static_pids: ', dump( $static_pids ) if $debug; |
194 |
|
foreach my $pid ( keys %$static_pids ) { |
195 |
|
my $path = $static_pids->{$pid}; |
196 |
|
if ( -d "/proc/$pid" ) { |
197 |
|
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
198 |
|
} elsif ( $param->{clean_completed_downloads} ) { |
199 |
|
delete $static_pids->{$pid} |
200 |
|
} else { |
201 |
|
push @rows, ( $path => "$pid competed" ); |
202 |
|
$below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|; |
203 |
|
} |
204 |
|
} |
205 |
|
|
206 |
|
print $client $ok |
207 |
|
, html::table( 2, @rows ) |
208 |
|
, $below_table |
209 |
|
, html::tabs( log::mac_changes ) |
210 |
|
, $debug_proc |
211 |
|
; |
212 |
|
|
213 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
214 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
215 |
warn $@ if $@; |
warn $@ if $@; |
216 |
print $client qq|HTTP/1.1 302 Found\r\nLocation: $url\r\nContent-type: text/html\r\n\r\n<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>|; |
217 |
} elsif ( $path =~ m{^/(screen|kvm)} ) { |
server::debug( $debug ) if $1 eq 'debug'; |
218 |
print $client $ok, start_stop($1); |
} elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes |
219 |
|
print $client $redirect, start_stop($1); |
220 |
|
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
221 |
|
$1->$2(); |
222 |
|
print $client $redirect; |
223 |
|
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
224 |
|
print $client $redirect; |
225 |
|
kill 1, $1 || kill 9, $2 && warn "killed $1"; |
226 |
|
} elsif ( $path eq '/exit' ) { |
227 |
|
# DESTROY; |
228 |
|
exit 0; |
229 |
} elsif ( $path =~ m{/boot} ) { |
} elsif ( $path =~ m{/boot} ) { |
230 |
print $client qq{$ok |
print $client qq{$ok |
231 |
#!gpxe |
#!gpxe |
241 |
|
|
242 |
} |
} |
243 |
|
|
244 |
|
use browser; |
245 |
|
|
246 |
sub start { |
sub start { |
247 |
|
|
248 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
254 |
|
|
255 |
print "url $url\n"; |
print "url $url\n"; |
256 |
|
|
257 |
system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &"; |
start_stop 'browser', $url; |
258 |
|
start_stop 'screen'; |
259 |
|
start_stop 'kvm'; |
260 |
|
|
261 |
while (my $client = $server->accept()) { |
while (1) { |
262 |
$client->autoflush(1); |
my $client = $server->accept() || next; # ALARM trickle us |
263 |
my $request = <$client>; |
my $request = <$client>; |
264 |
|
|
265 |
warn "request $request\n" if $debug; |
warn "request $request\n" if $debug; |
266 |
|
|
|
Module::Refresh->refresh; |
|
|
|
|
267 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
268 |
my $path = $1; |
my $path = $1; |
269 |
my $param; |
my $param; |
274 |
} |
} |
275 |
warn "param: ",dump( $param ) if $debug; |
warn "param: ",dump( $param ) if $debug; |
276 |
} |
} |
|
warn "path $path param: ",dump( $param ); |
|
277 |
get_request $client, $path, $param; |
get_request $client, $path, $param; |
278 |
} else { |
} else { |
279 |
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"; |
283 |
print $client qq{ |
print $client qq{ |
284 |
<div style="font-size: 80%; color: #888"> |
<div style="font-size: 80%; color: #888"> |
285 |
<a href="">reload</a> |
<a href="">reload</a> |
286 |
<a href="/">index</a> |
<a href=/>index</a> |
287 |
|
<a href=/exit>exit</a> |
288 |
</div> |
</div> |
289 |
} if $client->connected; |
} if $client->connected; |
290 |
|
|