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

Legend:
Removed from v.165  
changed lines
  Added in v.219

  ViewVC Help
Powered by ViewVC 1.1.26