Creating a python daemon to interact with gpio

Hi all - I’m new to snapping, and am trying to create a snap on an edge device running Ubuntu Core. Unfortunately after creating my yaml file:

 name: debounce-filter
 version: '0.1'
 summary: ##############
 description: |
 grade: devel
 confinement: devmode
 
 apps:
   filter:
     command: filter
     plugs: [gpio]
     daemon: simple
     restart-condition: always
 parts:
   filter:
     plugin: python
     python-version: python2
     source: .

and running the “snapcraft” command, my snap fails to build with the following error:

Failed to generate snap metadata: The specified command ‘filter’ defined in the app ‘filter’ does not exist or is not executable

From what I understand, the snapcraft program is searching for a bin that doesn’t exist, which results in the above error.

By comparing my current parts/stage/prime folders with those of another app that was successfully snapped, it seems that stage/bin is empty. I’d appreciate any help with troubleshooting what the issue could be; I’ve been looking into snappy-debug, but am having some difficulty getting this started as well. I appreciate any and all input. Thanks in advance!Preformatted text

Wanted to post an update in case there was anyone facing a similar issue - my updated yaml file is as follows (snaps and installs successfully although the service seems to fail; possibly because of permissions related to gpio access)

anyway, here’s a yaml file to simply create a daemon of a python file:

name: debounce-filter 
version: '0.1'
summary: ############# 
description: |
grade: devel
confinement: devmode

apps:
  filter:
    command: bin/filter
    plugs: [gpio]
    daemon: simple
    restart-condition: always
parts:
  filter:
    plugin: dump
    source: .
    stage-packages: [libdb5.3, python2.7]
    organize:
      debounce_filter.py: bin/filter

Just a word of caution, the use of sudo here will only work becuase you have set confinement to devmode. You should be using sudo from the calling shell instead for better future proofing.

Thanks for catching that! The daemon actually fails with sudo/I forgot to remove that before I posted.

For anyone reading, I’m currently stuck debugging why my daemon immediately exits with status 126. I’m inclined to believe the script doesn’t have permission to the gpio pins, although I’m not absolutely sure about this as the relevant gpio pins should be connected:

> sudo snap interfaces
   Slot                                              Plug
> caracalla:gpa-sku-gpio0              debounce-filter:gpio
> caracalla:gpa-sku-gpio7              debounce-filter:gpio

I’ll update further once I’ve figured out what the issue is, although input from someone with similar experience would be much appreciated.

Also changing the heading from ‘Empty “stage/bin” when creating a snap’ to ‘creating a python daemon to interact with gpio’ for clarity.

You can use snappy-debug.security scanlog to determine whether there are issues with confinement:

You need two terminals, one for the debug spew and the other to launch your application. On an Ubuntu Core device this can be achieved by connecting via SSH multiple times simultaneously.

# Terminal 1
sudo snap install snappy-debug
sudo snappy-debug.security scanlog
# Terminal 2

# For a user-launchable app
sudo snap run filter # or `sudo /snap/bin/filter`
                     # or just `sudo filter` as $PATH should cover /snap/bin anyway

# For a systemd daemon (you want this one for your yaml above)
sudo systemctl restart snap.debounce-filter.filter.service

Thanks for the response Daniel!

I successfully installed snappy-debug, but

results in “command not found”, and I can’t find the folder it’s installed in to manually run the command either.

OTOH

gives me the following:

Failed to restart snap.debounce-filter.filter.service: The name org.freedesktop.PolicyKit1 was not provided by any .service files
See system logs and ‘systemctl status snap.debounce-filter.filter.service’ for details.

Where the system logs and systemctl status have informed me that the exit status is 126.

try the full path:

sudo /snap/bin/snappy-debug.security scanlog

The path ‘/snap/bin’ doesn’t exist, although

$ snap find snappy-bug
Name Version Developer Notes Summary
snappy-debug 0.31.7-snapd2.28.5 canonical - Debug tools for ubuntu-core

and

$ snap list
snappy-debug 0.31.7-snapd2.28.5 219 stable canonical -`

so I’m not sure where my snaps are being installed. So far my attempts to find them have also been unsuccessful.

can you give us the output of snap version so we know what your setup is ?

Hi @ogra!

The output is:

$ snap version
snap    2.32.9
snapd   2.33
series  16
kernel  4.4.0-122-generic

this is extremely weird, the two should have the same version since on an UbuntuCore system they come from the same snap package. did you somehow tinker with the system installation ?

To my knowledge it’s still the same image that came preinstalled on the device. Could the different versions be a possible explanation for the behaviour of snap? I think the daemon issue is separate, but it’s definitely odd that I can’t find where snaps are being installed. What would you recommend going forward?

snaps usually get installed into /var/lib/snapd/snaps/ … the .snap files are squashfs’es that get mounted into /var/snap/ which is equivalent to /snap (both dirs should contain the same). apps defined in snapcraft.yaml of the snaps then get an executable entry in /snap/bin which typically is in your $PATH.

(i’m actually not sure the version difference is an actual issue, but i have never seen something like that on an UbuntuCore system … OTOH i am not even sure caracalla is shipped with only UbuntuCore, you might have one with a standard Ubuntu Server install, just running “apt” would tell you that you are on UbuntuCore and that apt is not supported in case you are on a Core system)

Thanks for clarifying that! I looked in /var/lib/snapd/snaps/, /var/lib/snapd/snaps/partial/ and /var/snap/ and they’re all empty.

‘apt’ does indeed confirm that I’m on Ubuntu Core, so I’m not really sure what’s happening.

Going back to the failure of the daemon to run - journalctl shows the following:

Jul 03 14:39:33 8L5T802 kernel: audit: type=1326 audit(1530628773.431:19691): auid=1000 uid=0 gid=1000 ses=27 pid=935 comm=“sudo” exe="/usr/bin/sudo" sig=0 arch=c000003e syscall=41 compat=0 ip=0x7f293e582
Jul 03 14:39:33 8L5T802 kernel: audit: type=1326 audit(1530628773.431:19692): auid=1000 uid=0 gid=1000 ses=27 pid=935 comm=“sudo” exe="/usr/bin/sudo" sig=0 arch=c000003e syscall=41 compat=0 ip=0x7f293e582
Jul 03 14:39:33 8L5T802 audit[935]: SECCOMP auid=1000 uid=0 gid=1000 ses=27 pid=935 comm=“sudo” exe="/usr/bin/sudo" sig=0 arch=c000003e syscall=41 compat=0 ip=0x7f293e5825a7 code=0x7ffc0000
Jul 03 14:39:33 8L5T802 audit[935]: SECCOMP auid=1000 uid=0 gid=1000 ses=27 pid=935 comm=“sudo” exe="/usr/bin/sudo" sig=0 arch=c000003e syscall=41 compat=0 ip=0x7f293e5825a7 code=0x7ffc0000
Jul 03 14:39:33 8L5T802 debounce-filter.filter[1156]: /snap/debounce-filter/x1/bin/filter: 1: /snap/debounce-filter/x1/bin/filter: import: not found
Jul 03 14:39:33 8L5T802 debounce-filter.filter[1156]: /snap/debounce-filter/x1/bin/filter: 2: /snap/debounce-filter/x1/bin/filter: import: not found
Jul 03 14:39:33 8L5T802 debounce-filter.filter[1156]: /snap/debounce-filter/x1/bin/filter: 3: /snap/debounce-filter/x1/bin/filter: from: not found
Jul 03 14:39:33 8L5T802 debounce-filter.filter[1156]: /snap/debounce-filter/x1/bin/filter: 4: /snap/debounce-filter/x1/bin/filter: import: not found
Jul 03 14:39:33 8L5T802 debounce-filter.filter[1156]: /snap/debounce-filter/x1/bin/filter: 6: /snap/debounce-filter/x1/bin/filter: Syntax error: word unexpected (expecting “)”)
Jul 03 14:39:33 8L5T802 systemd[1]: snap.debounce-filter.filter.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Jul 03 14:39:33 8L5T802 systemd[1]: snap.debounce-filter.filter.service: Unit entered failed state.
Jul 03 14:39:33 8L5T802 systemd[1]: snap.debounce-filter.filter.service: Failed with result ‘exit-code’.
Jul 03 14:39:33 8L5T802 systemd[1]: snap.debounce-filter.filter.service: Service hold-off time over, scheduling restart.
Jul 03 14:39:33 8L5T802 systemd[1]: Stopped Service for snap application debounce-filter.filter.

Which leads me to believe that ‘dump’ wasn’t the correct plugin to use in creating the Snap, as the necessary modules can’t be found. This seems a little odd to me as python2.7 is included in ‘stage-packages’…

I also tried specifying ‘python’ as the plugin and ‘python-version: python2’, but this results in a failure to create a snap.

Is there something I’m missing here?

That looks like it’s running bin/filter as a bash script, not a python script. Does the file have a correct #! line at the top? If you don’t have that line, or don’t want it, you can change your command to something like:

apps:
  filter:
    command: python $SNAP/bin/filter
1 Like