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; |
use Regexp::Common qw/net/; |
21 |
|
|
22 |
|
sub menu {qq{ |
23 |
|
|
24 |
|
<div style="font-size: 80%; color: #888"> |
25 |
|
<a href=/>home</a> |
26 |
|
<a href=/server>server</a> |
27 |
|
<a href=/client>client</a> |
28 |
|
</div> |
29 |
|
|
30 |
|
}} |
31 |
|
|
32 |
our $pids; |
our $pids; |
33 |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
48 |
|
|
49 |
use html; |
use html; |
50 |
our $static_pids; |
our $static_pids; |
51 |
|
use progress_bar; |
52 |
|
use config; |
53 |
|
use client; |
54 |
|
use log; |
55 |
|
use x11; |
56 |
|
use amt; |
57 |
|
|
58 |
sub static { |
sub static { |
59 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
80 |
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"; |
81 |
|
|
82 |
open(my $fh, $full); |
open(my $fh, $full); |
|
print "static $path $type $size\n"; |
|
83 |
|
|
84 |
my $block = 8192; |
my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it) |
85 |
my $buff; |
my $buff; |
86 |
my $pos = 0; |
my $pos = 0; |
87 |
|
|
88 |
|
warn "static $path $type $size block: $block\n"; |
89 |
|
|
90 |
|
progress_bar::start; |
91 |
|
|
92 |
while( my $len = read $fh, $buff, $block ) { |
while( my $len = read $fh, $buff, $block ) { |
93 |
print $client $buff; |
print $client $buff; |
94 |
|
$client->flush; |
95 |
$pos += $len; |
$pos += $len; |
96 |
printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size; |
progress_bar::tick( $path, $pos, $size ); |
97 |
} |
} |
98 |
close($fh); |
close($fh); |
99 |
close($client); |
close($client); |
100 |
|
|
101 |
print "$path $pos == $size OK\n"; |
print STDERR "\n"; |
102 |
|
|
103 |
exit; |
warn "exit static child"; |
104 |
|
|
105 |
|
exit(0); |
106 |
} |
} |
107 |
|
|
108 |
use boolean; |
use boolean; |
109 |
|
|
|
use screen; |
|
110 |
use kvm; |
use kvm; |
111 |
|
|
112 |
$SIG{CHLD} = 'IGNORE'; |
$SIG{CHLD} = 'IGNORE'; |
113 |
|
|
114 |
sub start_stop { |
sub start_stop { |
115 |
my $daemon = shift; |
my $daemon = shift; |
116 |
|
|
117 |
my $pid = $pids->{$daemon}; |
my $pid = $pids->{$daemon}; |
118 |
|
my $pid_path = "$server::conf/$daemon.pid"; |
119 |
|
|
120 |
warn "start_stop $daemon $pid pids: ",dump( $pids ); |
if ( ! $pid && -e $pid_path ) { |
121 |
|
my $p = read_file $pid_path; |
122 |
|
if ( kill 0, $p ) { |
123 |
|
warn "adopted $daemon $p\n"; |
124 |
|
return $pids->{$daemon} = $p; |
125 |
|
} else { |
126 |
|
warn "old $daemon $p not running"; |
127 |
|
unlink $pid_path; |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
|
$pid ||= 'not started'; |
132 |
|
warn "start_stop $daemon $pid\n"; |
133 |
|
|
134 |
if ( $pid =~ m{^\d+$} ) { |
if ( $pid =~ m{^\d+$} ) { |
135 |
my $pstree = `pstree -p $pid`; |
my $pstree = `pstree -p $pid`; |
142 |
if ( $pid = fork ) { |
if ( $pid = fork ) { |
143 |
# parent |
# parent |
144 |
$pids->{$daemon} = $pid; |
$pids->{$daemon} = $pid; |
145 |
warn "forked $daemon $pid"; |
write_file $pid_path, $pid; |
146 |
|
warn "forked $daemon $pid\n"; |
147 |
return qq|$daemon pid $pid started|; |
return qq|$daemon pid $pid started|; |
148 |
} elsif ( defined $pid ) { |
} elsif ( defined $pid ) { |
149 |
# child |
# child |
150 |
my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')'; |
my $invoke = 'start'; |
151 |
warn "eval $eval"; |
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
152 |
eval $eval; |
if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) { |
153 |
warn "can't start $daemon: $@" if $@; |
my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}"; |
154 |
|
warn "exec $exec"; |
155 |
|
x11::xterm( $daemon => $exec ); |
156 |
|
} else { |
157 |
|
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
158 |
|
warn "eval $eval"; |
159 |
|
eval $eval; |
160 |
|
warn "can't start $daemon: $@" if $@; |
161 |
|
} |
162 |
exit; |
exit; |
163 |
} else { |
} else { |
164 |
die "fork error $!"; |
die "fork error $!"; |
166 |
} |
} |
167 |
} |
} |
168 |
|
|
169 |
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|; |
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu(); |
170 |
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
171 |
|
|
172 |
sub get_request { |
sub get_request { |
173 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
174 |
|
|
175 |
warn "get_request $client $path ",dump( $param ); |
server->refresh; |
176 |
|
|
177 |
|
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
178 |
|
|
179 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
180 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
181 |
} elsif ( $path eq '/' ) { |
} elsif ( $path eq '/' ) { |
182 |
|
|
|
my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start'; |
|
|
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
|
|
$pids->{screen} ? qq|start| : qq|start screen first|; |
|
|
|
|
183 |
my @rows = ( |
my @rows = ( |
|
'ip', html::tt( $server::ip ), |
|
|
'netmask', html::tt( $server::netmask ), |
|
|
|
|
184 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
185 |
); |
); |
186 |
|
|
187 |
my $debug_proc; |
my $debug_proc = ''; |
188 |
|
|
189 |
warn 'pids: ', dump( $pids ); |
warn 'pids: ', dump( $pids ) if $debug; |
190 |
foreach my $name ( keys %$pids ) { |
foreach my $name ( sort keys %$pids ) { |
191 |
my $pid = $pids->{$name} || next; |
my $pid = $pids->{$name} || next; |
192 |
|
|
193 |
my $html = qq|<a href=/$name>$pid</a>|; |
my $html; |
194 |
|
|
195 |
if ( $debug ) { |
my $proc = "/proc/$pid/status"; |
|
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
|
196 |
|
|
197 |
my $proc = "/proc/$pid/status"; |
if ( -e $proc ) { |
198 |
$debug_proc |
$html .= qq|<a href=/start_stop/$name>$pid</a>|; |
199 |
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
if ( $debug ) { |
200 |
. read_file($proc) |
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
201 |
. qq|</pre>| |
|
202 |
if -e $proc; |
$debug_proc |
203 |
|
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
204 |
|
. read_file($proc) |
205 |
|
. qq|</pre>| |
206 |
|
; |
207 |
|
} |
208 |
|
|
209 |
|
if ( $name->can('start_fork') ) { |
210 |
|
$html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork; |
211 |
|
} |
212 |
|
|
213 |
|
if ( $name->can('actions') ) { |
214 |
|
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
215 |
|
} |
216 |
|
} else { |
217 |
|
$html .= qq|<a href=/start_stop/$name>restart</a> $pid exited|; |
218 |
} |
} |
219 |
|
|
220 |
push @rows, ( $name => $html ); |
push @rows, ( $name => $html ); |
221 |
} |
} |
222 |
|
|
223 |
warn 'static_pids: ', dump( $static_pids ); |
my $below_table = ''; |
224 |
|
|
225 |
|
warn 'static_pids: ', dump( $static_pids ) if $debug; |
226 |
foreach my $pid ( keys %$static_pids ) { |
foreach my $pid ( keys %$static_pids ) { |
227 |
my $path = $static_pids->{$pid}; |
my $path = $static_pids->{$pid}; |
228 |
if ( -d "/proc/$pid" ) { |
if ( -d "/proc/$pid" ) { |
229 |
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
230 |
|
} elsif ( $param->{clean_completed_downloads} ) { |
231 |
|
delete $static_pids->{$pid} |
232 |
} else { |
} else { |
233 |
push @rows, ( $path => "$pid competed" ); |
push @rows, ( $path => "$pid competed" ); |
234 |
|
$below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|; |
235 |
} |
} |
236 |
} |
} |
237 |
|
|
238 |
print $client $ok |
print $client $ok |
239 |
, html::table( 2, @rows ) |
, html::table( 2, @rows ) |
240 |
|
, $below_table |
241 |
, html::tabs( log::mac_changes ) |
, html::tabs( log::mac_changes ) |
242 |
, $debug_proc |
, $debug_proc |
243 |
; |
; |
244 |
|
|
245 |
|
} elsif ( $path =~ m{^/server} ) { |
246 |
|
print $client $ok |
247 |
|
, html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) |
248 |
|
; |
249 |
|
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { |
250 |
|
my $ip = $1 || $client->peerhost; |
251 |
|
if ( $ip ne $server::ip ) { |
252 |
|
my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} ); |
253 |
|
my $deploy = client::conf( $ip, 'deploy' => $param->{deploy} ); |
254 |
|
print $client $ok |
255 |
|
, qq|<form method=get>| |
256 |
|
, html::table( 2, |
257 |
|
'ip' => $ip, |
258 |
|
'mac' => client::mac( $ip ), |
259 |
|
'hostname' => qq|<input type=text name=hostname value=$hostname>|, |
260 |
|
'deploy' => html::select( 'deploy', $deploy, config::available ), |
261 |
|
) |
262 |
|
, qq|<input type=submit value=change></form>| |
263 |
|
, qq|<h2>PXElinux $deploy</h2><pre>| |
264 |
|
, config::for_ip( $ip ) |
265 |
|
, qq|</pre>| |
266 |
|
; |
267 |
|
|
268 |
|
if ( my $amt = client::conf( $ip, 'amt' ) ) { |
269 |
|
print $client amt::info( $amt ); |
270 |
|
} |
271 |
|
} else { |
272 |
|
print $client $ok |
273 |
|
, qq|<h2>Clients on $server::ip</h2>| |
274 |
|
, html::table( -4, |
275 |
|
'ip', 'mac', 'hostname', 'deploy', |
276 |
|
map { |
277 |
|
my $ip = $_; |
278 |
|
$ip =~ s{^.+/ip/}{}; |
279 |
|
( qq|<a href=/client/$ip>$ip</a>|, client::mac($ip), client::conf( $ip, 'hostname' ), client::conf( $ip, 'deploy' ) ); |
280 |
|
} |
281 |
|
glob("$server::conf/ip/*") |
282 |
|
) |
283 |
|
; |
284 |
|
} |
285 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
286 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
287 |
warn $@ if $@; |
warn $@ if $@; |
288 |
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>|; |
289 |
server::debug( $debug ) if $1 eq 'debug'; |
server::debug( $debug ) if $1 eq 'debug'; |
290 |
} elsif ( $path =~ m{^/(screen|kvm)} ) { |
} elsif ( $path =~ m{^/start_stop/(\S+)} ) { |
291 |
print $client $redirect, start_stop($1); |
print $client $redirect, start_stop($1); |
292 |
|
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
293 |
|
$1->$2(); |
294 |
|
print $client $redirect; |
295 |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
296 |
print $client $redirect; |
print $client $redirect; |
297 |
kill 9, $1 && warn "killed $1"; |
kill 1, $1 || kill 9, $2 && warn "killed $1"; |
|
} elsif ( $path eq '/exit' ) { |
|
|
# DESTROY; |
|
|
exit 0; |
|
|
} elsif ( $path =~ m{/boot} ) { |
|
|
print $client qq{$ok |
|
|
#!gpxe |
|
|
imgfree |
|
|
login |
|
|
chain http://$server::ip:$httpd::port/ |
|
|
|
|
|
}; |
|
298 |
} else { |
} else { |
299 |
print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>"; |
print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>"; |
300 |
warn "404 $path"; |
warn "404 $path"; |
303 |
} |
} |
304 |
|
|
305 |
use browser; |
use browser; |
306 |
|
use network; |
307 |
|
|
308 |
sub start { |
sub start { |
309 |
|
|
310 |
|
warn 'tap ', network::tap(); |
311 |
|
|
312 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
313 |
Proto => 'tcp', |
Proto => 'tcp', |
314 |
LocalPort => $httpd::port, |
LocalPort => $httpd::port, |
319 |
print "url $url\n"; |
print "url $url\n"; |
320 |
|
|
321 |
start_stop 'browser', $url; |
start_stop 'browser', $url; |
322 |
start_stop 'screen'; |
start_stop 'dhcpd'; |
323 |
start_stop 'kvm'; |
start_stop 'tftpd'; |
324 |
|
start_stop 'dnsd'; |
325 |
|
start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device |
326 |
|
|
327 |
while (my $client = $server->accept()) { |
while (1) { |
328 |
$client->autoflush(1); |
my $client = $server->accept() || next; # ALARM trickle us |
329 |
my $request = <$client>; |
my $request = <$client>; |
330 |
|
|
331 |
warn "request $request\n" if $debug; |
warn "request $request\n" if $debug; |
332 |
|
|
|
Module::Refresh->refresh; |
|
|
|
|
333 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
334 |
my $path = $1; |
my $path = $1; |
335 |
my $param; |
my $param; |
340 |
} |
} |
341 |
warn "param: ",dump( $param ) if $debug; |
warn "param: ",dump( $param ) if $debug; |
342 |
} |
} |
|
warn "path $path param: ",dump( $param ); |
|
343 |
get_request $client, $path, $param; |
get_request $client, $path, $param; |
344 |
} else { |
} else { |
345 |
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"; |
346 |
warn "500 $request"; |
warn "500 $request"; |
347 |
} |
} |
348 |
|
|
349 |
print $client qq{ |
print $client menu() if $client->connected; |
|
<div style="font-size: 80%; color: #888"> |
|
|
<a href="">reload</a> |
|
|
<a href=/>index</a> |
|
|
<a href=/exit>exit</a> |
|
|
</div> |
|
|
} if $client->connected; |
|
350 |
|
|
351 |
} |
} |
352 |
|
|