skip directories in all_conf
[pxelator] / lib / PXElator / client.pm
index cc889a7..89c3498 100644 (file)
@@ -12,6 +12,7 @@ use server;
 use format;
 use ip;
 use ping;
+use kvm;
 
 our $debug = $server::debug;
 
@@ -49,7 +50,8 @@ sub conf {
        }
 
        my $path = ip_path $ip;
-       mkdir $path unless -d $path;
+       mkdir $path unless -e $path;
+       warn "WARNING: $path not directory" unless -d $path;
        $path .= '/' . $name;
 
        if ( defined $value ) {
@@ -61,8 +63,11 @@ sub conf {
                write_file $path, $default;
                warn "default $path = $default";
                $value = $default;
+       } elsif ( -l $path ) {
+               $value = readlink $path;
        } elsif ( -f $path ) {
                $value = read_file $path;
+               chomp $value;
        } else {
                warn "# $name missing $path\n" if $debug;
        }
@@ -73,10 +78,14 @@ sub all_conf {
        my $ip = shift;
        my $path = ip_path $ip || return;
        my $conf;
-       foreach my $file ( glob("$path/*") ) {
+       foreach my $file ( glob("$path/*"), glob("$path/*/*") ) {
                my $name = $file;
-               $name =~ s{^.+/([^/]+)$}{$1};
-               $conf->{ $name } = read_file $file;
+               $name =~ s{^$path/+}{} || die "can't remove $path from $name";
+               next if -d $file;
+               $conf->{ $name } =
+                       -l $file ? readlink  $file :
+                       -f $file ? read_file $file :
+                       '?';
        }
        return $conf;
 }
@@ -84,8 +93,8 @@ sub next_ip($) {
        my $mac = shift;
        $mac = format::mac($mac);
 
-       if ( $server::new_clients-- ) {
-               warn "# clients left: $server::new_clients\n";
+       if ( $server::new_clients > 0 ) {
+               warn "# clients left: ", --$server::new_clients;
        } else {
                warn "W: no new clients accepted";
                return '0.0.0.0';
@@ -112,7 +121,7 @@ sub next_ip($) {
 sub save_ip_mac {
        my ($ip,$mac) = @_;
        $mac = format::mac($mac);
-       return if $mac eq '00:00:00:00:00:00';
+       return if $mac eq '00:00:00:00:00:00' || $ip eq '0.0.0.0';
 
        mkdir ip_path($ip) unless -e ip_path($ip);
 
@@ -165,13 +174,13 @@ sub all_ips {
        map {
                my $ip = $_;
                $ip =~ s{^.+/ip/}{};
+               autocreate_params( $ip );
                $ip;
        } glob("$server::conf/ip/*") 
 }
 
 sub remove {
        my $ip = shift;
-       unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
        if ( my $mac = mac_from_ip $ip ) {
                unlink "$server::conf/mac/$mac";
        }
@@ -200,10 +209,20 @@ sub rebuild_mac_links {
                my $mac = ip_path $ip, 'mac';
                if ( -e $mac ) {
                        $mac = read_file $mac;
+                       chomp $mac;
                        save_ip_mac( $ip, $mac );
                        warn "## $ip $mac\n";
                }
        }
 }
 
+sub autocreate_params {
+       my $ip = shift;
+       my $mac = mac_from_ip $ip;
+       if ( $mac =~ m{^AC:DE:48:00:00} && ! defined conf( $ip, 'kvm' ) ) {
+               conf( $ip, 'kvm', default => kvm::nr_from_mac( $mac ) );
+               warn "# create kvm for $ip";
+       }
+}
+
 1;