Snapping a binary with configuration file

I need help with creating a snap of a binary that needs a configuration file.
To run the application outside of a snap, I would run “ptpd2 -c ptp-server-sample.conf” where ptp-server-sample.conf is the configuration file needed.

How do I specify the configuration file to be included in the snap?

Here is a yaml i have used to build the binary from the source:

name: ptpd2
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
description: |
  This is my-snap's description. You have a paragraph or two to tell the
  most important story about your snap. Keep it under 100 words though,
  we live in tweetspace and your description wants to look good in the snap
  store.

grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots

apps:
  ptpd2:
    command: ptpd2 -c ptp-server-sample.conf # this is where I am stuck. The snap is unable to locate .conf
    daemon: simple
    restart-condition: always
    plugs: [network]
    
parts:
  gnu-ptp:
    source: https://github.com/ptpd/ptpd.git
    plugin: autotools

You haven’t indicated in your yaml file that the config should also be added to the snap. To see how the filesystem layout of your snap would look you could do snapcraft stage and then inspect the contents of your ./stage folder

An additional question: How do you plan the user to alter or provide their own configuration?

EDIT: As @kyleN commented below, the above should have been snapcraft prime and the ./prime folder.

I have updated the yaml to include the config in the yaml. I now see the config in the stage folder, however, the snap still indicates it cannot locate the file.

To answer your question…I do not know how I can provide the user with the ability to alter their configuration. Is there a way to do this?

name: ptpd2
version: ‘0.1’ # just for humans, typically ‘1.2+git’ or ‘1.3.2’
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
description: |
This is my-snap’s description. You have a paragraph or two to tell the
most important story about your snap. Keep it under 100 words though,
we live in tweetspace and your description wants to look good in the snap
store.

grade: devel # must be ‘stable’ to release into candidate/stable channels
confinement: devmode # use ‘strict’ once you have the right plugs and slots

apps:
ptpd2:
command: ptpd2 -c ptp-server-sample.conf
daemon: simple
restart-condition: always
plugs: [network]

parts:
gnu-ptp:
source: https://github.com/ptpd/ptpd.git
plugin: autotools

ptp-conf:
source: config/
plugin: dump
filesets:
config:
- ptp-server-sample.conf
stage:
- $config
prime:
- $config

I am currently in the train on my way home, so I can’t go into the different ways you can provide user configuration for your snap.

But I am wondering about one thing. If you only did snapcraft stage then the snap was not rebuilt. To rebuild the snap you would do snapcraft and then install the snap again. Check the timestamp on the snap to double check that it was rebuilt before you install it.

I tried running both snapcraft and snapcraft stage separately. The command snapcraft stage shows the config file in the root of the stage/ folder when using the above yaml (the binary of the app is located in stage/sbin/). Whereas snapcraft shows the same stage folder but the app cannot start after installing the app(err: cannot locate config file).

Is the command in the yaml incorrect? - command: ptpd2 -c ptp-server-sample.conf
What is the correct path for the config file?

Thanks for your help, enjoy the ride!

Hi again.

Where in the does the config folder reside in the prime directory? Directly under it, or in some subdirectory? You might also need to provide the full path to the configuration file just to be sure. The $SNAP variable points to the root of the folder the SNAP is installed in.

Regarding allowing the user of your snap to configure certain settings, take a look at this link on @kyrofa’s blog: https://kyrofa.com/posts/snap-configuration-the-configure-hook

Even better, run ‘snapcraft prime’ and inspect the prime/ folder. prime/ is the folder from which the actual snap is built and may be different from stage/.

1 Like

The configuration file is in the root of prime/.
It looks like if I add $SNAP/ptp-server-sample.conf as the path in the command the snap is able to locate the configuration. However, the snap still fails to run.

Do I need to add root rights to the snap? I can only run ptpd2 as root when running manually. How do I do this with a snap?

Here is the error:

Jul 11 13:03:51 ubuntu systemd[1]: Started Service for snap application ptpd2.ptpd2.
Jul 11 13:03:52 ubuntu ptpd2[2205]: PTPd version 2.3.2 starting
Jul 11 13:03:52 ubuntu ptpd2[2205]: Starting ptpd2 daemon with parameters: ptpd2 -c /snap/ptpd2/x3/ptp-server-sample.conf
Jul 11 13:03:52 ubuntu ptpd2[2205]: Loading configuration file: /snap/ptpd2/x3/ptp-server-sample.conf
Jul 11 13:03:52 ubuntu ptpd2[2205]: Checking configuration
Jul 11 13:03:52 ubuntu ptpd2[2205]: Configuration OK
Jul 11 13:03:52 ubuntu ptpd2[2205]: Successfully acquired lock on /var/run/ptpd2.lock
Jul 11 13:03:52 ubuntu systemd[1]: snap.ptpd2.ptpd2.service: Service hold-off time over, scheduling restart.
Jul 11 13:03:53 ubuntu systemd[1]: Stopped Service for snap application ptpd2.ptpd2.
Jul 11 13:03:53 ubuntu systemd[1]: snap.ptpd2.ptpd2.service: Start request repeated too quickly.

My guess is that the ptpd2 is a traditional unix daemon. So maybe try

daemon: forking

in your snapcraft.yaml?

That appears to have worked.

Does restart-condition still apply for daemon: forking? That is if I declare restart-condition: always with daemon: forking, will the snap automatically restart itself if the service crashes?

I should think so. I haven’t read anything in the documentation to make me think otherwise.