use File::Slurp;
use Data::Dump qw/dump/;
+use File::Path;
use server;
use format;
use ip;
use ping;
+use kvm;
our $debug = $server::debug;
$value = $default;
} elsif ( -f $path ) {
$value = read_file $path;
+ chomp $value;
} else {
warn "# $name missing $path\n" if $debug;
}
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";
+ $conf->{ $name } = read_file $file if -f $file;
}
return $conf;
}
my $mac = shift;
$mac = format::mac($mac);
- if ( my $clients_left = server::shared( 'new_clients' ) ) {
- server::shared( 'new_clients', $clients_left - 1 );
+ if ( $server::new_clients > 0 ) {
+ warn "# clients left: ", --$server::new_clients;
} else {
warn "W: no new clients accepted";
return '0.0.0.0';
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);
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";
}
- rmdir "$server::conf/ip/$ip";
+ rmtree "$server::conf/ip/$ip";
}
sub arp_mac_dev {
return $arp;
}
+sub rebuild_mac_links {
+ warn "# rebuild mac links";
+ foreach my $ip ( all_ips ) {
+ 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;