/[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

Annotation of /lib/PXElator/client.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 217 - (hide annotations)
Thu Aug 13 13:32:19 2009 UTC (14 years, 9 months ago) by dpavlin
File size: 2148 byte(s)
cleanup
1 dpavlin 156 package client;
2    
3     use warnings;
4     use strict;
5     use autodie;
6    
7     use server;
8     use File::Slurp;
9 dpavlin 168 use Net::Ping;
10 dpavlin 208 use format;
11 dpavlin 156
12 dpavlin 200 sub mac_path { $server::conf . '/mac/' . $_[0] }
13     sub ip_path { $server::conf . '/ip/' . join('/', @_) }
14    
15 dpavlin 156 sub conf {
16     my $ip = shift;
17     my $name = shift;
18     my ( $default, $value );
19     if ( $#_ == 0 ) {
20     $value = shift;
21     } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
22     $default = $_[1]
23     }
24    
25 dpavlin 200 my $path = ip_path $ip;
26 dpavlin 156 mkdir $path unless -d $path;
27     $path .= '/' . $name;
28    
29     if ( defined $value ) {
30     write_file $path, $value;
31     warn "update $path = $value";
32     } elsif ( ! -e $path && defined $default ) {
33     write_file $path, $default;
34     warn "default $path = $default";
35     $value = $default;
36 dpavlin 164 } elsif ( -e $path ) {
37     if ( -l $path ) {
38     $value = readlink $path;
39     $value =~ s{.*/([^/]+)$}{$1};
40     } else {
41     $value = read_file $path;
42     }
43 dpavlin 156 }
44     return $value;
45     }
46    
47 dpavlin 200 sub next_ip($) {
48     my $mac = shift;
49 dpavlin 168
50     my $p = Net::Ping->new;
51    
52     my $prefix = $server::ip;
53     $prefix =~ s{\.\d+$}{.};
54     my $addr = $server::ip_from || die;
55     my $ip = $prefix . $addr;
56    
57 dpavlin 200 while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
58 dpavlin 168 $ip = $prefix . $addr++;
59     die "all addresses allocated!" if $addr == $server::ip_to;
60     warn "skip $ip\n";
61     }
62    
63     warn "next_ip $ip\n";
64 dpavlin 200
65     mkdir ip_path($ip);
66    
67 dpavlin 217 my $mac_path = mac_path($mac);
68     unlink $mac_path if -e $mac_path; # XXX audit?
69     symlink ip_path($ip), $mac_path;
70 dpavlin 200 write_file ip_path($ip,'mac'), $mac;
71    
72 dpavlin 168 return $ip;
73    
74     }
75    
76 dpavlin 200 sub ip_from_mac($) {
77 dpavlin 194 my $mac = shift;
78    
79     $mac = lc $mac;
80     $mac =~ s{:}{}g;
81    
82 dpavlin 200 my $mac_path = mac_path $mac;
83 dpavlin 194 return unless -e $mac_path;
84    
85     my $ip;
86    
87     if ( -f $mac_path ) {
88     $ip = read_file $mac_path;
89     unlink $mac_path;
90 dpavlin 200 symlink ip_path($ip), $mac_path;
91 dpavlin 194 warn "I: upgrade to mac symlink $mac_path\n";
92     } elsif ( -l $mac_path ) {
93     $ip = readlink $mac_path;
94     $ip =~ s{^.+/([^/]+)$}{$1};
95     } else {
96     die "$mac_path not file or symlink";
97     }
98    
99     return $ip;
100     }
101    
102 dpavlin 200 sub mac_from_ip($) {
103     my $ip = shift;
104     return read_file ip_path($ip, 'mac');
105     }
106    
107     sub change_ip($$) {
108     my ($old, $new) = @_;
109     my $mac = mac_from_ip($old);
110     rename ip_path($old), ip_path($new);
111     unlink mac_path($mac);
112     symlink ip_path($new), mac_path($mac);
113     }
114    
115 dpavlin 156 1;

  ViewVC Help
Powered by ViewVC 1.1.26