/[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 168 by dpavlin, Thu Aug 6 21:31:10 2009 UTC revision 325 by dpavlin, Fri Aug 28 19:36:36 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    
10    use server;
11    use format;
12    use ip;
13    
14    our $debug = $server::debug;
15    
16    sub mkbasedir {
17            my $path = shift;
18            $path =~ s{(^.*)/[^/]+$}{$1};
19            mkdir $path unless -d $path;
20            return $path;
21    }
22    
23    sub mac_path { $server::conf . '/mac/' . $_[0] }
24    sub  ip_path { $server::conf . '/ip/'  . join('/', @_) }
25    sub conf_value {
26            my $path = shift;
27            my $value;
28            if ( -l $path ) {
29                    $value = readlink $path;
30                    $value =~ s{.*/([^/]+)$}{$1};
31            } elsif ( -f $path ) {
32                    $value = read_file $path;
33            } else {
34                    warn "W: $path not file or symlink\n";
35            }
36            return $value;
37    }
38    
39  sub conf {  sub conf {
40          my $ip  = shift;          my $ip  = shift;
41          my $name = shift;          my $name = shift;
# Line 18  sub conf { Line 46  sub conf {
46                  $default = $_[1]                  $default = $_[1]
47          }          }
48    
49          my $path ="$server::conf/ip/$ip";          my $path = ip_path $ip;
50          mkdir $path unless -d $path;          mkdir $path unless -d $path;
51          $path .= '/' . $name;          $path .= '/' . $name;
52    
53          if ( defined $value ) {          if ( defined $value ) {
54                    mkbasedir  $path;
55                  write_file $path, $value;                  write_file $path, $value;
56                  warn "update $path = $value";                  warn "update $path = $value";
57          } elsif ( ! -e $path && defined $default ) {          } elsif ( ! -e $path && defined $default ) {
58                    mkbasedir  $path;
59                  write_file $path, $default;                  write_file $path, $default;
60                  warn "default $path = $default";                  warn "default $path = $default";
61                  $value = $default;                  $value = $default;
62          } elsif ( -e $path ) {          } elsif ( -f $path ) {
63                  if ( -l $path ) {                  $value = read_file $path;
64                          $value = readlink $path;          } else {
65                          $value =~ s{.*/([^/]+)$}{$1};                  warn "# $name missing $path\n" if $debug;
                 } else {  
                         $value = read_file $path;  
                 }  
66          }          }
67          return $value;          return $value;
68  }  }
69    
70  sub mac {  sub all_conf {
71          my $ip = shift;          my $ip = shift;
72          my $mac = client::conf( $ip, 'mac' );          my $path = ip_path $ip || return;
73          $mac =~ s{(..)}{$1:}g;          my $conf;
74          $mac =~ s{:$}{};          foreach my $file ( glob("$path/*") ) {
75          $mac = qq|<tt>$mac</tt>| if (caller(1))[3] =~ m{^httpd};                  my $name = $file;
76          return uc($mac);                  $name =~ s{^.+/([^/]+)$}{$1};
77                    $conf->{ $name } = read_file $file;
78            }
79            return $conf;
80  }  }
81    sub next_ip($) {
82  sub next_ip {          my $mac = shift;
83            $mac = format::mac($mac);
84    
85          my $p = Net::Ping->new;          my $p = Net::Ping->new;
86    
# Line 58  sub next_ip { Line 89  sub next_ip {
89          my $addr = $server::ip_from || die;          my $addr = $server::ip_from || die;
90          my $ip = $prefix . $addr;          my $ip = $prefix . $addr;
91    
92          while ( -e "$server::conf/ip/$ip" || $p->ping( $ip, 0.7 ) ) {          while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
93                  $ip = $prefix . $addr++;                  $ip = $prefix . $addr++;
94                  die "all addresses allocated!" if $addr == $server::ip_to;                  die "all addresses allocated!" if $addr == $server::ip_to;
95                  warn "skip $ip\n";                  warn "skip $ip\n";
96          }          }
97    
98          warn "next_ip $ip\n";          warn "next_ip $ip\n";
99    
100            save_ip_mac( $ip, $mac );
101    
102            return $ip;
103    }
104    
105    sub save_ip_mac {
106            my ($ip,$mac) = @_;
107            $mac = format::mac($mac);
108            return if $mac eq '00:00:00:00:00:00';
109    
110            mkdir ip_path($ip) unless -e ip_path($ip);
111    
112            my $mac_path = mac_path($mac);
113            unlink $mac_path if -l $mac_path;       # XXX audit?
114            symlink ip_path($ip), $mac_path;
115            write_file( ip_path($ip,'mac'), $mac );
116    }
117    
118    sub ip_from_mac($) {
119            my $mac = shift;
120            $mac = format::mac($mac);
121    
122            my $mac_path = mac_path $mac;
123            return unless -e $mac_path;
124    
125            my $ip;
126    
127            if ( -f $mac_path ) {
128                    $ip = read_file $mac_path;
129                    unlink $mac_path;
130                    symlink ip_path($ip), $mac_path;
131                    warn "I: upgrade to mac symlink $mac_path\n";
132            } elsif ( -l $mac_path ) {
133                    $ip = conf_value $mac_path;
134            } else {
135                    die "$mac_path not file or symlink";
136            }
137    
138          return $ip;          return $ip;
139    }
140    
141    sub mac_from_ip($) {
142            my $ip = shift;
143            conf_value ip_path($ip, 'mac');
144    }
145    
146    sub change_ip($$) {
147            my ($old, $new) = @_;
148            return if $old eq $new;
149            my $mac = mac_from_ip($old) || die "no mac for $old";
150            rename ip_path($old), ip_path($new);
151            unlink mac_path($mac);
152            symlink ip_path($new), mac_path($mac);
153            return $new;
154    }
155    
156    sub all_ips {
157            sort { ip::to_int($a) cmp ip::to_int($b) }
158            map {
159                    my $ip = $_;
160                    $ip =~ s{^.+/ip/}{};
161                    $ip;
162            } glob("$server::conf/ip/*")
163    }
164    
165    sub remove {
166            my $ip = shift;
167            unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
168            if ( my $mac = mac_from_ip $ip ) {
169                    unlink "$server::conf/mac/$mac";
170            }
171            rmdir "$server::conf/ip/$ip";
172  }  }
173    
174  1;  1;

Legend:
Removed from v.168  
changed lines
  Added in v.325

  ViewVC Help
Powered by ViewVC 1.1.26