--- lib/PXElator/httpd.pm 2009/08/06 15:36:13 158 +++ lib/PXElator/httpd.pm 2009/08/09 21:13:36 185 @@ -17,6 +17,7 @@ use File::Slurp; #use JSON; use IO::Socket::INET; +use Regexp::Common qw/net/; sub menu {qq{ @@ -28,17 +29,6 @@ }} -our $pids; -$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh - -sub DESTROY { - warn "pids ",dump( $pids ); - foreach ( values %$pids ) { - warn "kill $_"; - kill 1,$_ || kill 9, $_; - } -} - our $port = 7777; use server; @@ -49,6 +39,17 @@ our $static_pids; use progress_bar; use config; +use client; +use log; +use x11; +use amt; +use boolean; +use daemons; + +use kvm; +use browser; +use network; + sub static { my ($client,$path) = @_; @@ -100,53 +101,7 @@ exit(0); } -use boolean; - -use kvm; - -$SIG{CHLD} = 'IGNORE'; - -sub start_stop { - my $daemon = shift; - my $pid = $pids->{$daemon} || 'not started'; - - warn "start_stop $daemon $pid\n"; - - if ( $pid =~ m{^\d+$} ) { - my $pstree = `pstree -p $pid`; - my @pids = $pstree =~ m{\((\d+)\)}g; - warn "pstree $pstree pids ",dump( @pids ); - kill 1, $_ foreach reverse @pids; - $pids->{$daemon} = 'stopped'; - return qq|$daemon pid $pid stopped|; - } else { - if ( $pid = fork ) { - # parent - $pids->{$daemon} = $pid; - warn "forked $daemon $pid\n"; - return qq|$daemon pid $pid started|; - } elsif ( defined $pid ) { - # child - my $invoke = 'start'; - $invoke = $1 if $daemon =~ s{/(.+)}{}; - if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) { - my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}"; - warn "exec $exec"; - exec "xterm -T $daemon -n $daemon -e $exec"; - } else { - my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; - warn "eval $eval"; - eval $eval; - warn "can't start $daemon: $@" if $@; - } - exit; - } else { - die "fork error $!"; - } - } -} - -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(); my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; sub get_request { @@ -166,15 +121,15 @@ my $debug_proc = ''; - warn 'pids: ', dump( $pids ) if $debug; - foreach my $name ( sort keys %$pids ) { - my $pid = $pids->{$name} || next; + foreach my $name ( sort keys %$daemons::pids ) { + my $pid = $daemons::pids->{$name} || next; - my $html = qq|$pid|; + my $html; my $proc = "/proc/$pid/status"; if ( -e $proc ) { + $html .= qq|$pid|; if ( $debug ) { $html .= qq| ?|; @@ -192,6 +147,8 @@ if ( $name->can('actions') ) { $html .= qq| $_| foreach $name->actions; } + } else { + $html .= qq|restart $pid exited|; } push @rows, ( $name => $html ); @@ -213,7 +170,6 @@ } print $client $ok - , menu() , html::table( 2, @rows ) , $below_table , html::tabs( log::mac_changes ) @@ -222,49 +178,57 @@ } elsif ( $path =~ m{^/server} ) { print $client $ok - , menu() - , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) - ; - } elsif ( $path =~ m{^/client} ) { - my $ip = $client->peerhost; - my $hostname = server::shared( "hostname/$ip", $param->{hostname} ); - my $deploy = server::shared( "deploy/$ip", $param->{deploy} ); - print $client $ok - , menu() - , qq|
| - , html::table( 2, - 'ip' => $ip, - 'hostname' => qq||, - 'deploy' => html::select( 'deploy', $deploy, config::available ), - ) - , qq|
|
-			, config::for_ip( $ip )
-			, qq|
| + , html::table( 2, map { ( $_, html::tt eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir', 'conf' ) ) ; + } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { + my $ip = $1 || $client->peerhost; + if ( $ip ne $server::ip ) { + my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} ); + my $deploy = client::conf( $ip, 'deploy' => $param->{deploy} ); + print $client $ok + , qq|
| + , html::table( 2, + 'ip' => $ip, + 'mac' => client::mac( $ip ), + 'hostname' => qq||, + 'deploy' => html::select( 'deploy', $deploy, config::available ), + ) + , qq|
| + , qq|

PXElinux $deploy

|
+				, config::for_ip( $ip )
+				, qq|
| + ; + + if ( my $amt = client::conf( $ip, 'amt' ) ) { + print $client amt::info( $amt ); + } + } else { + print $client $ok + , qq|

Clients on $server::ip

| + , html::table( -4, + 'ip', 'mac', 'hostname', 'deploy', + map { + my $ip = $_; + $ip =~ s{^.+/ip/}{}; + ( qq|$ip|, client::mac($ip), client::conf( $ip, 'hostname' ), html::tt client::conf( $ip, 'deploy' ) ); + } + glob("$server::conf/ip/*") + ) + ; + } } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { eval 'our $' . $1 . ' = ' . $2; warn $@ if $@; print $client $redirect, qq|$1 = $2
Location: $url|; server::debug( $debug ) if $1 eq 'debug'; } elsif ( $path =~ m{^/start_stop/(\S+)} ) { - print $client $redirect, start_stop($1); + print $client $redirect, daemons::start_stop($1); } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { $1->$2(); print $client $redirect; } elsif ( $path =~ m{^/kill/static/(\d+)} ) { print $client $redirect; 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/ - - }; } else { print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n404 $path"; warn "404 $path"; @@ -272,15 +236,13 @@ } -use browser; -use network; - sub start { warn 'tap ', network::tap(); my $server = IO::Socket::INET->new( Proto => 'tcp', + LocalAddr => $server::ip, LocalPort => $httpd::port, Listen => SOMAXCONN, Reuse => 1 @@ -288,11 +250,9 @@ print "url $url\n"; - start_stop 'browser', $url; - start_stop 'dhcpd'; - start_stop 'tftpd'; - start_stop 'dnsd'; - start_stop 'kvm'; + daemons::start_stop 'browser', $url; + daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd/ ); + daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device while (1) { my $client = $server->accept() || next; # ALARM trickle us