864684ae505c814adf6d99e8b17b4bb50ca07d5a
[pxelator] / lib / PXElator / httpd.pm
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
53 sub static {
54         my ($client,$path) = @_;
55
56         my $full = "$server::base_dir/tftp/$path";
57
58         return if ! -f $full;
59
60         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 = 'application/octet-stream';
69         $type = 'text/html' if $path =~ m{\.htm};
70         $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         open(my $fh, $full);
78
79         my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
80         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($fh);
94         close($client);
95
96         print STDERR "\n";
97
98         warn "exit static child";
99
100         exit(0);
101 }
102
103 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 {
153         my ( $client, $path, $param ) = @_;
154
155         server->refresh;
156
157         warn "get_request $path ", $param ? dump( $param ) : '', "\n";
158
159         if ( my $found = static( $client,$path ) ) {
160                 warn "static $found" if $debug;
161         } elsif ( $path eq '/' ) {
162
163                 my @rows = (
164                         '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+)} ) {
245                 eval 'our $' . $1 . ' = ' . $2;
246                 warn $@ if $@;
247                 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} ) {
261                 print $client qq{$ok
262 #!gpxe
263 imgfree
264 login
265 chain http://$server::ip:$httpd::port/
266
267                         };
268         } else {
269                 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
270                 warn "404 $path";
271         }
272
273 }
274
275 use browser;
276 use network;
277
278 sub start {
279
280         warn 'tap ', network::tap();
281
282         my $server = IO::Socket::INET->new(
283                         Proto     => 'tcp',
284                         LocalPort => $httpd::port,
285                         Listen    => SOMAXCONN,
286                         Reuse     => 1
287         ) || die "can't start server on $url: $!";
288
289         print "url $url\n";
290
291         start_stop 'browser', $url;
292         start_stop 'dhcpd';
293         start_stop 'tftpd';
294         start_stop 'dnsd';
295         start_stop 'kvm';
296
297         while (1) {
298                 my $client = $server->accept() || next; # ALARM trickle us
299                 my $request = <$client>;
300
301                 warn "request $request\n" if $debug;
302
303                 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
304                         my $path = $1;
305                         my $param;
306                         if ( $path =~ s{\?(.+)}{} ) {
307                                 foreach my $p ( split(/[&;]/, $1) ) {
308                                         my ($n,$v) = split(/=/, $p, 2);
309                                         $param->{$n} = $v;
310                                 }
311                                 warn "param: ",dump( $param ) if $debug;
312                         }
313                         get_request $client, $path, $param;
314                 } else {
315                         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";
317                 }
318
319                 print $client menu() if $client->connected;
320
321         }
322
323         die "server died";
324 }
325
326 warn "loaded";
327
328 1;