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

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

  ViewVC Help
Powered by ViewVC 1.1.26