From 0a24d45d8b52fafeb40633f7e75a1b2007708a72 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Mon, 10 Aug 2009 17:30:01 +0000 Subject: [PATCH] added client::ip_from_mac with database upgrade so that macs are now symlinks --- lib/PXElator/client.pm | 26 ++++++++++++++++++++++++++ lib/PXElator/dhcpd.pm | 5 ++--- lib/PXElator/t/client.t | 5 ++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/PXElator/client.pm b/lib/PXElator/client.pm index b1c9c93..cdd7761 100644 --- a/lib/PXElator/client.pm +++ b/lib/PXElator/client.pm @@ -71,4 +71,30 @@ sub next_ip { } +sub ip_from_mac { + my $mac = shift; + + $mac = lc $mac; + $mac =~ s{:}{}g; + + my $mac_path = "$server::conf/mac/$mac"; + return unless -e $mac_path; + + my $ip; + + if ( -f $mac_path ) { + $ip = read_file $mac_path; + unlink $mac_path; + symlink "$server::conf/ip/$ip", $mac_path; + warn "I: upgrade to mac symlink $mac_path\n"; + } elsif ( -l $mac_path ) { + $ip = readlink $mac_path; + $ip =~ s{^.+/([^/]+)$}{$1}; + } else { + die "$mac_path not file or symlink"; + } + + return $ip; +} + 1; diff --git a/lib/PXElator/dhcpd.pm b/lib/PXElator/dhcpd.pm index b4931a6..b9b5774 100644 --- a/lib/PXElator/dhcpd.pm +++ b/lib/PXElator/dhcpd.pm @@ -44,14 +44,13 @@ sub client_ip { my $ip; - if ( -e "$conf/mac/$mac" ) { - $ip = read_file "$conf/mac/$mac"; + if ( $ip = client::ip_from_mac( $mac ) ) { print "RENEW $mac $ip\n"; return $ip; } else { $ip = client::next_ip; print "NEW $mac $ip\n"; - write_file "$conf/mac/$mac", $ip; + symlink "$conf/ip/$ip", "$conf/mac/$mac"; } my $ip_path = "$conf/ip/$ip"; diff --git a/lib/PXElator/t/client.t b/lib/PXElator/t/client.t index c1d64eb..46c370f 100755 --- a/lib/PXElator/t/client.t +++ b/lib/PXElator/t/client.t @@ -4,7 +4,7 @@ use warnings; use strict; use autodie; -use Test::More tests => 8; +use Test::More tests => 9; use Data::Dump qw/dump/; use_ok 'client'; @@ -25,3 +25,6 @@ diag "cleanup"; ok( unlink($test_path), "unlink $test_path" ); $test_path =~ s{/[^/]+$}{}; ok( rmdir($test_path), "rmdir $test_path" ); + +ok( my $ip = client::ip_from_mac( 'AC:DE:48:00:00:00' ), 'ip_from_mac' ); +diag $ip; -- 2.20.1