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";
59 my ($client,$path) = @_;
61 my $full = "$server::base_dir/tftp/$path";
65 if ( my $pid = fork ) {
68 print "http static child $pid\n";
69 $static_pids->{$pid} = $path;
73 my $type = 'application/octet-stream';
74 $type = 'text/html' if $path =~ m{\.htm};
75 $type = 'application/javascript' if $path =~ m{\.js};
76 $type = 'text/plain' if $path =~ m{\.txt};
78 my $size = -s $full || return;
80 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
84 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
88 warn "static $path $type $size block: $block\n";
92 while( my $len = read $fh, $buff, $block ) {
96 progress_bar::tick( $path, $pos, $size );
103 warn "exit static child";
112 $SIG{CHLD} = 'IGNORE';
117 my $pid = $pids->{$daemon};
118 my $pid_path = "$server::conf/$daemon.pid";
120 if ( ! $pid && -e $pid_path ) {
121 my $p = read_file $pid_path;
123 warn "adopted $daemon $p\n";
124 return $pids->{$daemon} = $p;
126 warn "old $daemon $p not running";
131 $pid ||= 'not started';
132 warn "start_stop $daemon $pid\n";
134 if ( $pid =~ m{^\d+$} ) {
135 my $pstree = `pstree -p $pid`;
136 my @pids = $pstree =~ m{\((\d+)\)}g;
137 warn "pstree $pstree pids ",dump( @pids );
138 kill 1, $_ foreach reverse @pids;
139 $pids->{$daemon} = 'stopped';
140 return qq|$daemon pid $pid stopped|;
144 $pids->{$daemon} = $pid;
145 write_file $pid_path, $pid;
146 warn "forked $daemon $pid\n";
147 return qq|$daemon pid $pid started|;
148 } elsif ( defined $pid ) {
150 my $invoke = 'start';
151 $invoke = $1 if $daemon =~ s{/(.+)}{};
152 if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) {
153 my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}";
155 x11::xterm( $daemon => $exec );
157 my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
160 warn "can't start $daemon: $@" if $@;
169 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|;
173 my ( $client, $path, $param ) = @_;
177 warn "get_request $path ", $param ? dump( $param ) : '', "\n";
179 if ( my $found = static( $client,$path ) ) {
180 warn "static $found" if $debug;
181 } elsif ( $path eq '/' ) {
184 'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
189 warn 'pids: ', dump( $pids ) if $debug;
190 foreach my $name ( sort keys %$pids ) {
191 my $pid = $pids->{$name} || next;
195 my $proc = "/proc/$pid/status";
198 $html .= qq|<a href=/start_stop/$name>$pid</a>|;
200 $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
203 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
209 if ( $name->can('start_fork') ) {
210 $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
213 if ( $name->can('actions') ) {
214 $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
217 $html .= qq|<a href=/start_stop/$name>restart</a> $pid exited|;
220 push @rows, ( $name => $html );
223 my $below_table = '';
225 warn 'static_pids: ', dump( $static_pids ) if $debug;
226 foreach my $pid ( keys %$static_pids ) {
227 my $path = $static_pids->{$pid};
228 if ( -d "/proc/$pid" ) {
229 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
230 } elsif ( $param->{clean_completed_downloads} ) {
231 delete $static_pids->{$pid}
233 push @rows, ( $path => "$pid competed" );
234 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
239 , html::table( 2, @rows )
241 , html::tabs( log::mac_changes )
245 } elsif ( $path =~ m{^/server} ) {
247 , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )
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} );
255 , qq|<form method=get>|
258 'mac' => client::mac( $ip ),
259 'hostname' => qq|<input type=text name=hostname value=$hostname>|,
260 'deploy' => html::select( 'deploy', $deploy, config::available ),
262 , qq|<input type=submit value=change></form>|
263 , qq|<h2>PXElinux $deploy</h2><pre>|
264 , config::for_ip( $ip )
268 if ( my $amt = client::conf( $ip, 'amt' ) ) {
269 print $client amt::info( $amt );
273 , qq|<h2>Clients on $server::ip</h2>|
275 'ip', 'mac', 'hostname', 'deploy',
279 ( qq|<a href=/client/$ip>$ip</a>|, client::mac($ip), client::conf( $ip, 'hostname' ), client::conf( $ip, 'deploy' ) );
281 glob("$server::conf/ip/*")
285 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
286 eval 'our $' . $1 . ' = ' . $2;
288 print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
289 server::debug( $debug ) if $1 eq 'debug';
290 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
291 print $client $redirect, start_stop($1);
292 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
294 print $client $redirect;
295 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
296 print $client $redirect;
297 kill 1, $1 || kill 9, $2 && warn "killed $1";
299 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
310 warn 'tap ', network::tap();
312 my $server = IO::Socket::INET->new(
314 LocalPort => $httpd::port,
317 ) || die "can't start server on $url: $!";
321 start_stop 'browser', $url;
325 start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
328 my $client = $server->accept() || next; # ALARM trickle us
329 my $request = <$client>;
331 warn "request $request\n" if $debug;
333 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
336 if ( $path =~ s{\?(.+)}{} ) {
337 foreach my $p ( split(/[&;]/, $1) ) {
338 my ($n,$v) = split(/=/, $p, 2);
341 warn "param: ",dump( $param ) if $debug;
343 get_request $client, $path, $param;
345 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
349 print $client menu() if $client->connected;