Imported both gpgdir and gpgwrap projects.
[pgp-tools.git] / gpgdir / packaging / cd_rpmbuilder
1 #!/usr/bin/perl -w
2 #
3 #############################################################################
4 #
5 # File: cd_rpmbuilder "CipherDyne Rpm Builder"
6 #
7 # Purpose: Provides a consistent way to build RPMs of CipherDyne open source
8 # projects (psad, fwsnort, fwsknop, and gpgdir).
9 #
10 # Author: Michael Rash
11 #
12 # Copyright (C) 2006-2008 Michael Rash (mbr@cipherdyne.org)
13 #
14 # License (GNU Public License - GPLv2):
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 # USA
25 #
26 #############################################################################
27 #
28 # $Id: cd_rpmbuilder 1864 2008-08-22 03:16:19Z mbr $
29 #
30
31 use File::Find;
32 use File::Copy;
33 use Getopt::Long 'GetOptions';
34 use strict;
35
36 #============================ config =============================
37 my $rpm_root_dir = '/usr/src/redhat';
38 my $build_url_base = 'http://www.cipherdyne.org';
39
40 ### commands
41 my $rpmbuildCmd = '/usr/bin/rpmbuild';
42 my $wgetCmd = '/usr/bin/wget';
43 #========================== end config ===========================
44
45 my $version = '0.9';
46
47 my $project = '';
48 my $build_version = '';
49 my $print_version = 0;
50 my $nodeps = 0;
51 my $verbose = 0;
52 my $help = 0;
53
54 my @rpm_paths = ();
55
56 my $RM = 1;
57 my $PRINT = 2;
58
59 my %projects = (
60 'psad' => '',
61 'fwknop' => '',
62 'fwsnort' => '',
63 'gpgdir' => ''
64 );
65
66 Getopt::Long::Configure('no_ignore_case');
67 &usage() unless (GetOptions(
68 'project=s' => \$project,
69 'build-version=s' => \$build_version,
70 'rpm-build-dir=s' => \$rpm_root_dir,
71 'no-deps' => \$nodeps,
72 'verbose' => \$verbose,
73 'Version' => \$print_version,
74 'help' => \$help
75 ));
76 &usage() if $help;
77
78 if ($print_version) {
79 print "[+] cd_rpmbuilder by Michael Rash <mbr\@cipherdyne.org>\n";
80 exit 0;
81 }
82
83 if ($project) {
84 unless (defined $projects{$project}) {
85 print "[*] Unrecognized project: $project; must be one of:\n";
86 print $_, "\n" for keys %projects;
87 exit 1;
88 }
89 } else {
90 die "[*] Must specify a project with -p <project>\n";
91 }
92
93 die "[*] $wgetCmd is not a valid path to wget, update the config section."
94 unless -x $wgetCmd;
95 die "[*] $rpmbuildCmd is not a valid path to rpmbuild, update the config " .
96 "section." unless -x $rpmbuildCmd;
97
98 chdir "$rpm_root_dir/SPECS" or die "[*] Could not chdir $rpm_root_dir/SPECS";
99
100 unless ($build_version) {
101 ### we need to get the latest version from cipherdyne.org
102 &get_latest_version();
103 }
104
105 my $spec_file = "$project-$build_version.spec";
106 my $tar_file = "$project-$build_version.tar.gz";
107
108 if ($nodeps) {
109 $spec_file = "$project-nodeps-$build_version.spec";
110 $tar_file = "$project-nodeps-$build_version.tar.gz";
111 }
112
113 ### remove old RPMS
114 &find_rpms($RM);
115
116 ### get the remote spec file
117 &download_file($spec_file);
118 &md5_check($spec_file);
119
120 ### get the remote source tarball and md5 sum file
121 &download_file($tar_file);
122 &md5_check($tar_file);
123
124 if ($nodeps) {
125 move $tar_file, "../SOURCES/$project-$build_version.tar.gz" or die $!;
126 } else {
127 move $tar_file, '../SOURCES' or die $!;
128 }
129
130 ### build the rpm
131 &build_rpm();
132
133 ### print the paths to the new RPMS
134 &find_rpms($PRINT);
135
136 exit 0;
137 #======================= end main ========================
138
139 sub find_rpms() {
140 my $action = shift;
141 @rpm_paths = ();
142 find(\&get_rpms, "$rpm_root_dir/SRPMS");
143 find(\&get_rpms, "$rpm_root_dir/RPMS");
144 if ($action == $PRINT) {
145 if (@rpm_paths) {
146 print "[+] The following RPMS were successfully built:\n\n";
147 } else {
148 print "[-] No RPMS were successfully built; try running ",
149 "with --verbose\n";
150 }
151 }
152 for my $rpm_file (@rpm_paths) {
153 if ($action == $RM) {
154 unlink $rpm_file or die "[*] Could not unlink $rpm_file: $!";
155 } elsif ($action == $PRINT) {
156 if ($rpm_file =~ /\.src\.rpm/) {
157 print " $rpm_file (source RPM)\n";
158 } else {
159 print " $rpm_file\n";
160 }
161 }
162 }
163 print "\n" if $action == $PRINT;
164 return;
165 }
166
167 sub get_rpms() {
168 my $file = $File::Find::name;
169 if ($file =~ /$project-$build_version-.*\.rpm$/) {
170 push @rpm_paths, $file;
171 }
172 return;
173 }
174
175 sub download_file() {
176 my $file = shift;
177 unlink $file if -e $file;
178 print "[+] Downloading file:\n",
179 " $build_url_base/$project/download/$file\n";
180 my $cmd = "$wgetCmd $build_url_base/$project/download/$file";
181 unless ($verbose) {
182 $cmd .= ' > /dev/null 2>&1';
183 }
184 system $cmd;
185 die "[*] Could not download $file, try running with -v"
186 unless -e $file;
187 return;
188
189 }
190
191 sub md5_check() {
192 my $file = shift;
193 &download_file("$file.md5");
194 ### check MD5 sum
195 open MD5, "md5sum -c $file.md5 |"
196 or die $!;
197 my $sum_line = <MD5>;
198 close MD5;
199 unless ($sum_line =~ m/$file:\s+OK/) {
200 die "[*] MD5 sum check failed for $file, ",
201 "exiting.";
202 }
203 print "[+] Valid md5 sum check for $file\n";
204 unlink "$file.md5";
205 return;
206 }
207
208 sub build_rpm() {
209 print
210 "[+] Building RPM, this may take a little while (try -v if you want\n",
211 " to see all of the steps)...\n\n";
212 my $cmd = "$rpmbuildCmd -ba $spec_file";
213 unless ($verbose) {
214 $cmd .= ' > /dev/null 2>&1';
215 }
216 system $cmd;
217 return;
218 }
219
220 sub get_latest_version() {
221 unlink "$project-latest" if -e "$project-latest";
222 print "[+] Getting latest version file:\n",
223 " $build_url_base/$project/$project-latest\n";
224 my $cmd = "$wgetCmd $build_url_base/$project/$project-latest";
225 unless ($verbose) {
226 $cmd .= ' > /dev/null 2>&1';
227 }
228 system $cmd;
229 open F, "< $project-latest" or
230 die "[*] Could not open $project-latest: $!";
231 my $line = <F>;
232 close F;
233 chomp $line;
234 $build_version = $line;
235 die "[*] Could not get build version" unless $build_version;
236 unlink "$project-latest" if -e "$project-latest";
237 return;
238 }
239
240 sub usage() {
241 print <<_HELP_;
242
243 cd_rpmbuilder; the CipherDyne RPM builder
244 [+] Version: $version
245 [+] By Michael Rash (mbr\@cipherdyne.org, http://www.cipherdyne.org)
246
247 Usage: cd_rpmbuilder -p <project> [options]
248
249 Options:
250 -p, --project <name> - This can be one of "psad", "fwknop",
251 "gpgdir", or "fwsnort".
252 -b, --build-version <ver> - Build a specific project version.
253 -r, --rpm-build-dir <dir> - Change the RPM build directory from the
254 default of $rpm_root_dir.
255 -n, --no-deps - Build the specified project without any
256 dependencies (such as perl modules).
257 -v, --verbose - Run in verbose mode.
258 -V, --Version - Print version and exit.
259 -h, --help - Display usage information.
260 _HELP_
261 exit 0;
262 }