Pre-refresh hook: can we determine the upgrade version?

I have a check to compare versions. If the user is trying to downgrade, or skip a major version, it’s something the software in my snap doesn’t support. I have a post-refresh hook doing this comparison right now; I save the old $SNAP_VERSION with snapctl set while it’s running, then I compare it to $SNAP_VERSION in the post-refresh hook. If it looks unsupported, the hook exits non-zero and the refresh fails, rolling back to the old revision.

That works nicely, but it seems like it’s more work than necessary. The refresh very nearly is complete (old services are down, profiles are created) by the time the snap realizes it’s not going to work and rolls back. In an ideal world I could do this check in the pre-refresh hook of the existing snap: if I can take a peek at the SNAP_VERSION that we’re about to upgrade to, I could halt the upgrade before it even really begins, without any negative side effect for the services.

1 Like

In general the old version though cannot know if it’s compatible with the new version, no? only the new version can know in general?

Wouldn’t epochs maybe help set the guardrails you need?

Anyway we do plan to have a general mechanism for a snap via snapctl to query whether there is a new revision available, I think that mechanism if designed correctly should answer with being-refreshed-to version from the pre-refresh hook at least.

Actually, you’re probably right there. If you consider what happens if my snap later grew the ability to handle skipping a major version, there’s no way for the old revision to learn that. The only place this can reliably be determined is using the new version. I just wish I could do it without shutting the services down, do you see any reasonable way to do that?

Regarding epochs, in an ideal world yes, I’d be using them to avoid this type of issue. Sadly it’s too easy to shoot yourself in the foot with them, so I find myself avoiding them.