X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=lib%2FPXElator%2Fserver.pm;h=b42367457a24baebabc371e84ac5cf5f1a29a103;hb=9ae27c8736b87d2e1d065a5899395f599139a1e8;hp=a292a604df11152eb1320ffa5e4a19a8301884fc;hpb=96d4b9506d48f35e6575c4f770366686510fefb1;p=pxelator diff --git a/lib/PXElator/server.pm b/lib/PXElator/server.pm index a292a60..b423674 100644 --- a/lib/PXElator/server.pm +++ b/lib/PXElator/server.pm @@ -1,10 +1,85 @@ package server; -our $ip = '172.16.10.1'; -our $netmask = '255.255.255.0'; +use warnings; +use strict; -our ( $ip_from, $ip_to ) = ( 10, 100 ); +use Data::Dump qw(dump); +use YAML qw(); -our $base_dir = '/home/dpavlin/llin/pxelator'; +our $base_dir = '/srv/pxelator'; + +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"; + +our $conf = "$base_dir/conf"; +mkdir $conf unless -e $conf; + +sub conf { + warn "## conf $conf"; + $conf; +} + +use Module::Refresh qw//; +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; +} + +sub conf_default { shared($_[0]) || $_[1] } + +sub debug { shared('debug', @_) || 0 } + +sub as_hash_for { + my $ip = shift; + + my $server; + map { $server->{ $_ } = eval '$server::' . $_ } ( 'ip', 'netmask', 'bcast', 'domain', 'ip_from', 'ip_to', 'new_clients' ); + + my $server_path = "$server::conf/ip/$ip/server.yaml"; + $server = YAML::LoadFile $server_path if -e $server_path; + + return $server; +} warn "loaded"; + +1;