--- /dev/null
+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;
use server;
use pxelinux;
+use client;
use File::Slurp;
sub available { qw/debian_live webconverger debirf tinycore/ };
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{
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";
}
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" ) {
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;
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";
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";
--- /dev/null
+#!/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" );