When we complete an Ubuntu Core application, we may need our Ubuntu Core application to configure some parameters that we need to run according to our Settings. For example, I want to make sure that our Ubuntu Core system automatically updates. Or make separate Settings to our Ubuntu Core application to change how it works. In today’s tutorial, we’ll show you how to configure in an Ubuntu Core application.
\
For demonstration purposes, we have developed a routine. My routine can be found at the following address:
Github.com/liu-xiao-gu…
Our snapcraft. Yaml file is as follows:
Name: Hello version: "1.0" Summary: The 'hello-world' of snaps description: | This is a simple snap example that includes a few interesting binaries to demonstrate snaps and their confinement. * hello-world.env - dump the env of commands run inside app sandbox * hello-world.evil - show how snappy sandboxes binaries * hello-world.sh - enter interactive shell that runs in app sandbox * hello-world - simply output text confinement: strict type: app #it can be gadget or framework apps: env: command: bin/env evil: command: bin/evil sh: command: bin/sh hello-world: command: bin/echo plugs: [home,unity7,opengl] createfile: command: bin/createfile createfiletohome: command: bin/createfiletohome parts: hello: plugin: copy files: ./bin: bin config: plugin: dump source: . organize: configure: meta/hooks/configureCopy the code
\
In the future, maybe our Snapcraft tool does a better job of supporting Configure more easily. This may not be necessary to install the file using dump. The key here is that we need to package an executable file called configure in the meta/hooks/ directory. That will do. Our configure looks like this:
configure
#! /bin/sh if ! username=$(snapctl get username); then echo "Username is required" exit 1 fi if ! password=$(snapctl get password); then echo "Password is required" exit 1 fi # Handle username and password, perhaps write to a credential file of some sort. echo "user=$username" > $SNAP_DATA/credentials echo "password=$password" >> $SNAP_DATA/credentials chmod 600 $SNAP_DATA/credentialsCopy the code
In this script, we read the username and password and save them in the file $SNAP_DATA/credentials. \
After we pack, we’ll install our Hello Snap app.
liu-xiao-guo@localhost:~$snap List Name Version Rev Developer Notes Bluez 5.37-1 7 Canonical - Classic 16.04 14 Canonical DevMode Hello 1.0 x1-lights-app 0.1 x1 devMode LiveVideo 0.1 x1 devMode PI2-kernel 4.0-1030-3 22 Canonical - PI3 16.04-0.5 6 Canonical - Piglow -app 1.0 x2 devMode SensorTag 1.0 x3 devMode SnapWeb 0.21.2 25 Canonical - Ubuntu - Core 16.04.1 760 Canonical -Copy the code
We can run our hello.env application:
liu-xiao-guo@localhost:~$ sudo hello.env | grep SNAP SNAP_USER_COMMON=/root/snap/hello/common SNAP_REEXEC= SNAP_LIBRARY_PATH=/var/lib/snapd/lib/gl: SNAP_COMMON=/var/snap/hello/common SNAP_USER_DATA=/root/snap/hello/x1 SNAP_DATA=/var/snap/hello/x1 SNAP_REVISION=x1 SNAP_NAME = hello SNAP_ARCH = armhf SNAP_VERSION = 1.0 the SNAP = / SNAP/hello/x1Copy the code
From the above we can see that our SNAP_DATA directory is located in /var/snap/hello/current.
Let’s type the following command again:
liu-xiao-guo@localhost:~$ sudo snap set hello username=foo1 password=bar1
Copy the code
If there are no errors, we can tell that our setup has worked successfully. After executing the above command, the configure script in our project will be called automatically and put the Settings we need in the files we need. We can check the Settings we have made for the application by using the following command:
liu-xiao-guo@localhost:~$ sudo snap set hello username=foo1 password=bar1
liu-xiao-guo@localhost:~$ cd /var/snap/hello/current
liu-xiao-guo@localhost:/var/snap/hello/current$ cat credentials
cat: credentials: Permission denied
liu-xiao-guo@localhost:/var/snap/hello/current$ sudo cat credentials
user="foo1"
password="bar1"
Copy the code
From the above tests, we can see that we have put the Settings we want into the directories we need. When we run our application, we can do different things based on these Settings.
We can also get our Settings as follows:
liu-xiao-guo@localhost:~$ snap get hello username
foo1
Copy the code
For some reason, I have been successfully testing on Ubuntu devices so far, but snapd support on the 16.04 desktop is still a bit of a problem.
\
\
\
\
\
\