we’ve developed a java application which has some configurations that has to be changed according to the
user needs. we’re using the properties file for the configuration and it is placed in the app’s root directory.
After the snap package, I cannot edit this file since it is read only. I want the properties file to be editable for my users.
How to make the configuration file editable inside snap?
Usually the thing to do is to have an install hook which will copy the configuration files from $SNAP and into $SNAP_DATA or $SNAP_USER_DATA, both of which are writable and then point your application to read their config files from there. Then a user can edit those configuration files and your application will pick up the changes.
You don’t have to declare the network plug for your install hook unless you need to access the network. If all you need the install hook to do is copy files, this is unnecessary and you can omit that snippet from your snapcraft.yaml.
Hooks are implemented with files of the same name living in snap/hooks/..., or being put there by a part in your snapcraft.yaml. So the simplest thing for you may be to just create the file snap/hooks/install and make that executable, and put a basic shell script like this in that file:
#!/bin/sh -e
if [ ! -f $SNAP_USER_DATA/config.txt ]; then
cp $SNAP/config.txt $SNAP_USER_DATA/config.txt
fi
You should just be able to create the hooks dir in the snap folder in your project’s root directory, i.e. if your project is located in your home folder under project1, then you would have snapcraft.yaml either at:
/home/user/project1/snapcraft.yaml
or
/home/user/project1/snap/snapcraft.yaml
and run snapcraft from
/home/user/project1
In both of these cases, put the install hook here (with the filename verbatim install):
I’ve created a directory inside snap called hooks and placed install script there and made it executable. After the package installation it does not copy config files to $SNAP_USER_DATA automatically.
But If I go the snap environment using this command snap run --shell myapp and run the script manually it copies my config files to $SNAP_USER_DATA.
I don’t know what I’m missing. Please correct me if anything wrong.
the install hook only runs during installation of the snap.
this means it runs as root and not as some user.
as such, the $SNAP_USER_DATA variable only points to /root/snap/<packagename>/current/ at that time.
if you want to copy to $SNAP_USER_DATA during first start of the app when a user runs it (so the variable actually points to that users dir), you rather want to run your app from a wrapper script with a line like:
@ogra The solution you proposed works fine. But should I have to hardcode the config file path ($SNAP_USER_DATA/config.properties) inside my code or is there any other method to mention the path of configuration file?
I’d make the code read the environment variable, If it is set to something non-zero it is clear you want to use the config from there … if it is unset, use the default non-snap config location
I’ve also having another issue. My Java application is built with the help of third party SDK. So I have to use their license in order to run the application.
The license should be running as a separate instance in the installed machine.
After the packaging of application along with its dependencies and licenses using snap, Our Java application is not detecting the running license instance.
The License file will be run through the shell script which is provided by the SDK, will be running locally on port 5000.
Should we have follow any other steps while packaging the application particularly for this type of licensing?
I’ve attached my snapcraft.yaml file below
name: facecheck # you probably want to 'snapcraft register <name>'
base: core18 # the base snap is the execution environment for this snap
version: '1.0' # just for humans, typically '1.2+git' or '1.3.2'
summary: Face recognition # 79 char long summary
description: |
This application is used to recognise and detect the persons face
with the enrolled data from database.
grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots
apps:
facecheck:
# command: usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -jar $SNAP/Bin/Java/simple-surveillance-application.jar
command: wrapper
# environment:
# JAVA_HOME: $SNAP/usr/lib/jvm/java-1.8.0-openjdk-amd64
# PATH: $JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
# LD_LIBRARY_PATH: $SNAP/Lib/Linux_x86_64
plugs: [desktop, home, camera, x11, desktop-legacy, opengl, alsa, pulseaudio, network, network-bind]
autostart: Facecheck-surveillance.desktop
parts:
facecheck:
# See 'snapcraft plugins'
source: .
plugin: dump
build-packages:
- nvidia-384-dev
- libgtk-3-dev
- gstreamer1.0-vaapi
- vainfo
- openjdk-8-jre
- openjdk-8-demo
- libgdk-pixbuf2.0-dev
- alsa-utils
- libasound2-data
- libasound2-plugins
- libasound2
- libopus-dev
- libortp-dev
- gcc
- g++
- make
- libgudev-1.0-0
- libgudev-1.0-dev
- libgstreamer1.0-0
- gstreamer1.0-plugins-base
- gstreamer1.0-plugins-good
- gstreamer1.0-plugins-bad
- gstreamer1.0-plugins-ugly
- gstreamer1.0-libav
- gstreamer1.0-doc
- gstreamer1.0-tools
- gstreamer1.0-x
- gstreamer1.0-alsa
- gstreamer1.0-gl
- gstreamer1.0-gtk3
- gstreamer1.0-qt5
- gstreamer1.0-pulseaudio
- libfontconfig1-dev
- libfreetype6-dev
- libpng-dev
- libcairo2-dev
- libjpeg-dev
- libgif-dev
- libgstreamer-plugins-base1.0-dev
- python-gst-1.0
- python3-gst-1.0
- postgresql
- postgresql-contrib
- odbc-postgresql
- unixodbc
- unixodbc-dev
- build-essential
- manpages-dev
stage-packages:
- libgpm2
- libslang2
- libnvidia-compute-390
- openjdk-8-jre
- openjdk-8-demo
- nvidia-384-dev
- libgtk-3-dev
- gstreamer1.0-vaapi
- vainfo
- libgdk-pixbuf2.0-dev
- alsa-utils
- libasound2-data
- libasound2
- libasound2-plugins
- gcc
- g++
- make
- libgudev-1.0-0
- libgudev-1.0-dev
- libgstreamer1.0-0
- gstreamer1.0-plugins-base
- gstreamer1.0-plugins-good
- gstreamer1.0-plugins-bad
- gstreamer1.0-plugins-ugly
- gstreamer1.0-libav
- gstreamer1.0-doc
- gstreamer1.0-tools
- gstreamer1.0-x
- gstreamer1.0-alsa
- gstreamer1.0-gl
- gstreamer1.0-gtk3
- gstreamer1.0-qt5
- gstreamer1.0-pulseaudio
- libfontconfig1-dev
- libfreetype6-dev
- libpng-dev
- libcairo2-dev
- libjpeg-dev
- libgif-dev
- libgstreamer-plugins-base1.0-dev
- python-gst-1.0
- python3-gst-1.0
- postgresql
- postgresql-contrib
- odbc-postgresql
- unixodbc
- unixodbc-dev
- build-essential
- manpages-dev
I’m also getting some error from alsa, even though I’ve mentioned it in a plugs, and I can see the file /usr/share/alsa/alsa.conf in my snap application. But it says it can’t access