/[pxelator]/lib/PXElator/server.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /lib/PXElator/server.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 567 - (hide annotations)
Wed Nov 9 15:11:09 2011 UTC (10 years ago) by dpavlin
File size: 2026 byte(s)
use server.yaml as overlay instead of full replacement
1 dpavlin 42 package server;
2    
3 dpavlin 67 use warnings;
4     use strict;
5    
6 dpavlin 457 use Data::Dump qw(dump);
7     use YAML qw();
8    
9 dpavlin 393 our $base_dir = '/srv/pxelator';
10 dpavlin 118
11 dpavlin 410 use ties;
12 dpavlin 42
13 dpavlin 410 tie our $ip, 'ties', 'server_ip' => '172.16.10.1';
14 dpavlin 416 tie our $netmask, 'ties', 'netmask' => '255.255.255.0';
15 dpavlin 410 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 dpavlin 527 if ( my $dev = $ENV{DEV} ) {
22     my $ifconfig = `ifconfig $dev`;
23     die "can't ifconfig $dev" unless $ifconfig;
24 dpavlin 528 die "device $dev not up: ifconfig $dev\n$ifconfig" unless $ifconfig =~ m/UP/;
25 dpavlin 527 $ip = $1 if $ifconfig =~ m/inet addr:(\S+)/s;
26     $netmask = $1 if $ifconfig =~ m/Mask:(\S+)/s;
27 dpavlin 530 $bcast = $1 if $ifconfig =~ m/Bcast:(\S+)/s;
28 dpavlin 527 }
29    
30 dpavlin 393 warn "DEV $ip $bcast $netmask";
31 dpavlin 179
32 dpavlin 394 our $conf = "$base_dir/conf";
33 dpavlin 198 mkdir $conf unless -e $conf;
34 dpavlin 156
35 dpavlin 393 sub conf {
36     warn "## conf $conf";
37     $conf;
38     }
39    
40 dpavlin 138 use Module::Refresh qw//;
41 dpavlin 264 sub refresh {
42     Module::Refresh->refresh;
43     my $from = (caller(1))[3];
44     $from =~ s{^(\w+)::.+$}{$1};
45     my $eval = '$' . $from . '::debug = server::debug();';
46     warn "refresh $eval\n";
47     eval $eval;
48     warn $@ if $@;
49     };
50 dpavlin 74
51 dpavlin 156 mkdir $_ foreach grep { ! -d $_ } map { "$conf/$_" } ( 'ip', 'mac' );
52    
53 dpavlin 138 use File::Slurp;
54     sub shared {
55     my ($name, $value) = @_;
56 dpavlin 67
57 dpavlin 156 my $path ="$conf/$name";
58 dpavlin 138 if ( defined $value ) {
59     write_file $path, $value;
60 dpavlin 153 warn "update $path = $value";
61 dpavlin 138 } else {
62     $value = read_file $path if -e $path;
63     }
64     return $value;
65     }
66    
67 dpavlin 270 sub conf_default { shared($_[0]) || $_[1] }
68    
69 dpavlin 138 sub debug { shared('debug', @_) || 0 }
70    
71 dpavlin 457 sub as_hash_for {
72     my $ip = shift;
73    
74     my $server;
75 dpavlin 458 map { $server->{ $_ } = eval '$server::' . $_ } ( 'ip', 'netmask', 'bcast', 'domain', 'ip_from', 'ip_to', 'new_clients' );
76 dpavlin 457
77     my $server_path = "$server::conf/ip/$ip/server.yaml";
78 dpavlin 567 if ( -e $server_path ) {
79     my $overlay = YAML::LoadFile $server_path;
80     $server->{$_} = $overlay->{$_} foreach keys %$overlay;
81     }
82 dpavlin 457
83     return $server;
84     }
85    
86 dpavlin 45 warn "loaded";
87 dpavlin 134
88 dpavlin 410 1;

  ViewVC Help
Powered by ViewVC 1.1.26