11 perl -Ilib/PXElator -Mhttpd -e httpd::start
15 use Data::Dump qw/dump/;
20 use Regexp::Common qw/net/;
24 <div style="font-size: 80%; color: #888">
26 <a href=/server>server</a>
27 <a href=/client>client</a>
33 $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
36 warn "pids ",dump( $pids );
37 foreach ( values %$pids ) {
39 kill 1,$_ || kill 9, $_;
46 our $debug = server::debug;
47 our $url = "http://$server::ip:$port";
57 my ($client,$path) = @_;
59 my $full = "$server::base_dir/tftp/$path";
63 if ( my $pid = fork ) {
66 print "http static child $pid\n";
67 $static_pids->{$pid} = $path;
71 my $type = 'application/octet-stream';
72 $type = 'text/html' if $path =~ m{\.htm};
73 $type = 'application/javascript' if $path =~ m{\.js};
74 $type = 'text/plain' if $path =~ m{\.txt};
76 my $size = -s $full || return;
78 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
82 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
86 warn "static $path $type $size block: $block\n";
90 while( my $len = read $fh, $buff, $block ) {
94 progress_bar::tick( $path, $pos, $size );
101 warn "exit static child";
109 use qemu; $pids->{qemu} = 'stopped';
111 $SIG{CHLD} = 'IGNORE';
115 my $pid = $pids->{$daemon} || 'not started';
117 warn "start_stop $daemon $pid\n";
119 if ( $pid =~ m{^\d+$} ) {
120 my $pstree = `pstree -p $pid`;
121 my @pids = $pstree =~ m{\((\d+)\)}g;
122 warn "pstree $pstree pids ",dump( @pids );
123 kill 1, $_ foreach reverse @pids;
124 $pids->{$daemon} = 'stopped';
125 return qq|$daemon pid $pid stopped|;
129 $pids->{$daemon} = $pid;
130 warn "forked $daemon $pid\n";
131 return qq|$daemon pid $pid started|;
132 } elsif ( defined $pid ) {
134 my $invoke = 'start';
135 $invoke = $1 if $daemon =~ s{/(.+)}{};
136 if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) {
137 my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}";
139 exec "xterm -T $daemon -n $daemon -e $exec";
141 my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
144 warn "can't start $daemon: $@" if $@;
153 my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu();
154 my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
157 my ( $client, $path, $param ) = @_;
161 warn "get_request $path ", $param ? dump( $param ) : '', "\n";
163 if ( my $found = static( $client,$path ) ) {
164 warn "static $found" if $debug;
165 } elsif ( $path eq '/' ) {
168 'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
173 warn 'pids: ', dump( $pids ) if $debug;
174 foreach my $name ( sort keys %$pids ) {
175 my $pid = $pids->{$name} || next;
177 my $html = qq|<a href=/start_stop/$name>$pid</a>|;
179 my $proc = "/proc/$pid/status";
183 $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
186 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
192 if ( $name->can('start_fork') ) {
193 $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
196 if ( $name->can('actions') ) {
197 $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
201 push @rows, ( $name => $html );
204 my $below_table = '';
206 warn 'static_pids: ', dump( $static_pids ) if $debug;
207 foreach my $pid ( keys %$static_pids ) {
208 my $path = $static_pids->{$pid};
209 if ( -d "/proc/$pid" ) {
210 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
211 } elsif ( $param->{clean_completed_downloads} ) {
212 delete $static_pids->{$pid}
214 push @rows, ( $path => "$pid competed" );
215 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
220 , html::table( 2, @rows )
222 , html::tabs( log::mac_changes )
226 } elsif ( $path =~ m{^/server} ) {
228 , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )
230 } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
231 my $ip = $1 || $client->peerhost;
232 if ( $ip ne $server::ip ) {
233 my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} );
234 my $deploy = client::conf( $ip, 'deploy' => $param->{deploy} );
236 , qq|<form method=get>|
239 'mac' => client::mac( $ip ),
240 'hostname' => qq|<input type=text name=hostname value=$hostname>|,
241 'deploy' => html::select( 'deploy', $deploy, config::available ),
243 , qq|<input type=submit value=change></form>|
244 , qq|<h2>PXElinux $deploy</h2><pre>|
245 , config::for_ip( $ip )
250 , qq|<h2>Clients on $server::ip</h2>|
256 qq|<li><a href=/client/$ip>$ip</a> |, client::mac($ip), ' ', client::conf( $ip, 'hostname' ), qq|</li>|;
258 glob("$server::conf/ip/*")
263 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
264 eval 'our $' . $1 . ' = ' . $2;
266 print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
267 server::debug( $debug ) if $1 eq 'debug';
268 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
269 print $client $redirect, start_stop($1);
270 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
272 print $client $redirect;
273 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
274 print $client $redirect;
275 kill 1, $1 || kill 9, $2 && warn "killed $1";
277 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
288 warn 'tap ', network::tap();
290 my $server = IO::Socket::INET->new(
292 LocalPort => $httpd::port,
295 ) || die "can't start server on $url: $!";
299 start_stop 'browser', $url;
306 my $client = $server->accept() || next; # ALARM trickle us
307 my $request = <$client>;
309 warn "request $request\n" if $debug;
311 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
314 if ( $path =~ s{\?(.+)}{} ) {
315 foreach my $p ( split(/[&;]/, $1) ) {
316 my ($n,$v) = split(/=/, $p, 2);
319 warn "param: ",dump( $param ) if $debug;
321 get_request $client, $path, $param;
323 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
327 print $client menu() if $client->connected;