snap做包还在学习阶段,官网文档可查看:
The content interface | Snapcraft documentation
该例子由publiser和consumer两部分组成,一个提供一个只读的数据区,一个来进行读取其中的信息,这样就完成了content的交互。
publisher:
整体的结构如下:
snapcraft.yaml:
文件内容如下:
name: publisher
version: '1.0'
summary: data publisher
description: |
This demo is intended to show how to build your first snap
This code is part of the Snapcraft example tour at
https://developer.ubuntu.com/snapcraft-getting-started
grade: stable
confinement: strict
base: core20
parts:
data:
plugin: dump
source: ./env/
slots:
content:
interface: content
content: example-content
read:
- $SNAP/publisher-data
example.txt:
则自己随意写一个内容进行测试即可。
编译:
snapcraft
consumer:
整体结构如下:
snapcraft.yaml:
文件内容如下:
name: consumer
version: '1.0'
summary: data comsumer
description: |
This demo is intended to show how to build your first snap
This code is part of the Snapcraft example tour at
https://developer.ubuntu.com/snapcraft-getting-started
grade: stable
confinement: strict
base: core20
parts:
data:
plugin: dump
source: ./consumer-data/
apps:
print-cli:
command: print.sh
plugs:
content:
interface: content
content: example-content
target: $SNAP_DATA/publisher-data
print.sh:
echo "hello"
这里print.sh和consumer.txt并不是必要的,这里可以方便测试而已。
编译:
snapcraft
测试:
安装并添加interface的连接:
sudo snap install publisher_1.0_amd64.snap --dangerous
sudo snap install consumer_1.0_amd64.snap --dangerous
sudo snap connect consumer:content publisher:content
这里使用的是手动连接,使用 sudo snap connections publisher可以查看连接之后的状态。
验证:consumer可以访问到数据了:
sudo snap run --shell consumer.print-cli
就进入了consumer的文件系统,然后:
ls /var/snap/consumer/x1/publisher-data/
就应该看到example.txt文件已经乖乖在里面等着你去访问了。
当然你也可以将这个命令直接写入print.sh,然后使用命令行直接读取该数据。