17 |
use File::Slurp; |
use File::Slurp; |
18 |
#use JSON; |
#use JSON; |
19 |
use IO::Socket::INET; |
use IO::Socket::INET; |
20 |
use Module::Refresh; |
|
21 |
|
sub menu {qq{ |
22 |
|
|
23 |
|
<div style="font-size: 80%; color: #888"> |
24 |
|
<a href=/>home</a> |
25 |
|
<a href=/server>server</a> |
26 |
|
<a href=/client>client</a> |
27 |
|
</div> |
28 |
|
|
29 |
|
}} |
30 |
|
|
31 |
our $pids; |
our $pids; |
32 |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
47 |
|
|
48 |
use html; |
use html; |
49 |
our $static_pids; |
our $static_pids; |
50 |
|
use progress_bar; |
51 |
|
use config; |
52 |
|
use client; |
53 |
|
use log; |
54 |
|
|
55 |
sub static { |
sub static { |
56 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
77 |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n"; |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n"; |
78 |
|
|
79 |
open(my $fh, $full); |
open(my $fh, $full); |
|
print "static $path $type $size\n"; |
|
80 |
|
|
81 |
my $block = 8192; |
my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it) |
82 |
my $buff; |
my $buff; |
83 |
my $pos = 0; |
my $pos = 0; |
84 |
|
|
85 |
|
warn "static $path $type $size block: $block\n"; |
86 |
|
|
87 |
|
progress_bar::start; |
88 |
|
|
89 |
while( my $len = read $fh, $buff, $block ) { |
while( my $len = read $fh, $buff, $block ) { |
90 |
print $client $buff; |
print $client $buff; |
91 |
|
$client->flush; |
92 |
$pos += $len; |
$pos += $len; |
93 |
printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size; |
progress_bar::tick( $path, $pos, $size ); |
94 |
} |
} |
95 |
close($fh); |
close($fh); |
96 |
close($client); |
close($client); |
97 |
|
|
98 |
print "$path $pos == $size OK\n"; |
print STDERR "\n"; |
99 |
|
|
100 |
exit; |
warn "exit static child"; |
101 |
|
|
102 |
|
exit(0); |
103 |
} |
} |
104 |
|
|
105 |
use boolean; |
use boolean; |
106 |
|
|
|
use screen; |
|
107 |
use kvm; |
use kvm; |
108 |
|
|
109 |
$SIG{CHLD} = 'IGNORE'; |
$SIG{CHLD} = 'IGNORE'; |
110 |
|
|
111 |
sub start_stop { |
sub start_stop { |
112 |
my $daemon = shift; |
my $daemon = shift; |
113 |
my $pid = $pids->{$daemon}; |
my $pid = $pids->{$daemon} || 'not started'; |
114 |
|
|
115 |
warn "start_stop $daemon $pid pids: ",dump( $pids ); |
warn "start_stop $daemon $pid\n"; |
116 |
|
|
117 |
if ( $pid =~ m{^\d+$} ) { |
if ( $pid =~ m{^\d+$} ) { |
118 |
my $pstree = `pstree -p $pid`; |
my $pstree = `pstree -p $pid`; |
125 |
if ( $pid = fork ) { |
if ( $pid = fork ) { |
126 |
# parent |
# parent |
127 |
$pids->{$daemon} = $pid; |
$pids->{$daemon} = $pid; |
128 |
warn "forked $daemon $pid"; |
warn "forked $daemon $pid\n"; |
129 |
return qq|$daemon pid $pid started|; |
return qq|$daemon pid $pid started|; |
130 |
} elsif ( defined $pid ) { |
} elsif ( defined $pid ) { |
131 |
# child |
# child |
132 |
my $invoke = 'start'; |
my $invoke = 'start'; |
133 |
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
134 |
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) { |
135 |
warn "eval $eval"; |
my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}"; |
136 |
eval $eval; |
warn "exec $exec"; |
137 |
warn "can't start $daemon: $@" if $@; |
exec "xterm -T $daemon -n $daemon -e $exec"; |
138 |
|
} else { |
139 |
|
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
140 |
|
warn "eval $eval"; |
141 |
|
eval $eval; |
142 |
|
warn "can't start $daemon: $@" if $@; |
143 |
|
} |
144 |
exit; |
exit; |
145 |
} else { |
} else { |
146 |
die "fork error $!"; |
die "fork error $!"; |
154 |
sub get_request { |
sub get_request { |
155 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
156 |
|
|
157 |
warn "get_request $client $path ",dump( $param ); |
server->refresh; |
158 |
|
|
159 |
|
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
160 |
|
|
161 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
162 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
163 |
} elsif ( $path eq '/' ) { |
} elsif ( $path eq '/' ) { |
164 |
|
|
|
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|; |
|
|
|
|
165 |
my @rows = ( |
my @rows = ( |
|
'ip', html::tt( $server::ip ), |
|
|
'netmask', html::tt( $server::netmask ), |
|
|
|
|
166 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
167 |
); |
); |
168 |
|
|
169 |
my $debug_proc; |
my $debug_proc = ''; |
170 |
|
|
171 |
warn 'pids: ', dump( $pids ) if $debug; |
warn 'pids: ', dump( $pids ) if $debug; |
172 |
foreach my $name ( sort keys %$pids ) { |
foreach my $name ( sort keys %$pids ) { |
215 |
} |
} |
216 |
|
|
217 |
print $client $ok |
print $client $ok |
218 |
|
, menu() |
219 |
, html::table( 2, @rows ) |
, html::table( 2, @rows ) |
220 |
, $below_table |
, $below_table |
221 |
, html::tabs( log::mac_changes ) |
, html::tabs( log::mac_changes ) |
222 |
, $debug_proc |
, $debug_proc |
223 |
; |
; |
224 |
|
|
225 |
|
} elsif ( $path =~ m{^/server} ) { |
226 |
|
print $client $ok |
227 |
|
, menu() |
228 |
|
, html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) |
229 |
|
; |
230 |
|
} elsif ( $path =~ m{^/client} ) { |
231 |
|
my $ip = $client->peerhost; |
232 |
|
my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} ); |
233 |
|
my $deploy = client::conf( $ip, 'deploy' => $param->{deploy} ); |
234 |
|
print $client $ok |
235 |
|
, menu() |
236 |
|
, qq|<form method=get>| |
237 |
|
, html::table( 2, |
238 |
|
'ip' => $ip, |
239 |
|
'hostname' => qq|<input type=text name=hostname value=$hostname>|, |
240 |
|
'deploy' => html::select( 'deploy', $deploy, config::available ), |
241 |
|
) |
242 |
|
, qq|<input type=submit value=change></form><pre>| |
243 |
|
, config::for_ip( $ip ) |
244 |
|
, qq|</pre>| |
245 |
|
; |
246 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
247 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
248 |
warn $@ if $@; |
warn $@ if $@; |
249 |
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>|; |
250 |
server::debug( $debug ) if $1 eq 'debug'; |
server::debug( $debug ) if $1 eq 'debug'; |
251 |
} elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes |
} elsif ( $path =~ m{^/start_stop/(\S+)} ) { |
252 |
print $client $redirect, start_stop($1); |
print $client $redirect, start_stop($1); |
253 |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
254 |
$1->$2(); |
$1->$2(); |
275 |
} |
} |
276 |
|
|
277 |
use browser; |
use browser; |
278 |
|
use network; |
279 |
|
|
280 |
sub start { |
sub start { |
281 |
|
|
282 |
|
warn 'tap ', network::tap(); |
283 |
|
|
284 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
285 |
Proto => 'tcp', |
Proto => 'tcp', |
286 |
LocalPort => $httpd::port, |
LocalPort => $httpd::port, |
291 |
print "url $url\n"; |
print "url $url\n"; |
292 |
|
|
293 |
start_stop 'browser', $url; |
start_stop 'browser', $url; |
294 |
start_stop 'screen'; |
start_stop 'dhcpd'; |
295 |
|
start_stop 'tftpd'; |
296 |
|
start_stop 'dnsd'; |
297 |
start_stop 'kvm'; |
start_stop 'kvm'; |
298 |
|
|
299 |
while (my $client = $server->accept()) { |
while (1) { |
300 |
$client->autoflush(1); |
my $client = $server->accept() || next; # ALARM trickle us |
301 |
my $request = <$client>; |
my $request = <$client>; |
302 |
|
|
303 |
warn "request $request\n" if $debug; |
warn "request $request\n" if $debug; |
304 |
|
|
|
Module::Refresh->refresh; |
|
|
|
|
305 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
306 |
my $path = $1; |
my $path = $1; |
307 |
my $param; |
my $param; |
312 |
} |
} |
313 |
warn "param: ",dump( $param ) if $debug; |
warn "param: ",dump( $param ) if $debug; |
314 |
} |
} |
|
warn "path $path param: ",dump( $param ); |
|
315 |
get_request $client, $path, $param; |
get_request $client, $path, $param; |
316 |
} else { |
} else { |
317 |
print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request"; |
print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request"; |
318 |
warn "500 $request"; |
warn "500 $request"; |
319 |
} |
} |
320 |
|
|
321 |
print $client qq{ |
print $client menu() if $client->connected; |
|
<div style="font-size: 80%; color: #888"> |
|
|
<a href="">reload</a> |
|
|
<a href=/>index</a> |
|
|
<a href=/exit>exit</a> |
|
|
</div> |
|
|
} if $client->connected; |
|
322 |
|
|
323 |
} |
} |
324 |
|
|