hooks

pvanloo edited this page Aug 4, 2017 · 5 revisions

Hooks

There are a number of situations where snapd needs to notify a snap that something has happened. For example, when a snap is upgraded, it may need to run some sort of migration on the previous version's data in order to make it consumable by the new version. Or when an interface is connected or disconnected, the snap might need to obtain attributes specific to that connection. These types of situations are handled by hooks.

A hook is defined as an executable contained within the snap/hooks/ directory inside the snap. The file name of the executable is the name of the hook (e.g. the upgrade hook executable would be snap/hooks/upgrade).

As long as the file name of the executable corresponds to a supported hook name, that's all one needs to do in order to utilize a hook within their snap. Note that hooks, like apps, are executed within a confined environment. By default hooks will run with no plugs; if a hook needs more privileges one can use the top-level attribute hooks in snapcraft.yaml to request plugs, like so:

hooks: # Top-level YAML attribute, parallel to `apps`
    upgrade: # Hook name, corresponds to executable name
        plugs: [network] # Or any other plugs required by this hook

Note that hooks will be called with no parameters. If they need more information from snapd (or need to provide information to snapd) they can utilize the snapctl command (for more information on snapctl, see snapctl -h).

Supported Hooks

Note: The development of specific hooks is ongoing.

configure

The configure hook is called upon initial install, upgrade, and whenever the user requests a configuration change via the snap set command. The hook should use snapctl get to retrieve the requested configuration from snapd, and act upon it. If it exits non-zero, the configuration will not be applied.

configure example

Say the user runs:

$ snap set <snapname> username=foo password=bar

The configure hook would be located within the snap at snap/hooks/configure. An example of what it might contain is:

#!/bin/sh

username=$(snapctl get username)
password=$(snapctl get password)

if [ -z "$username" ]; then
    echo "Username is required"
    exit 1
fi

if [ -z "$password" ]; then
    echo "Password is required"
    exit 1
fi

# Handle username and password, perhaps write to a credential file of some sort.
echo "user=$username" > $SNAP_DATA/credentials
echo "password=$password" >> $SNAP_DATA/credentials
chmod 600 $SNAP_DATA/credentials

prepare-device (gadget hook)

See details in the gadget snap documentation.