/[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 208 by dpavlin, Wed Aug 12 23:59:01 2009 UTC revision 329 by dpavlin, Fri Aug 28 21:55:41 2009 UTC
# Line 4  use warnings; Line 4  use warnings;
4  use strict;  use strict;
5  use autodie;  use autodie;
6    
 use server;  
7  use File::Slurp;  use File::Slurp;
8  use Net::Ping;  use Net::Ping;
9    use Data::Dump qw/dump/;
10    
11    use server;
12  use format;  use format;
13    use ip;
14    
15    our $debug = $server::debug;
16    
17    sub mkbasedir {
18            my $path = shift;
19            $path =~ s{(^.*)/[^/]+$}{$1};
20            mkdir $path unless -d $path;
21            return $path;
22    }
23    
24  sub mac_path { $server::conf . '/mac/' . $_[0] }  sub mac_path { $server::conf . '/mac/' . $_[0] }
25  sub  ip_path { $server::conf . '/ip/'  . join('/', @_) }  sub  ip_path { $server::conf . '/ip/'  . join('/', @_) }
26    sub conf_value {
27            my $path = shift;
28            my $value;
29            if ( -l $path ) {
30                    $value = readlink $path;
31                    $value =~ s{.*/([^/]+)$}{$1};
32            } elsif ( -f $path ) {
33                    $value = read_file $path;
34            } else {
35                    warn "W: $path not file or symlink\n";
36            }
37            return $value;
38    }
39    
40  sub conf {  sub conf {
41          my $ip  = shift;          my $ip  = shift;
# Line 27  sub conf { Line 52  sub conf {
52          $path .= '/' . $name;          $path .= '/' . $name;
53    
54          if ( defined $value ) {          if ( defined $value ) {
55                    mkbasedir  $path;
56                  write_file $path, $value;                  write_file $path, $value;
57                  warn "update $path = $value";                  warn "update $path = $value";
58          } elsif ( ! -e $path && defined $default ) {          } elsif ( ! -e $path && defined $default ) {
59                    mkbasedir  $path;
60                  write_file $path, $default;                  write_file $path, $default;
61                  warn "default $path = $default";                  warn "default $path = $default";
62                  $value = $default;                  $value = $default;
63          } elsif ( -e $path ) {          } elsif ( -f $path ) {
64                  if ( -l $path ) {                  $value = read_file $path;
65                          $value = readlink $path;          } else {
66                          $value =~ s{.*/([^/]+)$}{$1};                  warn "# $name missing $path\n" if $debug;
                 } else {  
                         $value = read_file $path;  
                 }  
67          }          }
68          return $value;          return $value;
69  }  }
70    
71  sub mac {  sub all_conf {
72          my $ip = shift;          my $ip = shift;
73          my $mac = client::conf( $ip, 'mac' ) || return '';          my $path = ip_path $ip || return;
74          format::mac( $ip, @_ );          my $conf;
75            foreach my $file ( glob("$path/*") ) {
76                    my $name = $file;
77                    $name =~ s{^.+/([^/]+)$}{$1};
78                    $conf->{ $name } = read_file $file;
79            }
80            return $conf;
81  }  }
   
82  sub next_ip($) {  sub next_ip($) {
83          my $mac = shift;          my $mac = shift;
84            $mac = format::mac($mac);
85    
86          my $p = Net::Ping->new;          my $p = Net::Ping->new;
87    
# Line 68  sub next_ip($) { Line 98  sub next_ip($) {
98    
99          warn "next_ip $ip\n";          warn "next_ip $ip\n";
100    
101          mkdir ip_path($ip);          save_ip_mac( $ip, $mac );
   
         symlink ip_path($ip), mac_path($mac);  
         write_file ip_path($ip,'mac'), $mac;  
102    
103          return $ip;          return $ip;
104    }
105    
106    sub save_ip_mac {
107            my ($ip,$mac) = @_;
108            $mac = format::mac($mac);
109            return if $mac eq '00:00:00:00:00:00';
110    
111            mkdir ip_path($ip) unless -e ip_path($ip);
112    
113            my $mac_path = mac_path($mac);
114            unlink $mac_path if -l $mac_path;       # XXX audit?
115            symlink ip_path($ip), $mac_path;
116            write_file( ip_path($ip,'mac'), $mac );
117  }  }
118    
119  sub ip_from_mac($) {  sub ip_from_mac($) {
120          my $mac = shift;          my $mac = shift;
121            $mac = format::mac($mac);
         $mac = lc $mac;  
         $mac =~ s{:}{}g;  
122    
123          my $mac_path = mac_path $mac;          my $mac_path = mac_path $mac;
124          return unless -e $mac_path;          return unless -e $mac_path;
# Line 94  sub ip_from_mac($) { Line 131  sub ip_from_mac($) {
131                  symlink ip_path($ip), $mac_path;                  symlink ip_path($ip), $mac_path;
132                  warn "I: upgrade to mac symlink $mac_path\n";                  warn "I: upgrade to mac symlink $mac_path\n";
133          } elsif ( -l $mac_path ) {          } elsif ( -l $mac_path ) {
134                  $ip = readlink $mac_path;                  $ip = conf_value $mac_path;
                 $ip =~ s{^.+/([^/]+)$}{$1};  
135          } else {          } else {
136                  die "$mac_path not file or symlink";                  die "$mac_path not file or symlink";
137          }          }
# Line 105  sub ip_from_mac($) { Line 141  sub ip_from_mac($) {
141    
142  sub mac_from_ip($) {  sub mac_from_ip($) {
143          my $ip = shift;          my $ip = shift;
144          return read_file ip_path($ip, 'mac');          conf_value ip_path($ip, 'mac');
145  }  }
146    
147  sub change_ip($$) {  sub change_ip($$) {
148          my ($old, $new) = @_;          my ($old, $new) = @_;
149          my $mac = mac_from_ip($old);          return if $old eq $new;
150            my $mac = mac_from_ip($old) || die "no mac for $old";
151          rename ip_path($old), ip_path($new);          rename ip_path($old), ip_path($new);
152          unlink mac_path($mac);          unlink mac_path($mac);
153          symlink ip_path($new), mac_path($mac);          symlink ip_path($new), mac_path($mac);
154            return $new;
155    }
156    
157    sub all_ips {
158            sort { ip::to_int($a) cmp ip::to_int($b) }
159            map {
160                    my $ip = $_;
161                    $ip =~ s{^.+/ip/}{};
162                    $ip;
163            } glob("$server::conf/ip/*")
164    }
165    
166    sub remove {
167            my $ip = shift;
168            unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
169            if ( my $mac = mac_from_ip $ip ) {
170                    unlink "$server::conf/mac/$mac";
171            }
172            rmdir "$server::conf/ip/$ip";
173    }
174    
175    sub arp_mac_dev {
176            my $arp = {
177                    map {
178                            my @c = split(/\s+/,$_);
179                            if ( $#c == 5 ) {
180                                    client::save_ip_mac( $c[0], $c[3] );
181                                    ( uc $c[3] => $c[5] )
182                            } else {
183                            }
184                    } read_file('/proc/net/arp')
185            };
186    
187            warn "# arp ",dump( $arp );
188            return $arp;
189  }  }
190    
191  1;  1;

Legend:
Removed from v.208  
changed lines
  Added in v.329

  ViewVC Help
Powered by ViewVC 1.1.26