Snapcraft Offline Mode (proposal)

Objective

Improve Snapcraft behaviors when network connectivity is unavailable, either purposefully (airplane mode) or accidentally (temporary network outage).

Background

Network is required for Snapcraft to function for several components:

  • snap store (info, download, install)

  • apt (cache update, download, install)

  • network connectivity checks (starting container/VM instance)

  • lxd, multipass (namely to launch new container/VM)

  • plugin-specific tooling (such as npm, pip, go get)

  • remote-build

Proposed Changes

Add a new parameter --offline to Snapcraft to instruct Snapcraft to avoid network connectivity in situations where it may not be required.

The following sections describe these changes in behavior in more detail.

Provided Environments

Startup Behaviors: Network Connectivity

When launching an LXD container, Snapcraft performs a network connectivity check.

  • If --offline is used, Snapcraft shall skip this network connectivity check.

Startup Behaviors: Snapcraft Updates

When launching a build environment, Snapcraft ensures the Snapcraft in the environment is up-to-date.

  • If --offline is used, Snapcraft shall not attempt to update Snapcraft snap or its dependencies.

Apt Support

Apt Cache

Upon launching the build environment, Snapcraft may update the apt cache to ensure it is up to date.

  • If --offline is used, Snapcraft shall skip this update and warn accordingly.

  • The stage package cache shall be updated to use the host’s apt state such that the stage cache does not have to be updated independently.

Stage Packages

Stage packages and their versions are determined on every run of Snapcraft.

  • If --offline is used, Snapcraft shall unpack previously fetched stage-packages, erroring if the configured set of stage-packages has changed.

Build Packages

Build packages and their versions are determined on every run of Snapcraft.

  • If --offline is used, Snapcraft shall error only if the requested build-packages are not installed. The installed version shall be ignored.

Snaps

Stage Snaps

Stage snaps and their versions are determined on every run of Snapcraft.

  • If --offline is used, Snapcraft shall unpack previously fetched stage snaps, erroring if the configured set of stage-snaps has changed.

Build Snaps

Build snaps and their versions are determined on every run of Snapcraft.

  • If --offline is used, Snapcraft shall error only if the requested snap is not installed. The installed revision/channel shall be ignored.

Lifecycle Management

Automatic Part Cleaning

Snapcraft may automatically clean parts that have dependencies that changed (new sources, rebuilt, etc.).

  • If --offline is used, Snapcraft will skip the clean step for these parts but warn accordingly. This allows the part to maintain its previously fetched stage snaps, packages, and sources.

Reducing Network Accesses

Snapd API usage

Snapcraft uses Snapd’s /v2/find API which incurs a network access to the Snap Store. Some of these calls can be replaced with /v2/snaps API to limit the query to installed snaps.

Additionally, some /v2/find calls are redundant and may be consolidated.

Out of Scope

Future work will consider:

  • plugin behaviors
  • enabling usage of snapshots for LXD, etc.
2 Likes

There is the risk, by doing so, of keeping files in stage and prime (and consequently in the generated snap) that would otherwise be removed. Perhaps we could keep the downloaded packages, but unstage/unprime as usual and re-unpack stage packages if needed? This way only downloads would be skipped and the rest of the lifecycle behavior would remain unchanged.

Is this just a risk of extra files no longer shipped in a re-run? Personally I’m OK with that as the warning is not trying to convey that it is not a “clean” build which could be done with manually cleaning (or not using --offline). Regardless, I agree unstaging and unpriming would be best (and that you would be the expert that can sort out how to do that best :stuck_out_tongue_winking_eye:). I’ll investigate this a bit further.

Maybe cutting the stage to pull dependency is worth an investigation too. (This is similar to what craft-parts does)

2 Likes