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; |
use Regexp::Common qw/net/; |
21 |
|
|
22 |
|
sub menu {qq{ |
23 |
|
|
24 |
|
<div style="font-size: 80%; color: #888"> |
25 |
|
<a href=/>home</a> |
26 |
|
<a href=/server>server</a> |
27 |
|
<a href=/client>client</a> |
28 |
|
</div> |
29 |
|
|
30 |
|
}} |
31 |
|
|
32 |
our $pids; |
our $pids; |
33 |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
48 |
|
|
49 |
use html; |
use html; |
50 |
our $static_pids; |
our $static_pids; |
51 |
|
use progress_bar; |
52 |
|
use config; |
53 |
|
use client; |
54 |
|
use log; |
55 |
|
|
56 |
sub static { |
sub static { |
57 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
78 |
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"; |
79 |
|
|
80 |
open(my $fh, $full); |
open(my $fh, $full); |
|
print "static $path $type $size\n"; |
|
81 |
|
|
82 |
my $block = 8192; |
my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it) |
83 |
my $buff; |
my $buff; |
84 |
my $pos = 0; |
my $pos = 0; |
85 |
|
|
86 |
|
warn "static $path $type $size block: $block\n"; |
87 |
|
|
88 |
|
progress_bar::start; |
89 |
|
|
90 |
while( my $len = read $fh, $buff, $block ) { |
while( my $len = read $fh, $buff, $block ) { |
91 |
print $client $buff; |
print $client $buff; |
92 |
|
$client->flush; |
93 |
$pos += $len; |
$pos += $len; |
94 |
printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size; |
progress_bar::tick( $path, $pos, $size ); |
95 |
} |
} |
96 |
close($fh); |
close($fh); |
97 |
close($client); |
close($client); |
98 |
|
|
99 |
print "$path $pos == $size OK\n"; |
print STDERR "\n"; |
100 |
|
|
101 |
|
warn "exit static child"; |
102 |
|
|
103 |
exit; |
exit(0); |
104 |
} |
} |
105 |
|
|
106 |
use boolean; |
use boolean; |
107 |
|
|
|
use screen; |
|
108 |
use kvm; |
use kvm; |
109 |
|
use qemu; $pids->{qemu} = 'stopped'; |
110 |
|
|
111 |
$SIG{CHLD} = 'IGNORE'; |
$SIG{CHLD} = 'IGNORE'; |
112 |
|
|
113 |
sub start_stop { |
sub start_stop { |
114 |
my $daemon = shift; |
my $daemon = shift; |
115 |
my $pid = $pids->{$daemon}; |
my $pid = $pids->{$daemon} || 'not started'; |
116 |
|
|
117 |
warn "start_stop $daemon $pid pids: ",dump( $pids ); |
warn "start_stop $daemon $pid\n"; |
118 |
|
|
119 |
if ( $pid =~ m{^\d+$} ) { |
if ( $pid =~ m{^\d+$} ) { |
120 |
warn "kill 9 $pid"; |
my $pstree = `pstree -p $pid`; |
121 |
kill 9, $pid; |
my @pids = $pstree =~ m{\((\d+)\)}g; |
122 |
|
warn "pstree $pstree pids ",dump( @pids ); |
123 |
|
kill 1, $_ foreach reverse @pids; |
124 |
$pids->{$daemon} = 'stopped'; |
$pids->{$daemon} = 'stopped'; |
125 |
return qq|$daemon pid $pid stopped|; |
return qq|$daemon pid $pid stopped|; |
126 |
} else { |
} else { |
127 |
if ( $pid = fork ) { |
if ( $pid = fork ) { |
128 |
# parent |
# parent |
129 |
$pids->{$daemon} = $pid; |
$pids->{$daemon} = $pid; |
130 |
warn "forked $daemon $pid"; |
warn "forked $daemon $pid\n"; |
131 |
return qq|$daemon pid $pid started|; |
return qq|$daemon pid $pid started|; |
132 |
} elsif ( defined $pid ) { |
} elsif ( defined $pid ) { |
133 |
# child |
# child |
134 |
my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')'; |
my $invoke = 'start'; |
135 |
warn "eval $eval"; |
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
136 |
eval $eval; |
if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) { |
137 |
warn "can't start $daemon: $@" if $@; |
my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}"; |
138 |
|
warn "exec $exec"; |
139 |
|
exec "xterm -T $daemon -n $daemon -e $exec"; |
140 |
|
} else { |
141 |
|
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
142 |
|
warn "eval $eval"; |
143 |
|
eval $eval; |
144 |
|
warn "can't start $daemon: $@" if $@; |
145 |
|
} |
146 |
exit; |
exit; |
147 |
} else { |
} else { |
148 |
die "fork error $!"; |
die "fork error $!"; |
150 |
} |
} |
151 |
} |
} |
152 |
|
|
153 |
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(); |
154 |
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|; |
155 |
|
|
156 |
sub get_request { |
sub get_request { |
157 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
158 |
|
|
159 |
warn "get_request $client $path ",dump( $param ); |
server->refresh; |
160 |
|
|
161 |
|
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
162 |
|
|
163 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
164 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
165 |
} elsif ( $path eq '/' ) { |
} elsif ( $path eq '/' ) { |
166 |
|
|
|
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|; |
|
|
|
|
167 |
my @rows = ( |
my @rows = ( |
|
'ip', html::tt( $server::ip ), |
|
|
'netmask', html::tt( $server::netmask ), |
|
|
|
|
168 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
169 |
); |
); |
170 |
|
|
171 |
my $debug_proc; |
my $debug_proc = ''; |
172 |
|
|
173 |
warn 'pids: ', dump( $pids ); |
warn 'pids: ', dump( $pids ) if $debug; |
174 |
foreach my $name ( keys %$pids ) { |
foreach my $name ( sort keys %$pids ) { |
175 |
my $pid = $pids->{$name} || next; |
my $pid = $pids->{$name} || next; |
176 |
|
|
177 |
my $html = qq|<a href=/$name>$pid</a>|; |
my $html = qq|<a href=/start_stop/$name>$pid</a>|; |
178 |
|
|
179 |
|
my $proc = "/proc/$pid/status"; |
180 |
|
|
181 |
if ( $debug ) { |
if ( -e $proc ) { |
182 |
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
if ( $debug ) { |
183 |
|
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
184 |
|
|
185 |
|
$debug_proc |
186 |
|
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
187 |
|
. read_file($proc) |
188 |
|
. qq|</pre>| |
189 |
|
; |
190 |
|
} |
191 |
|
|
192 |
my $proc = "/proc/$pid/status"; |
if ( $name->can('start_fork') ) { |
193 |
$debug_proc |
$html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork; |
194 |
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
} |
195 |
. read_file($proc) |
|
196 |
. qq|</pre>| |
if ( $name->can('actions') ) { |
197 |
if -e $proc; |
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
198 |
|
} |
199 |
} |
} |
200 |
|
|
201 |
push @rows, ( $name => $html ); |
push @rows, ( $name => $html ); |
202 |
} |
} |
203 |
|
|
204 |
warn 'static_pids: ', dump( $static_pids ); |
my $below_table = ''; |
205 |
|
|
206 |
|
warn 'static_pids: ', dump( $static_pids ) if $debug; |
207 |
foreach my $pid ( keys %$static_pids ) { |
foreach my $pid ( keys %$static_pids ) { |
208 |
my $path = $static_pids->{$pid}; |
my $path = $static_pids->{$pid}; |
209 |
if ( -d "/proc/$pid" ) { |
if ( -d "/proc/$pid" ) { |
210 |
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
211 |
|
} elsif ( $param->{clean_completed_downloads} ) { |
212 |
|
delete $static_pids->{$pid} |
213 |
} else { |
} else { |
214 |
push @rows, ( $path => "$pid competed" ); |
push @rows, ( $path => "$pid competed" ); |
215 |
|
$below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|; |
216 |
} |
} |
217 |
} |
} |
218 |
|
|
219 |
print $client $ok |
print $client $ok |
220 |
, html::table( 2, @rows ) |
, html::table( 2, @rows ) |
221 |
|
, $below_table |
222 |
, html::tabs( log::mac_changes ) |
, html::tabs( log::mac_changes ) |
223 |
, $debug_proc |
, $debug_proc |
224 |
; |
; |
225 |
|
|
226 |
|
} elsif ( $path =~ m{^/server} ) { |
227 |
|
print $client $ok |
228 |
|
, html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) |
229 |
|
; |
230 |
|
} elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) { |
231 |
|
my $ip = $1 || $client->peerhost; |
232 |
|
if ( $ip ne $server::ip ) { |
233 |
|
my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} ); |
234 |
|
my $deploy = client::conf( $ip, 'deploy' => $param->{deploy} ); |
235 |
|
print $client $ok |
236 |
|
, qq|<form method=get>| |
237 |
|
, html::table( 2, |
238 |
|
'ip' => $ip, |
239 |
|
'mac' => client::mac( $ip ), |
240 |
|
'hostname' => qq|<input type=text name=hostname value=$hostname>|, |
241 |
|
'deploy' => html::select( 'deploy', $deploy, config::available ), |
242 |
|
) |
243 |
|
, qq|<input type=submit value=change></form>| |
244 |
|
, qq|<h2>PXElinux $deploy</h2><pre>| |
245 |
|
, config::for_ip( $ip ) |
246 |
|
, qq|</pre>| |
247 |
|
; |
248 |
|
} else { |
249 |
|
print $client $ok |
250 |
|
, qq|<h2>Clients on $server::ip</h2>| |
251 |
|
, qq|<ul>| |
252 |
|
, join("\n", |
253 |
|
map { |
254 |
|
my $ip = $_; |
255 |
|
$ip =~ s{^.+/ip/}{}; |
256 |
|
qq|<li><a href=/client/$ip>$ip</a> |, client::mac($ip), qq|</li>|; |
257 |
|
} |
258 |
|
glob("$server::conf/ip/*") |
259 |
|
) |
260 |
|
, qq|</ul>| |
261 |
|
; |
262 |
|
} |
263 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
264 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
265 |
warn $@ if $@; |
warn $@ if $@; |
266 |
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>|; |
267 |
server::debug( $debug ) if $1 eq 'debug'; |
server::debug( $debug ) if $1 eq 'debug'; |
268 |
} elsif ( $path =~ m{^/(screen|kvm)} ) { |
} elsif ( $path =~ m{^/start_stop/(\S+)} ) { |
269 |
print $client $redirect, start_stop($1); |
print $client $redirect, start_stop($1); |
270 |
|
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
271 |
|
$1->$2(); |
272 |
|
print $client $redirect; |
273 |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
274 |
print $client $redirect; |
print $client $redirect; |
275 |
kill 9, $1 && 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/ |
|
|
|
|
|
}; |
|
276 |
} else { |
} else { |
277 |
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>"; |
278 |
warn "404 $path"; |
warn "404 $path"; |
281 |
} |
} |
282 |
|
|
283 |
use browser; |
use browser; |
284 |
|
use network; |
285 |
|
|
286 |
sub start { |
sub start { |
287 |
|
|
288 |
|
warn 'tap ', network::tap(); |
289 |
|
|
290 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
291 |
Proto => 'tcp', |
Proto => 'tcp', |
292 |
LocalPort => $httpd::port, |
LocalPort => $httpd::port, |
297 |
print "url $url\n"; |
print "url $url\n"; |
298 |
|
|
299 |
start_stop 'browser', $url; |
start_stop 'browser', $url; |
300 |
start_stop 'screen'; |
start_stop 'dhcpd'; |
301 |
|
start_stop 'tftpd'; |
302 |
|
start_stop 'dnsd'; |
303 |
start_stop 'kvm'; |
start_stop 'kvm'; |
304 |
|
|
305 |
while (my $client = $server->accept()) { |
while (1) { |
306 |
$client->autoflush(1); |
my $client = $server->accept() || next; # ALARM trickle us |
307 |
my $request = <$client>; |
my $request = <$client>; |
308 |
|
|
309 |
warn "request $request\n" if $debug; |
warn "request $request\n" if $debug; |
310 |
|
|
|
Module::Refresh->refresh; |
|
|
|
|
311 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
312 |
my $path = $1; |
my $path = $1; |
313 |
my $param; |
my $param; |
318 |
} |
} |
319 |
warn "param: ",dump( $param ) if $debug; |
warn "param: ",dump( $param ) if $debug; |
320 |
} |
} |
|
warn "path $path param: ",dump( $param ); |
|
321 |
get_request $client, $path, $param; |
get_request $client, $path, $param; |
322 |
} else { |
} else { |
323 |
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"; |
324 |
warn "500 $request"; |
warn "500 $request"; |
325 |
} |
} |
326 |
|
|
327 |
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; |
|
328 |
|
|
329 |
} |
} |
330 |
|
|