/[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 67 by dpavlin, Thu Jul 30 21:31:30 2009 UTC revision 104 by dpavlin, Fri Jul 31 22:52:22 2009 UTC
# Line 19  use File::Slurp; Line 19  use File::Slurp;
19  use IO::Socket::INET;  use IO::Socket::INET;
20  use Module::Refresh;  use Module::Refresh;
21    
22  our $pids = { httpd => $$ };  our $pids;
23    $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
24    
25  sub DESTROY {  sub DESTROY {
26          warn "pids ",dump( $pids );          warn "pids ",dump( $pids );
# Line 36  our $debug = server::debug; Line 37  our $debug = server::debug;
37  our $url = "http://$server::ip:$port";  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) = @_;
# Line 44  sub static { Line 48  sub static {
48    
49          return if ! -f $full;          return if ! -f $full;
50    
51            my $start_t = time();
52    
53            if ( my $pid = fork ) {
54                    # parent
55                    close($client);
56                    print "http static child $pid\n";
57                    $static_pids->{$pid} = $path;
58                    return 1;
59            }
60    
61          my $type = 'application/octet-stream';          my $type = 'application/octet-stream';
62          $type = 'text/html' if $path =~ m{\.htm};          $type = 'text/html' if $path =~ m{\.htm};
63          $type = 'application/javascript' if $path =~ m{\.js};          $type = 'application/javascript' if $path =~ m{\.js};
# Line 51  sub static { Line 65  sub static {
65    
66          my $size = -s $full || return;          my $size = -s $full || return;
67    
68            $client->autoflush(1);
69    
70          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";
71    
72          open(my $fh, $full);          open(my $fh, $full);
         print "static $path $type $size\n";  
73    
74          my $block = 8192;          my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
75          my $buff;          my $buff;
76          my $pos = 0;          my $pos = 0;
77    
78            print "static $path $type $size block: $block\n";
79    
80          while( my $len = read $fh, $buff, $block ) {          while( my $len = read $fh, $buff, $block ) {
81                  print $client $buff;                  print $client $buff;
82                  $pos += $len;                  $pos += $len;
83                  printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;                  printf "%s %d/%d %.2f%% %.2f K/s\r"
84                            , $path, $pos
85                            , $size, $pos * 100 / $size
86                            , ( $pos / 1024 ) / ( time() - $start_t )
87                            ;
88          }          }
89          close($fh);          close($fh);
90          close($client);          close($client);
91    
92          print "$path $pos == $size OK\n";          print "\n";
93    
94          return $path;          exit;
95  }  }
96    
97  use boolean;  use boolean;
# Line 84  sub start_stop { Line 105  sub start_stop {
105          my $daemon = shift;          my $daemon = shift;
106          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon};
107    
108          warn "start_stop $daemon $pid pids: ",dump( $pids );          warn "start_stop $daemon $pid\n";
109    
110          if ( $pid =~ m{^\d+$} ) {          if ( $pid =~ m{^\d+$} ) {
111                  warn "kill 9 $pid";                  my $pstree = `pstree -p $pid`;
112                  kill 9, $pid;                  my @pids = $pstree =~ m{\((\d+)\)}g;
113                    warn "pstree $pstree pids ",dump( @pids );
114                    kill 1, $_ foreach reverse @pids;
115                  $pids->{$daemon} = 'stopped';                  $pids->{$daemon} = 'stopped';
116                  return qq|$daemon pid $pid stopped|;                  return qq|$daemon pid $pid stopped|;
117          } else {          } else {
118                  if ( $pid = fork ) {                  if ( $pid = fork ) {
119                          # parent                          # parent
120                          $pids->{$daemon} = $pid;                          $pids->{$daemon} = $pid;
121                          warn "forked $daemon $pid";                          warn "forked $daemon $pid\n";
122                          return qq|$daemon pid $pid started|;                          return qq|$daemon pid $pid started|;
123                  } elsif ( defined $pid ) {                  } elsif ( defined $pid ) {
124                          # child                          # child
125                          my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')';                          my $invoke = 'start';
126                            $invoke = $1 if $daemon =~ s{/(.+)}{};
127                            my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
128                          warn "eval $eval";                          warn "eval $eval";
129                          eval $eval;                          eval $eval;
130                          warn "can't start $daemon: $@" if $@;                          warn "can't start $daemon: $@" if $@;
# Line 116  my $redirect = qq|HTTP/1.1 302 Found\r\n Line 141  my $redirect = qq|HTTP/1.1 302 Found\r\n
141  sub get_request {  sub get_request {
142          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
143    
144          warn "get_request $client $path ",dump( $param );          warn "get_request $path ", $param ? dump( $param ) : '', "\n";
145    
146          if ( my $found = static( $client,$path ) ) {          if ( my $found = static( $client,$path ) ) {
147                  warn "static $found" if $debug;                  warn "static $found" if $debug;
# Line 132  sub get_request { Line 157  sub get_request {
157    
158                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
159                  );                  );
160                  foreach my $name ( %$pids ) {  
161                    my $debug_proc = '';
162    
163                    warn 'pids: ', dump( $pids ) if $debug;
164                    foreach my $name ( sort keys %$pids ) {
165                          my $pid = $pids->{$name} || next;                          my $pid = $pids->{$name} || next;
166    
167                            my $html = qq|<a href=/start_stop/$name>$pid</a>|;
168    
169                          my $proc = "/proc/$pid/status";                          my $proc = "/proc/$pid/status";
170    
171                          my $html = qq|<a href=/$name>$pid</a>|;                          if ( -e $proc ) {
172                          $html   .= qq|<pre style="font-size: 10%">|                                  if ( $debug ) {
173                                          .  ( $debug && -e $proc ? read_file($proc) : '' )                                          $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
174                                          .  qq|</pre>| if $debug;  
175                                            $debug_proc
176                                                    .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
177                                                    .  read_file($proc)
178                                                    .  qq|</pre>|
179                                                    ;
180                                    }
181    
182                                    if ( $name->can('start_fork') ) {
183                                            $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
184                                    }
185    
186                                    if ( $name->can('actions') ) {
187                                            $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
188                                    }
189                            }
190    
191                          push @rows, ( $name => $html );                          push @rows, ( $name => $html );
192                  }                  }
193    
194                  print $client $ok, html::table( 2, @rows );                  my $below_table = '';
195    
196                    warn 'static_pids: ', dump( $static_pids ) if $debug;
197                    foreach my $pid ( keys %$static_pids ) {
198                            my $path = $static_pids->{$pid};
199                            if ( -d "/proc/$pid" ) {
200                                    push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
201                            } elsif ( $param->{clean_completed_downloads} ) {
202                                    delete $static_pids->{$pid}
203                            } else {
204                                    push @rows, ( $path => "$pid competed" );
205                                    $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
206                            }
207                    }
208    
209                    print $client $ok
210                            , html::table( 2, @rows )
211                            , $below_table
212                            , html::tabs( log::mac_changes )
213                            , $debug_proc
214                            ;
215    
216          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
217                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
218                  warn $@ if $@;                  warn $@ if $@;
219                  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>|;
220                  server::debug( $debug ) if $1 eq 'debug';                  server::debug( $debug ) if $1 eq 'debug';
221          } elsif ( $path =~ m{^/(screen|kvm)} ) {          } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
222                  print $client $redirect, start_stop($1);                  print $client $redirect, start_stop($1);
223            } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
224                    $1->$2();
225                    print $client $redirect;
226            } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
227                    print $client $redirect;
228                    kill 1, $1 || kill 9, $2 && warn "killed $1";
229          } elsif ( $path eq '/exit' ) {          } elsif ( $path eq '/exit' ) {
230  #               DESTROY;  #               DESTROY;
231                  exit 0;                  exit 0;
# Line 206  sub start { Line 279  sub start {
279                                  }                                  }
280                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
281                          }                          }
                         warn "path $path param: ",dump( $param );  
282                          get_request $client, $path, $param;                          get_request $client, $path, $param;
283                  } else {                  } else {
284                          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.67  
changed lines
  Added in v.104

  ViewVC Help
Powered by ViewVC 1.1.26