如何利用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

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: .

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

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

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... " 

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

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

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

liuxg@liuxg:~/snappy/desktop/content-bin/hello-producer$ snap interfaces
Slot                          Plug
bluez:service                 -
hello-producer:content        -
ubuntu-app-platform:platform  -

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

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  -

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

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

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

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


分享可写的数据


到目前为止,我们建议的方法是分享整个数据目录: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: .

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

$ 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!!!

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

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值