/[vz-tools]/trunk/lib/VZ.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/VZ.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 40 - (show annotations)
Fri Feb 8 23:02:25 2008 UTC (16 years, 2 months ago) by dpavlin
File size: 1626 byte(s)
- added new common functions
- export $vz_root and $vz_conf
1 package VZ;
2 use Exporter 'import';
3 our @EXPORT = qw(
4 check_root
5 ip2hostname
6 hostname2ip
7 vzlist_configs
8 vzctl
9
10 $vz_root
11 $vz_conf
12 );
13
14 use warnings;
15 use strict;
16
17 use Socket;
18
19 our $vz_root = '/vz';
20 our $vz_conf = '/etc/vz/conf';
21
22 =head1 NAME
23
24 VZ - helper functions for VZ tools
25
26 =cut
27
28 =head1 FUNCTIONS
29
30 =head2 check_root
31
32 Restart as C<root> user if needed using C<sudo>
33
34 =cut
35
36 sub check_root {
37 if ( $> != 0 ) {
38 warn "WARNING: restarting as root using sudo\n";
39 exec 'sudo', $0, @ARGV;
40 }
41 }
42
43 =head2 hostname2ip
44
45 my $ip = hostname2ip('www.example.com');
46
47 =cut
48
49 sub hostname2ip {
50 my $hostname = shift || return;
51
52 my @addresses = gethostbyname($hostname)
53 or die "Can't resolve $hostname: $!\n";
54 @addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
55 return shift @addresses;
56 }
57
58 =head2 ip2hostname
59
60 my $hostname = ip2hostname('192.168.0.1');
61
62 =cut
63
64 sub ip2hostname {
65 my $ip = shift || return;
66
67 return gethostbyaddr(inet_aton($ip), AF_INET)
68 or die "Can't look up $ip: $!\n";
69 }
70
71 =head2 vzlist_configs
72
73 my @VEIDs = vzlist_configs;
74
75 =cut
76
77 sub vzlist_configs {
78 my @c;
79 open( my $cfgs, 'find /etc/vz/conf -maxdepth 1 -name "*.conf" |' ) || die "can't run find: $!";
80 while(<$cfgs>) {
81 chomp;
82 if ( m#^.+/(\d+)\.conf$# ) {
83 if ( -d "$vz_root/private/$1" ) {
84 push @c, $1;
85 } else {
86 warn "WARNING: have config for $1 but no private directory\n";
87 }
88 } else {
89 warn "SKIPPED: $_\n";
90 }
91 }
92 return @c;
93 }
94
95 =head2 vzctl
96
97 vzctl('set','--something',42);
98
99 =cut
100
101 sub vzctl {
102 my @args = @_;
103 warn "## vzctl ", join(" ",@args), "\n";
104 system('vzctl',@args) == 0 or die "vzctl @args failed: $?"
105 }
106
107 1;

  ViewVC Help
Powered by ViewVC 1.1.26