disable Module::Refresh
[pxelator] / lib / PXElator / server.pm
1 package server;
2
3 use warnings;
4 use strict;
5
6 use Data::Dump qw(dump);
7 use YAML qw();
8
9 our $base_dir = '/srv/pxelator';
10
11 use ties;
12
13 tie our $ip,      'ties', 'server_ip' => '172.16.10.1';
14 tie our $netmask, 'ties', 'netmask'   => '255.255.255.0';
15 tie our $bcast,   'ties', 'bcast'     => '172.16.10.254';
16 tie our $ip_from, 'ties', 'ip_from'   => 10;
17 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";
31
32 our $conf = "$base_dir/conf";
33 mkdir $conf unless -e $conf;
34
35 sub conf {
36         warn "## conf $conf";
37         $conf;
38 }
39
40 #use Module::Refresh qw//;
41 sub refresh {
42         return 1;
43 #       Module::Refresh->refresh;
44 #       my $from = (caller(1))[3];
45 #       $from =~ s{^(\w+)::.+$}{$1};
46 #       my $eval = '$' . $from . '::debug = server::debug();';
47 #       warn "refresh $eval\n";
48 #       eval $eval;
49 #       warn $@ if $@;
50 };
51
52 mkdir $_ foreach grep { ! -d $_ } map { "$conf/$_" } ( 'ip', 'mac' );
53
54 use File::Slurp;
55 sub shared {
56         my ($name, $value) = @_;
57
58         my $path ="$conf/$name";
59         if ( defined $value ) {
60                 write_file $path, $value;
61                 warn "update $path = $value";
62         } else {
63                 $value = read_file $path if -e $path;
64         }
65         return $value;
66 }
67
68 sub conf_default { shared($_[0]) || $_[1] }
69
70 sub debug { shared('debug', @_) || 0 }
71
72 sub as_hash_for {
73         my $ip = shift;
74
75         my $server;
76         map { $server->{ $_ } = eval '$server::' . $_ } ( 'ip', 'netmask', 'bcast', 'domain', 'ip_from', 'ip_to', 'new_clients' );
77
78         my $server_path = "$server::conf/ip/$ip/server.yaml";
79         if ( -e $server_path ) {
80                 my $overlay = YAML::LoadFile $server_path;
81                 $server->{$_} = $overlay->{$_} foreach keys %$overlay;
82         }
83
84         return $server;
85 }
86
87 warn "loaded";
88
89 1;