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