--- trunk/lib/VZ.pm 2008/02/08 21:46:09 39 +++ trunk/lib/VZ.pm 2008/02/08 23:02:25 40 @@ -2,7 +2,13 @@ use Exporter 'import'; our @EXPORT = qw( check_root +ip2hostname hostname2ip +vzlist_configs +vzctl + +$vz_root +$vz_conf ); use warnings; @@ -10,6 +16,23 @@ use Socket; +our $vz_root = '/vz'; +our $vz_conf = '/etc/vz/conf'; + +=head1 NAME + +VZ - helper functions for VZ tools + +=cut + +=head1 FUNCTIONS + +=head2 check_root + +Restart as C user if needed using C + +=cut + sub check_root { if ( $> != 0 ) { warn "WARNING: restarting as root using sudo\n"; @@ -17,12 +40,68 @@ } } +=head2 hostname2ip + + my $ip = hostname2ip('www.example.com'); + +=cut + sub hostname2ip { my $hostname = shift || return; - my @addresses = gethostbyname($hostname) or die "Can't resolve $hostname: $!\n"; + my @addresses = gethostbyname($hostname) + or die "Can't resolve $hostname: $!\n"; @addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses]; return shift @addresses; } +=head2 ip2hostname + + my $hostname = ip2hostname('192.168.0.1'); + +=cut + +sub ip2hostname { + my $ip = shift || return; + + return gethostbyaddr(inet_aton($ip), AF_INET) + or die "Can't look up $ip: $!\n"; +} + +=head2 vzlist_configs + + my @VEIDs = vzlist_configs; + +=cut + +sub vzlist_configs { + my @c; + open( my $cfgs, 'find /etc/vz/conf -maxdepth 1 -name "*.conf" |' ) || die "can't run find: $!"; + while(<$cfgs>) { + chomp; + if ( m#^.+/(\d+)\.conf$# ) { + if ( -d "$vz_root/private/$1" ) { + push @c, $1; + } else { + warn "WARNING: have config for $1 but no private directory\n"; + } + } else { + warn "SKIPPED: $_\n"; + } + } + return @c; +} + +=head2 vzctl + + vzctl('set','--something',42); + +=cut + +sub vzctl { + my @args = @_; + warn "## vzctl ", join(" ",@args), "\n"; + system('vzctl',@args) == 0 or die "vzctl @args failed: $?" +} + 1;