refactor code to make clinet::change_ip which works
[pxelator] / lib / PXElator / client.pm
1 package client;
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 use server;
8 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 {
15         my $ip  = shift;
16         my $name = shift;
17         my ( $default, $value );
18         if ( $#_ == 0 ) {
19                 $value = shift;
20         } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
21                 $default = $_[1]
22         }
23
24         my $path = ip_path $ip;
25         mkdir $path unless -d $path;
26         $path .= '/' . $name;
27
28         if ( defined $value ) {
29                 write_file $path, $value;
30                 warn "update $path = $value";
31         } elsif ( ! -e $path && defined $default ) {
32                 write_file $path, $default;
33                 warn "default $path = $default";
34                 $value = $default;
35         } elsif ( -e $path ) {
36                 if ( -l $path ) {
37                         $value = readlink $path;
38                         $value =~ s{.*/([^/]+)$}{$1};
39                 } else {
40                         $value = read_file $path;
41                 }
42         }
43         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;