/[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 96 by dpavlin, Fri Jul 31 20:45:09 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  sub static {  sub static {
43          my ($client,$path) = @_;          my ($client,$path) = @_;
# Line 44  sub static { Line 46  sub static {
46    
47          return if ! -f $full;          return if ! -f $full;
48    
49            if ( my $pid = fork ) {
50                    # parent
51                    close($client);
52                    print "http static child $pid\n";
53                    $static_pids->{$pid} = $path;
54                    return 1;
55            }
56    
57          my $type = 'application/octet-stream';          my $type = 'application/octet-stream';
58          $type = 'text/html' if $path =~ m{\.htm};          $type = 'text/html' if $path =~ m{\.htm};
59          $type = 'application/javascript' if $path =~ m{\.js};          $type = 'application/javascript' if $path =~ m{\.js};
# Line 51  sub static { Line 61  sub static {
61    
62          my $size = -s $full || return;          my $size = -s $full || return;
63    
64            $client->autoflush(1);
65    
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            print "static $path $type $size block: $block\n";
75    
76          while( my $len = read $fh, $buff, $block ) {          while( my $len = read $fh, $buff, $block ) {
77                  print $client $buff;                  print $client $buff;
78                  $pos += $len;                  $pos += $len;
# Line 68  sub static { Line 81  sub static {
81          close($fh);          close($fh);
82          close($client);          close($client);
83    
84          print "$path $pos == $size OK\n";          print "\n";
85    
86          return $path;          exit;
87  }  }
88    
89  use boolean;  use boolean;
# Line 84  sub start_stop { Line 97  sub start_stop {
97          my $daemon = shift;          my $daemon = shift;
98          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon};
99    
100          warn "start_stop $daemon $pid pids: ",dump( $pids );          warn "start_stop $daemon $pid\n";
101    
102          if ( $pid =~ m{^\d+$} ) {          if ( $pid =~ m{^\d+$} ) {
103                  warn "kill 9 $pid";                  my $pstree = `pstree -p $pid`;
104                  kill 9, $pid;                  my @pids = $pstree =~ m{\((\d+)\)}g;
105                    warn "pstree $pstree pids ",dump( @pids );
106                    kill 1, $_ foreach reverse @pids;
107                  $pids->{$daemon} = 'stopped';                  $pids->{$daemon} = 'stopped';
108                  return qq|$daemon pid $pid stopped|;                  return qq|$daemon pid $pid stopped|;
109          } else {          } else {
110                  if ( $pid = fork ) {                  if ( $pid = fork ) {
111                          # parent                          # parent
112                          $pids->{$daemon} = $pid;                          $pids->{$daemon} = $pid;
113                          warn "forked $daemon $pid";                          warn "forked $daemon $pid\n";
114                          return qq|$daemon pid $pid started|;                          return qq|$daemon pid $pid started|;
115                  } elsif ( defined $pid ) {                  } elsif ( defined $pid ) {
116                          # child                          # child
117                          my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')';                          my $invoke = 'start';
118                            $invoke = $1 if $daemon =~ s{/(.+)}{};
119                            my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
120                          warn "eval $eval";                          warn "eval $eval";
121                          eval $eval;                          eval $eval;
122                          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 133  my $redirect = qq|HTTP/1.1 302 Found\r\n
133  sub get_request {  sub get_request {
134          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
135    
136          warn "get_request $client $path ",dump( $param );          warn "get_request $path ", $param ? dump( $param ) : '', "\n";
137    
138          if ( my $found = static( $client,$path ) ) {          if ( my $found = static( $client,$path ) ) {
139                  warn "static $found" if $debug;                  warn "static $found" if $debug;
# Line 132  sub get_request { Line 149  sub get_request {
149    
150                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
151                  );                  );
152                  foreach my $name ( %$pids ) {  
153                    my $debug_proc = '';
154    
155                    warn 'pids: ', dump( $pids ) if $debug;
156                    foreach my $name ( sort keys %$pids ) {
157                          my $pid = $pids->{$name} || next;                          my $pid = $pids->{$name} || next;
158    
159                            my $html = qq|<a href=/start_stop/$name>$pid</a>|;
160    
161                          my $proc = "/proc/$pid/status";                          my $proc = "/proc/$pid/status";
162    
163                          my $html = qq|<a href=/$name>$pid</a>|;                          if ( -e $proc ) {
164                          $html   .= qq|<pre style="font-size: 10%">|                                  if ( $debug ) {
165                                          .  ( $debug && -e $proc ? read_file($proc) : '' )                                          $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
166                                          .  qq|</pre>| if $debug;  
167                                            $debug_proc
168                                                    .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
169                                                    .  read_file($proc)
170                                                    .  qq|</pre>|
171                                                    ;
172                                    }
173    
174                                    if ( $name->can('start_fork') ) {
175                                            $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
176                                    }
177    
178                                    if ( $name->can('actions') ) {
179                                            $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
180                                    }
181                            }
182    
183                          push @rows, ( $name => $html );                          push @rows, ( $name => $html );
184                  }                  }
185    
186                  print $client $ok, html::table( 2, @rows );                  my $below_table = '';
187    
188                    warn 'static_pids: ', dump( $static_pids ) if $debug;
189                    foreach my $pid ( keys %$static_pids ) {
190                            my $path = $static_pids->{$pid};
191                            if ( -d "/proc/$pid" ) {
192                                    push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
193                            } elsif ( $param->{clean_completed_downloads} ) {
194                                    delete $static_pids->{$pid}
195                            } else {
196                                    push @rows, ( $path => "$pid competed" );
197                                    $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
198                            }
199                    }
200    
201                    print $client $ok
202                            , html::table( 2, @rows )
203                            , $below_table
204                            , html::tabs( log::mac_changes )
205                            , $debug_proc
206                            ;
207    
208          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
209                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
210                  warn $@ if $@;                  warn $@ if $@;
211                  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>|;
212                  server::debug( $debug ) if $1 eq 'debug';                  server::debug( $debug ) if $1 eq 'debug';
213          } elsif ( $path =~ m{^/(screen|kvm)} ) {          } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
214                  print $client $redirect, start_stop($1);                  print $client $redirect, start_stop($1);
215            } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
216                    $1->$2();
217                    print $client $redirect;
218            } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
219                    print $client $redirect;
220                    kill 1, $1 || kill 9, $2 && warn "killed $1";
221          } elsif ( $path eq '/exit' ) {          } elsif ( $path eq '/exit' ) {
222  #               DESTROY;  #               DESTROY;
223                  exit 0;                  exit 0;
# Line 206  sub start { Line 271  sub start {
271                                  }                                  }
272                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
273                          }                          }
                         warn "path $path param: ",dump( $param );  
274                          get_request $client, $path, $param;                          get_request $client, $path, $param;
275                  } else {                  } else {
276                          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.96

  ViewVC Help
Powered by ViewVC 1.1.26