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

  ViewVC Help
Powered by ViewVC 1.1.26