/[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

Annotation of /trunk/lib/VZ.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 42 - (hide annotations)
Fri Feb 8 23:53:40 2008 UTC (16 years, 2 months ago) by dpavlin
File size: 2428 byte(s)
- added vzlist
- added tests
1 dpavlin 38 package VZ;
2     use Exporter 'import';
3     our @EXPORT = qw(
4     check_root
5 dpavlin 42
6 dpavlin 40 ip2hostname
7 dpavlin 38 hostname2ip
8 dpavlin 42
9     conf_veids
10    
11 dpavlin 40 vzctl
12 dpavlin 42 vzlist
13 dpavlin 40
14     $vz_root
15     $vz_conf
16 dpavlin 42
17     dump
18 dpavlin 38 );
19    
20     use warnings;
21     use strict;
22    
23     use Socket;
24 dpavlin 42 use Data::Dump qw/dump/;
25 dpavlin 38
26 dpavlin 40 our $vz_root = '/vz';
27     our $vz_conf = '/etc/vz/conf';
28    
29     =head1 NAME
30    
31     VZ - helper functions for VZ tools
32    
33     =cut
34    
35     =head1 FUNCTIONS
36    
37     =head2 check_root
38    
39     Restart as C<root> user if needed using C<sudo>
40    
41     =cut
42    
43 dpavlin 38 sub check_root {
44     if ( $> != 0 ) {
45     warn "WARNING: restarting as root using sudo\n";
46     exec 'sudo', $0, @ARGV;
47     }
48     }
49    
50 dpavlin 40 =head2 hostname2ip
51    
52     my $ip = hostname2ip('www.example.com');
53    
54     =cut
55    
56 dpavlin 38 sub hostname2ip {
57     my $hostname = shift || return;
58    
59 dpavlin 40 my @addresses = gethostbyname($hostname)
60     or die "Can't resolve $hostname: $!\n";
61 dpavlin 38 @addresses = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
62     return shift @addresses;
63     }
64    
65 dpavlin 40 =head2 ip2hostname
66    
67     my $hostname = ip2hostname('192.168.0.1');
68    
69     =cut
70    
71     sub ip2hostname {
72     my $ip = shift || return;
73    
74 dpavlin 42 my $hostname = gethostbyaddr(inet_aton($ip), AF_INET)
75 dpavlin 40 or die "Can't look up $ip: $!\n";
76 dpavlin 42 return $hostname;
77 dpavlin 40 }
78    
79 dpavlin 42 =head2 conf_veids
80 dpavlin 40
81 dpavlin 42 my @VEIDs = conf_veids;
82 dpavlin 40
83     =cut
84    
85 dpavlin 42 sub conf_veids {
86 dpavlin 40 my @c;
87     open( my $cfgs, 'find /etc/vz/conf -maxdepth 1 -name "*.conf" |' ) || die "can't run find: $!";
88     while(<$cfgs>) {
89     chomp;
90     if ( m#^.+/(\d+)\.conf$# ) {
91     if ( -d "$vz_root/private/$1" ) {
92     push @c, $1;
93     } else {
94     warn "WARNING: have config for $1 but no private directory\n";
95     }
96     } else {
97     warn "SKIPPED: $_\n";
98     }
99     }
100     return @c;
101     }
102    
103     =head2 vzctl
104    
105     vzctl('set','--something',42);
106    
107     =cut
108    
109     sub vzctl {
110     my @args = @_;
111     warn "## vzctl ", join(" ",@args), "\n";
112     system('vzctl',@args) == 0 or die "vzctl @args failed: $?"
113     }
114    
115 dpavlin 42 =head2 vzlist
116    
117     my $output = vzlist();
118     my $hash = vzlist( hash => 1 );
119    
120     =cut
121    
122     my @vzlist_fields;
123    
124     sub vzlist {
125     die "need hash as argument" unless $#_ % 2 == 1;
126     my $args = {@_};
127    
128    
129     my $output;
130    
131     if ( ! @vzlist_fields ) {
132     open(my $vzlist, 'vzlist -L |') || die "can't start vzlist -L: $!";
133     while(<$vzlist>) {
134     push @vzlist_fields, (split(/\s+/, $_, 2 ))[0];
135     }
136     }
137    
138     open(my $vzlist, 'vzlist -a -H -o ' . join(',', @vzlist_fields) . ' |') || die "can't start vzlist: $!";
139     while(<$vzlist>) {
140     s/^\s+//;
141     my @data = split(/\s+/, $_);
142    
143     if ( defined $args->{hash} ) {
144     my $hash;
145     $hash->{ $vzlist_fields[$_] } = $data[$_] foreach 1 .. $#data;
146     $output->{ $data[0] } = $hash;
147     } else {
148     $output .= $_;
149     }
150     }
151    
152     return $output;
153     }
154    
155 dpavlin 38 1;

  ViewVC Help
Powered by ViewVC 1.1.26