无法获得使用 Snapcraft 的基本内容界面示例

mof*_*ofo 2 snap

我一直在尝试content通过一个简单的例子来使界面正常工作。

消费者:

name: consumer # you probably want to 'snapcraft register <name>'
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
description: |

grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots

apps:
  consumer:
    command: ls -lR /snap/consumer/current/

parts:
  my-part:
    # See 'snapcraft plugins'
    plugin: nil

plugs:
  shared-files:
    content: shared-files
    interface: content
    target: shared
    default-provider: provider:shared-files
Run Code Online (Sandbox Code Playgroud)

提供者:

name: provider # you probably want to 'snapcraft register <name>'
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
description: |

grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots

parts:
  my-part:
    plugin: dump
    source: .

slots:
  shared-files:
    content: shared-files
    interface: content
    read:
    - /src
Run Code Online (Sandbox Code Playgroud)

/src里面放置了一些随机文件。我可以在树中看到它们,/snap/provider/current但在树上却找不到它们/snap/consumer/current——我认为它们应该出现在树上。 snap interfaces显示插头和插槽已连接。

我究竟做错了什么?

kyr*_*ofa 5

你太接近了!

内容共享接口将插槽绑定安装到插件的目标。为此,target参数必须指向现有目录(绑定挂载需要挂载在某个目录之上,就像任何其他挂载一样)。因此consumer,在您的 中,不要使用插件,而是nil使用dump插件并将空shared目录转储到快照的根目录中。然后你会看到provider$SNAP/src目录出现在consumer$SNAP/shared目录中。

请注意,您不会从系统角度看到这一点。如果您ls /snap/consumer/current/shared/来自系统,它将是您打包到快照中的空目录。但是,当应用程序启动时,它运行的上下文包含该绑定安装。让我证明一下:

$ snap run --shell consumer
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

$ ls $SNAP/shared/
file1  file2
Run Code Online (Sandbox Code Playgroud)

snap run --shell在将用于相关应用程序的确切环境中运行 shell。因此,运行时snap run --shell consumer您需要一个consumer具有与应用程序相同的限制和环境的外壳。这就是为什么我可以$SNAP在那里使用。请注意,file1和是my目录file2中包含的文件。providersrc

最后一点:假设您希望consumer应用程序列出共享目录的内容,您可以将其更改为如下所示(无需使用 /snap/consumer/current):

apps:
  consumer:
    command: ls -lR $SNAP/shared/
Run Code Online (Sandbox Code Playgroud)