File Coverage

blib/lib/Rex/Virtualization/CBSD/vm_os_profiles_hash.pm
Criterion Covered Total %
statement 21 46 45.6
branch 0 12 0.0
condition 0 2 0.0
subroutine 7 8 87.5
pod 0 1 0.0
total 28 69 40.5


line stmt bran cond sub pod time code
1             #
2             # (c) Zane C. Bowers-Hadley
3             #
4              
5             package Rex::Virtualization::CBSD::vm_os_profiles_hash;
6              
7 1     1   67971 use strict;
  1         8  
  1         31  
8 1     1   5 use warnings;
  1         2  
  1         39  
9              
10             our $VERSION = '0.0.1'; # VERSION
11              
12 1     1   451 use Rex::Logger;
  1         10591  
  1         39  
13 1     1   432 use Rex::Helper::Run;
  1         77475  
  1         72  
14 1     1   9 use Term::ANSIColor qw(colorstrip);
  1         2  
  1         1785  
15 1     1   14 use Rex::Commands::User;
  1         104664  
  1         10  
16 1     1   671 use Rex::Commands::Fs;
  1         2  
  1         6  
17              
18             sub execute {
19 0     0 0   my ( $class, %opts ) = @_;
20              
21 0           Rex::Logger::debug("Getting a list of VM OS types for CBSD ");
22              
23             # set cloudinit to false by default
24 0 0         if ( !defined( $opts{cloudinit} ) ) {
25 0           $opts{cloudinit} = 0;
26             }
27              
28             # get where CBSD is installed to
29 0           my %cbsd;
30 0 0         eval { %cbsd = get_user('cbsd'); } or do {
  0            
31 0   0       my $error = $@ || 'Unknown failure';
32 0           die( "get_user('cbsd') died with... " . $error );
33             };
34              
35 0           my $cbsd_etc_defaults_dir = $cbsd{home} . '/etc/defaults/';
36              
37             # get the VM OS/profile config lists
38 0           my @vm_configs = grep { /^vm\-/ } list_files($cbsd_etc_defaults_dir);
  0            
39              
40             # find the requested ones
41 0           my %profiles;
42 0           foreach my $config (@vm_configs) {
43 0           my ( $vm, $os, $profile ) = split( /\-/, $config, 3 );
44 0           $profile =~ s/\.conf$//;
45              
46 0           my $add_profile = 1;
47              
48             # if cloudinit is defined, only add cloudinit images
49 0 0         if ( $opts{cloudinit} ) {
50              
51             # since we are default adding, make sure it is not a cloudinit
52             # profile and don't add it if it is not
53 0 0         if ( $profile !~ /^cloud\-/ ) {
54 0           $add_profile = 0;
55             }
56             }
57              
58             # add the profile if needed
59 0 0         if ($add_profile) {
60 0 0         if ( !defined( $profiles{$os} ) ) {
61 0           $profiles{$os} = { $profile => 1 };
62             }
63             else {
64 0           $profiles{$os}{$profile} = 1;
65             }
66             }
67             }
68              
69 0           return %profiles;
70             }
71              
72             1;