| 1 | # | ||||||||||
| 2 | # (c) Oleg Hardt <litwol@litwol.com> | ||||||||||
| 3 | # | ||||||||||
| 4 | |||||||||||
| 5 | package Rex::Virtualization::Lxc::list; | ||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use v5.14.4; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use Rex::Logger; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Rex::Helper::Run; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 1 | my %allowed_states; | |||||||||
| 18 | 1 | $allowed_states{active} = 1; | |||||||||
| 19 | 1 | $allowed_states{running} = 1; | |||||||||
| 20 | 1 | $allowed_states{frozen} = 1; | |||||||||
| 21 | 1 | $allowed_states{stopped} = 1; | |||||||||
| 22 | |||||||||||
| 23 | 0 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||
| 24 | 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 | ▶ | $state = exists $allowed_states{$state} ? '--' . $state : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | my $format = | ||||||||||
| 33 | exists $opts->{format} | ||||||||||
| 34 | ? $opts->{format} | ||||||||||
| 35 | 0 | ▶ | : 'name,state,autostart,groups,ipv4,ipv6,pid'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 0 | ▶ | my $fancy = exists $opts->{fancy} ? '-f' : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | ▶ | my $groups = exists $opts->{groups} ? '-g' . $opts->{groups} : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | # When using not fancy output, lxc-ls defaults to outputting only name. | ||||||||||
| 40 | 0 | ▶ | if ( $fancy ne '-f' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | if ( $? != 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | if $line =~ m/NAME|AUTOSTART|STATE|IPV4|IPV6|AUTOSTART|PID|RAM|SWAP\s/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 1; | |||||||||