/[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 107 by dpavlin, Sat Aug 1 00:44:52 2009 UTC
# Line 18  use File::Slurp; Line 18  use File::Slurp;
18  #use JSON;  #use JSON;
19  use IO::Socket::INET;  use IO::Socket::INET;
20  use Module::Refresh;  use Module::Refresh;
 use Parallel::ForkManager;  
21    
22  our $port = 7777;  our $pids;
23  our $debug = 1;  $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
24    
25  my $pm = Parallel::ForkManager->new(10);  sub DESTROY {
26            warn "pids ",dump( $pids );
27            foreach ( values %$pids ) {
28                    warn "kill $_";
29                    kill 1,$_ || kill 9, $_;
30            }
31    }
32    
33  our $screen_pid;  our $port = 7777;
34    
35  use server;  use server;
36  our $url = "http://$server::ip:$port/";  our $debug = server::debug;
37    our $url = "http://$server::ip:$port";
38    
39  use html;  use html;
40    our $static_pids;
41    
42    use Time::HiRes qw/time/;
43    
44  sub static {  sub static {
45          my ($client,$path) = @_;          my ($client,$path) = @_;
46    
47          $path = "tftp/$path";          my $full = "$server::base_dir/tftp/$path";
48    
49            return if ! -f $full;
50    
51          if ( ! -e $path ||  -d $path ) {          if ( my $pid = fork ) {
52                  print $client "HTTP/1.0 404 $path not found\r\n";                  # parent
53                  return;                  close($client);
54                    print "http static child $pid\n";
55                    $static_pids->{$pid} = $path;
56                    return 1;
57          }          }
58    
59          my $type = 'text/plain';          my $type = 'application/octet-stream';
60          $type = 'text/html' if $path =~ m{\.htm};          $type = 'text/html' if $path =~ m{\.htm};
61          $type = 'application/javascript' if $path =~ m{\.js};          $type = 'application/javascript' if $path =~ m{\.js};
62            $type = 'text/plain' if $path =~ m{\.txt};
63    
64          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n";          my $size = -s $full || return;
65          open(my $html, $path);  
66          while(<$html>) {          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
67                  print $client $_;  
68            open(my $fh, $full);
69    
70            my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
71            my $buff;
72            my $pos = 0;
73    
74            STDERR->autoflush(1);
75            warn "static $path $type $size block: $block\n";
76    
77            my $start_t = time();
78            my $last_t = $start_t;
79    
80            while( my $len = read $fh, $buff, $block ) {
81                    print $client $buff;
82                    $client->flush;
83                    $pos += $len;
84                    my $t = time();
85                    next unless $t - $last_t > 0.75;
86                    $last_t = $t;
87                    my $speed = ( $pos ) / ( $t - $start_t );
88                    printf STDERR "%s %d/%d %.2f%% %.2f K/s ETA %.1fs   \r"
89                            , $path, $pos
90                            , $size, $pos * 100 / $size
91                            , $speed / 1024
92                            , ( $size - $pos ) / $speed
93                            ;
94          }          }
95          close($html);          close($fh);
96            close($client);
97    
98          return $path;          print STDERR "\n";
 }  
99    
100  my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";          warn "exit static child";
101    
102            exit(0);
103    }
104    
105  use boolean;  use boolean;
106    
107  use screen;  use screen;
108    use kvm;
109    
110    $SIG{CHLD} = 'IGNORE';
111    
112    sub start_stop {
113            my $daemon = shift;
114            my $pid = $pids->{$daemon};
115    
116            warn "start_stop $daemon $pid\n";
117    
118            if ( $pid =~ m{^\d+$} ) {
119                    my $pstree = `pstree -p $pid`;
120                    my @pids = $pstree =~ m{\((\d+)\)}g;
121                    warn "pstree $pstree pids ",dump( @pids );
122                    kill 1, $_ foreach reverse @pids;
123                    $pids->{$daemon} = 'stopped';
124                    return qq|$daemon pid $pid stopped|;
125            } else {
126                    if ( $pid = fork ) {
127                            # parent
128                            $pids->{$daemon} = $pid;
129                            warn "forked $daemon $pid\n";
130                            return qq|$daemon pid $pid started|;
131                    } elsif ( defined $pid ) {
132                            # child
133                            my $invoke = 'start';
134                            $invoke = $1 if $daemon =~ s{/(.+)}{};
135                            my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
136                            warn "eval $eval";
137                            eval $eval;
138                            warn "can't start $daemon: $@" if $@;
139                            exit;
140                    } else {
141                            die "fork error $!";
142                    }
143            }
144    }
145    
146    my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
147    my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
148    
149  sub get_request {  sub get_request {
150          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
151    
152          warn "get_request $client $path ",dump( $param );          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                    my @rows = (
163                            'ip',           html::tt( $server::ip ),
164                            'netmask',      html::tt( $server::netmask ),
165    
166                  print $client $ok,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
                 html::table( 2,  
                         'pid',   $$,  
                         'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,  
                         'screen', qq|<a href=/screen>$screen</a>|,  
167                  );                  );
168    
169          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {                  my $debug_proc = '';
170                  eval 'our $' . $1 . ' = ' . $2;  
171                  warn $@ if $@;                  warn 'pids: ', dump( $pids ) if $debug;
172                  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 ) {
173          } elsif ( $path =~ m{^/screen} ) {                          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 ( $screen_pid ) {                                  if ( $name->can('actions') ) {
195                          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: $!|;  
196                                  }                                  }
197                          }                          }
198                  } else {  
199                          if ( $screen_pid = $pm->start ) {                          push @rows, ( $name => $html );
200                                  print $client $ok, qq|screen started pid $screen_pid|;                  }
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 {                          } else {
212                                  screen::start;                                  push @rows, ( $path => "$pid competed" );
213                                  $pm->finish;                                  $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
214                          }                          }
215                  }                  }
216    
217                    print $client $ok
218                            , html::table( 2, @rows )
219                            , $below_table
220                            , html::tabs( log::mac_changes )
221                            , $debug_proc
222                            ;
223    
224            } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
225                    eval 'our $' . $1 . ' = ' . $2;
226                    warn $@ if $@;
227                    print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
228                    server::debug( $debug ) if $1 eq 'debug';
229            } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
230                    print $client $redirect, start_stop($1);
231            } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
232                    $1->$2();
233                    print $client $redirect;
234            } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
235                    print $client $redirect;
236                    kill 1, $1 || kill 9, $2 && warn "killed $1";
237            } elsif ( $path eq '/exit' ) {
238    #               DESTROY;
239                    exit 0;
240          } elsif ( $path =~ m{/boot} ) {          } elsif ( $path =~ m{/boot} ) {
241                  print $client qq{$ok                  print $client qq{$ok
242  #!gpxe  #!gpxe
# Line 119  chain http://$server::ip:$httpd::port/ Line 252  chain http://$server::ip:$httpd::port/
252    
253  }  }
254    
255    use browser;
256    
257  sub start {  sub start {
258    
259          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
# Line 130  sub start { Line 265  sub start {
265    
266          print "url $url\n";          print "url $url\n";
267    
268          system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";          start_stop 'browser', $url;
269            start_stop 'screen';
270            start_stop 'kvm';
271    
272          while (my $client = $server->accept()) {          while (my $client = $server->accept()) {
273                  $client->autoflush(1);                  $client->autoflush(1);
# Line 150  sub start { Line 287  sub start {
287                                  }                                  }
288                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
289                          }                          }
                         warn "path $path param: ",dump( $param );  
290                          get_request $client, $path, $param;                          get_request $client, $path, $param;
291                  } else {                  } else {
292                          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";
# Line 160  sub start { Line 296  sub start {
296                  print $client qq{                  print $client qq{
297                  <div style="font-size: 80%; color: #888">                  <div style="font-size: 80%; color: #888">
298                  <a href="">reload</a>                  <a href="">reload</a>
299                  <a href="/">index</a>                  <a href=/>index</a>
300                    <a href=/exit>exit</a>
301                  </div>                  </div>
302                  } if $client->connected;                  } if $client->connected;
303    

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

  ViewVC Help
Powered by ViewVC 1.1.26