/[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 181 by dpavlin, Sun Aug 9 19:00:52 2009 UTC
# Line 17  use Carp qw/confess/; Line 17  use Carp qw/confess/;
17  use File::Slurp;  use File::Slurp;
18  #use JSON;  #use JSON;
19  use IO::Socket::INET;  use IO::Socket::INET;
20  use Module::Refresh;  use Regexp::Common qw/net/;
21    
22  our $pids = { httpd => $$ };  sub menu {qq{
23    
24    <div style="font-size: 80%; color: #888">
25    <a href=/>home</a>
26    <a href=/server>server</a>
27    <a href=/client>client</a>
28    </div>
29    
30    }}
31    
32    our $pids;
33    $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
34    
35  sub DESTROY {  sub DESTROY {
36          warn "pids ",dump( $pids );          warn "pids ",dump( $pids );
# Line 36  our $debug = server::debug; Line 47  our $debug = server::debug;
47  our $url = "http://$server::ip:$port";  our $url = "http://$server::ip:$port";
48    
49  use html;  use html;
50    our $static_pids;
51    use progress_bar;
52    use config;
53    use client;
54    use log;
55    use x11;
56    use amt;
57    
58  sub static {  sub static {
59          my ($client,$path) = @_;          my ($client,$path) = @_;
# Line 44  sub static { Line 62  sub static {
62    
63          return if ! -f $full;          return if ! -f $full;
64    
65            if ( my $pid = fork ) {
66                    # parent
67                    close($client);
68                    print "http static child $pid\n";
69                    $static_pids->{$pid} = $path;
70                    return 1;
71            }
72    
73          my $type = 'application/octet-stream';          my $type = 'application/octet-stream';
74          $type = 'text/html' if $path =~ m{\.htm};          $type = 'text/html' if $path =~ m{\.htm};
75          $type = 'application/javascript' if $path =~ m{\.js};          $type = 'application/javascript' if $path =~ m{\.js};
# Line 54  sub static { Line 80  sub static {
80          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";
81    
82          open(my $fh, $full);          open(my $fh, $full);
         print "static $path $type $size\n";  
83    
84          my $block = 8192;          my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
85          my $buff;          my $buff;
86          my $pos = 0;          my $pos = 0;
87    
88            warn "static $path $type $size block: $block\n";
89    
90            progress_bar::start;
91    
92          while( my $len = read $fh, $buff, $block ) {          while( my $len = read $fh, $buff, $block ) {
93                  print $client $buff;                  print $client $buff;
94                    $client->flush;
95                  $pos += $len;                  $pos += $len;
96                  printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;                  progress_bar::tick( $path, $pos, $size );
97          }          }
98          close($fh);          close($fh);
99          close($client);          close($client);
100    
101          print "$path $pos == $size OK\n";          print STDERR "\n";
102    
103            warn "exit static child";
104    
105          return $path;          exit(0);
106  }  }
107    
108  use boolean;  use boolean;
109    
 use screen;  
110  use kvm;  use kvm;
111    
112  $SIG{CHLD} = 'IGNORE';  $SIG{CHLD} = 'IGNORE';
113    
114  sub start_stop {  sub start_stop {
115          my $daemon = shift;          my $daemon = shift;
116          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon} || 'not started';
117    
118          warn "start_stop $daemon $pid pids: ",dump( $pids );          warn "start_stop $daemon $pid\n";
119    
120          if ( $pid =~ m{^\d+$} ) {          if ( $pid =~ m{^\d+$} ) {
121                  warn "kill 9 $pid";                  my $pstree = `pstree -p $pid`;
122                  kill 9, $pid;                  my @pids = $pstree =~ m{\((\d+)\)}g;
123                    warn "pstree $pstree pids ",dump( @pids );
124                    kill 1, $_ foreach reverse @pids;
125                  $pids->{$daemon} = 'stopped';                  $pids->{$daemon} = 'stopped';
126                  return qq|$daemon pid $pid stopped|;                  return qq|$daemon pid $pid stopped|;
127          } else {          } else {
128                  if ( $pid = fork ) {                  if ( $pid = fork ) {
129                          # parent                          # parent
130                          $pids->{$daemon} = $pid;                          $pids->{$daemon} = $pid;
131                          warn "forked $daemon $pid";                          warn "forked $daemon $pid\n";
132                          return qq|$daemon pid $pid started|;                          return qq|$daemon pid $pid started|;
133                  } elsif ( defined $pid ) {                  } elsif ( defined $pid ) {
134                          # child                          # child
135                          my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')';                          my $invoke = 'start';
136                          warn "eval $eval";                          $invoke = $1 if $daemon =~ s{/(.+)}{};
137                          eval $eval;                          if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) {
138                          warn "can't start $daemon: $@" if $@;                                  my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}";
139                                    warn "exec $exec";
140                                    x11::xterm( $daemon => $exec );
141                            } else {
142                                    my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
143                                    warn "eval $eval";
144                                    eval $eval;
145                                    warn "can't start $daemon: $@" if $@;
146                            }
147                          exit;                          exit;
148                  } else {                  } else {
149                          die "fork error $!";                          die "fork error $!";
# Line 110  sub start_stop { Line 151  sub start_stop {
151          }          }
152  }  }
153    
154  my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;  my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu();
155  my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;  my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
156    
157  sub get_request {  sub get_request {
158          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
159    
160          warn "get_request $client $path ",dump( $param );          server->refresh;
161    
162            warn "get_request $path ", $param ? dump( $param ) : '', "\n";
163    
164          if ( my $found = static( $client,$path ) ) {          if ( my $found = static( $client,$path ) ) {
165                  warn "static $found" if $debug;                  warn "static $found" if $debug;
166          } elsif ( $path eq '/' ) {          } elsif ( $path eq '/' ) {
167    
                 my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>|        : 'start';  
                 my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :  
                                          $pids->{screen} ? qq|start|                                                    : qq|start screen first|;  
   
168                  my @rows = (                  my @rows = (
                         'ip',           html::tt( $server::ip ),  
                         'netmask',      html::tt( $server::netmask ),  
   
169                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
170                  );                  );
171                  foreach my $name ( %$pids ) {  
172                    my $debug_proc = '';
173    
174                    warn 'pids: ', dump( $pids ) if $debug;
175                    foreach my $name ( sort keys %$pids ) {
176                          my $pid = $pids->{$name} || next;                          my $pid = $pids->{$name} || next;
177    
178                            my $html;
179    
180                          my $proc = "/proc/$pid/status";                          my $proc = "/proc/$pid/status";
181    
182                          my $html = qq|<a href=/$name>$pid</a>|;                          if ( -e $proc ) {
183                          $html   .= qq|<pre style="font-size: 10%">|                                  $html .= qq|<a href=/start_stop/$name>$pid</a>|;
184                                          .  ( $debug && -e $proc ? read_file($proc) : '' )                                  if ( $debug ) {
185                                          .  qq|</pre>| if $debug;                                          $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
186    
187                                            $debug_proc
188                                                    .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
189                                                    .  read_file($proc)
190                                                    .  qq|</pre>|
191                                                    ;
192                                    }
193    
194                                    if ( $name->can('start_fork') ) {
195                                            $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
196                                    }
197    
198                                    if ( $name->can('actions') ) {
199                                            $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
200                                    }
201                            } else {
202                                    $html .= qq|<a href=/start_stop/$name>restart</a> $pid exited|;
203                            }
204    
205                          push @rows, ( $name => $html );                          push @rows, ( $name => $html );
206                  }                  }
207    
208                  print $client $ok, html::table( 2, @rows );                  my $below_table = '';
209    
210                    warn 'static_pids: ', dump( $static_pids ) if $debug;
211                    foreach my $pid ( keys %$static_pids ) {
212                            my $path = $static_pids->{$pid};
213                            if ( -d "/proc/$pid" ) {
214                                    push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
215                            } elsif ( $param->{clean_completed_downloads} ) {
216                                    delete $static_pids->{$pid}
217                            } else {
218                                    push @rows, ( $path => "$pid competed" );
219                                    $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
220                            }
221                    }
222    
223                    print $client $ok
224                            , html::table( 2, @rows )
225                            , $below_table
226                            , html::tabs( log::mac_changes )
227                            , $debug_proc
228                            ;
229    
230            } elsif ( $path =~ m{^/server} ) {
231                    print $client $ok
232                            , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )
233                            ;
234            } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
235                    my $ip = $1 || $client->peerhost;
236                    if ( $ip ne $server::ip ) {
237                            my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} );
238                            my $deploy   = client::conf( $ip, 'deploy'   => $param->{deploy}   );
239                            print $client $ok
240                                    , qq|<form method=get>|
241                                    , html::table( 2,
242                                            'ip' => $ip,
243                                            'mac' => client::mac( $ip ),
244                                            'hostname' => qq|<input type=text name=hostname value=$hostname>|,
245                                            'deploy' => html::select( 'deploy', $deploy, config::available ),
246                                    )
247                                    , qq|<input type=submit value=change></form>|
248                                    , qq|<h2>PXElinux $deploy</h2><pre>|
249                                    , config::for_ip( $ip )
250                                    , qq|</pre>|
251                                    ;
252    
253                            if ( my $amt = client::conf( $ip, 'amt' ) ) {
254                                    print $client amt::info( $amt );
255                            }
256                    } else {
257                            print $client $ok
258                                    , qq|<h2>Clients on $server::ip</h2>|
259                                    , html::table( -4,
260                                            'ip', 'mac', 'hostname', 'deploy',
261                                            map {
262                                                    my $ip = $_;
263                                                    $ip =~ s{^.+/ip/}{};
264                                                    ( qq|<a href=/client/$ip>$ip</a>|, client::mac($ip), client::conf( $ip, 'hostname' ), client::conf( $ip, 'deploy' ) );
265                                            }
266                                            glob("$server::conf/ip/*")
267                                    )
268                                    ;
269                    }
270          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
271                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
272                  warn $@ if $@;                  warn $@ if $@;
273                  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>|;
274                  server::debug( $debug ) if $1 eq 'debug';                  server::debug( $debug ) if $1 eq 'debug';
275          } elsif ( $path =~ m{^/(screen|kvm)} ) {          } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
276                  print $client $redirect, start_stop($1);                  print $client $redirect, start_stop($1);
277          } elsif ( $path eq '/exit' ) {          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
278  #               DESTROY;                  $1->$2();
279                  exit 0;                  print $client $redirect;
280          } elsif ( $path =~ m{/boot} ) {          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
281                  print $client qq{$ok                  print $client $redirect;
282  #!gpxe                  kill 1, $1 || kill 9, $2 && warn "killed $1";
 imgfree  
 login  
 chain http://$server::ip:$httpd::port/  
   
                         };  
283          } else {          } else {
284                  print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";                  print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
285                  warn "404 $path";                  warn "404 $path";
# Line 172  chain http://$server::ip:$httpd::port/ Line 288  chain http://$server::ip:$httpd::port/
288  }  }
289    
290  use browser;  use browser;
291    use network;
292    
293  sub start {  sub start {
294    
295            warn 'tap ', network::tap();
296    
297          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
298                          Proto     => 'tcp',                          Proto     => 'tcp',
299                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
# Line 185  sub start { Line 304  sub start {
304          print "url $url\n";          print "url $url\n";
305    
306          start_stop 'browser', $url;          start_stop 'browser', $url;
307          start_stop 'screen';          start_stop 'dhcpd';
308          start_stop 'kvm';          start_stop 'tftpd';
309            start_stop 'dnsd';
310            start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
311    
312          while (my $client = $server->accept()) {          while (1) {
313                  $client->autoflush(1);                  my $client = $server->accept() || next; # ALARM trickle us
314                  my $request = <$client>;                  my $request = <$client>;
315    
316                  warn "request $request\n" if $debug;                  warn "request $request\n" if $debug;
317    
                 Module::Refresh->refresh;  
   
318                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
319                          my $path = $1;                          my $path = $1;
320                          my $param;                          my $param;
# Line 206  sub start { Line 325  sub start {
325                                  }                                  }
326                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
327                          }                          }
                         warn "path $path param: ",dump( $param );  
328                          get_request $client, $path, $param;                          get_request $client, $path, $param;
329                  } else {                  } else {
330                          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";
331                          warn "500 $request";                          warn "500 $request";
332                  }                  }
333    
334                  print $client qq{                  print $client menu() if $client->connected;
                 <div style="font-size: 80%; color: #888">  
                 <a href="">reload</a>  
                 <a href=/>index</a>  
                 <a href=/exit>exit</a>  
                 </div>  
                 } if $client->connected;  
335    
336          }          }
337    

Legend:
Removed from v.67  
changed lines
  Added in v.181

  ViewVC Help
Powered by ViewVC 1.1.26