/[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 244 by dpavlin, Mon Aug 17 13:27:18 2009 UTC revision 443 by dpavlin, Tue Sep 29 19:28:24 2009 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;
14    use ping;
15    use kvm;
16    
17  our $debug = $server::debug;  our $debug = $server::debug;
18    
# Line 60  sub conf { Line 64  sub conf {
64                  $value = $default;                  $value = $default;
65          } elsif ( -f $path ) {          } elsif ( -f $path ) {
66                  $value = read_file $path;                  $value = read_file $path;
67                    chomp $value;
68          } else {          } else {
69                  warn "# $name missing $path\n" if $debug;                  warn "# $name missing $path\n" if $debug;
70          }          }
71          return $value;          return $value;
72  }  }
73    
74    sub all_conf {
75            my $ip = shift;
76            my $path = ip_path $ip || return;
77            my $conf;
78            foreach my $file ( glob("$path/*"), glob("$path/*/*") ) {
79                    my $name = $file;
80                    $name =~ s{^$path/+}{} || die "can't remove $path from $name";
81                    $conf->{ $name } = read_file $file if -f $file;
82            }
83            return $conf;
84    }
85  sub next_ip($) {  sub next_ip($) {
86          my $mac = shift;          my $mac = shift;
87            $mac = format::mac($mac);
88    
89          my $p = Net::Ping->new;          if ( $server::new_clients > 0 ) {
90                    warn "# clients left: ", --$server::new_clients;
91            } else {
92                    warn "W: no new clients accepted";
93                    return '0.0.0.0';
94            }
95    
96          my $prefix = $server::ip;          my $prefix = $server::ip;
97          $prefix =~ s{\.\d+$}{.};          $prefix =~ s{\.\d+$}{.};
98          my $addr = $server::ip_from || die;          my $addr = $server::ip_from || die;
99          my $ip = $prefix . $addr;          my $ip = $prefix . $addr;
100    
101          while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {          while ( -e ip_path($ip) || ping::host($ip) ) {
102                  $ip = $prefix . $addr++;                  $ip = $prefix . $addr++;
103                  die "all addresses allocated!" if $addr == $server::ip_to;                  die "all addresses allocated!" if $addr == $server::ip_to;
104                  warn "skip $ip\n";                  warn "skip $ip\n";
# Line 91  sub next_ip($) { Line 113  sub next_ip($) {
113    
114  sub save_ip_mac {  sub save_ip_mac {
115          my ($ip,$mac) = @_;          my ($ip,$mac) = @_;
116            $mac = format::mac($mac);
117            return if $mac eq '00:00:00:00:00:00' || $ip eq '0.0.0.0';
118    
119          mkdir ip_path($ip);          mkdir ip_path($ip) unless -e ip_path($ip);
120    
121          my $mac_path = mac_path($mac);          my $mac_path = mac_path($mac);
122          unlink $mac_path if -e $mac_path;       # XXX audit?          unlink $mac_path if -l $mac_path;       # XXX audit?
123          symlink ip_path($ip), $mac_path;          symlink ip_path($ip), $mac_path;
124          write_file ip_path($ip,'mac'), $mac;          write_file( ip_path($ip,'mac'), $mac );
125  }  }
126    
127  sub ip_from_mac($) {  sub ip_from_mac($) {
128          my $mac = shift;          my $mac = shift;
129            $mac = format::mac($mac);
         $mac = lc $mac;  
         $mac =~ s{:}{}g;  
130    
131          my $mac_path = mac_path $mac;          my $mac_path = mac_path $mac;
132          return unless -e $mac_path;          return unless -e $mac_path;
# Line 132  sub mac_from_ip($) { Line 154  sub mac_from_ip($) {
154    
155  sub change_ip($$) {  sub change_ip($$) {
156          my ($old, $new) = @_;          my ($old, $new) = @_;
157          my $mac = mac_from_ip($old);          return if $old eq $new;
158            my $mac = mac_from_ip($old) || die "no mac for $old";
159          rename ip_path($old), ip_path($new);          rename ip_path($old), ip_path($new);
160          unlink mac_path($mac);          unlink mac_path($mac);
161          symlink ip_path($new), mac_path($mac);          symlink ip_path($new), mac_path($mac);
162            return $new;
163    }
164    
165    sub all_ips {
166            sort { ip::to_int($a) cmp ip::to_int($b) }
167            map {
168                    my $ip = $_;
169                    $ip =~ s{^.+/ip/}{};
170                    autocreate_params( $ip );
171                    $ip;
172            } glob("$server::conf/ip/*")
173    }
174    
175    sub remove {
176            my $ip = shift;
177            if ( my $mac = mac_from_ip $ip ) {
178                    unlink "$server::conf/mac/$mac";
179            }
180            rmtree "$server::conf/ip/$ip";
181    }
182    
183    sub arp_mac_dev {
184            my $arp = {
185                    map {
186                            my @c = split(/\s+/,$_);
187                            if ( $#c == 5 ) {
188                                    client::save_ip_mac( $c[0], $c[3] );
189                                    ( uc $c[3] => $c[5] )
190                            } else {
191                            }
192                    } read_file('/proc/net/arp')
193            };
194    
195            warn "# arp ",dump( $arp );
196            return $arp;
197    }
198    
199    sub rebuild_mac_links {
200            warn "# rebuild mac links";
201            foreach my $ip ( all_ips ) {
202                    my $mac = ip_path $ip, 'mac';
203                    if ( -e $mac ) {
204                            $mac = read_file $mac;
205                            save_ip_mac( $ip, $mac );
206                            warn "## $ip $mac\n";
207                    }
208            }
209    }
210    
211    sub autocreate_params {
212            my $ip = shift;
213            my $mac = mac_from_ip $ip;
214            if ( $mac =~ m{^AC:DE:48:00:00} && ! defined conf( $ip, 'kvm' ) ) {
215                    conf( $ip, 'kvm', default => kvm::nr_from_mac( $mac ) );
216                    warn "# create kvm for $ip";
217            }
218  }  }
219    
220  1;  1;

Legend:
Removed from v.244  
changed lines
  Added in v.443

  ViewVC Help
Powered by ViewVC 1.1.26