11 perl -Ilib/PXElator -Mhttpd -e httpd::start
15 use Data::Dump qw/dump/;
23 <div style="font-size: 80%; color: #888">
25 <a href=/server>server</a>
26 <a href=/client>client</a>
32 $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
35 warn "pids ",dump( $pids );
36 foreach ( values %$pids ) {
38 kill 1,$_ || kill 9, $_;
45 our $debug = server::debug;
46 our $url = "http://$server::ip:$port";
54 my ($client,$path) = @_;
56 my $full = "$server::base_dir/tftp/$path";
60 if ( my $pid = fork ) {
63 print "http static child $pid\n";
64 $static_pids->{$pid} = $path;
68 my $type = 'application/octet-stream';
69 $type = 'text/html' if $path =~ m{\.htm};
70 $type = 'application/javascript' if $path =~ m{\.js};
71 $type = 'text/plain' if $path =~ m{\.txt};
73 my $size = -s $full || return;
75 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
79 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
83 warn "static $path $type $size block: $block\n";
87 while( my $len = read $fh, $buff, $block ) {
91 progress_bar::tick( $path, $pos, $size );
98 warn "exit static child";
108 $SIG{CHLD} = 'IGNORE';
112 my $pid = $pids->{$daemon} || 'not started';
114 warn "start_stop $daemon $pid\n";
116 if ( $pid =~ m{^\d+$} ) {
117 my $pstree = `pstree -p $pid`;
118 my @pids = $pstree =~ m{\((\d+)\)}g;
119 warn "pstree $pstree pids ",dump( @pids );
120 kill 1, $_ foreach reverse @pids;
121 $pids->{$daemon} = 'stopped';
122 return qq|$daemon pid $pid stopped|;
126 $pids->{$daemon} = $pid;
127 warn "forked $daemon $pid\n";
128 return qq|$daemon pid $pid started|;
129 } elsif ( defined $pid ) {
131 my $invoke = 'start';
132 $invoke = $1 if $daemon =~ s{/(.+)}{};
133 my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
136 warn "can't start $daemon: $@" if $@;
144 my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
145 my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
148 my ( $client, $path, $param ) = @_;
152 warn "get_request $path ", $param ? dump( $param ) : '', "\n";
154 if ( my $found = static( $client,$path ) ) {
155 warn "static $found" if $debug;
156 } elsif ( $path eq '/' ) {
158 my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start';
159 my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| :
160 $pids->{screen} ? qq|start| : qq|start screen first|;
163 'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
168 warn 'pids: ', dump( $pids ) if $debug;
169 foreach my $name ( sort keys %$pids ) {
170 my $pid = $pids->{$name} || next;
172 my $html = qq|<a href=/start_stop/$name>$pid</a>|;
174 my $proc = "/proc/$pid/status";
178 $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
181 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
187 if ( $name->can('start_fork') ) {
188 $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
191 if ( $name->can('actions') ) {
192 $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
196 push @rows, ( $name => $html );
199 my $below_table = '';
201 warn 'static_pids: ', dump( $static_pids ) if $debug;
202 foreach my $pid ( keys %$static_pids ) {
203 my $path = $static_pids->{$pid};
204 if ( -d "/proc/$pid" ) {
205 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
206 } elsif ( $param->{clean_completed_downloads} ) {
207 delete $static_pids->{$pid}
209 push @rows, ( $path => "$pid competed" );
210 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
216 , html::table( 2, @rows )
218 , html::tabs( log::mac_changes )
222 } elsif ( $path =~ m{^/server} ) {
225 , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )
227 } elsif ( $path =~ m{^/client} ) {
228 my $ip = $client->peerhost;
229 my $hostname = server::shared( "hostname/$ip", $param->{hostname} );
230 my $deploy = server::shared( "deploy/$ip", $param->{deploy} );
233 , qq|<form method=get>|
236 'hostname' => qq|<input type=text name=hostname value=$hostname>|,
237 'deploy' => html::select( 'deploy', $deploy, config::available ),
239 , qq|<input type=submit value=change></form><pre>|
240 , config::for_ip( $ip )
243 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
244 eval 'our $' . $1 . ' = ' . $2;
246 print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
247 server::debug( $debug ) if $1 eq 'debug';
248 } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
249 print $client $redirect, start_stop($1);
250 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
252 print $client $redirect;
253 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
254 print $client $redirect;
255 kill 1, $1 || kill 9, $2 && warn "killed $1";
256 } elsif ( $path eq '/exit' ) {
259 } elsif ( $path =~ m{/boot} ) {
264 chain http://$server::ip:$httpd::port/
268 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
279 warn 'tap ', network::tap();
281 my $server = IO::Socket::INET->new(
283 LocalPort => $httpd::port,
286 ) || die "can't start server on $url: $!";
290 start_stop 'browser', $url;
295 my $client = $server->accept() || next; # ALARM trickle us
296 my $request = <$client>;
298 warn "request $request\n" if $debug;
300 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
303 if ( $path =~ s{\?(.+)}{} ) {
304 foreach my $p ( split(/[&;]/, $1) ) {
305 my ($n,$v) = split(/=/, $p, 2);
308 warn "param: ",dump( $param ) if $debug;
310 get_request $client, $path, $param;
312 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
316 print $client menu() if $client->connected;