/[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

Diff of /lib/PXElator/httpd.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 53 by dpavlin, Thu Jul 30 12:10:53 2009 UTC revision 153 by dpavlin, Wed Aug 5 23:22:17 2009 UTC
# Line 17  use Carp qw/confess/; Line 17  use Carp qw/confess/;
17  use File::Slurp;  use File::Slurp;
18  #use JSON;  #use JSON;
19  use IO::Socket::INET;  use IO::Socket::INET;
 use Module::Refresh;  
 use Parallel::ForkManager;  
20    
21  our $port = 7777;  sub menu {qq{
 our $debug = 1;  
22    
23  my $pm = Parallel::ForkManager->new(10);  <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 $screen_pid;  our $port = 7777;
43    
44  use server;  use server;
45  our $url = "http://$server::ip:$port/";  our $debug = server::debug;
46    our $url = "http://$server::ip:$port";
47    
48  use html;  use html;
49    our $static_pids;
50    use progress_bar;
51    use config;
52    
53  sub static {  sub static {
54          my ($client,$path) = @_;          my ($client,$path) = @_;
55    
56          $path = "tftp/$path";          my $full = "$server::base_dir/tftp/$path";
57    
58            return if ! -f $full;
59    
60          if ( ! -e $path ||  -d $path ) {          if ( my $pid = fork ) {
61                  print $client "HTTP/1.0 404 $path not found\r\n";                  # parent
62                  return;                  close($client);
63                    print "http static child $pid\n";
64                    $static_pids->{$pid} = $path;
65                    return 1;
66          }          }
67    
68          my $type = 'text/plain';          my $type = 'application/octet-stream';
69          $type = 'text/html' if $path =~ m{\.htm};          $type = 'text/html' if $path =~ m{\.htm};
70          $type = 'application/javascript' if $path =~ m{\.js};          $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: ", -s $path,"\r\n\r\n";          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
76          open(my $html, $path);  
77          while(<$html>) {          open(my $fh, $full);
78                  print $client $_;  
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($html);          close($fh);
94            close($client);
95    
96          return $path;          print STDERR "\n";
 }  
97    
98  my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";          warn "exit static child";
99    
100            exit(0);
101    }
102    
103  use boolean;  use boolean;
104    
105  use screen;  use screen;
106    use kvm;
107    
108    $SIG{CHLD} = 'IGNORE';
109    
110    sub start_stop {
111            my $daemon = shift;
112            my $pid = $pids->{$daemon} || 'not started';
113    
114            warn "start_stop $daemon $pid\n";
115    
116            if ( $pid =~ m{^\d+$} ) {
117                    my $pstree = `pstree -p $pid`;
118                    my @pids = $pstree =~ m{\((\d+)\)}g;
119                    warn "pstree $pstree pids ",dump( @pids );
120                    kill 1, $_ foreach reverse @pids;
121                    $pids->{$daemon} = 'stopped';
122                    return qq|$daemon pid $pid stopped|;
123            } else {
124                    if ( $pid = fork ) {
125                            # parent
126                            $pids->{$daemon} = $pid;
127                            warn "forked $daemon $pid\n";
128                            return qq|$daemon pid $pid started|;
129                    } elsif ( defined $pid ) {
130                            # child
131                            my $invoke = 'start';
132                            $invoke = $1 if $daemon =~ s{/(.+)}{};
133                            my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
134                            warn "eval $eval";
135                            eval $eval;
136                            warn "can't start $daemon: $@" if $@;
137                            exit;
138                    } else {
139                            die "fork error $!";
140                    }
141            }
142    }
143    
144    my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
145    my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
146    
147  sub get_request {  sub get_request {
148          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
149    
150          warn "get_request $client $path ",dump( $param );          server->refresh;
151    
152            warn "get_request $path ", $param ? dump( $param ) : '', "\n";
153    
154          if ( my $found = static( $client,$path ) ) {          if ( my $found = static( $client,$path ) ) {
155                  warn "static $found" if $debug;                  warn "static $found" if $debug;
156          } elsif ( $path eq '/' ) {          } elsif ( $path eq '/' ) {
157    
158                  my $screen = $screen_pid ? qq|stop <tt>$screen_pid</tt>| : 'start';                  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                  print $client $ok,                  my @rows = (
163                  html::table( 2,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
                         'pid',   $$,  
                         'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,  
                         'screen', qq|<a href=/screen>$screen</a>|,  
164                  );                  );
165    
166          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {                  my $debug_proc = '';
167                  eval 'our $' . $1 . ' = ' . $2;  
168                  warn $@ if $@;                  warn 'pids: ', dump( $pids ) if $debug;
169                  print $client qq|HTTP/1.1 302 Found\r\nLocation: $url\r\nContent-type: text/html\r\n\r\n<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;                  foreach my $name ( sort keys %$pids ) {
170          } elsif ( $path =~ m{^/screen} ) {                          my $pid = $pids->{$name} || next;
171    
172                            my $html = qq|<a href=/start_stop/$name>$pid</a>|;
173    
174                            my $proc = "/proc/$pid/status";
175    
176                            if ( -e $proc ) {
177                                    if ( $debug ) {
178                                            $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
179    
180                                            $debug_proc
181                                                    .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
182                                                    .  read_file($proc)
183                                                    .  qq|</pre>|
184                                                    ;
185                                    }
186    
187                                    if ( $name->can('start_fork') ) {
188                                            $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
189                                    }
190    
191                  if ( $screen_pid ) {                                  if ( $name->can('actions') ) {
192                          if ( ! kill $screen_pid ) {                                          $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
                                 warn "kill -9 $screen_pid";  
                                 if ( kill 9, $screen_pid ) {  
                                         print $client $ok, qq|screen pid $screen_pid stopped|;  
                                         $screen_pid = 0;  
                                 } else {  
                                         print $client $ok, qq|can't stop screen $screen_pid: $!|;  
193                                  }                                  }
194                          }                          }
195                  } else {  
196                          if ( $screen_pid = $pm->start ) {                          push @rows, ( $name => $html );
197                                  print $client $ok, qq|screen started pid $screen_pid|;                  }
198    
199                    my $below_table = '';
200    
201                    warn 'static_pids: ', dump( $static_pids ) if $debug;
202                    foreach my $pid ( keys %$static_pids ) {
203                            my $path = $static_pids->{$pid};
204                            if ( -d "/proc/$pid" ) {
205                                    push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
206                            } elsif ( $param->{clean_completed_downloads} ) {
207                                    delete $static_pids->{$pid}
208                          } else {                          } else {
209                                  screen::start;                                  push @rows, ( $path => "$pid competed" );
210                                  $pm->finish;                                  $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
211                          }                          }
212                  }                  }
213    
214                    print $client $ok
215                            , menu()
216                            , html::table( 2, @rows )
217                            , $below_table
218                            , html::tabs( log::mac_changes )
219                            , $debug_proc
220                            ;
221    
222            } elsif ( $path =~ m{^/server} ) {
223                    print $client $ok
224                            , menu()
225                            , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )
226                            ;
227            } elsif ( $path =~ m{^/client} ) {
228                    my $ip = $client->peerhost;
229                    my $hostname = server::shared( "hostname/$ip", $param->{hostname} );
230                    my $deploy   = server::shared( "deploy/$ip",   $param->{deploy}   );
231                    print $client $ok
232                            , menu()
233                            , qq|<form method=get>|
234                            , html::table( 2,
235                                    'ip' => $ip,
236                                    'hostname' => qq|<input type=text name=hostname value=$hostname>|,
237                                    'deploy' => html::select( 'deploy', $deploy, config::available ),
238                            )
239                            , qq|<input type=submit value=change></form><pre>|
240                            , config::for_ip( $ip )
241                            , qq|</pre>|
242                            ;
243            } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
244                    eval 'our $' . $1 . ' = ' . $2;
245                    warn $@ if $@;
246                    print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
247                    server::debug( $debug ) if $1 eq 'debug';
248            } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
249                    print $client $redirect, start_stop($1);
250            } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
251                    $1->$2();
252                    print $client $redirect;
253            } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
254                    print $client $redirect;
255                    kill 1, $1 || kill 9, $2 && warn "killed $1";
256            } elsif ( $path eq '/exit' ) {
257    #               DESTROY;
258                    exit 0;
259          } elsif ( $path =~ m{/boot} ) {          } elsif ( $path =~ m{/boot} ) {
260                  print $client qq{$ok                  print $client qq{$ok
261  #!gpxe  #!gpxe
# Line 119  chain http://$server::ip:$httpd::port/ Line 271  chain http://$server::ip:$httpd::port/
271    
272  }  }
273    
274    use browser;
275    use network;
276    
277  sub start {  sub start {
278    
279            warn 'tap ', network::tap();
280    
281          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
282                          Proto     => 'tcp',                          Proto     => 'tcp',
283                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
# Line 130  sub start { Line 287  sub start {
287    
288          print "url $url\n";          print "url $url\n";
289    
290          system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";          start_stop 'browser', $url;
291            start_stop 'screen';
292            start_stop 'kvm';
293    
294          while (my $client = $server->accept()) {          while (1) {
295                  $client->autoflush(1);                  my $client = $server->accept() || next; # ALARM trickle us
296                  my $request = <$client>;                  my $request = <$client>;
297    
298                  warn "request $request\n" if $debug;                  warn "request $request\n" if $debug;
299    
                 Module::Refresh->refresh;  
   
300                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
301                          my $path = $1;                          my $path = $1;
302                          my $param;                          my $param;
# Line 150  sub start { Line 307  sub start {
307                                  }                                  }
308                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
309                          }                          }
                         warn "path $path param: ",dump( $param );  
310                          get_request $client, $path, $param;                          get_request $client, $path, $param;
311                  } else {                  } else {
312                          print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";                          print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
313                          warn "500 $request";                          warn "500 $request";
314                  }                  }
315    
316                  print $client qq{                  print $client menu() if $client->connected;
                 <div style="font-size: 80%; color: #888">  
                 <a href="">reload</a>  
                 <a href="/">index</a>  
                 </div>  
                 } if $client->connected;  
317    
318          }          }
319    

Legend:
Removed from v.53  
changed lines
  Added in v.153

  ViewVC Help
Powered by ViewVC 1.1.26