implement client::conf to fetch/set (with default) client configuration
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 6 Aug 2009 15:15:53 +0000 (15:15 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 6 Aug 2009 15:15:53 +0000 (15:15 +0000)
lib/PXElator/client.pm [new file with mode: 0644]
lib/PXElator/config.pm
lib/PXElator/dhcpd.pm
lib/PXElator/server.pm
lib/PXElator/t/client.t [new file with mode: 0755]

diff --git a/lib/PXElator/client.pm b/lib/PXElator/client.pm
new file mode 100644 (file)
index 0000000..9000639
--- /dev/null
@@ -0,0 +1,37 @@
+package client;
+
+use warnings;
+use strict;
+use autodie;
+
+use server;
+use File::Slurp;
+
+sub conf {
+       my $ip  = shift;
+       my $name = shift;
+       my ( $default, $value );
+       if ( $#_ == 0 ) {
+               $value = shift;
+       } elsif ( $#_ == 1 && $_[0] eq 'default' ) {
+               $default = $_[1]
+       }
+
+       my $path ="$server::conf/ip/$ip";
+       mkdir $path unless -d $path;
+       $path .= '/' . $name;
+
+       if ( defined $value ) {
+               write_file $path, $value;
+               warn "update $path = $value";
+       } elsif ( ! -e $path && defined $default ) {
+               write_file $path, $default;
+               warn "default $path = $default";
+               $value = $default;
+       } else {
+               $value = read_file $path if -e $path;
+       }
+       return $value;
+}
+
+1;
index c02b794..09f0fd4 100644 (file)
@@ -6,6 +6,7 @@ use autodie;
 
 use server;
 use pxelinux;
+use client;
 use File::Slurp;
 
 sub available { qw/debian_live webconverger debirf tinycore/ };
@@ -31,8 +32,8 @@ sub webconverger {
 
        upstream::iso( 'http://download.webconverger.com/webc-5.2.iso' );
 
-       my $homepage = server::shared( "$ip/homepage" ) || "http://${server::ip}:7777/client";
-       my $hostname = server::shared( "$ip/hostname" ) || 'webconverger';
+       my $homepage = client::conf( $ip => 'homepage', default => "http://${server::ip}:7777/client" );
+       my $hostname = client::conf( $ip => 'hostname', default => 'webconverger' );
 
        pxelinux::config_for_ip( $ip, qq{
 
@@ -83,7 +84,7 @@ label linux
 
 sub for_ip {
        my $ip = shift;
-       my $deploy = server::shared( "deploy/$ip" ) || 'webconverger';
+       my $deploy = client::conf( $ip => 'deploy', default => 'webconverger' );
        eval $deploy . '($ip)';
 #      $tftp::dir = "$server::base_dir/tftp/$pxelinux::path_prefix";
 }
index 2934aaf..83f1cd3 100644 (file)
@@ -40,7 +40,7 @@ my $addr = $server::ip_from;
 sub client_ip {
        my ( $mac ) = @_;
 
-       my $conf = "$server::base_dir/conf/$server::ip";
+       my $conf = $server::conf;
        mkdir $conf unless -e $conf;
 
        if ( -e "$conf/mac/$mac" ) {
@@ -54,6 +54,7 @@ sub client_ip {
        my $prefix = $server::ip;
        $prefix =~ s{\.\d+$}{.};
        my $ip = $prefix . $addr;
+
        while ( -e "$conf/ip/$ip" || $p->ping( $ip ) ) {
                $ip = $prefix . $addr++;
                die "all addresses allocated!" if $addr == $server::ip_to;
@@ -61,11 +62,14 @@ sub client_ip {
 
        write_file "$conf/mac/$mac", $ip;
 
-       if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac" ) {
-               unlink     "$conf/ip/$ip";
-               warn "$mac IP changed from ", readlink "$conf/ip/$ip", " to $ip";
+       my $ip_path = "$conf/ip/$ip";
+       mkdir $ip_path unless -e $ip_path;
+
+       if ( -l "$ip_path/mac" && readlink "$ip_path/mac" ne "$conf/mac/$mac" ) {
+               warn "$mac IP changed from ", readlink "$ip_path/mac", " to $ip";
+               unlink "$ip_path/mac";
        };
-       symlink    "$conf/mac/$mac", "$conf/ip/$ip";
+       symlink "$conf/mac/$mac", "$ip_path/mac";
 
        print "$mac NEW $ip\n";
 
index e8674e8..dda409c 100644 (file)
@@ -13,16 +13,19 @@ our $domain_name = 'pxelator.lan';
 
 our $base_dir = '/srv/pxelator';
 
+our $conf = "$base_dir/conf/$ip";
+
 use Module::Refresh qw//;
 sub refresh { Module::Refresh->refresh };
 
-mkdir $_ foreach grep { ! -e $_ } map { "$base_dir/conf/$server::ip/$_" } ( 'ip', 'mac', 'hostname', 'deploy' );
+
+mkdir $_ foreach grep { ! -d $_ } map { "$conf/$_" } ( 'ip', 'mac' );
 
 use File::Slurp;
 sub shared {
        my ($name, $value) = @_;
 
-       my $path ="$base_dir/conf/$server::ip/$name";
+       my $path ="$conf/$name";
        if ( defined $value ) {
                write_file $path, $value;
                warn "update $path = $value";
diff --git a/lib/PXElator/t/client.t b/lib/PXElator/t/client.t
new file mode 100755 (executable)
index 0000000..dda359e
--- /dev/null
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use autodie;
+
+use Test::More tests => 7;
+use Data::Dump qw/dump/;
+
+use_ok 'client';
+
+my $host = '127.0.0.1';
+my $test_path = "$server::base_dir/conf/$server::ip/ip/$host/test";
+unlink $test_path if -e $test_path;
+
+ok( client::conf( $host => 'test', default => 'default' ), 'conf default' );
+cmp_ok( client::conf( $host => 'test' ), 'eq', 'default', 'default' );
+ok( client::conf( $host => 'test' => 'value' ), 'conf set' );
+cmp_ok( client::conf( $host => 'test' ), 'eq', 'value', 'value' );
+
+diag "cleanup";
+ok( unlink($test_path), "unlink $test_path" );
+$test_path =~ s{/[^/]+$}{};
+ok( rmdir($test_path), "rmdir $test_path" );