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

Legend:
Removed from v.194  
changed lines
  Added in v.313

  ViewVC Help
Powered by ViewVC 1.1.26