3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
|
use Data::Dump qw(dump); |
7 |
|
use YAML qw(); |
8 |
|
|
9 |
our $base_dir = '/srv/pxelator'; |
our $base_dir = '/srv/pxelator'; |
10 |
|
|
11 |
tie our $ip, 'server::tie', 'server_ip' => '172.16.10.1'; |
use ties; |
12 |
tie our $netmask, 'server::tie', 'natmask' => '255.255.255.0'; |
|
13 |
tie our $bcast, 'server::tie', 'bcast' => '172.16.10.254'; |
tie our $ip, 'ties', 'server_ip' => '172.16.10.1'; |
14 |
tie our $ip_from, 'server::tie', 'ip_from' => 10; |
tie our $netmask, 'ties', 'netmask' => '255.255.255.0'; |
15 |
tie our $ip_to, 'server::tie', 'ip_to' => 100; |
tie our $bcast, 'ties', 'bcast' => '172.16.10.254'; |
16 |
tie our $domain, 'server::tie', 'domain' => 'pxelator.lan'; |
tie our $ip_from, 'ties', 'ip_from' => 10; |
17 |
tie our $new_clients, 'server::tie', 'new_clients' => $ip_to - $ip_from; |
tie our $ip_to, 'ties', 'ip_to' => 100; |
18 |
|
tie our $domain, 'ties', 'domain' => 'pxelator.lan'; |
19 |
|
tie our $new_clients, 'ties', 'new_clients' => $ip_to - $ip_from; |
20 |
|
|
21 |
|
if ( my $dev = $ENV{DEV} ) { |
22 |
|
my $ifconfig = `ifconfig $dev`; |
23 |
|
die "can't ifconfig $dev" unless $ifconfig; |
24 |
|
die "device $dev not up: ifconfig $dev\n$ifconfig" unless $ifconfig =~ m/UP/; |
25 |
|
$ip = $1 if $ifconfig =~ m/inet addr:(\S+)/s; |
26 |
|
$netmask = $1 if $ifconfig =~ m/Mask:(\S+)/s; |
27 |
|
$bcast = $1 if $ifconfig =~ m/Bcast:(\S+)/s; |
28 |
|
} |
29 |
|
|
30 |
warn "DEV $ip $bcast $netmask"; |
warn "DEV $ip $bcast $netmask"; |
31 |
|
|
68 |
|
|
69 |
sub debug { shared('debug', @_) || 0 } |
sub debug { shared('debug', @_) || 0 } |
70 |
|
|
71 |
warn "loaded"; |
sub as_hash_for { |
72 |
|
my $ip = shift; |
|
|
|
|
package server::tie; |
|
|
|
|
|
use File::Slurp; |
|
|
use Data::Dump qw/dump/; |
|
73 |
|
|
74 |
use server; |
my $server; |
75 |
|
map { $server->{ $_ } = eval '$server::' . $_ } ( 'ip', 'netmask', 'bcast', 'domain', 'ip_from', 'ip_to', 'new_clients' ); |
76 |
|
|
77 |
sub TIESCALAR { |
my $server_path = "$server::conf/ip/$ip/server.yaml"; |
78 |
warn dump @_; |
$server = YAML::LoadFile $server_path if -e $server_path; |
|
my ($class,$name,$default) = @_; |
|
79 |
|
|
80 |
my $path = $server::base_dir . '/conf/' . $name; |
return $server; |
|
|
|
|
my $o = { |
|
|
path => $path, |
|
|
}; |
|
|
write_file $o->{path}, $default unless -f $o->{path}; |
|
|
|
|
|
warn "TIESCALAR $name ", $o->{path}, " [$default]"; |
|
|
|
|
|
bless \$o,$class; |
|
81 |
} |
} |
82 |
|
|
83 |
sub STORE { |
warn "loaded"; |
|
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; |
|
|
} |
|
84 |
|
|
85 |
3; |
1; |