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 |
|
|
50 |
our $static_pids; |
our $static_pids; |
51 |
use progress_bar; |
use progress_bar; |
52 |
use config; |
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) = @_; |
113 |
|
|
114 |
sub start_stop { |
sub start_stop { |
115 |
my $daemon = shift; |
my $daemon = shift; |
|
my $pid = $pids->{$daemon} || 'not started'; |
|
116 |
|
|
117 |
|
my $pid = $pids->{$daemon}; |
118 |
|
my $pid_path = "$server::conf/$daemon.pid"; |
119 |
|
|
120 |
|
if ( ! $pid && -e $pid_path ) { |
121 |
|
my $p = read_file $pid_path; |
122 |
|
if ( kill 0, $p ) { |
123 |
|
warn "adopted $daemon $p\n"; |
124 |
|
return $pids->{$daemon} = $p; |
125 |
|
} else { |
126 |
|
warn "old $daemon $p not running"; |
127 |
|
unlink $pid_path; |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
|
$pid ||= 'not started'; |
132 |
warn "start_stop $daemon $pid\n"; |
warn "start_stop $daemon $pid\n"; |
133 |
|
|
134 |
if ( $pid =~ m{^\d+$} ) { |
if ( $pid =~ m{^\d+$} ) { |
142 |
if ( $pid = fork ) { |
if ( $pid = fork ) { |
143 |
# parent |
# parent |
144 |
$pids->{$daemon} = $pid; |
$pids->{$daemon} = $pid; |
145 |
|
write_file $pid_path, $pid; |
146 |
warn "forked $daemon $pid\n"; |
warn "forked $daemon $pid\n"; |
147 |
return qq|$daemon pid $pid started|; |
return qq|$daemon pid $pid started|; |
148 |
} elsif ( defined $pid ) { |
} elsif ( defined $pid ) { |
152 |
if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) { |
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}"; |
my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}"; |
154 |
warn "exec $exec"; |
warn "exec $exec"; |
155 |
exec "xterm -T $daemon -n $daemon -e $exec"; |
x11::xterm( $daemon => $exec ); |
156 |
} else { |
} else { |
157 |
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
158 |
warn "eval $eval"; |
warn "eval $eval"; |
166 |
} |
} |
167 |
} |
} |
168 |
|
|
169 |
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(); |
170 |
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|; |
171 |
|
|
172 |
sub get_request { |
sub get_request { |
190 |
foreach my $name ( sort keys %$pids ) { |
foreach my $name ( sort keys %$pids ) { |
191 |
my $pid = $pids->{$name} || next; |
my $pid = $pids->{$name} || next; |
192 |
|
|
193 |
my $html = qq|<a href=/start_stop/$name>$pid</a>|; |
my $html; |
194 |
|
|
195 |
my $proc = "/proc/$pid/status"; |
my $proc = "/proc/$pid/status"; |
196 |
|
|
197 |
if ( -e $proc ) { |
if ( -e $proc ) { |
198 |
|
$html .= qq|<a href=/start_stop/$name>$pid</a>|; |
199 |
if ( $debug ) { |
if ( $debug ) { |
200 |
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
201 |
|
|
213 |
if ( $name->can('actions') ) { |
if ( $name->can('actions') ) { |
214 |
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
215 |
} |
} |
216 |
|
} else { |
217 |
|
$html .= qq|<a href=/start_stop/$name>restart</a> $pid exited|; |
218 |
} |
} |
219 |
|
|
220 |
push @rows, ( $name => $html ); |
push @rows, ( $name => $html ); |
236 |
} |
} |
237 |
|
|
238 |
print $client $ok |
print $client $ok |
|
, menu() |
|
239 |
, html::table( 2, @rows ) |
, html::table( 2, @rows ) |
240 |
, $below_table |
, $below_table |
241 |
, html::tabs( log::mac_changes ) |
, html::tabs( log::mac_changes ) |
244 |
|
|
245 |
} elsif ( $path =~ m{^/server} ) { |
} elsif ( $path =~ m{^/server} ) { |
246 |
print $client $ok |
print $client $ok |
|
, menu() |
|
247 |
, 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' ) ) |
248 |
; |
; |
249 |
} elsif ( $path =~ m{^/client} ) { |
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { |
250 |
my $ip = $client->peerhost; |
my $ip = $1 || $client->peerhost; |
251 |
my $hostname = server::shared( "hostname/$ip", $param->{hostname} ); |
if ( $ip ne $server::ip ) { |
252 |
my $deploy = server::shared( "deploy/$ip", $param->{deploy} ); |
my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} ); |
253 |
print $client $ok |
my $deploy = client::conf( $ip, 'deploy' => $param->{deploy} ); |
254 |
, menu() |
print $client $ok |
255 |
, qq|<form method=get>| |
, qq|<form method=get>| |
256 |
, html::table( 2, |
, html::table( 2, |
257 |
'ip' => $ip, |
'ip' => $ip, |
258 |
'hostname' => qq|<input type=text name=hostname value=$hostname>|, |
'mac' => client::mac( $ip ), |
259 |
'deploy' => html::select( 'deploy', $deploy, config::available ), |
'hostname' => qq|<input type=text name=hostname value=$hostname>|, |
260 |
) |
'deploy' => html::select( 'deploy', $deploy, config::available ), |
261 |
, qq|<input type=submit value=change></form><pre>| |
) |
262 |
, config::for_ip( $ip ) |
, qq|<input type=submit value=change></form>| |
263 |
, qq|</pre>| |
, qq|<h2>PXElinux $deploy</h2><pre>| |
264 |
; |
, config::for_ip( $ip ) |
265 |
|
, qq|</pre>| |
266 |
|
; |
267 |
|
|
268 |
|
if ( my $amt = client::conf( $ip, 'amt' ) ) { |
269 |
|
print $client amt::info( $amt ); |
270 |
|
} |
271 |
|
} else { |
272 |
|
print $client $ok |
273 |
|
, qq|<h2>Clients on $server::ip</h2>| |
274 |
|
, html::table( -4, |
275 |
|
'ip', 'mac', 'hostname', 'deploy', |
276 |
|
map { |
277 |
|
my $ip = $_; |
278 |
|
$ip =~ s{^.+/ip/}{}; |
279 |
|
( qq|<a href=/client/$ip>$ip</a>|, client::mac($ip), client::conf( $ip, 'hostname' ), client::conf( $ip, 'deploy' ) ); |
280 |
|
} |
281 |
|
glob("$server::conf/ip/*") |
282 |
|
) |
283 |
|
; |
284 |
|
} |
285 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
286 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
287 |
warn $@ if $@; |
warn $@ if $@; |
295 |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
296 |
print $client $redirect; |
print $client $redirect; |
297 |
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/ |
|
|
|
|
|
}; |
|
298 |
} else { |
} else { |
299 |
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>"; |
300 |
warn "404 $path"; |
warn "404 $path"; |
322 |
start_stop 'dhcpd'; |
start_stop 'dhcpd'; |
323 |
start_stop 'tftpd'; |
start_stop 'tftpd'; |
324 |
start_stop 'dnsd'; |
start_stop 'dnsd'; |
325 |
start_stop 'kvm'; |
start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device |
326 |
|
|
327 |
while (1) { |
while (1) { |
328 |
my $client = $server->accept() || next; # ALARM trickle us |
my $client = $server->accept() || next; # ALARM trickle us |