canonical / charmcraft

Collaborate, build and publish charmed operators for Kubernetes, Linux and Windows.
Apache License 2.0
65 stars 70 forks source link

add `charmcraft purge` or `charmcraft cleanall` command to clean all instances #1042

Open sed-i opened 1 year ago

sed-i commented 1 year ago

I ran out of space because /var/snap/lxd/common/lxd/storage-pools/default/containers/charmcraft* had a lot of stuff in it.

$ sudo ls /var/snap/lxd/common/lxd/storage-pools/default/containers
charmcraft_base-instance-charmcraft-buildd-base-v00-36b64d8a932634714eb2  charmcraft_charmcraft-grafana-agent-22-0-0-amd64  charmcraft_charmcraft-prometheus-k8s-36-0-0-amd64
charmcraft_base-instance-charmcraft-buildd-base-v00-f36807e72ea445fd8492  charmcraft_charmcraft-grafana-agent-k8s-2-0-0-amd64   charmcraft_charmcraft-pusher-54-0-0-amd64
charmcraft_charmcraft-alertmanager-k8s-5-0-0-amd64           charmcraft_charmcraft-grafana-agent-k8s-22-0-0-amd64   juju-0728c2-2
charmcraft_charmcraft-application-307288-0-0-amd64           charmcraft_charmcraft-kafka-292835-0-0-amd64       juju-0728c2-4
charmcraft_charmcraft-avalanche-k8s-8-0-0-amd64              charmcraft_charmcraft-mimir-k8s-28-0-0-amd64       juju-541575-0

I wonder if it makes sense to add a --all flag to clean so it will run the equivalent of:

sudo bash -c "rm -rf /var/snap/lxd/common/lxd/storage-pools/default/containers/charmcraft*"

More precisely,

for instance in $(lxc list --project=charmcraft --format json | jq -r '.[] | select(.name | startswith("charmcraft-")) | .name');
do
    lxc --project=charmcraft delete $instance
done

Or, in nushell,

(lxc list --project=charmcraft --format json
| from json
| get name
| where { |it| $it | str starts-with 'charmcraft-' }
| each { |it| lxc --project=charmcraft delete $it }
)
mr-cal commented 1 year ago

Thanks for the suggestion! I've been in this same scenario before too.

charmcraft clean is a lifecycle command that only operates on the current charmcraft project. I think that adding a flag to clean that affects other projects wouldn't follow the "principle of least surprise". It would probably need to be a separate command.

Rockcraft recently got a new feature. When you uninstall rockcraft, it removes all rockcraft images and instances in lxd. If we added this to charmcraft, then uninstalling and re-installing charmcraft would solve your problem. However, that isn't a great user experience either.

sed-i commented 1 year ago

If it's just about naming then purge, reset, destroy may be less surprising. Or, because the action is very similar to clean, perhaps cleanall or cleanup?

Not sure about cleanup on uninstall. Would be nice have charms continue to repack quickly across snap refresh.

mr-cal commented 1 year ago

I like those ideas, I think something like charmcraft purge would be good. Thanks!

For what it's worth, the remove hook doesn't run on a snap refresh, so the LXD instances are retained when updating.

lengau commented 1 year ago

Perhaps a reset-system subcommand with a big (default aborting) warning message about how this will affect all charmcraft build environments on this system. (We can offer a --force or similar to non-interactively allow the command to proceed)

mkalcok commented 1 year ago

I just want to voice support for this feature (In any form) as having to manually delete bunch of containers every once in a while is just no fun.

jnsgruk commented 1 year ago

Docker has a docker image prune command and docker system prune - perhaps we could follow suit

That said, multipass uses multipass purge so maybe that's more appropriate?

sergiusens commented 6 months ago

Did not give it much thought, but something like snap set charmcraft provider.lxd=purge could work. I might also be butchering the system here

syncronize-issues-to-jira[bot] commented 6 months ago

Thank you for reporting us your feedback!

The internal ticket has been created: https://warthogs.atlassian.net/browse/CRAFT-2551.

This message was autogenerated

lengau commented 6 months ago

With fresh eyes I think charmcraft purge is probably the way to go with this.

There are several things we might want to clean up in this:

  1. Shared pip cache
  2. Build containers that are STOPPED
  3. Build containers in any state (probably behind an additional flag)
  4. Base images (behind its own flag)

Since the pip cache is attached to a specific base image, removing a base image can automatically remove its pip cache. However, I've personally come across use cases where I want to clear the pip cache without removing any build images, so I'd like that to be possible too.

paulomach commented 6 months ago

With fresh eyes I think charmcraft purge is probably the way to go with this. ... Since the pip cache is attached to a specific base image, removing a base image can automatically remove its pip cache. However, I've personally come across use cases where I want to clear the pip cache without removing any build images, so I'd like that to be possible too.

So how about just prompting with a [Y/n] per artifact category?

sed-i commented 5 months ago

cases where I want to clear the pip cache without removing any build images

I imagine 80%+ of the time users would want to wipe it all, so personally I'd like the plain purge to wipe it all. In any case, a cheatsheet style doc could help a lot with special cases.

lengau commented 5 months ago

@sed-i I think with the purge command 1 and 2 would be the most common needs, since #3 may interfere with another ongoing build and #4 would purge things that get automatically managed and cleaned up, but which are also far less likely to get into an unusable state. Something like:

Then to limit further we could have:

lengau commented 5 months ago

@paulomach that's a possibility too, as long as we provide a way to do it non-interactively as well. I'd want [Y/n] for "common" categories (1 and 2) and [y/N] for the others.

I'll need to talk with the rest of the team though, as whatever gets decided for Charmcraft will likely become the standard for all craft applications (with obviously the relevant changes), so whatever we decide should be done carefully.

sed-i commented 5 months ago

If it helps, we could start with just purge and purge --all for now.

But I wonder if the inverse is easier to grok and extend:

charmcraft purge  # all purged
charmcraft purge --keep-cache --keep-running --keep-builders  # nothing purged
lengau commented 1 week ago

Blocked by https://github.com/canonical/craft-providers/issues/639