--- lib/PXElator/server.pm 2009/09/09 10:13:08 408 +++ lib/PXElator/server.pm 2010/09/17 18:20:36 530 @@ -3,15 +3,29 @@ use warnings; use strict; +use Data::Dump qw(dump); +use YAML qw(); + our $base_dir = '/srv/pxelator'; -tie our $ip, 'server::tie', 'server_ip' => '172.16.10.1'; -tie our $netmask, 'server::tie', 'natmask' => '255.255.255.0'; -tie our $bcast, 'server::tie', 'bcast' => '172.16.10.254'; -tie our $ip_from, 'server::tie', 'ip_from' => 10; -tie our $ip_to, 'server::tie', 'ip_to' => 100; -tie our $domain, 'server::tie', 'domain' => 'pxelator.lan'; -tie our $new_clients, 'server::tie', 'new_clients' => $ip_to - $ip_from; +use ties; + +tie our $ip, 'ties', 'server_ip' => '172.16.10.1'; +tie our $netmask, 'ties', 'netmask' => '255.255.255.0'; +tie our $bcast, 'ties', 'bcast' => '172.16.10.254'; +tie our $ip_from, 'ties', 'ip_from' => 10; +tie our $ip_to, 'ties', 'ip_to' => 100; +tie our $domain, 'ties', 'domain' => 'pxelator.lan'; +tie our $new_clients, 'ties', 'new_clients' => $ip_to - $ip_from; + +if ( my $dev = $ENV{DEV} ) { + my $ifconfig = `ifconfig $dev`; + die "can't ifconfig $dev" unless $ifconfig; + die "device $dev not up: ifconfig $dev\n$ifconfig" unless $ifconfig =~ m/UP/; + $ip = $1 if $ifconfig =~ m/inet addr:(\S+)/s; + $netmask = $1 if $ifconfig =~ m/Mask:(\S+)/s; + $bcast = $1 if $ifconfig =~ m/Bcast:(\S+)/s; +} warn "DEV $ip $bcast $netmask"; @@ -54,44 +68,18 @@ sub debug { shared('debug', @_) || 0 } -warn "loaded"; - - -package server::tie; - -use File::Slurp; -use Data::Dump qw/dump/; +sub as_hash_for { + my $ip = shift; -use server; + my $server; + map { $server->{ $_ } = eval '$server::' . $_ } ( 'ip', 'netmask', 'bcast', 'domain', 'ip_from', 'ip_to', 'new_clients' ); -sub TIESCALAR { - warn dump @_; - my ($class,$name,$default) = @_; + my $server_path = "$server::conf/ip/$ip/server.yaml"; + $server = YAML::LoadFile $server_path if -e $server_path; - my $path = $server::base_dir . '/conf/' . $name; - - my $o = { - path => $path, - }; - write_file $o->{path}, $default unless -f $o->{path}; - -warn "TIESCALAR $name ", $o->{path}, " [$default]"; - - bless \$o,$class; + return $server; } -sub STORE { - warn dump @_; - my ( $self, $value ) = @_; - write_file $$self->{path}, $value; -} - -sub FETCH { - warn dump @_; - my $self = shift; - my $v = read_file $$self->{path}; - chomp($v); - $v; -} +warn "loaded"; -3; +1;