File Coverage

lib/Rex/Commands/PkgConf.pm
Criterion Covered Total %
statement 17 30 56.6
branch 0 2 0.0
condition n/a
subroutine 6 8 75.0
pod 2 2 100.0
total 25 42 59.5


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             =head1 NAME
6              
7             Rex::Commands::PkgConf - Configure packages
8              
9             =head1 DESCRIPTION
10              
11             With this module you can configure packages. Currently it only supports Debian
12             (using debconf), but it is designed to be extendable.
13              
14             =head1 SYNOPSIS
15              
16             my %options = get_pkgconf('postfix');
17             say $options{'postfix/relayhost'}->{value};
18              
19             # Only obtain one value
20             my %options = get_pkgconf('postfix', 'postfix/relayhost');
21             say $options{'postfix/relayhost'}->{value};
22              
23             # Set options
24             set_pkgconf("postfix", [
25             {question => 'chattr', type => 'boolean', value => 'false'},
26             {question => 'relayhost', type => 'string', value => 'relay.example.com'},
27             ]);
28              
29             # Don't update if it's already set
30             set_pkgconf("mysql-server-5.5", [
31             {question => 'mysql-server/root_password', type => 'string', value => 'mysecret'},
32             {question => 'mysql-server/root_password_again', type => 'string', value => 'mysecret'},
33             ], no_update => 1);
34              
35             =head1 EXPORTED FUNCTIONS
36              
37             =cut
38              
39             package Rex::Commands::PkgConf;
40              
41 1     1   14 use v5.12.5;
  1         4  
42 1     1   5 use warnings;
  1         2  
  1         51  
43              
44             our $VERSION = '1.14.2.2'; # TRIAL VERSION
45              
46 1     1   7 use Rex::PkgConf;
  1         3  
  1         13  
47 1     1   21 use Rex::Logger;
  1         2  
  1         4  
48              
49             require Rex::Exporter;
50              
51 1     1   36 use base qw(Rex::Exporter);
  1         6  
  1         112  
52 1     1   7 use vars qw(@EXPORT);
  1         3  
  1         349  
53              
54             @EXPORT = qw(get_pkgconf set_pkgconf);
55              
56             =head2 get_pkgconf($package, [$question])
57              
58             Use this to query existing package configurations.
59              
60             Without a question specified, it will return all options for
61             the specified package as a hash.
62              
63             With a question specified, it will return only that option
64              
65             Each question is returned with the question as the key, and
66             the value as a hashref. The hashref contains the keys: question,
67             value and already_set. already_set is true if the question has
68             already been answered.
69              
70             # Only obtain one value
71             my %options = get_pkgconf('postfix', 'postfix/relayhost');
72             say $options{'postfix/relayhost'}->{question};
73             say $options{'postfix/relayhost'}->{value};
74             say $options{'postfix/relayhost'}->{already_set};
75              
76             =cut
77              
78             sub get_pkgconf {
79 0     0 1   my ($package) = @_;
80              
81             Rex::get_current_connection()->{reporter}
82 0           ->report_resource_start( type => "pkgconf", name => $package );
83              
84 0           my $pkgconf = Rex::PkgConf->get;
85              
86 0           $pkgconf->get_options($package);
87             }
88              
89             =head2 set_pkgconf($package, $values, [%options])
90              
91             Use this to set package configurations.
92              
93             At least the package name and values must be specified. Values
94             must be an array ref, with each item containing a hashref with
95             the attributes specified that are required by the package
96             configuration program.
97              
98             For example, for debconf, this must be the question, the type
99             and answer. In this case, the types can be any accetable debconf
100             type: string, boolean, select, multiselect, note, text, password.
101              
102             Optionally the option "no_update" may be true, in which case the
103             question will not be updated if it has already been set.
104              
105             See the synopsis for examples.
106              
107             =cut
108              
109             sub set_pkgconf {
110 0     0 1   my ( $package, $values, %options ) = @_;
111              
112             Rex::get_current_connection()->{reporter}
113 0           ->report_resource_start( type => "pkgconf", name => $package );
114              
115 0           my $pkgconf = Rex::PkgConf->get;
116              
117 0           my $return = $pkgconf->set_options( $package, $values, %options );
118              
119 0 0         if ( $return->{changed} ) {
120             Rex::get_current_connection()->{reporter}->report(
121 0           changed => 1,
122 0           message => "Configuration values updated: @{$return->{names}}",
123             );
124             }
125             else {
126 0           Rex::get_current_connection()->{reporter}->report( changed => 0, );
127             }
128              
129             Rex::get_current_connection()->{reporter}
130 0           ->report_resource_end( type => "pkgconf", name => $package );
131             }
132              
133             1;