/[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 220 by dpavlin, Sat Aug 15 13:47:37 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 Carp qw/confess/;
10    
11    use server;
12  use format;  use format;
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] }  sub mac_path { $server::conf . '/mac/' . $_[0] }
24  sub  ip_path { $server::conf . '/ip/'  . join('/', @_) }  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                    confess "$path not file or symlink";
35            }
36            return $value;
37    }
38    
39  sub conf {  sub conf {
40          my $ip  = shift;          my $ip  = shift;
# Line 27  sub conf { Line 51  sub conf {
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    
 sub mac {  
         my $ip = shift;  
         my $mac = client::conf( $ip, 'mac' ) || return '';  
         format::mac( $ip, @_ );  
 }  
   
70  sub next_ip($) {  sub next_ip($) {
71          my $mac = shift;          my $mac = shift;
72    
# Line 70  sub next_ip($) { Line 87  sub next_ip($) {
87    
88          mkdir ip_path($ip);          mkdir ip_path($ip);
89    
90          symlink ip_path($ip), mac_path($mac);          my $mac_path = mac_path($mac);
91            unlink $mac_path if -e $mac_path;       # XXX audit?
92            symlink ip_path($ip), $mac_path;
93          write_file ip_path($ip,'mac'), $mac;          write_file ip_path($ip,'mac'), $mac;
94    
95          return $ip;          return $ip;
# Line 94  sub ip_from_mac($) { Line 113  sub ip_from_mac($) {
113                  symlink ip_path($ip), $mac_path;                  symlink ip_path($ip), $mac_path;
114                  warn "I: upgrade to mac symlink $mac_path\n";                  warn "I: upgrade to mac symlink $mac_path\n";
115          } elsif ( -l $mac_path ) {          } elsif ( -l $mac_path ) {
116                  $ip = readlink $mac_path;                  $ip = conf_value $mac_path;
                 $ip =~ s{^.+/([^/]+)$}{$1};  
117          } else {          } else {
118                  die "$mac_path not file or symlink";                  die "$mac_path not file or symlink";
119          }          }
# Line 105  sub ip_from_mac($) { Line 123  sub ip_from_mac($) {
123    
124  sub mac_from_ip($) {  sub mac_from_ip($) {
125          my $ip = shift;          my $ip = shift;
126          return read_file ip_path($ip, 'mac');          conf_value ip_path($ip, 'mac');
127  }  }
128    
129  sub change_ip($$) {  sub change_ip($$) {

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

  ViewVC Help
Powered by ViewVC 1.1.26