我们在先前的文章"利用ubuntu-app-platform提供的platform接口来减小Qt应用大小"已经感受到分享另外一个应用的库的好处.它可以使得我们利用另外一个应用提供的共享库从而使得我们的Qt应用的snap包的大小变得更小.在今天的教程中,我们来给大家介绍如何自己来实现应用之间的内容的分享.我们可以利用content这个接口来实现这个功能.在我们实现这个接口时,我们需要分别做一个consumer及一个producer两个应用.producer需要完成一个slot,从而把自己的content分享给consumers.consumer需要使用plug来进行连接.由于这个content的interface不是自动连接的,所有我们需要手动来连接这个slot和plug.另外,特别值得指出的是,当我们完成一个slot的应用时,在我们上传这个应用到商店,就会自动trigger人工review.这是因为任何带有slot的应用都可能会带来潜在的安全问题.目前建议在snap 2.17之后的版本中进行测试.
分享一个可以执行的文件
producer snapcraft.yaml
name: hello-producer version: "1.0" summary: The 'hell-producer' snap description: | This is a simple snap example showing how to share content grade: stable confinement: strict type: app #it can be gadget or framework apps: echo: command: bin/echo_producer slots: content: content: executables read: - $SNAP/bin parts: hello: plugin: dump source: .
在我们的这个hello-producer的应用中,我们定义了一个slot.我们把这个应用目录中的bin下的所有的文件分享为只读.它定义了一个叫做content的接口.为了能够说明问题,我们把confinement设置为strict,这样我们严格地让我们的应用在受限的方式下运行.
consumer snapcraft.yaml
name: hello-consumer version: "1.0" summary: The 'hello-consumer' snap description: | This is a simple snap example showing how to share content grade: stable confinement: strict type: app #it can be gadget or framework apps: echo: command: bin/echo_consumer plugs: content: content: executables target: $SNAP/extra-bin parts: hello: plugin: dump source: .
echo_consumer
#!/bin/bash echo "starting to exec a binary in the producer from consumer" exec "$SNAP/extra-bin/echo_producer" "$@"
显然这里的echo_producer文件来自于我们的producer snap.它调用在extra-bin目录中的echo_producer执行文件.
echo_producer
#!/bin/bash echo "Hello World in producer!" read -p "Press any key to continue... "
liuxg@liuxg:~/snappy/desktop/content-bin/hello-producer$ snap list Name Version Rev Developer Notes bluez 5.37-2 11 canonical - bmonitor 0.1 x1 devmode hello-consumer 1.0 x1 - hello-producer 1.0 x1 - hello-world 6.3 27 canonical - hello-xiaoguo 1.0 x1 devmode hellopy 0.1 x1 devmode
liuxg@liuxg:~/snappy/desktop/content-bin/hello-producer$ snap interfaces Slot Plug bluez:service - hello-producer:content - ubuntu-app-platform:platform -
liuxg@liuxg:~$ sudo snap connect hello-consumer:content hello-producer:content liuxg@liuxg:~$ snap interfaces Slot Plug bluez:service - hello-producer:content hello-consumer ubuntu-app-platform:platform -
liuxg@liuxg:~$ hello-consumer.echo starting to exec a binary in the producer from consumer Hello World in producer! Press any key to continue...
分享可写的数据
到目前为止,我们建议的方法是分享整个数据目录:SNAP_DATA 或 SNAP_COMMON.不过由于SNAP_DATA是一个和版本相关的目录,我个人建议不使用这个.否则我们需要得到producer的具体的版本信息才可以做到.在今天的练习中,我们使用SNAP_COMMON目录来做这个练习.
由于我们在上面已经做个一个练习.为了能够完全除去上一个练习所带来的安全的策略的设定,我们使用如下的命令来删除它:
$ sudo /usr/lib/snapd/snap-discard-ns hello-consumer
producer snapcraft.yaml
name: hello-producer version: "1.0" summary: The 'hell-producer' snap description: | This is a simple snap example showing how to share content grade: stable confinement: strict type: app #it can be gadget or framework apps: echo: command: bin/echo_producer slots: content: content: writable-data write: - $SNAP_COMMON parts: hello: plugin: dump source: .
在这里,从新定义了我们的content interface.在这里我们使用SNAP_COMMON目录来作为可以写入的空间.
consumer snapcraft.yaml
name: hello-consumer version: "1.0" summary: The 'hello-consumer' snap description: | This is a simple snap example showing how to share content grade: stable confinement: strict type: app #it can be gadget or framework apps: echo: command: bin/echo_consumer plugs: content: content: writable-data target: $SNAP_COMMON parts: hello: plugin: dump source: .
$ sudo snap connect hello-consumer:content hello-producer:content
liuxg@liuxg:~/snappy/desktop/content-data/hello-producer$ hello-consumer.echo starting to create a file in the producer's common directory /snap/hello-consumer/x1/bin/echo_consumer: line 4: /var/snap/hello-producer/common/test.txt: Permission denied If you see this,it is successful to write to the common directory!!! liuxg@liuxg:~/snappy/desktop/content-data/hello-producer$ sudo hello-consumer.echo starting to create a file in the producer's common directory If you see this,it is successful to write to the common directory!!!