/[pxelator]/lib/PXElator/client.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/client.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 323 by dpavlin, Fri Aug 28 18:51:43 2009 UTC revision 510 by dpavlin, Wed Jul 21 16:49:29 2010 UTC
# Line 5  use strict; Line 5  use strict;
5  use autodie;  use autodie;
6    
7  use File::Slurp;  use File::Slurp;
8  use Net::Ping;  use Data::Dump qw/dump/;
9    use File::Path;
10    
11  use server;  use server;
12  use format;  use format;
13  use ip;  use ip;
14    use ping;
15    use kvm;
16    
17  our $debug = $server::debug;  our $debug = $server::debug;
18    
# Line 59  sub conf { Line 62  sub conf {
62                  write_file $path, $default;                  write_file $path, $default;
63                  warn "default $path = $default";                  warn "default $path = $default";
64                  $value = $default;                  $value = $default;
65            } elsif ( -l $path ) {
66                    $value = readlink $path;
67          } elsif ( -f $path ) {          } elsif ( -f $path ) {
68                  $value = read_file $path;                  $value = read_file $path;
69                    chomp $value;
70          } else {          } else {
71                  warn "# $name missing $path\n" if $debug;                  warn "# $name missing $path\n" if $debug;
72          }          }
# Line 71  sub all_conf { Line 77  sub all_conf {
77          my $ip = shift;          my $ip = shift;
78          my $path = ip_path $ip || return;          my $path = ip_path $ip || return;
79          my $conf;          my $conf;
80          foreach my $file ( glob("$path/*") ) {          foreach my $file ( glob("$path/*"), glob("$path/*/*") ) {
81                  my $name = $file;                  my $name = $file;
82                  $name =~ s{^.+/([^/]+)$}{$1};                  $name =~ s{^$path/+}{} || die "can't remove $path from $name";
83                  $conf->{ $name } = read_file $file;                  $conf->{ $name } =
84                            -l $file ? readlink  $file :
85                            -f $file ? read_file $file :
86                            '?';
87          }          }
88          return $conf;          return $conf;
89  }  }
# Line 82  sub next_ip($) { Line 91  sub next_ip($) {
91          my $mac = shift;          my $mac = shift;
92          $mac = format::mac($mac);          $mac = format::mac($mac);
93    
94          my $p = Net::Ping->new;          if ( $server::new_clients > 0 ) {
95                    warn "# clients left: ", --$server::new_clients;
96            } else {
97                    warn "W: no new clients accepted";
98                    return '0.0.0.0';
99            }
100    
101          my $prefix = $server::ip;          my $prefix = $server::ip;
102          $prefix =~ s{\.\d+$}{.};          $prefix =~ s{\.\d+$}{.};
103          my $addr = $server::ip_from || die;          my $addr = $server::ip_from || die;
104          my $ip = $prefix . $addr;          my $ip = $prefix . $addr;
105    
106          while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {          while ( -e ip_path($ip) || ping::host($ip) ) {
107                  $ip = $prefix . $addr++;                  $ip = $prefix . $addr++;
108                  die "all addresses allocated!" if $addr == $server::ip_to;                  die "all addresses allocated!" if $addr == $server::ip_to;
109                  warn "skip $ip\n";                  warn "skip $ip\n";
# Line 105  sub next_ip($) { Line 119  sub next_ip($) {
119  sub save_ip_mac {  sub save_ip_mac {
120          my ($ip,$mac) = @_;          my ($ip,$mac) = @_;
121          $mac = format::mac($mac);          $mac = format::mac($mac);
122            return if $mac eq '00:00:00:00:00:00' || $ip eq '0.0.0.0';
123    
124          mkdir ip_path($ip) unless -e ip_path($ip);          mkdir ip_path($ip) unless -e ip_path($ip);
125    
# Line 157  sub all_ips { Line 172  sub all_ips {
172          map {          map {
173                  my $ip = $_;                  my $ip = $_;
174                  $ip =~ s{^.+/ip/}{};                  $ip =~ s{^.+/ip/}{};
175                    autocreate_params( $ip );
176                  $ip;                  $ip;
177          } glob("$server::conf/ip/*")          } glob("$server::conf/ip/*")
178  }  }
179    
180  sub remove {  sub remove {
181          my $ip = shift;          my $ip = shift;
         unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );  
182          if ( my $mac = mac_from_ip $ip ) {          if ( my $mac = mac_from_ip $ip ) {
183                  unlink "$server::conf/mac/$mac";                  unlink "$server::conf/mac/$mac";
184          }          }
185          rmdir "$server::conf/ip/$ip";          rmtree "$server::conf/ip/$ip";
186    }
187    
188    sub arp_mac_dev {
189            my $arp = {
190                    map {
191                            my @c = split(/\s+/,$_);
192                            if ( $#c == 5 ) {
193                                    client::save_ip_mac( $c[0], $c[3] );
194                                    ( uc $c[3] => $c[5] )
195                            } else {
196                            }
197                    } read_file('/proc/net/arp')
198            };
199    
200            warn "# arp ",dump( $arp );
201            return $arp;
202    }
203    
204    sub rebuild_mac_links {
205            warn "# rebuild mac links";
206            foreach my $ip ( all_ips ) {
207                    my $mac = ip_path $ip, 'mac';
208                    if ( -e $mac ) {
209                            $mac = read_file $mac;
210                            chomp $mac;
211                            save_ip_mac( $ip, $mac );
212                            warn "## $ip $mac\n";
213                    }
214            }
215    }
216    
217    sub autocreate_params {
218            my $ip = shift;
219            my $mac = mac_from_ip $ip;
220            if ( $mac =~ m{^AC:DE:48:00:00} && ! defined conf( $ip, 'kvm' ) ) {
221                    conf( $ip, 'kvm', default => kvm::nr_from_mac( $mac ) );
222                    warn "# create kvm for $ip";
223            }
224  }  }
225    
226  1;  1;

Legend:
Removed from v.323  
changed lines
  Added in v.510

  ViewVC Help
Powered by ViewVC 1.1.26