Creating RPMs – Simple RPM Build Script

Here’s a simple script that helps package up RPM’s – designed for
packaging up php code from a /var/www/html/your_project directory – can
be easily modified to suite other needs.

(first time i posted this stripslashes() did some damage, think it’s all fixed but let me know if it’s still borked)

Save the below into a file called “build_rpm.sh” and then run it from a shell like:

$ ./build_rpm.sh

It will ask you questions and then spit out an RPM.
Designed
to run on RHEL5 boxes – if you want to run it on other systems then
check that /usr/src/redhat/ exists. Might just be /usr/src/.


#!/bin/bash
# $BASENAME is the name of your software package
BASENAME="my_program_name";
SOURCELOC='/var/www/my_php_program';

echo "Building RPM for Your Program";

echo -n "Enter Version Number for RPM: (eg: 1) ";
read VERSION;
echo -n "Enter Release Number for RPM: (eg: 2) ";
read RELEASE;

cd /usr/src/redhat/SPECS/;
echo "Building $BASENAME-$VERSION-$RELEASE.spec";
echo "Summary: Summary to be included in RPM package
Name: $BASENAME
Version: $VERSION
Release: $RELEASE
Group: Applications/Internet
License: commercial
Source: $BASENAME-$VERSION-$RELEASE.tar.gz
BuildRoot: /var/tmp/%{name}-buildroot

%description
Description to be included in RPM package

%prep
%setup -q

%build

%install
cp -rvf $RPM_BUILD_DIR/$BASENAME-$VERSION $RPM_BUILD_ROOT

%clean
if( [ $RPM_BUILD_ROOT != '/' ] ); then rm -rf $RPM_BUILD_ROOT; fi;

%files
/.

%post
echo "your program has been installed in /var/www/your_program";
echo "restarting apache... (or whatever you want to do after install here) ";
/etc/init.d/httpd restart;" > "$BASENAME-$VERSION-$RELEASE.spec";

cd /usr/src/redhat/SOURCES/
echo "Making /usr/src/redhat/SOURCES/$BASENAME-$VERSION$SOURCELOC";
if( [ -d $BASENAME-$VERSION ] ); then
# needed if previous script build was inturrupted
echo " ( removing old directory $BASENAME-$VERSION ) ";
rm -rf $BASENAME-$VERSION;
fi;
mkdir -p $BASENAME-$VERSION$SOURCELOC;
echo "Copying Code From $SOURCELOC to ";
echo "/usr/src/redhat/SOURCES/$BASENAME-$VERSION$SOURCELOC";
# bit of a dodgey - so it copies .files
cp -rf $SOURCELOC $BASENAME-$VERSION$SOURCELOC/../;

# this program requires a custom apache config -
# just using apache as an example - you can put anything here
echo "Making the apache config";
mkdir -p $BASENAME-$VERSION/etc/httpd/conf.d/;
echo "<VirtualHost *:1234>
ServerAdmin your@email
DocumentRoot /var/www/your_app
ServerName workflow
<Directory "/var/www/your_app/">
AllowOverride All
</Directory>
</VirtualHost>" > $BASENAME-$VERSION/etc/httpd/conf.d/your_app.conf;

echo "Removing some un-needed files";
#any files from your locally running /var/www/your_app that
# you dont want to include in rpm
# eg : password config files etc..
rm -rf $BASENAME-$VERSION$SOURCELOC/includes/config/;

echo "Tar/Gzipping code";
tar -cf $BASENAME-$VERSION-$RELEASE.tar $BASENAME-$VERSION;
gzip $BASENAME-$VERSION-$RELEASE.tar;
echo "About to build RPM with rpmbuild";
sleep 5;
rpmbuild -ba /usr/src/redhat/SPECS/$BASENAME-$VERSION-$RELEASE.spec;
echo "RPM built in /usr/src/redhat/RPMS/i386/";
ls -lh /usr/src/redhat/RPMS/i386/;

ps:
this script works fine for me – however use at own risk etc.. it does a
few rm’s – make sure it’s not deleting anything you need

Leave a Reply

Your email address will not be published.