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