17 |
use File::Slurp; |
use File::Slurp; |
18 |
#use JSON; |
#use JSON; |
19 |
use IO::Socket::INET; |
use IO::Socket::INET; |
20 |
|
use Regexp::Common qw/net/; |
21 |
|
|
22 |
sub menu {qq{ |
sub menu {qq{ |
23 |
|
|
24 |
<div style="font-size: 80%; color: #888"> |
<div style="font-size: 80%; color: #888"> |
25 |
<a href=/>home</a> |
<a href=/>home</a> |
26 |
<a href=/server>server</a> |
<a href=/server>server</a> |
27 |
|
<a href=/client>client</a> |
28 |
</div> |
</div> |
29 |
|
|
30 |
}} |
}} |
49 |
use html; |
use html; |
50 |
our $static_pids; |
our $static_pids; |
51 |
use progress_bar; |
use progress_bar; |
52 |
|
use config; |
53 |
|
use client; |
54 |
|
use log; |
55 |
|
use x11; |
56 |
|
use amt; |
57 |
|
|
58 |
sub static { |
sub static { |
59 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
107 |
|
|
108 |
use boolean; |
use boolean; |
109 |
|
|
|
use screen; |
|
110 |
use kvm; |
use kvm; |
111 |
|
|
112 |
$SIG{CHLD} = 'IGNORE'; |
$SIG{CHLD} = 'IGNORE'; |
134 |
# child |
# child |
135 |
my $invoke = 'start'; |
my $invoke = 'start'; |
136 |
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
137 |
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) { |
138 |
warn "eval $eval"; |
my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}"; |
139 |
eval $eval; |
warn "exec $exec"; |
140 |
warn "can't start $daemon: $@" if $@; |
x11::xterm( $daemon => $exec ); |
141 |
|
} else { |
142 |
|
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
143 |
|
warn "eval $eval"; |
144 |
|
eval $eval; |
145 |
|
warn "can't start $daemon: $@" if $@; |
146 |
|
} |
147 |
exit; |
exit; |
148 |
} else { |
} else { |
149 |
die "fork error $!"; |
die "fork error $!"; |
151 |
} |
} |
152 |
} |
} |
153 |
|
|
154 |
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(); |
155 |
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|; |
156 |
|
|
157 |
sub get_request { |
sub get_request { |
165 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
166 |
} elsif ( $path eq '/' ) { |
} elsif ( $path eq '/' ) { |
167 |
|
|
|
my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start'; |
|
|
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
|
|
$pids->{screen} ? qq|start| : qq|start screen first|; |
|
|
|
|
168 |
my @rows = ( |
my @rows = ( |
169 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
170 |
); |
); |
175 |
foreach my $name ( sort keys %$pids ) { |
foreach my $name ( sort keys %$pids ) { |
176 |
my $pid = $pids->{$name} || next; |
my $pid = $pids->{$name} || next; |
177 |
|
|
178 |
my $html = qq|<a href=/start_stop/$name>$pid</a>|; |
my $html; |
179 |
|
|
180 |
my $proc = "/proc/$pid/status"; |
my $proc = "/proc/$pid/status"; |
181 |
|
|
182 |
if ( -e $proc ) { |
if ( -e $proc ) { |
183 |
|
$html .= qq|<a href=/start_stop/$name>$pid</a>|; |
184 |
if ( $debug ) { |
if ( $debug ) { |
185 |
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
186 |
|
|
198 |
if ( $name->can('actions') ) { |
if ( $name->can('actions') ) { |
199 |
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
200 |
} |
} |
201 |
|
} else { |
202 |
|
$html .= qq|<a href=/start_stop/$name>restart</a> $pid exited|; |
203 |
} |
} |
204 |
|
|
205 |
push @rows, ( $name => $html ); |
push @rows, ( $name => $html ); |
221 |
} |
} |
222 |
|
|
223 |
print $client $ok |
print $client $ok |
|
, menu() |
|
224 |
, html::table( 2, @rows ) |
, html::table( 2, @rows ) |
225 |
, $below_table |
, $below_table |
226 |
, html::tabs( log::mac_changes ) |
, html::tabs( log::mac_changes ) |
229 |
|
|
230 |
} elsif ( $path =~ m{^/server} ) { |
} elsif ( $path =~ m{^/server} ) { |
231 |
print $client $ok |
print $client $ok |
|
, menu() |
|
232 |
, html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) |
, html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) |
233 |
; |
; |
234 |
|
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { |
235 |
|
my $ip = $1 || $client->peerhost; |
236 |
|
if ( $ip ne $server::ip ) { |
237 |
|
my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} ); |
238 |
|
my $deploy = client::conf( $ip, 'deploy' => $param->{deploy} ); |
239 |
|
print $client $ok |
240 |
|
, qq|<form method=get>| |
241 |
|
, html::table( 2, |
242 |
|
'ip' => $ip, |
243 |
|
'mac' => client::mac( $ip ), |
244 |
|
'hostname' => qq|<input type=text name=hostname value=$hostname>|, |
245 |
|
'deploy' => html::select( 'deploy', $deploy, config::available ), |
246 |
|
) |
247 |
|
, qq|<input type=submit value=change></form>| |
248 |
|
, qq|<h2>PXElinux $deploy</h2><pre>| |
249 |
|
, config::for_ip( $ip ) |
250 |
|
, qq|</pre>| |
251 |
|
; |
252 |
|
|
253 |
|
if ( my $amt = client::conf( $ip, 'amt' ) ) { |
254 |
|
print $client amt::info( $amt ); |
255 |
|
} |
256 |
|
} else { |
257 |
|
print $client $ok |
258 |
|
, qq|<h2>Clients on $server::ip</h2>| |
259 |
|
, html::table( -4, |
260 |
|
'ip', 'mac', 'hostname', 'deploy', |
261 |
|
map { |
262 |
|
my $ip = $_; |
263 |
|
$ip =~ s{^.+/ip/}{}; |
264 |
|
( qq|<a href=/client/$ip>$ip</a>|, client::mac($ip), client::conf( $ip, 'hostname' ), client::conf( $ip, 'deploy' ) ); |
265 |
|
} |
266 |
|
glob("$server::conf/ip/*") |
267 |
|
) |
268 |
|
; |
269 |
|
} |
270 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
271 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
272 |
warn $@ if $@; |
warn $@ if $@; |
273 |
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>|; |
274 |
server::debug( $debug ) if $1 eq 'debug'; |
server::debug( $debug ) if $1 eq 'debug'; |
275 |
} elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes |
} elsif ( $path =~ m{^/start_stop/(\S+)} ) { |
276 |
print $client $redirect, start_stop($1); |
print $client $redirect, start_stop($1); |
277 |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
278 |
$1->$2(); |
$1->$2(); |
280 |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
281 |
print $client $redirect; |
print $client $redirect; |
282 |
kill 1, $1 || kill 9, $2 && warn "killed $1"; |
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/ |
|
|
|
|
|
}; |
|
283 |
} else { |
} else { |
284 |
print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>"; |
print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>"; |
285 |
warn "404 $path"; |
warn "404 $path"; |
304 |
print "url $url\n"; |
print "url $url\n"; |
305 |
|
|
306 |
start_stop 'browser', $url; |
start_stop 'browser', $url; |
307 |
start_stop 'screen'; |
start_stop 'dhcpd'; |
308 |
start_stop 'kvm'; |
start_stop 'tftpd'; |
309 |
|
start_stop 'dnsd'; |
310 |
|
start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device |
311 |
|
|
312 |
while (1) { |
while (1) { |
313 |
my $client = $server->accept() || next; # ALARM trickle us |
my $client = $server->accept() || next; # ALARM trickle us |