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

Diff of /trunk/lib/VZ.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 40 by dpavlin, Fri Feb 8 23:02:25 2008 UTC revision 44 by dpavlin, Sat Feb 9 00:28:10 2008 UTC
# Line 2  package VZ; Line 2  package VZ;
2  use Exporter 'import';  use Exporter 'import';
3  our @EXPORT = qw(  our @EXPORT = qw(
4  check_root  check_root
5    
6  ip2hostname  ip2hostname
7  hostname2ip  hostname2ip
8  vzlist_configs  
9    conf_veids
10    runscript
11    
12  vzctl  vzctl
13    vzlist
14    
15  $vz_root  $vz_root
16  $vz_conf  $vz_conf
17    
18    dump
19  );  );
20    
21  use warnings;  use warnings;
22  use strict;  use strict;
23    
24  use Socket;  use Socket;
25    use Data::Dump qw/dump/;
26    
27  our $vz_root = '/vz';  our $vz_root = '/vz';
28  our $vz_conf = '/etc/vz/conf';  our $vz_conf = '/etc/vz/conf';
# Line 64  sub hostname2ip { Line 72  sub hostname2ip {
72  sub ip2hostname {  sub ip2hostname {
73          my $ip = shift || return;          my $ip = shift || return;
74    
75          return gethostbyaddr(inet_aton($ip), AF_INET)          my $hostname = gethostbyaddr(inet_aton($ip), AF_INET)
76                  or die "Can't look up $ip: $!\n";                  or die "Can't look up $ip: $!\n";
77            return $hostname;
78  }  }
79    
80  =head2 vzlist_configs  =head2 conf_veids
81    
82    my @VEIDs = vzlist_configs;    my @VEIDs = conf_veids;
83    
84  =cut  =cut
85    
86  sub vzlist_configs {  sub conf_veids {
87          my @c;          my @c;
88          open( my $cfgs, 'find /etc/vz/conf -maxdepth 1 -name "*.conf" |' ) || die "can't run find: $!";          open( my $cfgs, 'find /etc/vz/conf -maxdepth 1 -name "*.conf" |' ) || die "can't run find: $!";
89          while(<$cfgs>) {          while(<$cfgs>) {
# Line 92  sub vzlist_configs { Line 101  sub vzlist_configs {
101          return @c;          return @c;
102  }  }
103    
104    =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  =head2 vzctl  =head2 vzctl
127    
128    vzctl('set','--something',42);    vzctl('set','--something',42);
# Line 100  sub vzlist_configs { Line 131  sub vzlist_configs {
131    
132  sub vzctl {  sub vzctl {
133          my @args = @_;          my @args = @_;
134          warn "## vzctl ", join(" ",@args), "\n";          my $a = join(' ', @args);
135            # hide passwords from output
136            $a =~ s/(--userpasswd\s+\w+:)\w+/$1********/;
137            warn "## vzctl $a\n";
138          system('vzctl',@args) == 0 or die "vzctl @args failed: $?"          system('vzctl',@args) == 0 or die "vzctl @args failed: $?"
139  }  }
140    
141    =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  1;  1;

Legend:
Removed from v.40  
changed lines
  Added in v.44

  ViewVC Help
Powered by ViewVC 1.1.26