Order in which daemons and configure hook are run?

When we have a configure hook and some daemons in a snap, in which order do they run on install / refresh? Afaik there are three possibilities:

  1. Configure hook runs and stops, then the daemons are started
  2. Daemons are started first, then the configure hook
  3. Configure hook is started, then the daemons are started without waiting for the hook to finish

Actually 2 and 3 are mostly the same as you run both things in parallel.

Whatever the current behaviour of snapd is, is that considered a contract or something that might change?

snapd runs operations as a set of “tasks” in a given “change.” You can see these changes with snap changes:

$ snap changes
ID    Status  Spawn                 Ready                 Summary
2181  Done    2018-02-07T22:31:36Z  2018-02-07T22:31:44Z  Install "nextcloud" snap
2182  Done    2018-02-07T22:35:47Z  2018-02-07T22:36:51Z  Refresh "nextcloud" snap from "candidate" channel
2183  Done    2018-02-07T22:37:43Z  2018-02-07T22:38:20Z  Remove "nextcloud" snap
2184  Done    2018-02-07T23:54:53Z  2018-02-07T23:55:55Z  Install "nextcloud" snap from "13/candidate"

You can see the individual tasks by using snap change:

$ snap change 2181
Status  Spawn                 Ready                 Summary
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:36Z  Ensure prerequisites for "nextcloud" are available
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:36Z  Download snap "nextcloud" (4958) from channel "stable"
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:39Z  Fetch and check assertions for snap "nextcloud" (4958)
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:39Z  Mount snap "nextcloud" (4958)
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:39Z  Copy snap "nextcloud" data
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:42Z  Setup snap "nextcloud" (4958) security profiles
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:43Z  Make snap "nextcloud" (4958) available to the system
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:44Z  Set automatic aliases for snap "nextcloud"
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:44Z  Setup snap "nextcloud" aliases
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:44Z  Run install hook of "nextcloud" snap if present
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:44Z  Start snap "nextcloud" (4958) services
Done    2018-02-07T22:31:36Z  2018-02-07T22:31:44Z  Run configure hook of "nextcloud" snap if present

You can tell from the task ordering that this is (2) from the list of possibilities you provided. As I recall it was placed after services fired up so that it could poke at them as necessary, as such I don’t think this will change, but I don’t suggest taking my word for it. @niemeyer can say for certain.

3 Likes

@kyrofa thanks for the nice explanation and example, that answers the question.

Really helpful this information. I think it worth putting on the configure-hook documentation.

In my case, I was trying to copy some files and change ownership on the configure hook and the systemd logs was showing that files and directories didn’t exist.

After trying to start the service 5 times, the daemon stopped and when I restarted using systemctl the snap was able to start and files and paths were there. Now I can see that install hook it’s better suited for this task, but it took me some time to understand what was going on.

I agree, this stuff isn’t documented terribly well. In case it helps, I have a few blog posts about this: