/[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 164 by dpavlin, Thu Aug 6 18:43:55 2009 UTC revision 200 by dpavlin, Tue Aug 11 15:55:26 2009 UTC
# Line 6  use autodie; Line 6  use autodie;
6    
7  use server;  use server;
8  use File::Slurp;  use File::Slurp;
9    use Net::Ping;
10    
11    sub mac_path { $server::conf . '/mac/' . $_[0] }
12    sub  ip_path { $server::conf . '/ip/'  . join('/', @_) }
13    
14  sub conf {  sub conf {
15          my $ip  = shift;          my $ip  = shift;
# Line 17  sub conf { Line 21  sub conf {
21                  $default = $_[1]                  $default = $_[1]
22          }          }
23    
24          my $path ="$server::conf/ip/$ip";          my $path = ip_path $ip;
25          mkdir $path unless -d $path;          mkdir $path unless -d $path;
26          $path .= '/' . $name;          $path .= '/' . $name;
27    
# Line 39  sub conf { Line 43  sub conf {
43          return $value;          return $value;
44  }  }
45    
46    sub mac {
47            my ( $ip, $op ) = @_;
48            $op ||= 'html';
49            my $mac = client::conf( $ip, 'mac' );
50            return '' unless $mac;
51            $mac =~ s{(..)}{$1:}g;
52            $mac =~ s{:$}{};
53            $mac = qq|<tt>$mac</tt>| if (caller(1))[3] =~ m{^httpd} && $op ne 'clean';
54            return uc($mac);
55    }
56    
57    sub next_ip($) {
58            my $mac = shift;
59    
60            my $p = Net::Ping->new;
61    
62            my $prefix = $server::ip;
63            $prefix =~ s{\.\d+$}{.};
64            my $addr = $server::ip_from || die;
65            my $ip = $prefix . $addr;
66    
67            while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
68                    $ip = $prefix . $addr++;
69                    die "all addresses allocated!" if $addr == $server::ip_to;
70                    warn "skip $ip\n";
71            }
72    
73            warn "next_ip $ip\n";
74    
75            mkdir ip_path($ip);
76    
77            symlink ip_path($ip), mac_path($mac);
78            write_file ip_path($ip,'mac'), $mac;
79    
80            return $ip;
81    
82    }
83    
84    sub ip_from_mac($) {
85            my $mac = shift;
86    
87            $mac = lc $mac;
88            $mac =~ s{:}{}g;
89    
90            my $mac_path = mac_path $mac;
91            return unless -e $mac_path;
92    
93            my $ip;
94    
95            if ( -f $mac_path ) {
96                    $ip = read_file $mac_path;
97                    unlink $mac_path;
98                    symlink ip_path($ip), $mac_path;
99                    warn "I: upgrade to mac symlink $mac_path\n";
100            } elsif ( -l $mac_path ) {
101                    $ip = readlink $mac_path;
102                    $ip =~ s{^.+/([^/]+)$}{$1};
103            } else {
104                    die "$mac_path not file or symlink";
105            }
106    
107            return $ip;
108    }
109    
110    sub mac_from_ip($) {
111            my $ip = shift;
112            return read_file ip_path($ip, 'mac');
113    }
114    
115    sub change_ip($$) {
116            my ($old, $new) = @_;
117            my $mac = mac_from_ip($old);
118            rename ip_path($old), ip_path($new);
119            unlink mac_path($mac);
120            symlink ip_path($new), mac_path($mac);
121    }
122    
123  1;  1;

Legend:
Removed from v.164  
changed lines
  Added in v.200

  ViewVC Help
Powered by ViewVC 1.1.26