How to programmatically uninstall self?

I have a snap written in python 3. I’d like to provide a button to allow users to uninstall the app from inside itself. On button-click, how can I get the app to self-uninstall?

I know the terminal way of doing things:
sudo snap remove <my-snap-name>

I could call that from python like so:
subprocess.call('sudo snap remove <my-snap-name>', shell=True)

But that code only works when the user has sudo privileges, and these are just regular users. Not positive what user the snap is actually running as, but it isn’t sudo.

I tried using this library:
https://pypi.org/project/elevate/

…but when I called it like so:
elevate(show_console=False, graphical=True)

…I got the following error message:
[Errno 13] Permission denied

Apparently I don’t have permission to ask for permission. LOL

Is there another way to uninstall the snap provided by snapcraft? Or is there a way to launch an elevated privileges window gui to establish sudo permissions for the regular user?

Many thanks!

the only way to do this properly from a confined snap would be through an API call to the snapd socket via the snapd-control interface … but this interface is restricted to usage inside brand stores, you will not be able to upload it to the global store …

2 Likes

Maybe this particular access could be allowed through mediation - e.g. by a new api endpoint that can be called by snapctl - so that a snap can self-destruct, but not touch any other snaps.

1 Like

Although, a self-destructing snap will have to have a means of ensuring that no other users want the snap to remain. It wouldn’t be nice on a multiuser system to install a snap that then gets used by multiple users and one of those users decides to trigger a self-destruct thereby nuking the other users’ data.

2 Likes

If the user demonstrated sudo permissions, then the assumption should probably be that they understand what they are doing and have the right to do so, I would think.

Well you won’t be able to use sudo from inside a strict snap anyways, regardless of whether we added permission for a snap to remove itself. Elevating permissions from inside a snap is not allowed for security reasons.

1 Like