/[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 168 by dpavlin, Thu Aug 6 21:31:10 2009 UTC revision 409 by dpavlin, Wed Sep 9 10:36:00 2009 UTC
# Line 4  use warnings; Line 4  use warnings;
4  use strict;  use strict;
5  use autodie;  use autodie;
6    
 use server;  
7  use File::Slurp;  use File::Slurp;
8  use Net::Ping;  use Data::Dump qw/dump/;
9    use File::Path;
10    
11    use server;
12    use format;
13    use ip;
14    use ping;
15    use kvm;
16    
17    our $debug = $server::debug;
18    
19    sub mkbasedir {
20            my $path = shift;
21            $path =~ s{(^.*)/[^/]+$}{$1};
22            mkdir $path unless -d $path;
23            return $path;
24    }
25    
26    sub mac_path { $server::conf . '/mac/' . $_[0] }
27    sub  ip_path { $server::conf . '/ip/'  . join('/', @_) }
28    sub conf_value {
29            my $path = shift;
30            my $value;
31            if ( -l $path ) {
32                    $value = readlink $path;
33                    $value =~ s{.*/([^/]+)$}{$1};
34            } elsif ( -f $path ) {
35                    $value = read_file $path;
36            } else {
37                    warn "W: $path not file or symlink\n";
38            }
39            return $value;
40    }
41    
42  sub conf {  sub conf {
43          my $ip  = shift;          my $ip  = shift;
# Line 18  sub conf { Line 49  sub conf {
49                  $default = $_[1]                  $default = $_[1]
50          }          }
51    
52          my $path ="$server::conf/ip/$ip";          my $path = ip_path $ip;
53          mkdir $path unless -d $path;          mkdir $path unless -d $path;
54          $path .= '/' . $name;          $path .= '/' . $name;
55    
56          if ( defined $value ) {          if ( defined $value ) {
57                    mkbasedir  $path;
58                  write_file $path, $value;                  write_file $path, $value;
59                  warn "update $path = $value";                  warn "update $path = $value";
60          } elsif ( ! -e $path && defined $default ) {          } elsif ( ! -e $path && defined $default ) {
61                    mkbasedir  $path;
62                  write_file $path, $default;                  write_file $path, $default;
63                  warn "default $path = $default";                  warn "default $path = $default";
64                  $value = $default;                  $value = $default;
65          } elsif ( -e $path ) {          } elsif ( -f $path ) {
66                  if ( -l $path ) {                  $value = read_file $path;
67                          $value = readlink $path;          } else {
68                          $value =~ s{.*/([^/]+)$}{$1};                  warn "# $name missing $path\n" if $debug;
                 } else {  
                         $value = read_file $path;  
                 }  
69          }          }
70          return $value;          return $value;
71  }  }
72    
73  sub mac {  sub all_conf {
74          my $ip = shift;          my $ip = shift;
75          my $mac = client::conf( $ip, 'mac' );          my $path = ip_path $ip || return;
76          $mac =~ s{(..)}{$1:}g;          my $conf;
77          $mac =~ s{:$}{};          foreach my $file ( glob("$path/*") ) {
78          $mac = qq|<tt>$mac</tt>| if (caller(1))[3] =~ m{^httpd};                  my $name = $file;
79          return uc($mac);                  $name =~ s{^.+/([^/]+)$}{$1};
80                    $conf->{ $name } = read_file $file;
81            }
82            return $conf;
83  }  }
84    sub next_ip($) {
85  sub next_ip {          my $mac = shift;
86            $mac = format::mac($mac);
87          my $p = Net::Ping->new;  
88            if ( $server::new_clients > 0 ) {
89                    warn "# clients left: ", --$server::new_clients;
90            } else {
91                    warn "W: no new clients accepted";
92                    return '0.0.0.0';
93            }
94    
95          my $prefix = $server::ip;          my $prefix = $server::ip;
96          $prefix =~ s{\.\d+$}{.};          $prefix =~ s{\.\d+$}{.};
97          my $addr = $server::ip_from || die;          my $addr = $server::ip_from || die;
98          my $ip = $prefix . $addr;          my $ip = $prefix . $addr;
99    
100          while ( -e "$server::conf/ip/$ip" || $p->ping( $ip, 0.7 ) ) {          while ( -e ip_path($ip) || ping::host($ip) ) {
101                  $ip = $prefix . $addr++;                  $ip = $prefix . $addr++;
102                  die "all addresses allocated!" if $addr == $server::ip_to;                  die "all addresses allocated!" if $addr == $server::ip_to;
103                  warn "skip $ip\n";                  warn "skip $ip\n";
104          }          }
105    
106          warn "next_ip $ip\n";          warn "next_ip $ip\n";
107    
108            save_ip_mac( $ip, $mac );
109    
110            return $ip;
111    }
112    
113    sub save_ip_mac {
114            my ($ip,$mac) = @_;
115            $mac = format::mac($mac);
116            return if $mac eq '00:00:00:00:00:00';
117    
118            mkdir ip_path($ip) unless -e ip_path($ip);
119    
120            my $mac_path = mac_path($mac);
121            unlink $mac_path if -l $mac_path;       # XXX audit?
122            symlink ip_path($ip), $mac_path;
123            write_file( ip_path($ip,'mac'), $mac );
124    }
125    
126    sub ip_from_mac($) {
127            my $mac = shift;
128            $mac = format::mac($mac);
129    
130            my $mac_path = mac_path $mac;
131            return unless -e $mac_path;
132    
133            my $ip;
134    
135            if ( -f $mac_path ) {
136                    $ip = read_file $mac_path;
137                    unlink $mac_path;
138                    symlink ip_path($ip), $mac_path;
139                    warn "I: upgrade to mac symlink $mac_path\n";
140            } elsif ( -l $mac_path ) {
141                    $ip = conf_value $mac_path;
142            } else {
143                    die "$mac_path not file or symlink";
144            }
145    
146          return $ip;          return $ip;
147    }
148    
149    sub mac_from_ip($) {
150            my $ip = shift;
151            conf_value ip_path($ip, 'mac');
152    }
153    
154    sub change_ip($$) {
155            my ($old, $new) = @_;
156            return if $old eq $new;
157            my $mac = mac_from_ip($old) || die "no mac for $old";
158            rename ip_path($old), ip_path($new);
159            unlink mac_path($mac);
160            symlink ip_path($new), mac_path($mac);
161            return $new;
162    }
163    
164    sub all_ips {
165            sort { ip::to_int($a) cmp ip::to_int($b) }
166            map {
167                    my $ip = $_;
168                    $ip =~ s{^.+/ip/}{};
169                    autocreate_params( $ip );
170                    $ip;
171            } glob("$server::conf/ip/*")
172    }
173    
174    sub remove {
175            my $ip = shift;
176            unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
177            if ( my $mac = mac_from_ip $ip ) {
178                    unlink "$server::conf/mac/$mac";
179            }
180            rmtree "$server::conf/ip/$ip";
181    }
182    
183    sub arp_mac_dev {
184            my $arp = {
185                    map {
186                            my @c = split(/\s+/,$_);
187                            if ( $#c == 5 ) {
188                                    client::save_ip_mac( $c[0], $c[3] );
189                                    ( uc $c[3] => $c[5] )
190                            } else {
191                            }
192                    } read_file('/proc/net/arp')
193            };
194    
195            warn "# arp ",dump( $arp );
196            return $arp;
197    }
198    
199    sub rebuild_mac_links {
200            warn "# rebuild mac links";
201            foreach my $ip ( all_ips ) {
202                    my $mac = ip_path $ip, 'mac';
203                    if ( -e $mac ) {
204                            $mac = read_file $mac;
205                            save_ip_mac( $ip, $mac );
206                            warn "## $ip $mac\n";
207                    }
208            }
209    }
210    
211    sub autocreate_params {
212            my $ip = shift;
213            my $mac = mac_from_ip $ip;
214            if ( $mac =~ m{^AC:DE:48:00:00} && ! defined conf( $ip, 'kvm' ) ) {
215                    conf( $ip, 'kvm', default => kvm::nr_from_mac( $mac ) );
216                    warn "# create kvm for $ip";
217            }
218  }  }
219    
220  1;  1;

Legend:
Removed from v.168  
changed lines
  Added in v.409

  ViewVC Help
Powered by ViewVC 1.1.26