Writing Portable RPM Specification Files
I started building RPMs for our software, but I'm not sure if I use the system on my own. I would like to create a spec / source RPM package that I can use for different RPM systems, eg. fedora, centos and opensuse (which I like about rpm).
However, I am beginning to understand that a lot of scenarios, for example, %pre
and %post
depend on the distribution. For example, my pre script stops Apache:
service httpd stop
However, this command does not work on OpenSUSE. This is just one example, there are many other things in my script that differ between distributions.
So what's the usual way to do this? Should I just create separate .spec files for each distribution? Or are there tricks to make things portable?
source to share
Add a wrapper script to the top of your spec file that executes the command, something like this:
%define distribution %(sh -c "[ -f /etc/redhat-release ] && cat /etc/redhat-release | awk '{print $1}' || [ -f /etc/SuSE-release ] && head -n1 /etc/SuSE-release | awk '{print $1}' || echo 'Unknown'")
And further in your scripts %pre
and %post
etc you can do:
distribution=${distribution}
case $distribution in
"Fedora")
# do fedora stuff
;;
"openSUSE")
# do suse stuff
;;
*)
# handle error condition
exit 1
;;
esac
And add which allocation lines you need and their associated scripts.
source to share