/[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 71 by dpavlin, Thu Jul 30 22:55:08 2009 UTC revision 107 by dpavlin, Sat Aug 1 00:44:52 2009 UTC
# Line 39  our $url = "http://$server::ip:$port"; Line 39  our $url = "http://$server::ip:$port";
39  use html;  use html;
40  our $static_pids;  our $static_pids;
41    
42    use Time::HiRes qw/time/;
43    
44  sub static {  sub static {
45          my ($client,$path) = @_;          my ($client,$path) = @_;
46    
# Line 64  sub static { Line 66  sub static {
66          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\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";
67    
68          open(my $fh, $full);          open(my $fh, $full);
         print "static $path $type $size\n";  
69    
70          my $block = 8192;          my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
71          my $buff;          my $buff;
72          my $pos = 0;          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 ) {          while( my $len = read $fh, $buff, $block ) {
81                  print $client $buff;                  print $client $buff;
82                    $client->flush;
83                  $pos += $len;                  $pos += $len;
84                  printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;                  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($fh);          close($fh);
96          close($client);          close($client);
97    
98          print "$path $pos == $size OK\n";          print STDERR "\n";
99    
100          exit;          warn "exit static child";
101    
102            exit(0);
103  }  }
104    
105  use boolean;  use boolean;
# Line 94  sub start_stop { Line 113  sub start_stop {
113          my $daemon = shift;          my $daemon = shift;
114          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon};
115    
116          warn "start_stop $daemon $pid pids: ",dump( $pids );          warn "start_stop $daemon $pid\n";
117    
118          if ( $pid =~ m{^\d+$} ) {          if ( $pid =~ m{^\d+$} ) {
119                  warn "kill 9 $pid";                  my $pstree = `pstree -p $pid`;
120                  kill 9, $pid;                  my @pids = $pstree =~ m{\((\d+)\)}g;
121                    warn "pstree $pstree pids ",dump( @pids );
122                    kill 1, $_ foreach reverse @pids;
123                  $pids->{$daemon} = 'stopped';                  $pids->{$daemon} = 'stopped';
124                  return qq|$daemon pid $pid stopped|;                  return qq|$daemon pid $pid stopped|;
125          } else {          } else {
126                  if ( $pid = fork ) {                  if ( $pid = fork ) {
127                          # parent                          # parent
128                          $pids->{$daemon} = $pid;                          $pids->{$daemon} = $pid;
129                          warn "forked $daemon $pid";                          warn "forked $daemon $pid\n";
130                          return qq|$daemon pid $pid started|;                          return qq|$daemon pid $pid started|;
131                  } elsif ( defined $pid ) {                  } elsif ( defined $pid ) {
132                          # child                          # child
133                          my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')';                          my $invoke = 'start';
134                            $invoke = $1 if $daemon =~ s{/(.+)}{};
135                            my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
136                          warn "eval $eval";                          warn "eval $eval";
137                          eval $eval;                          eval $eval;
138                          warn "can't start $daemon: $@" if $@;                          warn "can't start $daemon: $@" if $@;
# Line 126  my $redirect = qq|HTTP/1.1 302 Found\r\n Line 149  my $redirect = qq|HTTP/1.1 302 Found\r\n
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;
# Line 143  sub get_request { Line 166  sub get_request {
166                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
167                  );                  );
168    
169                  my $debug_proc;                  my $debug_proc = '';
170    
171                  warn 'pids: ', dump( $pids );                  warn 'pids: ', dump( $pids ) if $debug;
172                  foreach my $name ( keys %$pids ) {                  foreach my $name ( sort keys %$pids ) {
173                          my $pid = $pids->{$name} || next;                          my $pid = $pids->{$name} || next;
174    
175                          my $html = qq|<a href=/$name>$pid</a>|;                          my $html = qq|<a href=/start_stop/$name>$pid</a>|;
176    
177                            my $proc = "/proc/$pid/status";
178    
179                          if ( $debug ) {                          if ( -e $proc ) {
180                                  $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;                                  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                                  my $proc = "/proc/$pid/status";                                  if ( $name->can('start_fork') ) {
191                                  $debug_proc                                          $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
192                                          .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|                                  }
193                                          .  read_file($proc)  
194                                          .  qq|</pre>|                                  if ( $name->can('actions') ) {
195                                          if -e $proc;                                          $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
196                                    }
197                          }                          }
198    
199                          push @rows, ( $name => $html );                          push @rows, ( $name => $html );
200                  }                  }
201    
202                  warn 'static_pids: ', dump( $static_pids );                  my $below_table = '';
203    
204                    warn 'static_pids: ', dump( $static_pids ) if $debug;
205                  foreach my $pid ( keys %$static_pids ) {                  foreach my $pid ( keys %$static_pids ) {
206                          my $path = $static_pids->{$pid};                          my $path = $static_pids->{$pid};
207                          if ( -d "/proc/$pid" ) {                          if ( -d "/proc/$pid" ) {
208                                  push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );                                  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                                  push @rows, ( $path => "$pid competed" );                                  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, html::table( 2, @rows ), $debug_proc;                  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+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
225                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
226                  warn $@ if $@;                  warn $@ if $@;
227                  print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;                  print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
228                  server::debug( $debug ) if $1 eq 'debug';                  server::debug( $debug ) if $1 eq 'debug';
229          } elsif ( $path =~ m{^/(screen|kvm)} ) {          } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
230                  print $client $redirect, start_stop($1);                  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+)} ) {          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
235                  print $client $redirect;                  print $client $redirect;
236                  kill 9, $1 && warn "killed $1";                  kill 1, $1 || kill 9, $2 && warn "killed $1";
237          } elsif ( $path eq '/exit' ) {          } elsif ( $path eq '/exit' ) {
238  #               DESTROY;  #               DESTROY;
239                  exit 0;                  exit 0;
# Line 240  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";

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

  ViewVC Help
Powered by ViewVC 1.1.26