ec904e5dca12950cc820133c45a328b3368aff35
[pxelator] / lib / PXElator / server.pm
1 package server;
2
3 use warnings;
4 use strict;
5
6 our $base_dir = '/srv/pxelator';
7
8 use ties;
9
10 tie our $ip,      'ties', 'server_ip' => '172.16.10.1';
11 tie our $netmask, 'ties', 'netmask'   => '255.255.255.0';
12 tie our $bcast,   'ties', 'bcast'     => '172.16.10.254';
13 tie our $ip_from, 'ties', 'ip_from'   => 10;
14 tie our $ip_to,   'ties', 'ip_to'     => 100;
15 tie our $domain,  'ties', 'domain'    => 'pxelator.lan';
16 tie our $new_clients, 'ties', 'new_clients' => $ip_to - $ip_from;
17
18 warn "DEV $ip $bcast $netmask";
19
20 our $conf = "$base_dir/conf";
21 mkdir $conf unless -e $conf;
22
23 sub conf {
24         warn "## conf $conf";
25         $conf;
26 }
27
28 use Module::Refresh qw//;
29 sub refresh {
30         Module::Refresh->refresh;
31         my $from = (caller(1))[3];
32         $from =~ s{^(\w+)::.+$}{$1};
33         my $eval = '$' . $from . '::debug = server::debug();';
34         warn "refresh $eval\n";
35         eval $eval;
36         warn $@ if $@;
37 };
38
39 mkdir $_ foreach grep { ! -d $_ } map { "$conf/$_" } ( 'ip', 'mac' );
40
41 use File::Slurp;
42 sub shared {
43         my ($name, $value) = @_;
44
45         my $path ="$conf/$name";
46         if ( defined $value ) {
47                 write_file $path, $value;
48                 warn "update $path = $value";
49         } else {
50                 $value = read_file $path if -e $path;
51         }
52         return $value;
53 }
54
55 sub conf_default { shared($_[0]) || $_[1] }
56
57 sub debug { shared('debug', @_) || 0 }
58
59 warn "loaded";
60
61 1;