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; |
32 |
|
$pids = { httpd => $$ } unless defined $pids; # keep pids on refresh |
33 |
|
|
34 |
|
sub DESTROY { |
35 |
|
warn "pids ",dump( $pids ); |
36 |
|
foreach ( values %$pids ) { |
37 |
|
warn "kill $_"; |
38 |
|
kill 1,$_ || kill 9, $_; |
39 |
|
} |
40 |
|
} |
41 |
|
|
42 |
our $port = 7777; |
our $port = 7777; |
|
our $debug = 1; |
|
43 |
|
|
44 |
use server; |
use server; |
45 |
our $url = "http://$server::ip:$port/"; |
our $debug = server::debug; |
46 |
|
our $url = "http://$server::ip:$port"; |
47 |
|
|
48 |
use html; |
use html; |
49 |
|
our $static_pids; |
50 |
|
use progress_bar; |
51 |
|
use config; |
52 |
|
|
53 |
sub static { |
sub static { |
54 |
my ($client,$path) = @_; |
my ($client,$path) = @_; |
55 |
|
|
56 |
$path = "tftp/$path"; |
my $full = "$server::base_dir/tftp/$path"; |
57 |
|
|
58 |
if ( ! -e $path || -d $path ) { |
return if ! -f $full; |
59 |
print $client "HTTP/1.0 404 $path not found\r\n"; |
|
60 |
return; |
if ( my $pid = fork ) { |
61 |
|
# parent |
62 |
|
close($client); |
63 |
|
print "http static child $pid\n"; |
64 |
|
$static_pids->{$pid} = $path; |
65 |
|
return 1; |
66 |
} |
} |
67 |
|
|
68 |
my $type = 'text/plain'; |
my $type = 'application/octet-stream'; |
69 |
$type = 'text/html' if $path =~ m{\.htm}; |
$type = 'text/html' if $path =~ m{\.htm}; |
70 |
$type = 'application/javascript' if $path =~ m{\.js}; |
$type = 'application/javascript' if $path =~ m{\.js}; |
71 |
|
$type = 'text/plain' if $path =~ m{\.txt}; |
72 |
|
|
73 |
|
my $size = -s $full || return; |
74 |
|
|
75 |
|
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n"; |
76 |
|
|
77 |
print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n"; |
open(my $fh, $full); |
78 |
open(my $html, $path); |
|
79 |
while(<$html>) { |
my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it) |
80 |
print $client $_; |
my $buff; |
81 |
|
my $pos = 0; |
82 |
|
|
83 |
|
warn "static $path $type $size block: $block\n"; |
84 |
|
|
85 |
|
progress_bar::start; |
86 |
|
|
87 |
|
while( my $len = read $fh, $buff, $block ) { |
88 |
|
print $client $buff; |
89 |
|
$client->flush; |
90 |
|
$pos += $len; |
91 |
|
progress_bar::tick( $path, $pos, $size ); |
92 |
} |
} |
93 |
close($html); |
close($fh); |
94 |
|
close($client); |
95 |
|
|
96 |
return $path; |
print STDERR "\n"; |
|
} |
|
97 |
|
|
98 |
my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"; |
warn "exit static child"; |
99 |
|
|
100 |
|
exit(0); |
101 |
|
} |
102 |
|
|
103 |
use boolean; |
use boolean; |
104 |
|
|
105 |
|
use kvm; |
106 |
|
|
107 |
|
$SIG{CHLD} = 'IGNORE'; |
108 |
|
|
109 |
|
sub start_stop { |
110 |
|
my $daemon = shift; |
111 |
|
my $pid = $pids->{$daemon} || 'not started'; |
112 |
|
|
113 |
|
warn "start_stop $daemon $pid\n"; |
114 |
|
|
115 |
|
if ( $pid =~ m{^\d+$} ) { |
116 |
|
my $pstree = `pstree -p $pid`; |
117 |
|
my @pids = $pstree =~ m{\((\d+)\)}g; |
118 |
|
warn "pstree $pstree pids ",dump( @pids ); |
119 |
|
kill 1, $_ foreach reverse @pids; |
120 |
|
$pids->{$daemon} = 'stopped'; |
121 |
|
return qq|$daemon pid $pid stopped|; |
122 |
|
} else { |
123 |
|
if ( $pid = fork ) { |
124 |
|
# parent |
125 |
|
$pids->{$daemon} = $pid; |
126 |
|
warn "forked $daemon $pid\n"; |
127 |
|
return qq|$daemon pid $pid started|; |
128 |
|
} elsif ( defined $pid ) { |
129 |
|
# child |
130 |
|
my $invoke = 'start'; |
131 |
|
$invoke = $1 if $daemon =~ s{/(.+)}{}; |
132 |
|
if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) { |
133 |
|
my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}"; |
134 |
|
warn "exec $exec"; |
135 |
|
exec "xterm -T $daemon -n $daemon -e $exec"; |
136 |
|
} else { |
137 |
|
my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')'; |
138 |
|
warn "eval $eval"; |
139 |
|
eval $eval; |
140 |
|
warn "can't start $daemon: $@" if $@; |
141 |
|
} |
142 |
|
exit; |
143 |
|
} else { |
144 |
|
die "fork error $!"; |
145 |
|
} |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
|
my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|; |
150 |
|
my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|; |
151 |
|
|
152 |
sub get_request { |
sub get_request { |
153 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
154 |
|
|
155 |
warn "get_request $client $path $param"; |
server->refresh; |
156 |
|
|
157 |
|
warn "get_request $path ", $param ? dump( $param ) : '', "\n"; |
158 |
|
|
159 |
if ( my $found = static( $client,$path ) ) { |
if ( my $found = static( $client,$path ) ) { |
160 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
161 |
} elsif ( $path eq '/' ) { |
} elsif ( $path eq '/' ) { |
162 |
print $client $ok, |
|
163 |
html::table( 2, |
my @rows = ( |
164 |
'pid', $$, |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
|
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
|
165 |
); |
); |
166 |
|
|
167 |
|
my $debug_proc = ''; |
168 |
|
|
169 |
|
warn 'pids: ', dump( $pids ) if $debug; |
170 |
|
foreach my $name ( sort keys %$pids ) { |
171 |
|
my $pid = $pids->{$name} || next; |
172 |
|
|
173 |
|
my $html = qq|<a href=/start_stop/$name>$pid</a>|; |
174 |
|
|
175 |
|
my $proc = "/proc/$pid/status"; |
176 |
|
|
177 |
|
if ( -e $proc ) { |
178 |
|
if ( $debug ) { |
179 |
|
$html .= qq| <a name=$pid href=#proc-$pid>?</a>|; |
180 |
|
|
181 |
|
$debug_proc |
182 |
|
.= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">| |
183 |
|
. read_file($proc) |
184 |
|
. qq|</pre>| |
185 |
|
; |
186 |
|
} |
187 |
|
|
188 |
|
if ( $name->can('start_fork') ) { |
189 |
|
$html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork; |
190 |
|
} |
191 |
|
|
192 |
|
if ( $name->can('actions') ) { |
193 |
|
$html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions; |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
|
push @rows, ( $name => $html ); |
198 |
|
} |
199 |
|
|
200 |
|
my $below_table = ''; |
201 |
|
|
202 |
|
warn 'static_pids: ', dump( $static_pids ) if $debug; |
203 |
|
foreach my $pid ( keys %$static_pids ) { |
204 |
|
my $path = $static_pids->{$pid}; |
205 |
|
if ( -d "/proc/$pid" ) { |
206 |
|
push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| ); |
207 |
|
} elsif ( $param->{clean_completed_downloads} ) { |
208 |
|
delete $static_pids->{$pid} |
209 |
|
} else { |
210 |
|
push @rows, ( $path => "$pid competed" ); |
211 |
|
$below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|; |
212 |
|
} |
213 |
|
} |
214 |
|
|
215 |
|
print $client $ok |
216 |
|
, menu() |
217 |
|
, html::table( 2, @rows ) |
218 |
|
, $below_table |
219 |
|
, html::tabs( log::mac_changes ) |
220 |
|
, $debug_proc |
221 |
|
; |
222 |
|
|
223 |
|
} elsif ( $path =~ m{^/server} ) { |
224 |
|
print $client $ok |
225 |
|
, menu() |
226 |
|
, html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) ) |
227 |
|
; |
228 |
|
} elsif ( $path =~ m{^/client} ) { |
229 |
|
my $ip = $client->peerhost; |
230 |
|
my $hostname = server::shared( "hostname/$ip", $param->{hostname} ); |
231 |
|
my $deploy = server::shared( "deploy/$ip", $param->{deploy} ); |
232 |
|
print $client $ok |
233 |
|
, menu() |
234 |
|
, qq|<form method=get>| |
235 |
|
, html::table( 2, |
236 |
|
'ip' => $ip, |
237 |
|
'hostname' => qq|<input type=text name=hostname value=$hostname>|, |
238 |
|
'deploy' => html::select( 'deploy', $deploy, config::available ), |
239 |
|
) |
240 |
|
, qq|<input type=submit value=change></form><pre>| |
241 |
|
, config::for_ip( $ip ) |
242 |
|
, qq|</pre>| |
243 |
|
; |
244 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
245 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
246 |
warn $@ if $@; |
warn $@ if $@; |
247 |
print $client qq|HTTP/1.1 302 Found\r\nLocation: $url\r\nContent-type: text/html\r\n\r\n<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>|; |
248 |
|
server::debug( $debug ) if $1 eq 'debug'; |
249 |
|
} elsif ( $path =~ m{^/start_stop/(\S+)} ) { |
250 |
|
print $client $redirect, start_stop($1); |
251 |
|
} elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) { |
252 |
|
$1->$2(); |
253 |
|
print $client $redirect; |
254 |
|
} elsif ( $path =~ m{^/kill/static/(\d+)} ) { |
255 |
|
print $client $redirect; |
256 |
|
kill 1, $1 || kill 9, $2 && warn "killed $1"; |
257 |
|
} elsif ( $path eq '/exit' ) { |
258 |
|
# DESTROY; |
259 |
|
exit 0; |
260 |
} elsif ( $path =~ m{/boot} ) { |
} elsif ( $path =~ m{/boot} ) { |
261 |
print $client qq{$ok |
print $client qq{$ok |
262 |
#!gpxe |
#!gpxe |
271 |
} |
} |
272 |
|
|
273 |
} |
} |
274 |
|
|
275 |
|
use browser; |
276 |
|
use network; |
277 |
|
|
278 |
sub start { |
sub start { |
279 |
|
|
280 |
|
warn 'tap ', network::tap(); |
281 |
|
|
282 |
my $server = IO::Socket::INET->new( |
my $server = IO::Socket::INET->new( |
283 |
Proto => 'tcp', |
Proto => 'tcp', |
284 |
LocalPort => $httpd::port, |
LocalPort => $httpd::port, |
288 |
|
|
289 |
print "url $url\n"; |
print "url $url\n"; |
290 |
|
|
291 |
system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &"; |
start_stop 'browser', $url; |
292 |
|
start_stop 'dhcpd'; |
293 |
|
start_stop 'tftpd'; |
294 |
|
start_stop 'dnsd'; |
295 |
|
start_stop 'kvm'; |
296 |
|
|
297 |
while (my $client = $server->accept()) { |
while (1) { |
298 |
$client->autoflush(1); |
my $client = $server->accept() || next; # ALARM trickle us |
299 |
my $request = <$client>; |
my $request = <$client>; |
300 |
|
|
301 |
warn "request $request\n" if $debug; |
warn "request $request\n" if $debug; |
302 |
|
|
|
Module::Refresh->refresh; |
|
|
|
|
303 |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { |
304 |
my $path = $1; |
my $path = $1; |
305 |
my $param; |
my $param; |
310 |
} |
} |
311 |
warn "param: ",dump( $param ) if $debug; |
warn "param: ",dump( $param ) if $debug; |
312 |
} |
} |
|
warn "path $path param: ",dump( $param ); |
|
313 |
get_request $client, $path, $param; |
get_request $client, $path, $param; |
314 |
} else { |
} else { |
315 |
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"; |
316 |
warn "500 $request"; |
warn "500 $request"; |
317 |
} |
} |
318 |
|
|
319 |
print $client qq{ |
print $client menu() if $client->connected; |
|
<div style="font-size: 80%; color: #888"> |
|
|
<a href="">reload</a> |
|
|
<a href="/">index</a> |
|
|
</div> |
|
|
} if $client->connected; |
|
320 |
|
|
321 |
} |
} |
322 |
|
|
323 |
die "server died"; |
die "server died"; |
324 |
} |
} |
325 |
|
|
326 |
|
warn "loaded"; |
327 |
|
|
328 |
1; |
1; |