I am using incus 6.0.2 LTS extensively for scripted container builds, and there are a number of cases where it would be helpful to have some basic idempotent operations.
The simplest case is when I want to ensure that a container is running, regardless of whether it was previous running or not. incus start foo
gives an error if foo
is already running. I don’t want to do:
incus start "$container" || true
because that will ignore all possible startup errors. So I end up doing something fragile like:
[ "$(incus list -c s -f csv name=$container)" = "RUNNING" ] || incus start "$container"
(which also has a race condition). Similarly for stopping a container which may or may not already be stopped.
Another common case is to ensure that a container is deleted, before I rebuild it. Again, deleting a non-existing container fails, so:
# not ideal
incus delete -f "$container" || true
# a bit better
incus info "$container" && incus delete -f "$container"
It would make things a lot tidier if I could do:
incus start --idempotent "$container"
incus stop --idempotent "$container"
incus delete -f --idempotent "$container"
(I’m not worried about the actual flag names; things like --start-if-stopped
would be OK too)
Those are the most basic operations. Potentially there could also be:
incus config device add --idempotent "$container" http proxy "listen=tcp:[::]:20080" "connect=tcp:[::]:80"
If the device already exists with those settings, do nothing instead of giving an error. Potentially it could go further:
- If the device already exists with the same type (proxy) then replace the settings, like
incus config device set
(andunset
anything not listed) - If the device already exists with a different type, then
delete
andadd
Those latter cases are less important to me; incus config device remove --idempotent
could be useful though.
As an alternative, I looked at incus config edit
to set all devices in one go, but that requires an external merge of all other non-device config keys:
incus config edit "$container" <<EOS
devices:
eth0:
... stuff here
EOS
Error: Volatile idmap keys can't be deleted by the user
Maybe using yq could achieve that. (AFAICS there’s no incus config show --format json
to use with jq)
Anyway… just a suggestion. Thanks for all the great work on incus!