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";
107 $SIG{CHLD} = 'IGNORE';
111 my $pid = $pids->{$daemon} || 'not started';
113 warn "start_stop $daemon $pid\n";
115 if ( $pid =~ m{^\d+$} ) {
116 my $pstree = `pstree -p $pid`;
117 my @pids = $pstree =~ m{\((\d+)\)}g;
118 warn "pstree $pstree pids ",dump( @pids );
119 kill 1, $_ foreach reverse @pids;
120 $pids->{$daemon} = 'stopped';
121 return qq|$daemon pid $pid stopped|;
125 $pids->{$daemon} = $pid;
126 warn "forked $daemon $pid\n";
127 return qq|$daemon pid $pid started|;
128 } elsif ( defined $pid ) {
130 my $invoke = 'start';
131 $invoke = $1 if $daemon =~ s{/(.+)}{};
132 if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) {
133 my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}";
135 exec "xterm -T $daemon -n $daemon -e $exec";
137 my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
140 warn "can't start $daemon: $@" if $@;
149 my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
150 my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
153 my ( $client, $path, $param ) = @_;
157 warn "get_request $path ", $param ? dump( $param ) : '', "\n";
159 if ( my $found = static( $client,$path ) ) {
160 warn "static $found" if $debug;
161 } elsif ( $path eq '/' ) {
164 'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
169 warn 'pids: ', dump( $pids ) if $debug;
170 foreach my $name ( sort keys %$pids ) {
171 my $pid = $pids->{$name} || next;
173 my $html = qq|<a href=/start_stop/$name>$pid</a>|;
175 my $proc = "/proc/$pid/status";
179 $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
182 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
188 if ( $name->can('start_fork') ) {
189 $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
192 if ( $name->can('actions') ) {
193 $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
197 push @rows, ( $name => $html );
200 my $below_table = '';
202 warn 'static_pids: ', dump( $static_pids ) if $debug;
203 foreach my $pid ( keys %$static_pids ) {
204 my $path = $static_pids->{$pid};
205 if ( -d "/proc/$pid" ) {
206 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
207 } elsif ( $param->{clean_completed_downloads} ) {
208 delete $static_pids->{$pid}
210 push @rows, ( $path => "$pid competed" );
211 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
217 , html::table( 2, @rows )
219 , html::tabs( log::mac_changes )
223 } elsif ( $path =~ m{^/server} ) {
226 , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )
228 } elsif ( $path =~ m{^/client} ) {
229 my $ip = $client->peerhost;
230 my $hostname = server::shared( "hostname/$ip", $param->{hostname} );
231 my $deploy = server::shared( "deploy/$ip", $param->{deploy} );
234 , qq|<form method=get>|
237 'hostname' => qq|<input type=text name=hostname value=$hostname>|,
238 'deploy' => html::select( 'deploy', $deploy, config::available ),
240 , qq|<input type=submit value=change></form><pre>|
241 , config::for_ip( $ip )
244 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
245 eval 'our $' . $1 . ' = ' . $2;
247 print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
248 server::debug( $debug ) if $1 eq 'debug';
249 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
250 print $client $redirect, start_stop($1);
251 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
253 print $client $redirect;
254 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
255 print $client $redirect;
256 kill 1, $1 || kill 9, $2 && warn "killed $1";
257 } elsif ( $path eq '/exit' ) {
260 } elsif ( $path =~ m{/boot} ) {
265 chain http://$server::ip:$httpd::port/
269 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
280 warn 'tap ', network::tap();
282 my $server = IO::Socket::INET->new(
284 LocalPort => $httpd::port,
287 ) || die "can't start server on $url: $!";
291 start_stop 'browser', $url;
298 my $client = $server->accept() || next; # ALARM trickle us
299 my $request = <$client>;
301 warn "request $request\n" if $debug;
303 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
306 if ( $path =~ s{\?(.+)}{} ) {
307 foreach my $p ( split(/[&;]/, $1) ) {
308 my ($n,$v) = split(/=/, $p, 2);
311 warn "param: ",dump( $param ) if $debug;
313 get_request $client, $path, $param;
315 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
319 print $client menu() if $client->connected;