如何为我们的Ubuntu Core应用进行设置

来源:互联网 发布:石头网络刷钻软件 编辑:程序博客网 时间:2024/06/06 13:59

当我们完成一个Ubuntu Core应用的时候,可能需要我们的Ubuntu Core应用根据我们的设置需要来配置我们的运行所需要的一些参数,比如,我想确消我们Ubuntu Core系统的自动更新功能,或对我们的Ubuntu Core应用进行分别的设置从而改变该应用的运行.在今天的教程中,我们来展示如何在Ubuntu Core应用中进行配置.


为了展示,我们已经开发一个例程.在如下的地址可以找到我的例程:

https://github.com/liu-xiao-guo/helloworld-configure

我们的snapcraft.yaml文件如下:

name: helloversion: "1.0"summary: The 'hello-world' of snapsdescription: |    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 textconfinement: stricttype: app  #it can be gadget or frameworkapps: 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/createfiletohomeparts: hello:  plugin: copy  files:    ./bin: bin config:  plugin: dump  source: .  organize:    configure: meta/hooks/configure    


在未来,也许我们的snapcraft工具做得更好,以更方便地支持configure.这样我们可能不必要使用dump来安装这个文件.在这里最关键的是:我们需要把一个叫做configure的可以执行的文件打包于meta/hooks/目录下.这样就可以了.我们的configure的内容如下:

configure

#!/bin/shif ! username=$(snapctl get username); then    echo "Username is required"    exit 1fiif ! password=$(snapctl get password); then    echo "Password is required"    exit 1fi# Handle username and password, perhaps write to a credential file of some sort.echo "user=$username" > $SNAP_DATA/credentialsecho "password=$password" >> $SNAP_DATA/credentialschmod 600 $SNAP_DATA/credentials
在这个脚本中,我们读取username及password,并把它们存于到$SNAP_DATA/credentials的文件中.

等我们打包成功后,再安装好我们的hello snap应用.

liu-xiao-guo@localhost:~$ snap listName         Version       Rev  Developer  Notesbluez        5.37-1        7    canonical  -classic      16.04         14   canonical  devmodehello        1.0           x1              -lights-app   0.1           x1              devmodelivevideo    0.1           x1              devmodepi2-kernel   4.4.0-1030-3  22   canonical  -pi3          16.04-0.5     6    canonical  -piglow-app   1.0           x2              devmodesensortag    1.0           x3              devmodesnapweb      0.21.2        25   canonical  -ubuntu-core  16.04.1       760  canonical  -

我们可以运行我们的hello.env应用:

liu-xiao-guo@localhost:~$ sudo hello.env | grep SNAPSNAP_USER_COMMON=/root/snap/hello/commonSNAP_REEXEC=SNAP_LIBRARY_PATH=/var/lib/snapd/lib/gl:SNAP_COMMON=/var/snap/hello/commonSNAP_USER_DATA=/root/snap/hello/x1SNAP_DATA=/var/snap/hello/x1SNAP_REVISION=x1SNAP_NAME=helloSNAP_ARCH=armhfSNAP_VERSION=1.0SNAP=/snap/hello/x1

从上面我们可以看出来我们的SNAP_DATA目录位于/var/snap/hello/current目录中.

我们再打入如下的命令:

liu-xiao-guo@localhost:~$ sudo snap set hello username=foo1 password=bar1

如果没有任何的错误,我们可以看出来我们的设置已经成功运行.在上面的命令执行后,我们的项目中的configure脚本会自动被调用,并把我们所需要的设置置于我们所需要的文件中.我们可以通过如下的命令来检查我们给应用所做的设置:

liu-xiao-guo@localhost:~$ sudo snap set hello username=foo1 password=bar1liu-xiao-guo@localhost:~$ cd /var/snap/hello/currentliu-xiao-guo@localhost:/var/snap/hello/current$ cat credentials cat: credentials: Permission deniedliu-xiao-guo@localhost:/var/snap/hello/current$ sudo cat credentials user="foo1"password="bar1"

通过上面的测试,我们可以看出来,我们已经把我们想要的设置放入到我们所需要的目录中.在我们运行我们的应用时,我们可以根据这些设置来做不同的事情.

我们也可以通过如下的方法来得到我们的设置:

liu-xiao-guo@localhost:~$ snap get hello usernamefoo1

由于一些原因,目前我在ubuntu的设备上测试成功,在16.04的桌面上snapd的支持还有一点问题.







0 0
原创粉丝点击