File Coverage

lib/Rex/Virtualization/Lxc/list.pm
Criterion Covered Total %
statement 11 35 31.4
branch 0 14 0.0
condition n/a
subroutine 4 5 80.0
pod 0 1 0.0
total 15 55 27.2


line stmt bran cond sub pod time code
1             #
2             # (c) Oleg Hardt
3             #
4              
5             package Rex::Virtualization::Lxc::list;
6              
7 1     1   15 use v5.12.5;
  1         4  
8 1     1   6 use warnings;
  1         2  
  1         43  
9              
10             our $VERSION = '1.14.2.2'; # TRIAL VERSION
11              
12 1     1   6 use Rex::Logger;
  1         1  
  1         6  
13 1     1   20 use Rex::Helper::Run;
  1         2  
  1         516  
14              
15             # Not sure if global scope is good place to keep this.
16             # Also not sure "polluting" execute method wih this is also an option.
17             my %allowed_states;
18             $allowed_states{active} = 1;
19             $allowed_states{running} = 1;
20             $allowed_states{frozen} = 1;
21             $allowed_states{stopped} = 1;
22              
23             sub execute {
24 0     0 0   my ( $class, $state, %opt ) = @_;
25 0           my @containers;
26              
27 0           my $opts = \%opt;
28              
29 0           Rex::Logger::debug("Getting Linux Containers list");
30              
31 0 0         $state = exists $allowed_states{$state} ? '--' . $state : '';
32             my $format =
33             exists $opts->{format}
34             ? $opts->{format}
35 0 0         : 'name,state,autostart,groups,ipv4,ipv6,pid';
36 0 0         my $fancy = exists $opts->{fancy} ? '-f' : '';
37 0 0         my $groups = exists $opts->{groups} ? '-g' . $opts->{groups} : '';
38              
39             # When using not fancy output, lxc-ls defaults to outputting only name.
40 0 0         if ( $fancy ne '-f' ) {
41 0           $format = 'name';
42             }
43              
44 0           my $command_to_run = "lxc-ls -1 $state $groups $fancy -F\"$format\"";
45 0           @containers = i_run $command_to_run, fail_ok => 1;
46 0 0         if ( $? != 0 ) {
47 0           die("Error running lxc-ls");
48             }
49              
50 0           my @columns = split( ',', $format );
51 0           my @ret = ();
52 0           for my $line (@containers) {
53             next
54 0 0         if $line =~ m/NAME|AUTOSTART|STATE|IPV4|IPV6|AUTOSTART|PID|RAM|SWAP\s/;
55 0           my @values = split( /\s{1,}/, $line );
56              
57             # Convert provided format into hash values.
58 0           my %row = ();
59 0           foreach my $column ( 0 .. $#columns ) {
60 0           $row{ $columns[$column] } = $values[$column];
61             }
62              
63 0           push( @ret, \%row, );
64             }
65              
66 0           return \@ret;
67             }
68              
69             1;