Blame doc/manual/triggers

2ff057
/*! \page triggers Trigger scriptlets
2ff057
2ff057
Triggers provide a well-defined method for packages to interact with one
2ff057
another at package install and uninstall time. They are an extension
2ff057
of the normal installation scripts (i.e. %pre) which allows one package
2ff057
(the "source" of the trigger package [which I often think of as the 
2ff057
"triggered package"]) to execute an action when the installation status
2ff057
of another package (the "target" of the trigger) changes.
2ff057
2ff057
\section triggers_example A Simple Example
2ff057
2ff057
Say the package "mymailer" needs an /etc/mymailer/mailer symlink which points
2ff057
to the mail transport agent to use. If sendmail is installed, the link should
2ff057
point to /usr/bin/sendmail, but it vmail is installed, the link should 
2ff057
instead point to /usr/bin/vmail. If both packages are present, we don't care
2ff057
where the link points (realistically, sendmail and vmail should conflict
2ff057
with one another), while if neither package is installed the link should
2ff057
not exist at all.
2ff057
2ff057
This can be accomplished by mymailer providing trigger scripts which 
2ff057
move the symlink when any of the following occurs:
2ff057
2ff057
\verbatim
2ff057
	1) sendmail is installed
2ff057
	2) vmail is installed
2ff057
	3) sendmail is removed
2ff057
	4) vmail is removed
2ff057
\endverbatim
2ff057
2ff057
The first two of these scripts would look like this:
2ff057
2ff057
\verbatim
2ff057
	%triggerin -- sendmail
2ff057
	ln -sf /usr/bin/sendmail /etc/mymailer/mailer
2ff057
2ff057
	%triggerin -- vmail
2ff057
	ln -sf /usr/bin/vmail /etc/mymailer/mailer
2ff057
\endverbatim
2ff057
2ff057
These are two installation triggers, triggered by one of sendmail or vmail.
2ff057
They are run when:
2ff057
2ff057
\verbatim
2ff057
	1) mymailer is already installed, and sendmail is installed or
2ff057
	   upgraded
2ff057
	2) mymailer is already installed, and vmail is installed or
2ff057
	   upgraded
2ff057
	3) sendmail is already installed, and mymailer is installed or
2ff057
	   upgraded
2ff057
	4) vmail is already installed, and mymailer is installed or
2ff057
	   upgraded
2ff057
\endverbatim
2ff057
2ff057
For the upgrading, the strategy is a little different. Rather then
2ff057
setting the link to point to the trigger, the link is set to point to
2ff057
the *other* mailer (if it exists), as follows:
2ff057
2ff057
\verbatim
2ff057
	%triggerun -- sendmail
2ff057
	[ $2 = 0 ] || exit 0
2ff057
	if [ -f /usr/bin/vmail ]; then
2ff057
		ln -sf /usr/bin/vmail /etc/mymailer/mailer
2ff057
	else
2ff057
		rm -f /etc/mymailer/mailer
2ff057
2ff057
	fi
2ff057
2ff057
	%triggerun -- vmail
2ff057
	[ $2 = 0 ] || exit 0
2ff057
	if [ -f /usr/bin/sendmail ]; then
2ff057
		ln -sf /usr/bin/sendmail /etc/mymailer/mailer
2ff057
	else
2ff057
		rm -f /etc/mymailer/mailer
2ff057
2ff057
	fi
2ff057
2ff057
	%postun
2ff057
	[ $1 = 0 ] && rm -f /etc/mymailer/mailer
2ff057
\endverbatim
2ff057
2ff057
These trigger scripts get run when:
2ff057
2ff057
\verbatim
2ff057
	1) sendmail is installed, and mymailer is removed
2ff057
	2) vmail is installed, and mymailer is removed
2ff057
	3) mymailer is installed, and sendmail gets removed
2ff057
	4) mymailer is installed, and vmail gets removed
2ff057
\endverbatim
2ff057
2ff057
The %postun insures that /etc/mymailer/mailer is removed when mymailer
2ff057
is removed (triggers get run at the same time as %preun scripts, so 
2ff057
doing this in the %postun is safe). Note that the triggers are testing
2ff057
$2 to see if any action should occur. Recall that the $1 passed to regular
2ff057
scripts contains the number of instances of the package which will be 
2ff057
installed when the operation has completed. $1 for triggers is exactly
2ff057
the same -- it is the number of instances of the source (or triggered)
2ff057
package which will remain when the trigger has completed. Similarly, $2
2ff057
is the number of instances of the target package which will remain. In
2ff057
this case, if any of the targets will remain after the uninstall, the
2ff057
trigger doesn't do anything (as it's probably being triggered by an
2ff057
upgrade).
2ff057
2ff057
\section triggers_syntax Trigger Syntax
2ff057
2ff057
Trigger specifications are of the form:
2ff057
2ff057
\verbatim
2ff057
	%trigger{un|in|postun} [[-n] <subpackage>] [-p <program>] -- <trigger>
2ff057
\endverbatim
2ff057
2ff057
The -n and -p arguments are the same as for %post scripts.  The
2ff057
\<trigger\> portion is syntactically equivalent to a "Requires"
2ff057
specification (version numbers may be used). If multiple items are
2ff057
given (comma separated), the trigger is run when *any* of those
2ff057
conditions becomes true (the , can be read as "or"). For example:
2ff057
2ff057
\verbatim
2ff057
	%triggerin -n package -p /usr/bin/perl -- fileutils > 3.0, perl < 1.2
2ff057
	print "I'm in my trigger!\n";
2ff057
\endverbatim
2ff057
2ff057
Will put a trigger in package 'package' which runs when the installation
2ff057
status of either fileutils > 3.0 or perl < 1.2 is changed. The script will
2ff057
be run through /usr/bin/perl rather then /bin/sh (which is the default).
2ff057
2ff057
\section triggers_unusual An Unusual Case
2ff057
2ff057
There is one other type of trigger available -- %triggerpostun. These are
2ff057
triggers that are run after their target package has been removed; they will
2ff057
never be run when the package containing the trigger is removed. 
2ff057
2ff057
While this type of trigger is almost never useful, they allow a package to
2ff057
fix errors introduced by the %postun of another package (or by an earlier 
2ff057
version of that package).
2ff057
2ff057
\section triggers_order Order of Script Execution
2ff057
2ff057
For reference, here's the order in which scripts are executed on a single
2ff057
package upgrade:
2ff057
2ff057
\verbatim
2ff057
  all-%pretrans
2ff057
  ...
2ff057
  any-%triggerprein (%triggerprein from other packages set off by new install)
2ff057
  new-%triggerprein
2ff057
  new-%pre	for new version of package being installed
2ff057
  ...		(all new files are installed)
2ff057
  new-%post	for new version of package being installed
2ff057
2ff057
  any-%triggerin (%triggerin from other packages set off by new install)
2ff057
  new-%triggerin
2ff057
  old-%triggerun
2ff057
  any-%triggerun (%triggerun from other packages set off by old uninstall)
2ff057
2ff057
  old-%preun	for old version of package being removed
2ff057
  ...		(all old files are removed)
2ff057
  old-%postun	for old version of package being removed
2ff057
2ff057
  old-%triggerpostun
2ff057
  any-%triggerpostun (%triggerpostun from other packages set off by old un
2ff057
		install)
2ff057
  ...
2ff057
  all-%posttrans
2ff057
\endverbatim
2ff057
*/