Worst mistake when creating a RPM

… is ignoring the run order of the scriptlets. Because you may want to arrive to a result close to this one:

  • When installing the rpm for the first time, add the service to chkconfig and get it in the running state;

  • When removing (for good), stop the service and remove it from chkconfig;

  • When upgrading, stop the current running service, do the binary upgrade and start the new service (do not alter chkconfig).

A quick approach to this would ignore the upgrade part completely, hoping that coupling together the removal and the installation would do the trick. Something like the following in the .spec file:

%post
chkconfig --add mywow
service mywow start
  
%preun
service mywow stop
chkconfig --del mywow

This will actually do the job if installation and removal of the rpm package were the only operations to be considered. The problem is that during an upgrade this will go horribly wrong as the scriptlet running order is different than the one expected by the previous sample:

  1. %pre of the new package

  2. %post of the new package

  3. %preun of the old package

  4. %postun of the old package

The obvious result is that an upgrade with those scriptlets will leave the service stopped and out of chkconfig. A real disaster.

The solution is to look for the first parameter that gets passed when running each scriptlet. The rpm program passes a certain value to indicate if it’s an installation / removal or an upgrade. Considering that value we can write some better scripts:

%post
if [ $1 -gt 1 ] ; then
  service mynow restart
else
  chkconfig --add mywow
  service mywow start
fi
  
%preun
if [ $1 -eq 0 ] ; then
  service mynow stop
  chkconfig --del mywow
fi

Fixed. Now we are good with this.

Hope you enjoyed it!

Leave a Reply

Your email address will not be published. Required fields are marked *