Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/snippets.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Snippets For Argument Usage
=========

.. toctree::
:maxdepth: 1
:glob:

snippets/*
88 changes: 88 additions & 0 deletions docs/snippets/triggered.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
Debian Triggers
===============

Debian packages can install commands to be run after installation and configuration of all packages.
Those commands are called triggers as other packages can ask the package manager to run those.

Sample trigger script
---------------------

This script defines two triggers. First the label based trigger `some-trigger` which runs `some-command`. Second the path based trigger `/etc/foobar` which runs a find-command.

::

for trigger in "$@"; do
case "$trigger" in
some-trigger)
some-command
;;
/etc/foobar)
find /etc/foobar -type f -ls
;;
esac
done

Install trigger
---------------

The following fpm command installs the trigger:

fpm --triggered /path/to/triggers.sh

The resulting postinst-script in the packages looks like this:

::

#!/bin/sh

after_upgrade() {
:
}

after_install() {
:
}

triggered() {
:
for trigger in "$@"; do
case "$trigger" in
some-trigger)
some-command
;;
/etc/foobar)
find /etc/foobar -type f -ls
;;
esac
done
}

if [ "${1}" = "configure" -a -z "${2}" ] || \
[ "${1}" = "abort-remove" ]
then
# "after install" here
# "abort-remove" happens when the pre-removal script failed.
# In that case, this script, which should be idemptoent, is run
# to ensure a clean roll-back of the removal.
after_install
elif [ "${1}" = "configure" -a -n "${2}" ]
then
upgradeFromVersion="${2}"
# "after upgrade" here
# NOTE: This slot is also used when deb packages are removed,
# but their config files aren't, but a newer version of the
# package is installed later, called "Config-Files" state.
# basically, that still looks a _lot_ like an upgrade to me.
after_upgrade "${2}"
elif [ "${1}" = "triggered" ]
then
# "triggered" here
# NOTE: This slot allows implementing package triggers according to
# https://wiki.debian.org/DpkgTriggers
shift
triggered "${@}"
elif echo "${1}" | grep -E -q "(abort|fail)"
then
echo "Failed to install before the post-installation script was run." >&2
exit 1
fi
8 changes: 8 additions & 0 deletions lib/fpm/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ def help(*args)
File.expand_path(val) # Get the full path to the script
end # --before-upgrade

option "--triggered", "FILE",
"A script snippets to define and run package triggers, see\n" \
"https://wiki.debian.org/DpkgTriggers and\n" \
"https://stackoverflow.com/questions/15276535/dpkg-how-to-use-trigger\n" do |val|
File.expand_path(val) # Get the full path to the script
end # --triggered

option "--template-scripts", :flag,
"Allow scripts to be templated. This lets you use ERB to template your " \
"packaging scripts (for --after-install, etc). For example, you can do " \
Expand Down Expand Up @@ -466,6 +473,7 @@ def execute
setscript.call(:after_remove)
setscript.call(:before_upgrade)
setscript.call(:after_upgrade)
setscript.call(:triggered)

# Bail if any setscript calls had errors. We don't need to log
# anything because we've already logged the error(s) above.
Expand Down
8 changes: 5 additions & 3 deletions lib/fpm/package/deb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,15 @@ def output(output_path)
attributes[:deb_systemd] << name
end

if script?(:before_upgrade) or script?(:after_upgrade) or attributes[:deb_systemd].any?
if script?(:before_upgrade) or script?(:after_upgrade) or attributes[:deb_systemd].any? or script?(:triggered)
puts "Adding action files"
if script?(:before_install) or script?(:before_upgrade)
scripts[:before_install] = template("deb/preinst_upgrade.sh.erb").result(binding)
end
if script?(:before_remove) or not attributes[:deb_systemd].empty?
scripts[:before_remove] = template("deb/prerm_upgrade.sh.erb").result(binding)
end
if script?(:after_install) or script?(:after_upgrade) or attributes[:deb_systemd].any?
if script?(:after_install) or script?(:after_upgrade) or attributes[:deb_systemd].any? or script?(:triggered)
scripts[:after_install] = template("deb/postinst_upgrade.sh.erb").result(binding)
end
if script?(:after_remove)
Expand Down Expand Up @@ -593,7 +593,9 @@ def output(output_path)

if File.exists?(dest_changelog) and not File.exists?(dest_upstream_changelog)
# see https://www.debian.org/doc/debian-policy/ch-docs.html#s-changelogs
File.rename(dest_changelog, dest_upstream_changelog)
# to solve Lintian rule debian-changelog-file-missing-or-wrong-name the file
# is copied and not renamed
FileUtils.cp(dest_changelog, dest_upstream_changelog)
end

attributes.fetch(:deb_init_list, []).each do |init|
Expand Down
14 changes: 14 additions & 0 deletions templates/deb/postinst_upgrade.sh.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ $debsystemctl start <%= service %> >/dev/null || true
<% end -%>
}

triggered() {
: # Ensure this if-clause is not empty. If it were empty, and we had an 'else', then it is an error in shell syntax
<% if script?(:triggered) -%>
<%= script(:triggered) %>
<% end -%>
}

if [ "${1}" = "configure" -a -z "${2}" ] || \
[ "${1}" = "abort-remove" ]
then
Expand All @@ -77,6 +84,13 @@ then
# package is installed later, called "Config-Files" state.
# basically, that still looks a _lot_ like an upgrade to me.
after_upgrade "${2}"
elif [ "${1}" = "triggered" ]
then
# "triggered" here
# NOTE: This slot allows implementing package triggers according to
# https://wiki.debian.org/DpkgTriggers
shift
triggered "${@}"
elif echo "${1}" | grep -E -q "(abort|fail)"
then
echo "Failed to install before the post-installation script was run." >&2
Expand Down