加入收藏 | 设为首页 | 会员中心 | 我要投稿 阜阳站长网 (https://www.0558zz.com/)- 科技、建站、内容创作、云计算、网络安全!
当前位置: 首页 > 综合聚焦 > Ubuntu > 正文

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

发布时间:2023-12-14 11:16:35 所属栏目:Ubuntu 来源:DaWei
导读: 当我们完成一个Ubuntu Core应用的时候,可能需要我们的Ubuntu Core应用根据我们的设置需要来配置我们的运行所需要的一些参数,比如,我想确消我们Ubuntu Core系统的自动更新功能,或对我们的

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


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

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

我们的snapcraft.yaml文件如下:

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/configure


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

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/credentials在这个脚本中,我们读取username及password,并把它们存于到$SNAP_DATA/credentials的文件中.

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

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.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 -
我们可以运行我们的hello.env应用: 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 SNAP=/snap/hello/x1

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

我们再打入如下的命令:

liu-xiao-guo@localhost:~$ sudo snap set hello username=foo1 password=bar1 liu-xiao-guo@localhost:~$
如果没有任何的错误,我们可以看出来我们的设置已经成功运行.在上面的命令执行后,我们的项目中的configure脚本会自动被调用,并把我们所需要的设置置于我们所需要的文件中.我们可以通过如下的命令来检查我们给应用所做的设置: 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"

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

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

(编辑:阜阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章