/[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 168 - (hide annotations)
Thu Aug 6 21:31:10 2009 UTC (14 years, 9 months ago) by dpavlin
File size: 1324 byte(s)
extracted client::next_ip
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 156
11     sub conf {
12     my $ip = shift;
13     my $name = shift;
14     my ( $default, $value );
15     if ( $#_ == 0 ) {
16     $value = shift;
17     } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
18     $default = $_[1]
19     }
20    
21     my $path ="$server::conf/ip/$ip";
22     mkdir $path unless -d $path;
23     $path .= '/' . $name;
24    
25     if ( defined $value ) {
26     write_file $path, $value;
27     warn "update $path = $value";
28     } elsif ( ! -e $path && defined $default ) {
29     write_file $path, $default;
30     warn "default $path = $default";
31     $value = $default;
32 dpavlin 164 } elsif ( -e $path ) {
33     if ( -l $path ) {
34     $value = readlink $path;
35     $value =~ s{.*/([^/]+)$}{$1};
36     } else {
37     $value = read_file $path;
38     }
39 dpavlin 156 }
40     return $value;
41     }
42    
43 dpavlin 165 sub mac {
44     my $ip = shift;
45     my $mac = client::conf( $ip, 'mac' );
46     $mac =~ s{(..)}{$1:}g;
47     $mac =~ s{:$}{};
48     $mac = qq|<tt>$mac</tt>| if (caller(1))[3] =~ m{^httpd};
49     return uc($mac);
50     }
51    
52 dpavlin 168 sub next_ip {
53    
54     my $p = Net::Ping->new;
55    
56     my $prefix = $server::ip;
57     $prefix =~ s{\.\d+$}{.};
58     my $addr = $server::ip_from || die;
59     my $ip = $prefix . $addr;
60    
61     while ( -e "$server::conf/ip/$ip" || $p->ping( $ip, 0.7 ) ) {
62     $ip = $prefix . $addr++;
63     die "all addresses allocated!" if $addr == $server::ip_to;
64     warn "skip $ip\n";
65     }
66    
67     warn "next_ip $ip\n";
68     return $ip;
69    
70     }
71    
72 dpavlin 156 1;

  ViewVC Help
Powered by ViewVC 1.1.26