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

Contents of /lib/PXElator/client.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 220 - (show annotations)
Sat Aug 15 13:47:37 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 2526 byte(s)
fix handling of non-existing configuration files and test it
1 package client;
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 use File::Slurp;
8 use Net::Ping;
9 use Carp qw/confess/;
10
11 use server;
12 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] }
24 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 {
40 my $ip = shift;
41 my $name = shift;
42 my ( $default, $value );
43 if ( $#_ == 0 ) {
44 $value = shift;
45 } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
46 $default = $_[1]
47 }
48
49 my $path = ip_path $ip;
50 mkdir $path unless -d $path;
51 $path .= '/' . $name;
52
53 if ( defined $value ) {
54 mkbasedir $path;
55 write_file $path, $value;
56 warn "update $path = $value";
57 } elsif ( ! -e $path && defined $default ) {
58 mkbasedir $path;
59 write_file $path, $default;
60 warn "default $path = $default";
61 $value = $default;
62 } elsif ( -f $path ) {
63 $value = read_file $path;
64 } else {
65 warn "# $name missing $path\n" if $debug;
66 }
67 return $value;
68 }
69
70 sub next_ip($) {
71 my $mac = shift;
72
73 my $p = Net::Ping->new;
74
75 my $prefix = $server::ip;
76 $prefix =~ s{\.\d+$}{.};
77 my $addr = $server::ip_from || die;
78 my $ip = $prefix . $addr;
79
80 while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {
81 $ip = $prefix . $addr++;
82 die "all addresses allocated!" if $addr == $server::ip_to;
83 warn "skip $ip\n";
84 }
85
86 warn "next_ip $ip\n";
87
88 mkdir ip_path($ip);
89
90 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;
94
95 return $ip;
96
97 }
98
99 sub ip_from_mac($) {
100 my $mac = shift;
101
102 $mac = lc $mac;
103 $mac =~ s{:}{}g;
104
105 my $mac_path = mac_path $mac;
106 return unless -e $mac_path;
107
108 my $ip;
109
110 if ( -f $mac_path ) {
111 $ip = read_file $mac_path;
112 unlink $mac_path;
113 symlink ip_path($ip), $mac_path;
114 warn "I: upgrade to mac symlink $mac_path\n";
115 } elsif ( -l $mac_path ) {
116 $ip = conf_value $mac_path;
117 } else {
118 die "$mac_path not file or symlink";
119 }
120
121 return $ip;
122 }
123
124 sub mac_from_ip($) {
125 my $ip = shift;
126 conf_value ip_path($ip, 'mac');
127 }
128
129 sub change_ip($$) {
130 my ($old, $new) = @_;
131 my $mac = mac_from_ip($old);
132 rename ip_path($old), ip_path($new);
133 unlink mac_path($mac);
134 symlink ip_path($new), mac_path($mac);
135 }
136
137 1;

  ViewVC Help
Powered by ViewVC 1.1.26