18 |
#use JSON; |
#use JSON; |
19 |
use IO::Socket::INET; |
use IO::Socket::INET; |
20 |
use Module::Refresh; |
use Module::Refresh; |
|
use Parallel::ForkManager; |
|
21 |
|
|
22 |
our $port = 7777; |
our $port = 7777; |
23 |
our $debug = 1; |
our $debug = 1; |
24 |
|
|
|
my $pm = Parallel::ForkManager->new(10); |
|
|
|
|
|
our $screen_pid; |
|
|
|
|
25 |
use server; |
use server; |
26 |
our $url = "http://$server::ip:$port/"; |
our $url = "http://$server::ip:$port"; |
27 |
|
|
28 |
use html; |
use html; |
29 |
|
|
54 |
my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"; |
my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"; |
55 |
|
|
56 |
use boolean; |
use boolean; |
57 |
|
|
58 |
use screen; |
use screen; |
59 |
|
use kvm; |
60 |
|
our $pids; |
61 |
|
|
62 |
|
$SIG{CHLD} = 'IGNORE'; |
63 |
|
|
64 |
|
sub start_stop { |
65 |
|
my $daemon = shift; |
66 |
|
my $pid = $pids->{$daemon}; |
67 |
|
|
68 |
|
warn "start_stop $daemon pids: ",dump( $pids ); |
69 |
|
|
70 |
|
if ( $pid ) { |
71 |
|
warn "kill 9 $pid"; |
72 |
|
kill 9, $pid; |
73 |
|
delete $pids->{$daemon}; |
74 |
|
return qq|$daemon pid $pid stopped|; |
75 |
|
} else { |
76 |
|
if ( $pid = fork ) { |
77 |
|
# parent |
78 |
|
$pids->{$daemon} = $pid; |
79 |
|
warn "forked $daemon $pid"; |
80 |
|
return qq|$daemon pid $pid started|; |
81 |
|
} elsif ( defined $pid ) { |
82 |
|
# child |
83 |
|
my $eval = $daemon . '::start'; |
84 |
|
warn "eval $eval"; |
85 |
|
eval $eval; |
86 |
|
warn "can't start $daemon: $@" if $@; |
87 |
|
exit; |
88 |
|
} else { |
89 |
|
die "fork error $!"; |
90 |
|
} |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
sub get_request { |
sub get_request { |
95 |
my ( $client, $path, $param ) = @_; |
my ( $client, $path, $param ) = @_; |
100 |
warn "static $found" if $debug; |
warn "static $found" if $debug; |
101 |
} elsif ( $path eq '/' ) { |
} elsif ( $path eq '/' ) { |
102 |
|
|
103 |
my $screen = $screen_pid ? qq|stop <tt>$screen_pid</tt>| : 'start'; |
my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start'; |
104 |
|
my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| : |
105 |
|
$pids->{screen} ? qq|start| : qq|start screen first|; |
106 |
|
|
107 |
print $client $ok, |
print $client $ok, |
108 |
html::table( 2, |
html::table( 2, |
109 |
'pid', $$, |
'pid', html::tt( $$ ), |
110 |
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
'ip', html::tt( $server::ip ), |
111 |
'screen', qq|<a href=/screen>$screen</a>|, |
'netmask', html::tt( $server::netmask ), |
112 |
|
'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|, |
113 |
|
'screen', qq|<a href=/screen>$screen</a>|, |
114 |
|
'kvm', qq|<a href=/kvm>$kvm</a>|, |
115 |
); |
); |
116 |
|
|
117 |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
} elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { |
118 |
eval 'our $' . $1 . ' = ' . $2; |
eval 'our $' . $1 . ' = ' . $2; |
119 |
warn $@ if $@; |
warn $@ if $@; |
120 |
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>|; |
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>|; |
121 |
} elsif ( $path =~ m{^/screen} ) { |
} elsif ( $path =~ m{^/(screen|kvm)} ) { |
122 |
|
print $client $ok, start_stop($1); |
|
if ( $screen_pid ) { |
|
|
if ( ! kill $screen_pid ) { |
|
|
warn "kill -9 $screen_pid"; |
|
|
if ( kill 9, $screen_pid ) { |
|
|
print $client $ok, qq|screen pid $screen_pid stopped|; |
|
|
$screen_pid = 0; |
|
|
} else { |
|
|
print $client $ok, qq|can't stop screen $screen_pid: $!|; |
|
|
} |
|
|
} |
|
|
} else { |
|
|
if ( $screen_pid = $pm->start ) { |
|
|
print $client $ok, qq|screen started pid $screen_pid|; |
|
|
} else { |
|
|
screen::start; |
|
|
$pm->finish; |
|
|
} |
|
|
} |
|
|
|
|
123 |
} elsif ( $path =~ m{/boot} ) { |
} elsif ( $path =~ m{/boot} ) { |
124 |
print $client qq{$ok |
print $client qq{$ok |
125 |
#!gpxe |
#!gpxe |