/[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 88 by dpavlin, Fri Jul 31 18:16:14 2009 UTC revision 105 by dpavlin, Fri Jul 31 23:22:05 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;                  syswrite $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;          exit;
101  }  }
# Line 94  sub start_stop { Line 111  sub start_stop {
111          my $daemon = shift;          my $daemon = shift;
112          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon};
113    
114          warn "start_stop $daemon $pid pids: ",dump( $pids );          warn "start_stop $daemon $pid\n";
115    
116          if ( $pid =~ m{^\d+$} ) {          if ( $pid =~ m{^\d+$} ) {
117                  my $pstree = `pstree -p $pid`;                  my $pstree = `pstree -p $pid`;
# Line 107  sub start_stop { Line 124  sub start_stop {
124                  if ( $pid = fork ) {                  if ( $pid = fork ) {
125                          # parent                          # parent
126                          $pids->{$daemon} = $pid;                          $pids->{$daemon} = $pid;
127                          warn "forked $daemon $pid";                          warn "forked $daemon $pid\n";
128                          return qq|$daemon pid $pid started|;                          return qq|$daemon pid $pid started|;
129                  } elsif ( defined $pid ) {                  } elsif ( defined $pid ) {
130                          # child                          # child
# Line 130  my $redirect = qq|HTTP/1.1 302 Found\r\n Line 147  my $redirect = qq|HTTP/1.1 302 Found\r\n
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 );          warn "get_request $path ", $param ? dump( $param ) : '', "\n";
151    
152          if ( my $found = static( $client,$path ) ) {          if ( my $found = static( $client,$path ) ) {
153                  warn "static $found" if $debug;                  warn "static $found" if $debug;
# Line 147  sub get_request { Line 164  sub get_request {
164                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
165                  );                  );
166    
167                  my $debug_proc;                  my $debug_proc = '';
168    
169                  warn 'pids: ', dump( $pids ) if $debug;                  warn 'pids: ', dump( $pids ) if $debug;
170                  foreach my $name ( sort keys %$pids ) {                  foreach my $name ( sort keys %$pids ) {
171                          my $pid = $pids->{$name} || next;                          my $pid = $pids->{$name} || next;
172    
173                          my $html = qq|<a href=/$name>$pid</a>|;                          my $html = qq|<a href=/start_stop/$name>$pid</a>|;
174    
175                          my $proc = "/proc/$pid/status";                          my $proc = "/proc/$pid/status";
176    
# Line 180  sub get_request { Line 197  sub get_request {
197                          push @rows, ( $name => $html );                          push @rows, ( $name => $html );
198                  }                  }
199    
200                    my $below_table = '';
201    
202                  warn 'static_pids: ', dump( $static_pids ) if $debug;                  warn 'static_pids: ', dump( $static_pids ) if $debug;
203                  foreach my $pid ( keys %$static_pids ) {                  foreach my $pid ( keys %$static_pids ) {
204                          my $path = $static_pids->{$pid};                          my $path = $static_pids->{$pid};
205                          if ( -d "/proc/$pid" ) {                          if ( -d "/proc/$pid" ) {
206                                  push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );                                  push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
207                            } elsif ( $param->{clean_completed_downloads} ) {
208                                    delete $static_pids->{$pid}
209                          } else {                          } else {
210                                  push @rows, ( $path => "$pid competed" );                                  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                  print $client $ok
216                          , html::table( 2, @rows )                          , html::table( 2, @rows )
217                            , $below_table
218                          , html::tabs( log::mac_changes )                          , html::tabs( log::mac_changes )
219                          , $debug_proc                          , $debug_proc
220                          ;                          ;
# Line 201  sub get_request { Line 224  sub get_request {
224                  warn $@ if $@;                  warn $@ if $@;
225                  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>|;
226                  server::debug( $debug ) if $1 eq 'debug';                  server::debug( $debug ) if $1 eq 'debug';
227          } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) {          } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
228                  print $client $redirect, start_stop($1);                  print $client $redirect, start_stop($1);
229          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
230                  $1->$2();                  $1->$2();
# Line 262  sub start { Line 285  sub start {
285                                  }                                  }
286                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
287                          }                          }
                         warn "path $path param: ",dump( $param );  
288                          get_request $client, $path, $param;                          get_request $client, $path, $param;
289                  } else {                  } else {
290                          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.88  
changed lines
  Added in v.105

  ViewVC Help
Powered by ViewVC 1.1.26