/[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 305 - (hide annotations)
Thu Aug 27 14:30:55 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 2826 byte(s)
added all_conf which return all configuration variables for single IP address

1 dpavlin 156 package client;
2    
3     use warnings;
4     use strict;
5     use autodie;
6    
7     use File::Slurp;
8 dpavlin 168 use Net::Ping;
9 dpavlin 219
10     use server;
11 dpavlin 208 use format;
12 dpavlin 156
13 dpavlin 220 our $debug = $server::debug;
14    
15 dpavlin 219 sub mkbasedir {
16     my $path = shift;
17     $path =~ s{(^.*)/[^/]+$}{$1};
18     mkdir $path unless -d $path;
19     return $path;
20     }
21    
22 dpavlin 200 sub mac_path { $server::conf . '/mac/' . $_[0] }
23     sub ip_path { $server::conf . '/ip/' . join('/', @_) }
24 dpavlin 219 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 dpavlin 244 warn "W: $path not file or symlink\n";
34 dpavlin 219 }
35     return $value;
36     }
37 dpavlin 200
38 dpavlin 156 sub conf {
39     my $ip = shift;
40     my $name = shift;
41     my ( $default, $value );
42     if ( $#_ == 0 ) {
43     $value = shift;
44     } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
45     $default = $_[1]
46     }
47    
48 dpavlin 200 my $path = ip_path $ip;
49 dpavlin 156 mkdir $path unless -d $path;
50     $path .= '/' . $name;
51    
52     if ( defined $value ) {
53 dpavlin 219 mkbasedir $path;
54 dpavlin 156 write_file $path, $value;
55     warn "update $path = $value";
56     } elsif ( ! -e $path && defined $default ) {
57 dpavlin 219 mkbasedir $path;
58 dpavlin 156 write_file $path, $default;
59     warn "default $path = $default";
60     $value = $default;
61 dpavlin 219 } elsif ( -f $path ) {
62     $value = read_file $path;
63     } else {
64 dpavlin 220 warn "# $name missing $path\n" if $debug;
65 dpavlin 156 }
66     return $value;
67     }
68    
69 dpavlin 305 sub all_conf {
70     my $ip = shift;
71     my $path = ip_path $ip || return;
72     my $conf;
73     foreach my $file ( glob("$path/*") ) {
74     my $name = $file;
75     $name =~ s{^.+/([^/]+)$}{$1};
76     $conf->{ $name } = read_file $file;
77     }
78     return $conf;
79     }
80 dpavlin 200 sub next_ip($) {
81     my $mac = shift;
82 dpavlin 168
83     my $p = Net::Ping->new;
84    
85     my $prefix = $server::ip;
86     $prefix =~ s{\.\d+$}{.};
87     my $addr = $server::ip_from || die;
88     my $ip = $prefix . $addr;
89    
90 dpavlin 200 while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
91 dpavlin 168 $ip = $prefix . $addr++;
92     die "all addresses allocated!" if $addr == $server::ip_to;
93     warn "skip $ip\n";
94     }
95    
96     warn "next_ip $ip\n";
97 dpavlin 200
98 dpavlin 244 save_ip_mac( $ip, $mac );
99    
100     return $ip;
101     }
102    
103     sub save_ip_mac {
104     my ($ip,$mac) = @_;
105    
106 dpavlin 254 mkdir ip_path($ip) unless -e ip_path($ip);
107 dpavlin 200
108 dpavlin 217 my $mac_path = mac_path($mac);
109 dpavlin 286 unlink $mac_path if -l $mac_path; # XXX audit?
110 dpavlin 217 symlink ip_path($ip), $mac_path;
111 dpavlin 200 write_file ip_path($ip,'mac'), $mac;
112 dpavlin 168 }
113    
114 dpavlin 200 sub ip_from_mac($) {
115 dpavlin 194 my $mac = shift;
116    
117     $mac = lc $mac;
118     $mac =~ s{:}{}g;
119    
120 dpavlin 200 my $mac_path = mac_path $mac;
121 dpavlin 194 return unless -e $mac_path;
122    
123     my $ip;
124    
125     if ( -f $mac_path ) {
126     $ip = read_file $mac_path;
127     unlink $mac_path;
128 dpavlin 200 symlink ip_path($ip), $mac_path;
129 dpavlin 194 warn "I: upgrade to mac symlink $mac_path\n";
130     } elsif ( -l $mac_path ) {
131 dpavlin 219 $ip = conf_value $mac_path;
132 dpavlin 194 } else {
133     die "$mac_path not file or symlink";
134     }
135    
136     return $ip;
137     }
138    
139 dpavlin 200 sub mac_from_ip($) {
140     my $ip = shift;
141 dpavlin 219 conf_value ip_path($ip, 'mac');
142 dpavlin 200 }
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 dpavlin 156 1;

  ViewVC Help
Powered by ViewVC 1.1.26