/[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 128 by dpavlin, Mon Aug 3 19:59:08 2009 UTC revision 166 by dpavlin, Thu Aug 6 18:55:51 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 Regexp::Common qw/net/;
21    
22    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;  our $pids;
33  $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh  $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
# Line 38  our $url = "http://$server::ip:$port"; Line 49  our $url = "http://$server::ip:$port";
49  use html;  use html;
50  our $static_pids;  our $static_pids;
51  use progress_bar;  use progress_bar;
52    use config;
53    use client;
54    use log;
55    
56  sub static {  sub static {
57          my ($client,$path) = @_;          my ($client,$path) = @_;
# Line 91  sub static { Line 105  sub static {
105    
106  use boolean;  use boolean;
107    
 use screen;  
108  use kvm;  use kvm;
109    use qemu; $pids->{qemu} = 'stopped';
110    
111  $SIG{CHLD} = 'IGNORE';  $SIG{CHLD} = 'IGNORE';
112    
113  sub start_stop {  sub start_stop {
114          my $daemon = shift;          my $daemon = shift;
115          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon} || 'not started';
116    
117          warn "start_stop $daemon $pid\n";          warn "start_stop $daemon $pid\n";
118    
# Line 119  sub start_stop { Line 133  sub start_stop {
133                          # child                          # child
134                          my $invoke = 'start';                          my $invoke = 'start';
135                          $invoke = $1 if $daemon =~ s{/(.+)}{};                          $invoke = $1 if $daemon =~ s{/(.+)}{};
136                          my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';                          if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) {
137                          warn "eval $eval";                                  my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}";
138                          eval $eval;                                  warn "exec $exec";
139                          warn "can't start $daemon: $@" if $@;                                  exec "xterm -T $daemon -n $daemon -e $exec";
140                            } else {
141                                    my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
142                                    warn "eval $eval";
143                                    eval $eval;
144                                    warn "can't start $daemon: $@" if $@;
145                            }
146                          exit;                          exit;
147                  } else {                  } else {
148                          die "fork error $!";                          die "fork error $!";
# Line 130  sub start_stop { Line 150  sub start_stop {
150          }          }
151  }  }
152    
153  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();
154  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|;
155    
156  sub get_request {  sub get_request {
# Line 144  sub get_request { Line 164  sub get_request {
164                  warn "static $found" if $debug;                  warn "static $found" if $debug;
165          } elsif ( $path eq '/' ) {          } elsif ( $path eq '/' ) {
166    
                 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|;  
   
167                  my @rows = (                  my @rows = (
                         'ip',           html::tt( $server::ip ),  
                         'netmask',      html::tt( $server::netmask ),  
   
168                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
169                  );                  );
170    
# Line 210  sub get_request { Line 223  sub get_request {
223                          , $debug_proc                          , $debug_proc
224                          ;                          ;
225    
226            } elsif ( $path =~ m{^/server} ) {
227                    print $client $ok
228                            , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )
229                            ;
230            } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
231                    my $ip = $1 || $client->peerhost;
232                    if ( $ip ne $server::ip ) {
233                            my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} );
234                            my $deploy   = client::conf( $ip, 'deploy'   => $param->{deploy}   );
235                            print $client $ok
236                                    , qq|<form method=get>|
237                                    , html::table( 2,
238                                            'ip' => $ip,
239                                            'mac' => client::mac( $ip ),
240                                            'hostname' => qq|<input type=text name=hostname value=$hostname>|,
241                                            'deploy' => html::select( 'deploy', $deploy, config::available ),
242                                    )
243                                    , qq|<input type=submit value=change></form>|
244                                    , qq|<h2>PXElinux $deploy</h2><pre>|
245                                    , config::for_ip( $ip )
246                                    , qq|</pre>|
247                                    ;
248                    } else {
249                            print $client $ok
250                                    , qq|<h2>Clients on $server::ip</h2>|
251                                    , qq|<ul>|
252                                    , join("\n",
253                                            map {
254                                                    my $ip = $_;
255                                                    $ip =~ s{^.+/ip/}{};
256                                                    qq|<li><a href=/client/$ip>$ip</a> |, client::mac($ip), ' ', client::conf( $ip, 'hostname' ), qq|</li>|;
257                                            }
258                                            glob("$server::conf/ip/*")
259                                    )
260                                    , qq|</ul>|
261                                    ;
262                    }
263          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
264                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
265                  warn $@ if $@;                  warn $@ if $@;
266                  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>|;
267                  server::debug( $debug ) if $1 eq 'debug';                  server::debug( $debug ) if $1 eq 'debug';
268          } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes          } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
269                  print $client $redirect, start_stop($1);                  print $client $redirect, start_stop($1);
270          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
271                  $1->$2();                  $1->$2();
# Line 223  sub get_request { Line 273  sub get_request {
273          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
274                  print $client $redirect;                  print $client $redirect;
275                  kill 1, $1 || kill 9, $2 && warn "killed $1";                  kill 1, $1 || kill 9, $2 && warn "killed $1";
         } elsif ( $path eq '/exit' ) {  
 #               DESTROY;  
                 exit 0;  
         } elsif ( $path =~ m{/boot} ) {  
                 print $client qq{$ok  
 #!gpxe  
 imgfree  
 login  
 chain http://$server::ip:$httpd::port/  
   
                         };  
276          } else {          } else {
277                  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>";
278                  warn "404 $path";                  warn "404 $path";
# Line 242  chain http://$server::ip:$httpd::port/ Line 281  chain http://$server::ip:$httpd::port/
281  }  }
282    
283  use browser;  use browser;
284    use network;
285    
286  sub start {  sub start {
287    
288            warn 'tap ', network::tap();
289    
290          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
291                          Proto     => 'tcp',                          Proto     => 'tcp',
292                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
# Line 255  sub start { Line 297  sub start {
297          print "url $url\n";          print "url $url\n";
298    
299          start_stop 'browser', $url;          start_stop 'browser', $url;
300          start_stop 'screen';          start_stop 'dhcpd';
301            start_stop 'tftpd';
302            start_stop 'dnsd';
303          start_stop 'kvm';          start_stop 'kvm';
304    
305          while (1) {          while (1) {
# Line 280  sub start { Line 324  sub start {
324                          warn "500 $request";                          warn "500 $request";
325                  }                  }
326    
327                  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;  
328    
329          }          }
330    

Legend:
Removed from v.128  
changed lines
  Added in v.166

  ViewVC Help
Powered by ViewVC 1.1.26