4

In %post, I am running few tests that will validate the rpm installation. But If tests got failed or post script failed, How can I revert the rpm installation ?

Mohyt
  • 990
  • 2
  • 10
  • 26

2 Answers2

2

You can't really and you shouldn't.

You should be asserting everything you possibly can via `Requires: lines in your spec file to prevent you from even getting that far.

%post is much too late to abort. All your files have already been put on disk/etc.

You can disable yourself so you don't run but that's about it.

If these are things that you really can't allow to fail then the best you can do is test during %pre and abort there (but even that is evil).

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • How can you test during `%pre`? That's a chicken and the egg problem. – shrewmouse Nov 11 '18 at 20:25
  • 1
    Curious how it's "too late" to roll-back if `%post` fails. Of course, the installer knows what it's done so far and can revert, right? – Josh M. Apr 22 '20 at 01:33
0

This is the hack I used in %post to roll-back:

if [ ! -d %{basedeploymentpath} ]; then
  echo "ERROR: %{basedeploymentpath} does not exist or is inaccessible. I will uninstall myself."
  rpm -ev ${APP_NAME} &
  exit 1
fi

In the above snippet, if variable %{basedeploymentpath}, which represents a directory, is not found, then call rpm -ev on the name of the package, using the & to fork the process. The %post script exits with code 1 upon the condition mentioned.

If the install process is then run at the command line (rpm -ivh) and the error condition exists at post-install, this result will appear: warning: %post(...) scriptlet failed, exit status 1

It is an ugly hack but it works. NOTE: the exit status of the rpm -ivh command will still appear as successful (exit 0) if you execute echo $? after running the rpm -ivh operation.

mikequentel
  • 253
  • 4
  • 14