29 |
|
|
30 |
}} |
}} |
31 |
|
|
|
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, $_; |
|
|
} |
|
|
} |
|
|
|
|
32 |
our $port = 7777; |
our $port = 7777; |
33 |
|
|
34 |
use server; |
use server; |
41 |
use config; |
use config; |
42 |
use client; |
use client; |
43 |
use log; |
use log; |
44 |
|
use x11; |
45 |
|
use amt; |
46 |
|
use boolean; |
47 |
|
use daemons; |
48 |
|
|
49 |
|
use kvm; |
50 |
|
use browser; |
51 |
|
use network; |
52 |
|
|
53 |
|
|
54 |
sub static { |
sub static { |
55 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
101 |
exit(0); |
exit(0); |
102 |
} |
} |
103 |
|
|
|
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 $!"; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
104 |
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu(); |
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu(); |
105 |
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
106 |
|
|
121 |
|
|
122 |
my $debug_proc = ''; |
my $debug_proc = ''; |
123 |
|
|
124 |
warn 'pids: ', dump( $pids ) if $debug; |
foreach my $name ( sort keys %$daemons::pids ) { |
125 |
foreach my $name ( sort keys %$pids ) { |
my $pid = $daemons::pids->{$name} || next; |
|
my $pid = $pids->{$name} || next; |
|
126 |
|
|
127 |
my $html = qq|<a href=/start_stop/$name>$pid</a>|; |
my $html; |
128 |
|
|
129 |
my $proc = "/proc/$pid/status"; |
my $proc = "/proc/$pid/status"; |
130 |
|
|
131 |
if ( -e $proc ) { |
if ( -e $proc ) { |
132 |
|
$html .= qq|<a href=/start_stop/$name>$pid</a>|; |
133 |
if ( $debug ) { |
if ( $debug ) { |
134 |
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
135 |
|
|
147 |
if ( $name->can('actions') ) { |
if ( $name->can('actions') ) { |
148 |
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
149 |
} |
} |
150 |
|
} else { |
151 |
|
$html .= qq|<a href=/start_stop/$name>restart</a> $pid exited|; |
152 |
} |
} |
153 |
|
|
154 |
push @rows, ( $name => $html ); |
push @rows, ( $name => $html ); |
178 |
|
|
179 |
} elsif ( $path =~ m{^/server} ) { |
} elsif ( $path =~ m{^/server} ) { |
180 |
print $client $ok |
print $client $ok |
181 |
, html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) |
, html::table( 2, map { ( $_, html::tt eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir', 'conf' ) ) |
182 |
; |
; |
183 |
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { |
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { |
184 |
my $ip = $1 || $client->peerhost; |
my $ip = $1 || $client->peerhost; |
189 |
, qq|<form method=get>| |
, qq|<form method=get>| |
190 |
, html::table( 2, |
, html::table( 2, |
191 |
'ip' => $ip, |
'ip' => $ip, |
192 |
|
'mac' => client::mac( $ip ), |
193 |
'hostname' => qq|<input type=text name=hostname value=$hostname>|, |
'hostname' => qq|<input type=text name=hostname value=$hostname>|, |
194 |
'deploy' => html::select( 'deploy', $deploy, config::available ), |
'deploy' => html::select( 'deploy', $deploy, config::available ), |
195 |
) |
) |
198 |
, config::for_ip( $ip ) |
, config::for_ip( $ip ) |
199 |
, qq|</pre>| |
, qq|</pre>| |
200 |
; |
; |
201 |
|
|
202 |
|
if ( my $amt = client::conf( $ip, 'amt' ) ) { |
203 |
|
print $client amt::info( $amt ); |
204 |
|
} |
205 |
} else { |
} else { |
206 |
print $client $ok |
print $client $ok |
207 |
, qq|<h2>Clients on $server::ip</h2>| |
, qq|<h2>Clients on $server::ip</h2>| |
208 |
, qq|<ul>| |
, html::table( -4, |
209 |
, join("\n", |
'ip', 'mac', 'hostname', 'deploy', |
210 |
map { |
map { |
211 |
my $ip = $_; |
my $ip = $_; |
212 |
$ip =~ s{^.+/ip/}{}; |
$ip =~ s{^.+/ip/}{}; |
213 |
qq|<li><a href=/client/$ip>$ip</a></li>| |
( qq|<a href=/client/$ip>$ip</a>|, client::mac($ip), client::conf( $ip, 'hostname' ), html::tt client::conf( $ip, 'deploy' ) ); |
214 |
} |
} |
215 |
glob("$server::conf/ip/*") |
glob("$server::conf/ip/*") |
216 |
) |
) |
|
, qq|</ul>| |
|
217 |
; |
; |
218 |
} |
} |
219 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
222 |
print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|; |
print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|; |
223 |
server::debug( $debug ) if $1 eq 'debug'; |
server::debug( $debug ) if $1 eq 'debug'; |
224 |
} elsif ( $path =~ m{^/start_stop/(\S+)} ) { |
} elsif ( $path =~ m{^/start_stop/(\S+)} ) { |
225 |
print $client $redirect, start_stop($1); |
print $client $redirect, daemons::start_stop($1); |
226 |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
227 |
$1->$2(); |
$1->$2(); |
228 |
print $client $redirect; |
print $client $redirect; |
236 |
|
|
237 |
} |
} |
238 |
|
|
|
use browser; |
|
|
use network; |
|
|
|
|
239 |
sub start { |
sub start { |
240 |
|
|
241 |
warn 'tap ', network::tap(); |
warn 'tap ', network::tap(); |
242 |
|
|
243 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
244 |
Proto => 'tcp', |
Proto => 'tcp', |
245 |
|
LocalAddr => $server::ip, |
246 |
LocalPort => $httpd::port, |
LocalPort => $httpd::port, |
247 |
Listen => SOMAXCONN, |
Listen => SOMAXCONN, |
248 |
Reuse => 1 |
Reuse => 1 |
250 |
|
|
251 |
print "url $url\n"; |
print "url $url\n"; |
252 |
|
|
253 |
start_stop 'browser', $url; |
daemons::start_stop 'browser', $url; |
254 |
start_stop 'dhcpd'; |
daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd/ ); |
255 |
start_stop 'tftpd'; |
daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device |
|
start_stop 'dnsd'; |
|
|
start_stop 'kvm'; |
|
256 |
|
|
257 |
while (1) { |
while (1) { |
258 |
my $client = $server->accept() || next; # ALARM trickle us |
my $client = $server->accept() || next; # ALARM trickle us |