turn httpd server into real process manager which starts other components
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 30 Jul 2009 20:15:39 +0000 (20:15 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 30 Jul 2009 20:15:39 +0000 (20:15 +0000)
lib/PXElator/browser.pm [new file with mode: 0644]
lib/PXElator/httpd.pm
lib/PXElator/t/browser.t [new file with mode: 0755]

diff --git a/lib/PXElator/browser.pm b/lib/PXElator/browser.pm
new file mode 100644 (file)
index 0000000..884d536
--- /dev/null
@@ -0,0 +1,8 @@
+package browser;
+
+sub start {
+       my $url = shift;
+       exec "/mnt/llin/rest/cvs/uzbl/uzbl -u $url";
+}
+
+1;
index fd451b7..3971385 100644 (file)
@@ -19,8 +19,18 @@ use File::Slurp;
 use IO::Socket::INET;
 use Module::Refresh;
 
+our $pids = { httpd => $$ };
+
+sub DESTROY {
+       warn "pids ",dump( $pids );
+       foreach ( values %$pids ) {
+               warn "kill $_";
+               kill 1,$_ || kill 9, $_;
+       }
+}
+
 our $port = 7777;
-our $debug = 1;
+our $debug = 0;
 
 use server;
 our $url = "http://$server::ip:$port";
@@ -69,7 +79,6 @@ use boolean;
 
 use screen;
 use kvm;
-our $pids;
 
 $SIG{CHLD} = 'IGNORE';
 
@@ -82,7 +91,7 @@ sub start_stop {
        if ( $pid ) {
                warn "kill 9 $pid";
                kill 9, $pid;
-               delete $pids->{$daemon};
+               $pids->{$daemon} = 'stopped';
                return qq|$daemon pid $pid stopped|;
        } else {
                if ( $pid = fork ) {
@@ -92,7 +101,7 @@ sub start_stop {
                        return qq|$daemon pid $pid started|;
                } elsif ( defined $pid ) {
                        # child
-                       my $eval = $daemon . '::start';
+                       my $eval = $daemon . '::start(' . dump(@_) . ')';
                        warn "eval $eval";
                        eval $eval;
                        warn "can't start $daemon: $@" if $@;
@@ -116,15 +125,24 @@ sub get_request {
                my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :
                                         $pids->{screen} ? qq|start|                                                    : qq|start screen first|;
 
-               print $client $ok,
-               html::table( 2,
-                       'pid',          html::tt( $$ ),
+               my @rows = (
                        'ip',           html::tt( $server::ip ),
                        'netmask',      html::tt( $server::netmask ),
+
                        '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>|,
                );
+               foreach my $name ( %$pids ) {
+                       my $pid = $pids->{$name} || next;
+
+                       my $html = qq|<a href=/$name>$pid</a>|;
+                       $html   .= qq|<pre style="font-size: 10%">|
+                                       .  ( $debug ? read_file("/proc/$pid/status") : '' )
+                                       .  qq|</pre>| if $debug;
+
+                       push @rows, ( $name => $html );
+               }
+
+               print $client $ok, html::table( 2, @rows );
 
        } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
                eval 'our $' . $1 . ' = ' . $2;
@@ -132,6 +150,9 @@ sub get_request {
                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>|;
        } elsif ( $path =~ m{^/(screen|kvm)} ) {
                print $client $ok, start_stop($1);
+       } elsif ( $path eq '/exit' ) {
+#              DESTROY;
+               exit 0;
        } elsif ( $path =~ m{/boot} ) {
                print $client qq{$ok
 #!gpxe
@@ -147,6 +168,8 @@ chain http://$server::ip:$httpd::port/
 
 }
 
+use browser;
+
 sub start {
 
        my $server = IO::Socket::INET->new(
@@ -158,7 +181,9 @@ sub start {
 
        print "url $url\n";
 
-       system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";
+       start_stop 'browser', $url;
+       start_stop 'screen';
+       start_stop 'kvm';
 
        while (my $client = $server->accept()) {
                $client->autoflush(1);
@@ -188,7 +213,8 @@ sub start {
                print $client qq{
                <div style="font-size: 80%; color: #888">
                <a href="">reload</a>
-               <a href="/">index</a>
+               <a href=/>index</a>
+               <a href=/exit>exit</a>
                </div>
                } if $client->connected;
 
diff --git a/lib/PXElator/t/browser.t b/lib/PXElator/t/browser.t
new file mode 100755 (executable)
index 0000000..764e47e
--- /dev/null
@@ -0,0 +1,11 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use autodie;
+
+use Test::More tests => 2;
+
+use_ok 'browser';
+
+ok( browser::start( 'http://localhost' ), 'start' );