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

  ViewVC Help
Powered by ViewVC 1.1.26