root/psad/tags/psad-2.1.2/packaging/cd_rpmbuilder

Revision 2072, 6.7 kB (checked in by mbr, 2 years ago)

updated to remove md5 sum files

  • Property svn:executable set to *
Line 
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 Michael Rash (mbr@cipherdyne.org)
13 #
14 # License (GNU Public License):
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 698 2007-06-06 05:26:21Z 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 $verbose = 0;
51 my $help = 0;
52
53 my @rpm_paths = ();
54
55 my $RM    = 1;
56 my $PRINT = 2;
57
58 my %projects = (
59     'psad'    => '',
60     'fwknop'  => '',
61     'fwsnort' => '',
62     'gpgdir'  => ''
63 );
64
65 Getopt::Long::Configure('no_ignore_case');
66 &usage() unless (GetOptions(
67     'project=s' => \$project,
68     'build-version=s' => \$build_version,
69     'rpm-build-dir=s' => \$rpm_root_dir,
70     'verbose' => \$verbose,
71     'Version' => \$print_version,
72     'help'    => \$help
73 ));
74 &usage() if $help;
75
76 if ($print_version) {
77     print "[+] cd_rpmbuilder by Michael Rash <mbr\@cipherdyne.org>\n";
78     exit 0;
79 }
80
81 if ($project) {
82     unless (defined $projects{$project}) {
83         print "[*] Unrecognized project: $project; must be one of:\n";
84         print $_, "\n" for keys %projects;
85         exit 1;
86     }
87 } else {
88     die "[*] Must specify a project with -p <project>\n";
89 }
90
91 die "[*] $wgetCmd is not a valid path to wget, update the config section."
92     unless -x $wgetCmd;
93 die "[*] $rpmbuildCmd is not a valid path to rpmbuild, update the config " .
94     "section." unless -x $rpmbuildCmd;
95
96 chdir "$rpm_root_dir/SPECS" or die "[*] Could not chdir $rpm_root_dir/SPECS";
97
98 unless ($build_version) {
99     ### we need to get the latest version from cipherdyne.org
100     &get_latest_version();
101 }
102
103 ### remove old RPMS
104 &find_rpms($RM);
105
106 ### get the remote spec file
107 &download_file("$project-$build_version.spec");
108 &md5_check("$project-$build_version.spec");
109
110 ### get the remote source tarball and md5 sum file
111 &download_file("$project-$build_version.tar.gz");
112 &md5_check("$project-$build_version.tar.gz");
113
114 move "$project-$build_version.tar.gz", '../SOURCES' or die $!;
115
116 ### build the rpm
117 &build_rpm();
118
119 ### print the paths to the new RPMS
120 &find_rpms($PRINT);
121
122 exit 0;
123 #======================= end main ========================
124
125 sub find_rpms() {
126     my $action = shift;
127     @rpm_paths = ();
128     find(\&get_rpms, "$rpm_root_dir/SRPMS");
129     find(\&get_rpms, "$rpm_root_dir/RPMS");
130     if ($action == $PRINT) {
131         if (@rpm_paths) {
132             print "[+] The following RPMS were successfully built:\n\n";
133         } else {
134             print "[-] No RPMS were successfully built; try running ",
135                 "with --verbose\n";
136         }
137     }
138     for my $rpm_file (@rpm_paths) {
139         if ($action == $RM) {
140             unlink $rpm_file or die "[*] Could not unlink $rpm_file: $!";
141         } elsif ($action == $PRINT) {
142             if ($rpm_file =~ /\.src\.rpm/) {
143                 print "      $rpm_file (source RPM)\n";
144             } else {
145                 print "      $rpm_file\n";
146             }
147         }
148     }
149     print "\n" if $action == $PRINT;
150     return;
151 }
152
153 sub get_rpms() {
154     my $file = $File::Find::name;
155     if ($file =~ /$project-$build_version-.*\.rpm$/) {
156         push @rpm_paths, $file;
157     }
158     return;
159 }
160
161 sub download_file() {
162     my $file = shift;
163     unlink $file if -e $file;
164     print "[+] Downloading file:\n",
165         "      $build_url_base/$project/download/$file\n";
166     my $cmd = "$wgetCmd $build_url_base/$project/download/$file";
167     unless ($verbose) {
168         $cmd .= ' > /dev/null 2>&1';
169     }
170     system $cmd;
171     die "[*] Could not download $file, try running with -v"
172         unless -e $file;
173     return;
174
175 }
176
177 sub md5_check() {
178     my $file = shift;
179     &download_file("$file.md5");
180     ### check MD5 sum
181     open MD5, "md5sum -c $file.md5 |"
182         or die $!;
183     my $sum_line = <MD5>;
184     close MD5;
185     unless ($sum_line =~ m/$file:\s+OK/) {
186         die "[*] MD5 sum check failed for $file, ",
187             "exiting.";
188     }
189     print "[+] Valid md5 sum check for $file\n";
190     unlink "$file.md5";
191     return;
192 }
193
194 sub build_rpm() {
195     print
196 "[+] Building RPM, this may take a little while (try -v if you want\n",
197 "    to see all of the steps)...\n\n";
198     my $cmd = "$rpmbuildCmd -ba $project-$build_version.spec";
199     unless ($verbose) {
200         $cmd .= ' > /dev/null 2>&1';
201     }
202     system $cmd;
203     return;
204 }
205
206 sub get_latest_version() {
207     unlink "$project-latest" if -e "$project-latest";
208     print "[+] Getting latest version file:\n",
209         "      $build_url_base/$project/$project-latest\n";
210     my $cmd = "$wgetCmd $build_url_base/$project/$project-latest";
211     unless ($verbose) {
212         $cmd .= ' > /dev/null 2>&1';
213     }
214     system $cmd;
215     open F, "< $project-latest" or
216             die "[*] Could not open $project-latest: $!";
217     my $line = <F>;
218     close F;
219     chomp $line;
220     $build_version = $line;
221     die "[*] Could not get build version" unless $build_version;
222     unlink "$project-latest" if -e "$project-latest";
223     return;
224 }
225
226 sub usage() {
227     print <<_HELP_;
228
229 cd_rpmbuilder; the CipherDyne RPM builder
230 [+] Version: $version
231 [+] By Michael Rash (mbr\@cipherdyne.org, http://www.cipherdyne.org)
232
233 Usage: cd_rpmbuilder -p <project> [-b <version>] [-r <dir>] [-v] [-V] [-h]
234
235 Options:
236     -p, --project <name>       - This can be one of "psad", "fwknop",
237                                  "gpgdir", or "fwsnort".
238     -b, --build-version <ver>  - Build a specific project version.
239     -r, --rpm-build-dir <dir>  - Change the RPM build directory from the
240                                  default of $rpm_root_dir.
241     -v, --verbose              - Run in verbose mode.
242     -V, --Version              - Print version and exit.
243     -h, --help                 - Display usage information.
244 _HELP_
245     exit 0;
246 }
Note: See TracBrowser for help on using the browser.