如何利用content接口在Ubuntu Core应用之间互相分享数据

前端之家收集整理的这篇文章主要介绍了如何利用content接口在Ubuntu Core应用之间互相分享数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我们在先前的文章利用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

  1. name: hello-producer
  2. version: "1.0"
  3. summary: The 'hell-producer' snap
  4. description: |
  5. This is a simple snap example showing how to share content
  6. grade: stable
  7. confinement: strict
  8. type: app #it can be gadget or framework
  9.  
  10. apps:
  11. echo:
  12. command: bin/echo_producer
  13.  
  14. slots:
  15. content:
  16. content: executables
  17. read:
  18. - $SNAP/bin
  19.  
  20. parts:
  21. hello:
  22. plugin: dump
  23. source: .


在我们的这个hello-producer的应用中,我们定义了一个slot.我们把这个应用目录中的bin下的所有的文件分享为只读.它定义了一个叫做content的接口.为了能够说明问题,我们把confinement设置为strict,这样我们严格地让我们的应用在受限的方式下运行.

consumer snapcraft.yaml

  1. name: hello-consumer
  2. version: "1.0"
  3. summary: The 'hello-consumer' snap
  4. description: |
  5. This is a simple snap example showing how to share content
  6. grade: stable
  7. confinement: strict
  8. type: app #it can be gadget or framework
  9.  
  10. apps:
  11. echo:
  12. command: bin/echo_consumer
  13.  
  14. plugs:
  15. content:
  16. content: executables
  17. target: $SNAP/extra-bin
  18.  
  19. parts:
  20. hello:
  21. plugin: dump
  22. source: .

在这个consumer的snapcraft.yaml中,我们定义了一个plug.这个接口的名称为content.如果我们连接这个plug到上面我们定义的slot时,它会自动帮我们把producer下的bin文件目录mount于这个应用的extra-bin目录中,从而我们可以在consumer应用中使用在这个文件目录下的所有文件.当然,这些文件只是可读的.在我们打包时,我们必须在当前的目录下创建一个空的被叫做 extra-bin的目录.这将被提供为一个mount点之用.

在我们的consumer snap的bin目录下,我们有这样一个脚本文件

echo_consumer

  1. #!/bin/bash
  2.  
  3. echo "starting to exec a binary in the producer from consumer"
  4. exec "$SNAP/extra-bin/echo_producer" "$@"

显然这里的echo_producer文件来自于我们的producer snap.它调用在extra-bin目录中的echo_producer执行文件

echo_producer

  1. #!/bin/bash
  2.  
  3. echo "Hello World in producer!"
  4. read -p "Press any key to continue... "

当我们打包好我们的应用后,并分别安装consumer及producer的两个应用:

  1. liuxg@liuxg:~/snappy/desktop/content-bin/hello-producer$ snap list
  2. Name Version Rev Developer Notes
  3. bluez 5.37-2 11 canonical -
  4. bmonitor 0.1 x1 devmode
  5. hello-consumer 1.0 x1 -
  6. hello-producer 1.0 x1 -
  7. hello-world 6.3 27 canonical -
  8. hello-xiaoguo 1.0 x1 devmode
  9. hellopy 0.1 x1 devmode

注意这里,我们没有使用devmode来安装我们的应用.

  1. liuxg@liuxg:~/snappy/desktop/content-bin/hello-producer$ snap interfaces
  2. Slot Plug
  3. bluez:service -
  4. hello-producer:content -
  5. ubuntu-app-platform:platform -

从上面我们可以看出来,我们的hello-producer中的content interface没有人的应用来连接它.我们可以通过如下的方式来进行手动连接:

  1. liuxg@liuxg:~$ sudo snap connect hello-consumer:content hello-producer:content
  2. liuxg@liuxg:~$ snap interfaces
  3. Slot Plug
  4. bluez:service -
  5. hello-producer:content hello-consumer
  6. ubuntu-app-platform:platform -

现在我们可以看到content interface已经被成功连接.我们接下来执行如下的命令:

  1. liuxg@liuxg:~$ hello-consumer.echo
  2. starting to exec a binary in the producer from consumer
  3. Hello World in producer!
  4. Press any key to continue...

很显然,它执行了在producer应用中的脚本.

整个项目的源码在: https://github.com/liu-xiao-guo/content-bin


分享可写的数据


到目前为止,我们建议的方法分享整个数据目录:SNAP_DATA 或 SNAP_COMMON.不过由于SNAP_DATA是一个和版本相关的目录,我个人建议不使用这个.否则我们需要得到producer的具体的版本信息才可以做到.在今天的练习中,我们使用SNAP_COMMON目录来做这个练习.

由于我们在上面已经做个一个练习.为了能够完全除去上一个练习所带来的安全的策略的设定,我们使用如下的命令来删除它:

  1. $ sudo /usr/lib/snapd/snap-discard-ns hello-consumer

producer snapcraft.yaml

  1. name: hello-producer
  2. version: "1.0"
  3. summary: The 'hell-producer' snap
  4. description: |
  5. This is a simple snap example showing how to share content
  6. grade: stable
  7. confinement: strict
  8. type: app #it can be gadget or framework
  9.  
  10. apps:
  11. echo:
  12. command: bin/echo_producer
  13.  
  14. slots:
  15. content:
  16. content: writable-data
  17. write:
  18. - $SNAP_COMMON
  19.  
  20. parts:
  21. hello:
  22. plugin: dump
  23. source: .

在这里,从新定义了我们的content interface.在这里我们使用SNAP_COMMON目录来作为可以写入的空间.

consumer snapcraft.yaml

  1. name: hello-consumer
  2. version: "1.0"
  3. summary: The 'hello-consumer' snap
  4. description: |
  5. This is a simple snap example showing how to share content
  6. grade: stable
  7. confinement: strict
  8. type: app #it can be gadget or framework
  9.  
  10. apps:
  11. echo:
  12. command: bin/echo_consumer
  13.  
  14. plugs:
  15. content:
  16. content: writable-data
  17. target: $SNAP_COMMON
  18.  
  19. parts:
  20. hello:
  21. plugin: dump
  22. source: .

打包完我们的snap,并使用如下的命令来进行连接:

  1. $ sudo snap connect hello-consumer:content hello-producer:content

运行我们的应用:

  1. liuxg@liuxg:~/snappy/desktop/content-data/hello-producer$ hello-consumer.echo
  2. starting to create a file in the producer's common directory
  3. /snap/hello-consumer/x1/bin/echo_consumer: line 4: /var/snap/hello-producer/common/test.txt: Permission denied
  4. If you see this,it is successful to write to the common directory!!!
  5. liuxg@liuxg:~/snappy/desktop/content-data/hello-producer$ sudo hello-consumer.echo
  6. starting to create a file in the producer's common directory
  7. If you see this,it is successful to write to the common directory!!!

我们需要注意的是在写入SNAP_COMMON目录时,需要有root的权限,所以我们必须加上sudo才可以完成.

整个项目的源码在: https://github.com/liu-xiao-guo/content-data

猜你在找的Ubuntu相关文章