/[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 64 by dpavlin, Thu Jul 30 16:26:54 2009 UTC revision 162 by dpavlin, Thu Aug 6 18:10:31 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 Module::Refresh;  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;
33    $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
34    
35    sub DESTROY {
36            warn "pids ",dump( $pids );
37            foreach ( values %$pids ) {
38                    warn "kill $_";
39                    kill 1,$_ || kill 9, $_;
40            }
41    }
42    
43  our $port = 7777;  our $port = 7777;
 our $debug = 1;  
44    
45  use server;  use server;
46    our $debug = server::debug;
47  our $url = "http://$server::ip:$port";  our $url = "http://$server::ip:$port";
48    
49  use html;  use html;
50    our $static_pids;
51    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) = @_;
58    
59          $path = "tftp/$path";          my $full = "$server::base_dir/tftp/$path";
60    
61            return if ! -f $full;
62    
63          if ( ! -e $path ||  -d $path ) {          if ( my $pid = fork ) {
64                  print $client "HTTP/1.0 404 $path not found\r\n";                  # parent
65                  return;                  close($client);
66                    print "http static child $pid\n";
67                    $static_pids->{$pid} = $path;
68                    return 1;
69          }          }
70    
71          my $type = 'text/plain';          my $type = 'application/octet-stream';
72          $type = 'text/html' if $path =~ m{\.htm};          $type = 'text/html' if $path =~ m{\.htm};
73          $type = 'application/javascript' if $path =~ m{\.js};          $type = 'application/javascript' if $path =~ m{\.js};
74            $type = 'text/plain' if $path =~ m{\.txt};
75    
76            my $size = -s $full || return;
77    
78            print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
79    
80            open(my $fh, $full);
81    
82          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n";          my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
83          open(my $html, $path);          my $buff;
84          while(<$html>) {          my $pos = 0;
85                  print $client $_;  
86            warn "static $path $type $size block: $block\n";
87    
88            progress_bar::start;
89    
90            while( my $len = read $fh, $buff, $block ) {
91                    print $client $buff;
92                    $client->flush;
93                    $pos += $len;
94                    progress_bar::tick( $path, $pos, $size );
95          }          }
96          close($html);          close($fh);
97            close($client);
98    
99          return $path;          print STDERR "\n";
100  }  
101            warn "exit static child";
102    
103  my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";          exit(0);
104    }
105    
106  use boolean;  use boolean;
107    
 use screen;  
108  use kvm;  use kvm;
 our $pids;  
109    
110  $SIG{CHLD} = 'IGNORE';  $SIG{CHLD} = 'IGNORE';
111    
112  sub start_stop {  sub start_stop {
113          my $daemon = shift;          my $daemon = shift;
114          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon} || 'not started';
115    
116          warn "start_stop $daemon pids: ",dump( $pids );          warn "start_stop $daemon $pid\n";
117    
118          if ( $pid ) {          if ( $pid =~ m{^\d+$} ) {
119                  warn "kill 9 $pid";                  my $pstree = `pstree -p $pid`;
120                  kill 9, $pid;                  my @pids = $pstree =~ m{\((\d+)\)}g;
121                  delete $pids->{$daemon};                  warn "pstree $pstree pids ",dump( @pids );
122                    kill 1, $_ foreach reverse @pids;
123                    $pids->{$daemon} = 'stopped';
124                  return qq|$daemon pid $pid stopped|;                  return qq|$daemon pid $pid stopped|;
125          } else {          } else {
126                  if ( $pid = fork ) {                  if ( $pid = fork ) {
127                          # parent                          # parent
128                          $pids->{$daemon} = $pid;                          $pids->{$daemon} = $pid;
129                          warn "forked $daemon $pid";                          warn "forked $daemon $pid\n";
130                          return qq|$daemon pid $pid started|;                          return qq|$daemon pid $pid started|;
131                  } elsif ( defined $pid ) {                  } elsif ( defined $pid ) {
132                          # child                          # child
133                          my $eval = $daemon . '::start';                          my $invoke = 'start';
134                          warn "eval $eval";                          $invoke = $1 if $daemon =~ s{/(.+)}{};
135                          eval $eval;                          if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) {
136                          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}";
137                                    warn "exec $exec";
138                                    exec "xterm -T $daemon -n $daemon -e $exec";
139                            } else {
140                                    my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
141                                    warn "eval $eval";
142                                    eval $eval;
143                                    warn "can't start $daemon: $@" if $@;
144                            }
145                          exit;                          exit;
146                  } else {                  } else {
147                          die "fork error $!";                          die "fork error $!";
# Line 91  sub start_stop { Line 149  sub start_stop {
149          }          }
150  }  }
151    
152    my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu();
153    my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
154    
155  sub get_request {  sub get_request {
156          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
157    
158          warn "get_request $client $path ",dump( $param );          server->refresh;
159    
160            warn "get_request $path ", $param ? dump( $param ) : '', "\n";
161    
162          if ( my $found = static( $client,$path ) ) {          if ( my $found = static( $client,$path ) ) {
163                  warn "static $found" if $debug;                  warn "static $found" if $debug;
164          } elsif ( $path eq '/' ) {          } elsif ( $path eq '/' ) {
165    
166                  my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>|        : 'start';                  my @rows = (
                 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( $$ ),  
                         'ip',           html::tt( $server::ip ),  
                         'netmask',      html::tt( $server::netmask ),  
167                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          '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>|,  
168                  );                  );
169    
170                    my $debug_proc = '';
171    
172                    warn 'pids: ', dump( $pids ) if $debug;
173                    foreach my $name ( sort keys %$pids ) {
174                            my $pid = $pids->{$name} || next;
175    
176                            my $html = qq|<a href=/start_stop/$name>$pid</a>|;
177    
178                            my $proc = "/proc/$pid/status";
179    
180                            if ( -e $proc ) {
181                                    if ( $debug ) {
182                                            $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
183    
184                                            $debug_proc
185                                                    .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
186                                                    .  read_file($proc)
187                                                    .  qq|</pre>|
188                                                    ;
189                                    }
190    
191                                    if ( $name->can('start_fork') ) {
192                                            $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
193                                    }
194    
195                                    if ( $name->can('actions') ) {
196                                            $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
197                                    }
198                            }
199    
200                            push @rows, ( $name => $html );
201                    }
202    
203                    my $below_table = '';
204    
205                    warn 'static_pids: ', dump( $static_pids ) if $debug;
206                    foreach my $pid ( keys %$static_pids ) {
207                            my $path = $static_pids->{$pid};
208                            if ( -d "/proc/$pid" ) {
209                                    push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
210                            } elsif ( $param->{clean_completed_downloads} ) {
211                                    delete $static_pids->{$pid}
212                            } else {
213                                    push @rows, ( $path => "$pid competed" );
214                                    $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
215                            }
216                    }
217    
218                    print $client $ok
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                            , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )
228                            ;
229            } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
230                    my $ip = $1 || $client->peerhost;
231                    if ( $ip ne $server::ip ) {
232                            my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} );
233                            my $deploy   = client::conf( $ip, 'deploy'   => $param->{deploy}   );
234                            print $client $ok
235                                    , qq|<form method=get>|
236                                    , html::table( 2,
237                                            'ip' => $ip,
238                                            'hostname' => qq|<input type=text name=hostname value=$hostname>|,
239                                            'deploy' => html::select( 'deploy', $deploy, config::available ),
240                                    )
241                                    , qq|<input type=submit value=change></form>|
242                                    , qq|<h2>PXElinux $deploy</h2><pre>|
243                                    , config::for_ip( $ip )
244                                    , qq|</pre>|
245                                    ;
246                    } else {
247                            print $client $ok
248                                    , qq|<h2>Clients on $server::ip</h2>|
249                                    , qq|<ul>|
250                                    , join("\n",
251                                            map {
252                                                    my $ip = $_;
253                                                    $ip =~ s{^.+/ip/}{};
254                                                    qq|<li><a href=/client/$ip>$ip</a></li>|
255                                            }
256                                            glob("$server::conf/ip/*")
257                                    )
258                                    , qq|</ul>|
259                                    ;
260                    }
261          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
262                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
263                  warn $@ if $@;                  warn $@ if $@;
264                  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 $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
265          } elsif ( $path =~ m{^/(screen|kvm)} ) {                  server::debug( $debug ) if $1 eq 'debug';
266                  print $client $ok, start_stop($1);          } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
267          } elsif ( $path =~ m{/boot} ) {                  print $client $redirect, start_stop($1);
268                  print $client qq{$ok          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
269  #!gpxe                  $1->$2();
270  imgfree                  print $client $redirect;
271  login          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
272  chain http://$server::ip:$httpd::port/                  print $client $redirect;
273                    kill 1, $1 || kill 9, $2 && warn "killed $1";
                         };  
274          } else {          } else {
275                  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>";
276                  warn "404 $path";                  warn "404 $path";
# Line 135  chain http://$server::ip:$httpd::port/ Line 278  chain http://$server::ip:$httpd::port/
278    
279  }  }
280    
281    use browser;
282    use network;
283    
284  sub start {  sub start {
285    
286            warn 'tap ', network::tap();
287    
288          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
289                          Proto     => 'tcp',                          Proto     => 'tcp',
290                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
# Line 146  sub start { Line 294  sub start {
294    
295          print "url $url\n";          print "url $url\n";
296    
297          system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";          start_stop 'browser', $url;
298            start_stop 'dhcpd';
299            start_stop 'tftpd';
300            start_stop 'dnsd';
301            start_stop 'kvm';
302    
303          while (my $client = $server->accept()) {          while (1) {
304                  $client->autoflush(1);                  my $client = $server->accept() || next; # ALARM trickle us
305                  my $request = <$client>;                  my $request = <$client>;
306    
307                  warn "request $request\n" if $debug;                  warn "request $request\n" if $debug;
308    
                 Module::Refresh->refresh;  
   
309                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
310                          my $path = $1;                          my $path = $1;
311                          my $param;                          my $param;
# Line 166  sub start { Line 316  sub start {
316                                  }                                  }
317                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
318                          }                          }
                         warn "path $path param: ",dump( $param );  
319                          get_request $client, $path, $param;                          get_request $client, $path, $param;
320                  } else {                  } else {
321                          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";
322                          warn "500 $request";                          warn "500 $request";
323                  }                  }
324    
325                  print $client qq{                  print $client menu() if $client->connected;
                 <div style="font-size: 80%; color: #888">  
                 <a href="">reload</a>  
                 <a href="/">index</a>  
                 </div>  
                 } if $client->connected;  
326    
327          }          }
328    

Legend:
Removed from v.64  
changed lines
  Added in v.162

  ViewVC Help
Powered by ViewVC 1.1.26