first step towards configurable server (broken in it's current state)
[pxelator] / lib / PXElator / server.pm
index ca57c60..581a086 100644 (file)
@@ -3,29 +3,25 @@ package server;
 use warnings;
 use strict;
 
+our $base_dir = '/srv/pxelator';
 
-our $ip      = '172.16.10.1';
-our $netmask = '255.255.255.0';
-our $bcast   = '172.16.10.255';
-
-sub setup_from_dev {
-       my $dev = shift || return;
-       my $ifconfig = `/sbin/ifconfig $dev`;
-       ( $ip, $bcast, $netmask ) = ( $1, $2, $3 ) if $ifconfig =~ m{inet addr:(\S+)\s+Bcast:(\S+)\s+Mask:(\S+)}s;
-       warn "DEV $dev $ip $bcast $netmask";
-}
-
-setup_from_dev( $ENV{DEV} || 'virtual' );
-
-our ( $ip_from, $ip_to ) = ( 10, 100 );
-
-our $domain_name = 'pxelator.lan';
+tie our $ip,      'server::tie', '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';
 
-our $base_dir = '/srv/pxelator';
+warn "DEV $ip $bcast $netmask";
 
 our $conf = "$base_dir/conf/$ip";
 mkdir $conf unless -e $conf;
 
+sub conf {
+       warn "## conf $conf";
+       $conf;
+}
+
 use Module::Refresh qw//;
 sub refresh {
        Module::Refresh->refresh;
@@ -59,4 +55,45 @@ sub debug { shared('debug', @_) || 0 }
 
 warn "loaded";
 
-1;
+
+package server::tie;
+
+use File::Slurp;
+use Data::Dump qw/dump/;
+
+use server;
+
+sub TIESCALAR {
+       warn dump @_;
+       my ($class,$name,$default) = @_;
+
+       my $path = $server::base_dir;
+       if ( $name eq 'ip' ) {
+               $path .= '/conf/' . $ip;
+       } else {
+               $path .= '/conf/' . $server::ip;
+       }
+
+       my $o = {
+               path => "$path/$name",
+       };
+       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};
+}
+
+3;