/[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 159 - (show annotations)
Thu Aug 6 15:40:42 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 7570 byte(s)
use clinet::conf instead of server::shared
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
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;
43
44 use server;
45 our $debug = server::debug;
46 our $url = "http://$server::ip:$port";
47
48 use html;
49 our $static_pids;
50 use progress_bar;
51 use config;
52 use client;
53 use log;
54
55 sub static {
56 my ($client,$path) = @_;
57
58 my $full = "$server::base_dir/tftp/$path";
59
60 return if ! -f $full;
61
62 if ( my $pid = fork ) {
63 # parent
64 close($client);
65 print "http static child $pid\n";
66 $static_pids->{$pid} = $path;
67 return 1;
68 }
69
70 my $type = 'application/octet-stream';
71 $type = 'text/html' if $path =~ m{\.htm};
72 $type = 'application/javascript' if $path =~ m{\.js};
73 $type = 'text/plain' if $path =~ m{\.txt};
74
75 my $size = -s $full || return;
76
77 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);
80
81 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
82 my $buff;
83 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 ) {
90 print $client $buff;
91 $client->flush;
92 $pos += $len;
93 progress_bar::tick( $path, $pos, $size );
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 kvm;
108
109 $SIG{CHLD} = 'IGNORE';
110
111 sub start_stop {
112 my $daemon = shift;
113 my $pid = $pids->{$daemon} || 'not started';
114
115 warn "start_stop $daemon $pid\n";
116
117 if ( $pid =~ m{^\d+$} ) {
118 my $pstree = `pstree -p $pid`;
119 my @pids = $pstree =~ m{\((\d+)\)}g;
120 warn "pstree $pstree pids ",dump( @pids );
121 kill 1, $_ foreach reverse @pids;
122 $pids->{$daemon} = 'stopped';
123 return qq|$daemon pid $pid stopped|;
124 } else {
125 if ( $pid = fork ) {
126 # parent
127 $pids->{$daemon} = $pid;
128 warn "forked $daemon $pid\n";
129 return qq|$daemon pid $pid started|;
130 } elsif ( defined $pid ) {
131 # child
132 my $invoke = 'start';
133 $invoke = $1 if $daemon =~ s{/(.+)}{};
134 if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) {
135 my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}";
136 warn "exec $exec";
137 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;
145 } else {
146 die "fork error $!";
147 }
148 }
149 }
150
151 my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
152 my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
153
154 sub get_request {
155 my ( $client, $path, $param ) = @_;
156
157 server->refresh;
158
159 warn "get_request $path ", $param ? dump( $param ) : '', "\n";
160
161 if ( my $found = static( $client,$path ) ) {
162 warn "static $found" if $debug;
163 } elsif ( $path eq '/' ) {
164
165 my @rows = (
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 , menu()
219 , html::table( 2, @rows )
220 , $below_table
221 , html::tabs( log::mac_changes )
222 , $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+)} ) {
247 eval 'our $' . $1 . ' = ' . $2;
248 warn $@ if $@;
249 print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
250 server::debug( $debug ) if $1 eq 'debug';
251 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
252 print $client $redirect, start_stop($1);
253 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
254 $1->$2();
255 print $client $redirect;
256 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
257 print $client $redirect;
258 kill 1, $1 || kill 9, $2 && warn "killed $1";
259 } elsif ( $path eq '/exit' ) {
260 # DESTROY;
261 exit 0;
262 } elsif ( $path =~ m{/boot} ) {
263 print $client qq{$ok
264 #!gpxe
265 imgfree
266 login
267 chain http://$server::ip:$httpd::port/
268
269 };
270 } else {
271 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
272 warn "404 $path";
273 }
274
275 }
276
277 use browser;
278 use network;
279
280 sub start {
281
282 warn 'tap ', network::tap();
283
284 my $server = IO::Socket::INET->new(
285 Proto => 'tcp',
286 LocalPort => $httpd::port,
287 Listen => SOMAXCONN,
288 Reuse => 1
289 ) || die "can't start server on $url: $!";
290
291 print "url $url\n";
292
293 start_stop 'browser', $url;
294 start_stop 'dhcpd';
295 start_stop 'tftpd';
296 start_stop 'dnsd';
297 start_stop 'kvm';
298
299 while (1) {
300 my $client = $server->accept() || next; # ALARM trickle us
301 my $request = <$client>;
302
303 warn "request $request\n" if $debug;
304
305 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
306 my $path = $1;
307 my $param;
308 if ( $path =~ s{\?(.+)}{} ) {
309 foreach my $p ( split(/[&;]/, $1) ) {
310 my ($n,$v) = split(/=/, $p, 2);
311 $param->{$n} = $v;
312 }
313 warn "param: ",dump( $param ) if $debug;
314 }
315 get_request $client, $path, $param;
316 } else {
317 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";
319 }
320
321 print $client menu() if $client->connected;
322
323 }
324
325 die "server died";
326 }
327
328 warn "loaded";
329
330 1;

  ViewVC Help
Powered by ViewVC 1.1.26