/[pxelator]/lib/PXElator/httpd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/PXElator/httpd.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 107 - (show annotations)
Sat Aug 1 00:44:52 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 7071 byte(s)
flush data to client and display estimated time to finish
1 package httpd;
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 =head1 httpd
8
9 Start with:
10
11 perl -Ilib/PXElator -Mhttpd -e httpd::start
12
13 =cut
14
15 use Data::Dump qw/dump/;
16 use Carp qw/confess/;
17 use File::Slurp;
18 #use JSON;
19 use IO::Socket::INET;
20 use Module::Refresh;
21
22 our $pids;
23 $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
24
25 sub DESTROY {
26 warn "pids ",dump( $pids );
27 foreach ( values %$pids ) {
28 warn "kill $_";
29 kill 1,$_ || kill 9, $_;
30 }
31 }
32
33 our $port = 7777;
34
35 use server;
36 our $debug = server::debug;
37 our $url = "http://$server::ip:$port";
38
39 use html;
40 our $static_pids;
41
42 use Time::HiRes qw/time/;
43
44 sub static {
45 my ($client,$path) = @_;
46
47 my $full = "$server::base_dir/tftp/$path";
48
49 return if ! -f $full;
50
51 if ( my $pid = fork ) {
52 # parent
53 close($client);
54 print "http static child $pid\n";
55 $static_pids->{$pid} = $path;
56 return 1;
57 }
58
59 my $type = 'application/octet-stream';
60 $type = 'text/html' if $path =~ m{\.htm};
61 $type = 'application/javascript' if $path =~ m{\.js};
62 $type = 'text/plain' if $path =~ m{\.txt};
63
64 my $size = -s $full || return;
65
66 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
67
68 open(my $fh, $full);
69
70 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
71 my $buff;
72 my $pos = 0;
73
74 STDERR->autoflush(1);
75 warn "static $path $type $size block: $block\n";
76
77 my $start_t = time();
78 my $last_t = $start_t;
79
80 while( my $len = read $fh, $buff, $block ) {
81 print $client $buff;
82 $client->flush;
83 $pos += $len;
84 my $t = time();
85 next unless $t - $last_t > 0.75;
86 $last_t = $t;
87 my $speed = ( $pos ) / ( $t - $start_t );
88 printf STDERR "%s %d/%d %.2f%% %.2f K/s ETA %.1fs \r"
89 , $path, $pos
90 , $size, $pos * 100 / $size
91 , $speed / 1024
92 , ( $size - $pos ) / $speed
93 ;
94 }
95 close($fh);
96 close($client);
97
98 print STDERR "\n";
99
100 warn "exit static child";
101
102 exit(0);
103 }
104
105 use boolean;
106
107 use screen;
108 use kvm;
109
110 $SIG{CHLD} = 'IGNORE';
111
112 sub start_stop {
113 my $daemon = shift;
114 my $pid = $pids->{$daemon};
115
116 warn "start_stop $daemon $pid\n";
117
118 if ( $pid =~ m{^\d+$} ) {
119 my $pstree = `pstree -p $pid`;
120 my @pids = $pstree =~ m{\((\d+)\)}g;
121 warn "pstree $pstree pids ",dump( @pids );
122 kill 1, $_ foreach reverse @pids;
123 $pids->{$daemon} = 'stopped';
124 return qq|$daemon pid $pid stopped|;
125 } else {
126 if ( $pid = fork ) {
127 # parent
128 $pids->{$daemon} = $pid;
129 warn "forked $daemon $pid\n";
130 return qq|$daemon pid $pid started|;
131 } elsif ( defined $pid ) {
132 # child
133 my $invoke = 'start';
134 $invoke = $1 if $daemon =~ s{/(.+)}{};
135 my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
136 warn "eval $eval";
137 eval $eval;
138 warn "can't start $daemon: $@" if $@;
139 exit;
140 } else {
141 die "fork error $!";
142 }
143 }
144 }
145
146 my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
147 my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
148
149 sub get_request {
150 my ( $client, $path, $param ) = @_;
151
152 warn "get_request $path ", $param ? dump( $param ) : '', "\n";
153
154 if ( my $found = static( $client,$path ) ) {
155 warn "static $found" if $debug;
156 } elsif ( $path eq '/' ) {
157
158 my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start';
159 my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| :
160 $pids->{screen} ? qq|start| : qq|start screen first|;
161
162 my @rows = (
163 'ip', html::tt( $server::ip ),
164 'netmask', html::tt( $server::netmask ),
165
166 'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
167 );
168
169 my $debug_proc = '';
170
171 warn 'pids: ', dump( $pids ) if $debug;
172 foreach my $name ( sort keys %$pids ) {
173 my $pid = $pids->{$name} || next;
174
175 my $html = qq|<a href=/start_stop/$name>$pid</a>|;
176
177 my $proc = "/proc/$pid/status";
178
179 if ( -e $proc ) {
180 if ( $debug ) {
181 $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
182
183 $debug_proc
184 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
185 . read_file($proc)
186 . qq|</pre>|
187 ;
188 }
189
190 if ( $name->can('start_fork') ) {
191 $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
192 }
193
194 if ( $name->can('actions') ) {
195 $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
196 }
197 }
198
199 push @rows, ( $name => $html );
200 }
201
202 my $below_table = '';
203
204 warn 'static_pids: ', dump( $static_pids ) if $debug;
205 foreach my $pid ( keys %$static_pids ) {
206 my $path = $static_pids->{$pid};
207 if ( -d "/proc/$pid" ) {
208 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
209 } elsif ( $param->{clean_completed_downloads} ) {
210 delete $static_pids->{$pid}
211 } else {
212 push @rows, ( $path => "$pid competed" );
213 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
214 }
215 }
216
217 print $client $ok
218 , html::table( 2, @rows )
219 , $below_table
220 , html::tabs( log::mac_changes )
221 , $debug_proc
222 ;
223
224 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
225 eval 'our $' . $1 . ' = ' . $2;
226 warn $@ if $@;
227 print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
228 server::debug( $debug ) if $1 eq 'debug';
229 } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
230 print $client $redirect, start_stop($1);
231 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
232 $1->$2();
233 print $client $redirect;
234 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
235 print $client $redirect;
236 kill 1, $1 || kill 9, $2 && warn "killed $1";
237 } elsif ( $path eq '/exit' ) {
238 # DESTROY;
239 exit 0;
240 } elsif ( $path =~ m{/boot} ) {
241 print $client qq{$ok
242 #!gpxe
243 imgfree
244 login
245 chain http://$server::ip:$httpd::port/
246
247 };
248 } else {
249 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
250 warn "404 $path";
251 }
252
253 }
254
255 use browser;
256
257 sub start {
258
259 my $server = IO::Socket::INET->new(
260 Proto => 'tcp',
261 LocalPort => $httpd::port,
262 Listen => SOMAXCONN,
263 Reuse => 1
264 ) || die "can't start server on $url: $!";
265
266 print "url $url\n";
267
268 start_stop 'browser', $url;
269 start_stop 'screen';
270 start_stop 'kvm';
271
272 while (my $client = $server->accept()) {
273 $client->autoflush(1);
274 my $request = <$client>;
275
276 warn "request $request\n" if $debug;
277
278 Module::Refresh->refresh;
279
280 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
281 my $path = $1;
282 my $param;
283 if ( $path =~ s{\?(.+)}{} ) {
284 foreach my $p ( split(/[&;]/, $1) ) {
285 my ($n,$v) = split(/=/, $p, 2);
286 $param->{$n} = $v;
287 }
288 warn "param: ",dump( $param ) if $debug;
289 }
290 get_request $client, $path, $param;
291 } else {
292 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
293 warn "500 $request";
294 }
295
296 print $client qq{
297 <div style="font-size: 80%; color: #888">
298 <a href="">reload</a>
299 <a href=/>index</a>
300 <a href=/exit>exit</a>
301 </div>
302 } if $client->connected;
303
304 }
305
306 die "server died";
307 }
308
309 warn "loaded";
310
311 1;

  ViewVC Help
Powered by ViewVC 1.1.26