/[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 65 by dpavlin, Thu Jul 30 17:07:48 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;
23    $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
24    
25    sub DESTROY {
26            warn "pids ",dump( $pids );
27            foreach ( values %$pids ) {
28                    warn "kill $_";
29                    kill 1,$_ || kill 9, $_;
30            }
31    }
32    
33  our $port = 7777;  our $port = 7777;
 our $debug = 1;  
34    
35  use server;  use server;
36    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 34  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 41  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 58  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    
 my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";  
   
89  use boolean;  use boolean;
90    
91  use screen;  use screen;
92  use kvm;  use kvm;
 our $pids;  
93    
94  $SIG{CHLD} = 'IGNORE';  $SIG{CHLD} = 'IGNORE';
95    
# Line 77  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 pids: ",dump( $pids );          warn "start_stop $daemon $pid\n";
101    
102          if ( $pid ) {          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                  delete $pids->{$daemon};                  warn "pstree $pstree pids ",dump( @pids );
106                    kill 1, $_ foreach reverse @pids;
107                    $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';                          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 103  sub start_stop { Line 127  sub start_stop {
127          }          }
128  }  }
129    
130    my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
131    my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
132    
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 116  sub get_request { Line 143  sub get_request {
143                  my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :                  my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :
144                                           $pids->{screen} ? qq|start|                                                    : qq|start screen first|;                                           $pids->{screen} ? qq|start|                                                    : qq|start screen first|;
145    
146                  print $client $ok,                  my @rows = (
                 html::table( 2,  
                         'pid',          html::tt( $$ ),  
147                          'ip',           html::tt( $server::ip ),                          'ip',           html::tt( $server::ip ),
148                          'netmask',      html::tt( $server::netmask ),                          'netmask',      html::tt( $server::netmask ),
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>|,
                         'screen',       qq|<a href=/screen>$screen</a>|,  
                         'kvm',          qq|<a href=/kvm>$kvm</a>|,  
151                  );                  );
152    
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;
158    
159                            my $html = qq|<a href=/start_stop/$name>$pid</a>|;
160    
161                            my $proc = "/proc/$pid/status";
162    
163                            if ( -e $proc ) {
164                                    if ( $debug ) {
165                                            $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
166    
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 );
184                    }
185    
186                    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 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>|;                  print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
212          } elsif ( $path =~ m{^/(screen|kvm)} ) {                  server::debug( $debug ) if $1 eq 'debug';
213                  print $client $ok, start_stop($1);          } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
214                    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' ) {
222    #               DESTROY;
223                    exit 0;
224          } elsif ( $path =~ m{/boot} ) {          } elsif ( $path =~ m{/boot} ) {
225                  print $client qq{$ok                  print $client qq{$ok
226  #!gpxe  #!gpxe
# Line 147  chain http://$server::ip:$httpd::port/ Line 236  chain http://$server::ip:$httpd::port/
236    
237  }  }
238    
239    use browser;
240    
241  sub start {  sub start {
242    
243          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
# Line 158  sub start { Line 249  sub start {
249    
250          print "url $url\n";          print "url $url\n";
251    
252          system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";          start_stop 'browser', $url;
253            start_stop 'screen';
254            start_stop 'kvm';
255    
256          while (my $client = $server->accept()) {          while (my $client = $server->accept()) {
257                  $client->autoflush(1);                  $client->autoflush(1);
# Line 178  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";
# Line 188  sub start { Line 280  sub start {
280                  print $client qq{                  print $client qq{
281                  <div style="font-size: 80%; color: #888">                  <div style="font-size: 80%; color: #888">
282                  <a href="">reload</a>                  <a href="">reload</a>
283                  <a href="/">index</a>                  <a href=/>index</a>
284                    <a href=/exit>exit</a>
285                  </div>                  </div>
286                  } if $client->connected;                  } if $client->connected;
287    

Legend:
Removed from v.65  
changed lines
  Added in v.96

  ViewVC Help
Powered by ViewVC 1.1.26