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

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

  ViewVC Help
Powered by ViewVC 1.1.26