Running a snap command inside of a hook

Hallo,

I have this application(gizmo) which I have configured to run from the snapcraft.yaml file. I am making changes and I want to run this application from a configure hook.

So I tried to accomplish this by putting the application binary in the path “snap/hooks/configure”.

When I try to execute the application using “sudo snap get gizmo”

I get the error: “snap gizmo has no configuration”

Thanks for the help.

The configure hook is called (meaning it runs/executes) when one of the following actions happen:

  • initial snap installation
  • snap refresh
  • whenever the user runs snap set|unset to change a configuration option

So the hook will not run when you run snap get , only when you run snap set. See The configure hook for more information.

I get the error: “snap gizmo has no configuration”

This error means that no configuration values have been set. There are two ways to set config values:

  • A user running sudo snap set ...
  • The snap itself running snapctl set ...

See Adding snap configuration for more information.

Does this help you further?

Thanks it helps . I have gone through the documentation but I still have issues.

This is a sample of my project

The snap name is “gizmo” and app name “mundane”

I execute the app from the snapcraft.yaml file using
gizmo.mundane

To run this from the configure hook

I set the command in the configure hook path; snap/hooks/configure as:
gizmo.mundane

Following the documentation I tried to set it using:
snap set gizmo mundane

Also, using
snap set gizmo.mundane

but I end up with the error :
invalid configuration: "mundane" (want key=value)

You can’t set an empty entry, you need to use key=value:

snap set gizmo config_key=config_value

Can you give more explanation of what you are trying to do? What do you want to do with the configure hook and what does your app do?

Do you mean that you want to run the gizmo.mundane command in the configure hook of your snap? You cannot use the gizmo.mundane command itself for that. You need to manually execute the binary in your application and use snapcraft-runner. This is an example of how to do that: https://github.com/IBCNServices/easy-openvpn-server/blob/master/snap/hooks/configure

This is the configure hook:

#!/bin/bash
set -e

export LC_ALL=C.UTF-8
export LANG=C.UTF-8
$SNAP/snap/command-chain/snapcraft-runner $SNAP/bin/setup.py setup

The setup.py script that this hook runs is also one of the commands of my app.

apps:
  easy-openvpn-server:
    command: bin/setup.py
    plugs:
      - network    
    environment:
      LC_ALL: C.UTF-8
      LANG: C.UTF-8

Users can run easy-openvpn-server, but these commands are only available to users, not to the snap itself. So the configure hook of the snap needs to run bin/setup.py directly. Note that the configure hook always runs as root so your application will always be executed as root if you execute it from the configure hook.

  • When a user runs the snap set key=value1 command, the configure hook of the application will run, and in that hook, you can see value1 by running snap get key.
  • When a configure hook runs the command snap set key=value2, value1 is now value2, but this does not execute anything.
1 Like

It’s a weather application and I want to run the gizmo.mundane command in the configure hook.

I will try to make use of the example you’ve given.

Thanks.

1 Like