Dynamic command

I’m trying to have my snap using config values in the command, like the below.

    command: bin/flask run -h -p $port
    daemon: simple
    restart-condition: always
    plugs: [network-bind]
      FLASK_APP: ./server.py
      LC_ALL: C.UTF-8
      LANG: C.UTF-8
      port: $(snapctl get port)

Fails as this.

feb 01 00:56:21 frozen microsample.microsample[286081]: Error: Invalid value for '--port' / '-p': '$(snapctl get port)' is not a valid integer.
feb 01 00:56:21 frozen systemd[1]: snap.microsample.microsample.service: Main process exited, code=exited, status=2/INVALIDARGUMENT

What can I do to dynamically change the command to use the value for the port in the snap?

Here is my repo: https://github.com/erik78se/microsample-flask-snap/

You could consider starting the app with a wrapper script, e.g.

    command: bin/flask-wrapper # you could add more args here if needed

Then in bin/flask-wrapper:

#!/usr/bin/env bash

port="$(snapctl get port)"

exec /bin/flask run -h -p $port $@

Cheers, Jon


environment variables do not allow scripts, they are not processed by a shell but just exported into the environment, what Jon said is correct, you should use a wrapper script for such a case …

Is there a good document describing the process of adding wrappers to a snapcraft.yaml ?

… also, when I do as recommended - I get this error when I install the snap:

feb 01 14:27:01 frozen microsample.microsample[37167]: /snap/microsample/x1/bin/flask-wrapper: 5: exec: /bin/flask: not found
feb 01 14:27:01 frozen systemd[1]: snap.microsample.microsample.service: Main process exited, code=exited, status=127/n/a

Okay - so I don’t know the path of flask in your app :wink: you might need to use ${SNAP}/bin/flask instead.

There is an example wrapper I worked on here: https://github.com/parca-dev/parca-agent/blob/main/snap/local/parca-agent-wrapper

EDIT: and the corresponding snapcraft.yaml

1 Like

This made the trick!

1 Like

@jnsgruk @ogra thanx alot!

This is all for a preparation for a juju workshop where I will be embedding this snap into a charm as described here.

Combining the snap with juju.

Its super useful to be able to describe what goes on all the way.

1 Like