File Coverage

lib/RPM/Packager.pm
Criterion Covered Total %
statement 104 116 89.6
branch 10 14 71.4
condition 11 15 73.3
subroutine 19 21 90.4
pod 1 10 10.0
total 145 176 82.3


line stmt bran cond sub pod time code
1             package RPM::Packager;
2              
3 1     1   35669 use strict;
  1         2  
  1         25  
4 1     1   5 use warnings;
  1         2  
  1         30  
5 1     1   5 use Data::Dumper;
  1         5  
  1         51  
6 1     1   1118 use File::Temp;
  1         22917  
  1         80  
7 1     1   753 use File::Copy qw(copy);
  1         2423  
  1         56  
8 1     1   6 use File::Path qw(make_path);
  1         2  
  1         38  
9 1     1   5 use Cwd;
  1         2  
  1         53  
10 1     1   1213 use Expect;
  1         30023  
  1         67  
11 1     1   554 use RPM::Packager::Utils;
  1         3  
  1         1095  
12              
13             =head1 NAME
14              
15             RPM::Packager - Manifest-based approach for building RPMs
16              
17             =head1 VERSION
18              
19             Version 0.3.0
20              
21             =cut
22              
23             our $VERSION = 'v0.3.0';
24              
25             =head1 SYNOPSIS
26              
27             Building RPMs should be easy.
28              
29             This is a manifest approach to easily create custom RPMs. Once this module is installed, building RPMs should be as
30             simple as writing a YAML file that looks like the following:
31              
32             ---
33             name: testpackage
34             version: grep Changelog # version string or some command to retrieve it
35             os: el6 # optional, don't specify anything if package is os-independent
36             dependencies:
37             - perl-YAML > 0.5
38             - perl-JSON
39             files:
40             bin: /usr/local/bin # directory-based mapping. RPM will install CWD/bin/* to /usr/local/bin.
41             user: apache # specify the owner of files. default: root
42             group: apache # specify the group owner of files. default: root
43             sign: # optionally, gpg signing of RPM
44             gpg_name: ED16CAB # provide the GPG key ID
45             passphrase_cmd: cat secret_file # command to retrieve the secret
46             after_install: /path/to/script # script to run after the package is installed (%post)
47              
48             Then run:
49              
50             rpm_packager.pl
51              
52             Note : You need to have fpm available in PATH. For GPG signing, you need to have proper keys imported.
53              
54             Note2: The 'release' field of RPM will be determined by the BUILD_NUMBER env variable plus 'os' field, like '150.el7'.
55             If BUILD_NUMBER is unavailable, 1 will be used. If os is unspecified, nothing will be appended.
56              
57             You may also interact with the library directly as long as you pass in the manifest information in a hash:
58              
59             use RPM::Packager;
60              
61             my %args = (
62             name => 'testpackage',
63             version => 'grep Changelog',
64             files => { bin => '/usr/local/bin' },
65             dependencies => [
66             'perl-YAML > 0.5',
67             'perl-JSON'
68             ],
69             os => 'el6',
70             user => 'apache',
71             group => 'apache',
72             sign => {
73             'gpg_name' => 'ED16CAB',
74             'passphrase_cmd' => 'cat secret_file'
75             },
76             after_install => 'foo/bar/baz.sh'
77             );
78              
79             my $obj = RPM::Packager->new(%args);
80             $obj->create_rpm(); # RPM produced in CWD
81              
82             =head1 SUBROUTINES/METHODS
83              
84             =head2 new(%args)
85              
86             Constructor. Pass in a hash containing manifest info.
87              
88             =cut
89              
90             sub new {
91 15     15 1 44560 my ( $class, %args ) = @_;
92 15         116222 chomp( my $fpm = `which fpm 2>/dev/null` );
93              
94 15         931 my $self = {
95             fpm => $fpm,
96             cwd => getcwd(),
97             tempdir => File::Temp->newdir(),
98             %args
99             };
100 15         11710 return bless $self, $class;
101             }
102              
103             sub find_version {
104 3     3 0 745 my $self = shift;
105 3         32 my $value = $self->{version};
106 3 100       43 ( RPM::Packager::Utils::is_command($value) ) ? RPM::Packager::Utils::eval_command($value) : $value;
107             }
108              
109             sub generate_dependency_opts {
110 3     3 0 735 my $self = shift;
111 3   100     44 my $dependencies = $self->{dependencies} || [];
112 3         8 my @chunks;
113 3         8 for my $dependency ( @{$dependencies} ) {
  3         13  
114 2         27 push @chunks, "-d '$dependency'";
115             }
116 3         57 return join( " ", @chunks );
117             }
118              
119             sub generate_user_group {
120 3     3 0 530 my $self = shift;
121 3   100     31 my $user = $self->{user} || 'root';
122 3   100     20 my $group = $self->{group} || 'root';
123 3         15 return ( $user, $group );
124             }
125              
126             sub copy_to_tempdir {
127 1     1 0 119 my $self = shift;
128              
129 1         10 my $cwd = $self->{cwd};
130 1         51 my %hash = %{ $self->{files} };
  1         16  
131 1         4 my $tempdir = $self->{tempdir};
132              
133 1         12 for my $key ( keys %hash ) {
134 1         6 my $src = "$cwd/$key";
135 1         6 my $dst = $hash{$key};
136 1         14 my $target_dir = "$tempdir$dst";
137 1         683 make_path($target_dir);
138 1         23 my @files = RPM::Packager::Utils::find_files($src);
139 1         8 my @relative_paths = RPM::Packager::Utils::find_relative_paths( $src, @files );
140              
141 1         8 for my $entry (@relative_paths) {
142 3         1262 my $dir = "$target_dir/$entry->{dirname}";
143 3         13 my $file = "$src/$entry->{rel_path}";
144 3 50       94 make_path($dir) if ( !-d $dir );
145 3         28 copy $file, $dir;
146             }
147             }
148 1         697 return 1;
149             }
150              
151             sub add_gpg_opts {
152 3     3 0 29 my $self = shift;
153              
154 3 100       22 return unless ( $self->should_gpgsign() );
155              
156 2         19 my $gpg_name = $self->{sign}->{gpg_name};
157 2         12 my $passphrase_cmd = $self->{sign}->{passphrase_cmd};
158 2   50     39 my $opts = $self->{opts} || [];
159 2         10 push @{$opts}, '--rpm-sign', '--rpm-rpmbuild-define', "'_gpg_name $gpg_name'";
  2         20  
160 2         12 $self->{opts} = $opts;
161 2         36 $self->{gpg_passphrase} = RPM::Packager::Utils::eval_command($passphrase_cmd);
162             }
163              
164             sub add_after_install {
165 2     2 0 17 my $self = shift;
166 2 100       22 return unless ( $self->{after_install} );
167              
168 1   50     36 my $opts = $self->{opts} || [];
169 1         6 push @{$opts}, '--after-install', $self->{after_install};
  1         11  
170 1         11 $self->{opts} = $opts;
171             }
172              
173             sub populate_opts {
174 1     1 0 19 my $self = shift;
175 1         8 my $version = $self->find_version();
176 1   50     20 my $release = $ENV{BUILD_NUMBER} || 1;
177 1         5 my $os = $self->{os};
178 1 50       12 my $iteration = ($os) ? "$release.$os" : $release;
179 1         9 my $dependency_opts = $self->generate_dependency_opts();
180 1         8 my ( $user, $group ) = $self->generate_user_group();
181              
182             my @opts = (
183             $self->{fpm}, '-v', $version, '--rpm-user', $user, '--rpm-group',
184             $group, '--iteration', $iteration, '-n', $self->{name}, $dependency_opts,
185             '-s', 'dir', '-t', 'rpm', '-C', $self->{tempdir}
186 1         14 );
187              
188 1         12 $self->{opts} = [@opts];
189 1         5 $self->add_gpg_opts();
190 1         5 $self->add_after_install();
191 1         2 push @{ $self->{opts} }, '.'; # relative to the temporary directory
  1         10  
192             }
193              
194             sub handle_interactive_prompt {
195 1     1 0 74 my $self = shift;
196 1         5 my $opts = $self->{opts};
197 1         6 my $cmd = join( ' ', @{$opts} );
  1         6  
198 1         5 my $pass = $self->{gpg_passphrase};
199              
200 1         22 my $exp = Expect->new();
201 1         13552 $exp->spawn($cmd);
202             $exp->expect(
203             undef,
204             [
205             qr/Enter pass phrase:/i => sub {
206 0     0   0 my $exp = shift;
207 0         0 $exp->send("$pass\n");
208 0         0 exp_continue;
209             }
210 1         46 ]
211             );
212 1         21 return 1;
213             }
214              
215             sub should_gpgsign {
216 5     5 0 529 my $self = shift;
217 5         31 my $gpg_name = $self->{sign}->{gpg_name};
218 5         16 my $passphrase_cmd = $self->{sign}->{passphrase_cmd};
219 5 100 66     99 ( $gpg_name && $passphrase_cmd ) ? 1 : 0;
220             }
221              
222             =head2 create_rpm
223              
224             Creates RPM based on the information in the object
225              
226             =cut
227              
228             sub create_rpm {
229 0     0     my $self = shift;
230              
231 0           $self->copy_to_tempdir();
232 0           $self->populate_opts();
233              
234 0 0         if ( $self->should_gpgsign() ) {
235 0           $self->handle_interactive_prompt();
236             }
237             else {
238 0           my $cmd = join( ' ', @{ $self->{opts} } );
  0            
239 0           system($cmd);
240             }
241 0           return 1;
242             }
243              
244             =head1 AUTHOR
245              
246             Satoshi Yagi, C<< >>
247              
248             =head1 BUGS
249              
250             Please report any bugs or feature requests to C, or through
251             the web interface at L. I will be notified, and then you'll
252             automatically be notified of progress on your bug as I make changes.
253              
254              
255              
256              
257             =head1 SUPPORT
258              
259             You can find documentation for this module with the perldoc command.
260              
261             perldoc RPM::Packager
262              
263              
264             You can also look for information at:
265              
266             =over 4
267              
268             =item * RT: CPAN's request tracker (report bugs here)
269              
270             L
271              
272             =item * AnnoCPAN: Annotated CPAN documentation
273              
274             L
275              
276             =item * CPAN Ratings
277              
278             L
279              
280             =item * Search CPAN
281              
282             L
283              
284             =back
285              
286              
287             =head1 ACKNOWLEDGEMENTS
288              
289              
290             =head1 LICENSE AND COPYRIGHT
291              
292             Copyright 2015 Satoshi Yagi.
293              
294             This program is free software; you can redistribute it and/or modify it
295             under the terms of the the Artistic License (2.0). You may obtain a
296             copy of the full license at:
297              
298             L
299              
300             Any use, modification, and distribution of the Standard or Modified
301             Versions is governed by this Artistic License. By using, modifying or
302             distributing the Package, you accept this license. Do not use, modify,
303             or distribute the Package, if you do not accept this license.
304              
305             If your Modified Version has been derived from a Modified Version made
306             by someone other than you, you are nevertheless required to ensure that
307             your Modified Version complies with the requirements of this license.
308              
309             This license does not grant you the right to use any trademark, service
310             mark, tradename, or logo of the Copyright Holder.
311              
312             This license includes the non-exclusive, worldwide, free-of-charge
313             patent license to make, have made, use, offer to sell, sell, import and
314             otherwise transfer the Package with respect to any patent claims
315             licensable by the Copyright Holder that are necessarily infringed by the
316             Package. If you institute patent litigation (including a cross-claim or
317             counterclaim) against any party alleging that the Package constitutes
318             direct or contributory patent infringement, then this Artistic License
319             to you shall terminate on the date that such litigation is filed.
320              
321             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
322             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
323             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
324             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
325             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
326             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
327             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
328             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
329              
330              
331             =cut
332              
333             1; # End of RPM::Packager