File Coverage

blib/lib/Rex/Apache/Build/rpm.pm
Criterion Covered Total %
statement 39 114 34.2
branch 0 16 0.0
condition 0 18 0.0
subroutine 13 17 76.4
pod 0 4 0.0
total 52 169 30.7


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4             # vim: set ts=2 sw=2 tw=0:
5             # vim: set expandtab:
6              
7             =head1 NAME
8              
9             Rex::Apache::Build::rpm - Build rpm packages
10              
11             =head1 DESCRIPTION
12              
13             With this module you can build RedHat packages to distribute your application.
14              
15             =head1 SYNOPSIS
16              
17             build "my-software",
18             type => "rpm",
19             version => "1.0",
20             source => "/path/to/your/software",
21             path => "/path/to/deploy/target",
22             # below this, it is all optional
23             description => "some description of your package",
24             url => "website of the package",
25             depends => [qw/httpd perl/],
26             release => 1,
27             epoch => 1,
28             vendor => "some vendor",
29             license => "your license for ex. GPL2",
30             section => "some/section",
31             conflicts => [qw/somepkg/],
32             provides => "some-package-name",
33             arch => "x86_64",
34             target => "linux / the platform",
35             post_install => "filename or script to run after installation",
36             pre_install => "filename or script to run before installation",
37             post_uninstall => "filename or script to run after uninstall",
38             pre_uninstall => "filename or script to run before uninstall",
39             exclude => [qw/file1 file2/],
40             maintainer => "your name",
41             config_files => [qw/special files for configuration mostly for etc directory/];
42              
43              
44             =cut
45              
46             package Rex::Apache::Build::rpm;
47              
48 1     1   2056 use strict;
  1         2  
  1         26  
49 1     1   7 use warnings;
  1         2  
  1         23  
50 1     1   8 use attributes;
  1         2  
  1         7  
51              
52 1     1   45 use Cwd qw(getcwd);
  1         3  
  1         51  
53 1     1   6 use Digest::MD5;
  1         2  
  1         30  
54 1     1   5 use Rex::Logger;
  1         2  
  1         7  
55 1     1   30 use Rex::Commands::Run;
  1         2  
  1         7  
56 1     1   89 use Rex::Commands::Fs;
  1         3  
  1         7  
57 1     1   425 use Rex::Commands::File;
  1         3  
  1         6  
58 1     1   372 use Rex::Template;
  1         2  
  1         7  
59 1     1   29 use Data::Dumper;
  1         3  
  1         50  
60              
61 1     1   5 use Rex::Apache::Build::Base;
  1         2  
  1         6  
62 1     1   32 use base qw(Rex::Apache::Build::Base);
  1         2  
  1         1128  
63              
64             sub new {
65 0     0 0   my $that = shift;
66 0   0       my $proto = ref($that) || $that;
67 0           my $self = $proto->SUPER::new(@_);
68              
69 0           bless($self, $proto);
70              
71 0   0       $self->{license} ||= "unknown";
72 0   0       $self->{file_user} ||= "root";
73 0   0       $self->{file_group} ||= "root";
74 0   0       $self->{release} ||= 1;
75 0   0       $self->{exclude} ||= [];
76              
77 0           push(@{ $self->{exclude} }, qr{^Rexfile$}, qr{^Rexfile\.lock$}, qr{^\.git}, qr{^\.svn}, qr{.*~$}, qr{\.sw[a-z]$}, qr{\.rpm$});
  0            
78              
79 0           return $self;
80             }
81              
82             sub build {
83 0     0 0   my ($self, $name) = @_;
84              
85 0   0       $name ||= $self->name;
86              
87 0           mkdir "temp-rpm-build";
88 0           mkdir "temp-rpm-build/tree";
89 0           mkdir "temp-rpm-build/tree" . $self->prefix;
90              
91 0           my @dirs = $self->find_dirs;
92 0           push(@dirs, $self->prefix);
93              
94 0           my @files = $self->find_files;
95              
96 0           file "temp-rpm-build/$name.spec",
97             content => template('@spec.template', pkg => $self, cur_dir => getcwd(), files => \@files, dirs => \@dirs);
98              
99 0           chdir "temp-rpm-build";
100              
101 0   0       my $rpmbuild_path = Rex::Config->get("rpmbuild") || "rpmbuild";
102 0           run "$rpmbuild_path --buildroot=" . getcwd() . "/tree -bb $name.spec";
103              
104 0           chdir "..";
105              
106 0           my $arch = $self->arch;
107 0           cp "temp-rpm-build/$arch/*.rpm", ".";
108              
109 0           rmdir "temp-rpm-build";
110              
111 0           my $build_file = "$name-" . $self->version . "-" . $self->release . "." . $self->arch . ".rpm";
112 0           Rex::Logger::info("Your build is now available: $build_file");
113              
114 0           return $build_file;
115             }
116              
117             sub find_files {
118 0     0 0   my ($self) = @_;
119              
120 0           my @ret;
121              
122 0           my @dirs = ($self->{source});
123 0           for my $dir (@dirs) {
124 0 0         opendir(my $dh, $dir) or die($!);
125 0           while(my $entry = readdir($dh)) {
126 0 0         next if($entry eq ".");
127 0 0         next if($entry eq "..");
128              
129 0 0         if(-d "$dir/$entry") {
130 0           push(@dirs, "$dir/$entry");
131 0           next;
132             }
133              
134 0           Rex::Logger::debug("Adding file: " . getcwd() . "/$dir/$entry => $dir/$entry");
135 0           push(@ret, [getcwd() . "/$dir/$entry", "$dir/$entry"]);
136             }
137 0           closedir($dh);
138             }
139              
140             # free conffiles
141 0           for my $conf (@{ $self->config_files }) {
  0            
142 0           @ret = grep { $_->[0] !~ m/$conf/ } @ret;
  0            
143             }
144              
145 0           my $top = $self->source . "/";
146 0           map { $_->[1] =~ s/^$top// } @ret;
  0            
147              
148 0           Rex::Logger::debug("==== files ====");
149 0           Rex::Logger::debug(Dumper(\@ret));
150              
151 0           return @ret;
152             }
153              
154             sub find_dirs {
155 0     0 0   my ($self) = @_;
156              
157 0           my @ret;
158              
159 0           my @dirs = ($self->{source});
160 0           for my $dir (@dirs) {
161 0 0         opendir(my $dh, $dir) or die($!);
162 0           while(my $entry = readdir($dh)) {
163 0 0         next if($entry eq ".");
164 0 0         next if($entry eq "..");
165              
166 0 0         if(-d "$dir/$entry") {
167 0           push(@dirs, "$dir/$entry");
168              
169 0           Rex::Logger::debug("Adding directory: $dir/$entry");
170 0           push(@ret, "$dir/$entry");
171             }
172              
173             }
174 0           closedir($dh);
175             }
176              
177 0           my $top = $self->source;
178 0           map { s/^$top//; $_ = $self->prefix . $_ } @ret;
  0            
  0            
179              
180 0           Rex::Logger::debug("==== directories ====");
181 0           Rex::Logger::debug(Dumper(\@ret));
182              
183 0           return @ret;
184             }
185              
186              
187              
188             1;
189              
190             __DATA__