--- lib/PXElator/server.pm 2009/08/04 13:18:36 134 +++ lib/PXElator/server.pm 2009/09/08 17:30:14 396 @@ -3,21 +3,93 @@ use warnings; use strict; +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; + +warn "DEV $ip $bcast $netmask"; + +our $conf = "$base_dir/conf"; +mkdir $conf unless -e $conf; + +sub conf { + warn "## conf $conf"; + $conf; +} + use Module::Refresh qw//; -sub x_refresh { Module::Refresh->refresh }; +sub refresh { + Module::Refresh->refresh; + my $from = (caller(1))[3]; + $from =~ s{^(\w+)::.+$}{$1}; + my $eval = '$' . $from . '::debug = server::debug();'; + warn "refresh $eval\n"; + eval $eval; + warn $@ if $@; +}; + +mkdir $_ foreach grep { ! -d $_ } map { "$conf/$_" } ( 'ip', 'mac' ); + +use File::Slurp; +sub shared { + my ($name, $value) = @_; + + my $path ="$conf/$name"; + if ( defined $value ) { + write_file $path, $value; + warn "update $path = $value"; + } else { + $value = read_file $path if -e $path; + } + return $value; +} -our $ip = '172.16.10.1'; -our $netmask = '255.255.255.0'; +sub conf_default { shared($_[0]) || $_[1] } -our ( $ip_from, $ip_to ) = ( 10, 100 ); +sub debug { shared('debug', @_) || 0 } -our $base_dir = '/srv/pxelator'; +warn "loaded"; -use config; -our $debug; -sub debug { $debug = config::shared('debug', @_) || 0 } +package server::tie; -warn "loaded"; +use File::Slurp; +use Data::Dump qw/dump/; + +use server; + +sub TIESCALAR { + warn dump @_; + my ($class,$name,$default) = @_; + + 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; +} + +sub STORE { + warn dump @_; + my ( $self, $value ) = @_; + write_file $$self->{path}, $value; +} + +sub FETCH { + warn dump @_; + my $self = shift; + read_file $$self->{path}; +} -1; +3;