👏
Snapの接続
初めに
- 本記事では、2つのSnapを接続する方法を紹介します。
Snapの接続
- Snapを接続して使用する際、下記のようなprovidesとconsumesのそれぞれ2つの役割があります。
・provides:接続したSnapにファイル等を共有する側
・consumes:接続したSnapのファイル等を使用する側
- provides側ではslot、consumes側ではplugという機能を使用して2つのSnapを接続します。
Snapの作成
provides側
- provides側のSnapにはconsumes側に提供するシェルスクリプトを用意します。
$ tree .
.
├── slot.sh
└── snap
└── snapcraft.yaml
1 directory, 2 files
slot.shの中身は下記になります。
#!/bin/bash
echo "slot.sh is snapped in slot-snap."
slot.shに権限を付与します。
$ sudo chmod +x slot.sh
- provides側のSnapのsnapcraft.yamlは下記のように記述します。
name: slot-snap # you probably want to 'snapcraft register <name>'
base: core22 # the base snap is the execution environment for this snap
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: |
This is my-snap's description. You have a paragraph or two to tell the
most important story about your snap. Keep it under 100 words though,
we live in tweetspace and your description wants to look good in the snap
store.
grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots
parts:
slot:
# See 'snapcraft plugins'
plugin: dump
source: .
slots:
test-interface:
content: test-interface:
interface: content
read:
- .
snapcraft.yamlの内容
- dumpはsourceで指定したファイルに対して何もせずSnap化します。
sourceはsnapディレクトリと同じ階層配下のディレクトリをSnap化します。
plugin: dump
source: .
- slot名を指定します。
slots:
test-interface:
- 上記と同様にslot名を指定します。
content: test-interface:
- interfaceを使用する際の決まり文句です。
interface: content
- consumes側に読み出し権限を与え、読み出し権限を与えるディレクトリのパスを指定します。
read:
- .
consumes側
- consumes側では、provides側のシェルを使用するシェルスクリプトを用意します。
$ tree
.
├── plug.sh
└── snap
└── snapcraft.yaml
1 directory, 2 files
- plug.shの中身は下記になります。
#!/bin/bash
echo "ls $SNAP"
ls $SNAP
echo "sh $SNAP/slot-bin/slot.sh"
sh $SNAP/slot-bin/slot.sh
- plug.shに権限を付与します。
$ sudo chmod +x plug.sh
- comsumes側のsnapcraft.yamlは下記のように記述します。
name: plug-snap # you probably want to 'snapcraft register <name>'
base: core22 # the base snap is the execution environment for this snap
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: |
This is my-snap's description. You have a paragraph or two to tell the
most important story about your snap. Keep it under 100 words though,
we live in tweetspace and your description wants to look good in the snap
store.
grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots
apps:
plug:
command: plug.sh
parts:
plug:
# See 'snapcraft plugins'
plugin: dump
source: .
command: plug.sh
plugs:
test-interface:
interface: content
contents: executables
target: $snap/slot-bin
snapcraft.yamlの内容
- plug名を指定します。
plugs:
test-interface:
- slotと同様にinterfaceを使用する際に決まり文句です。
interface: content
- Snap化したファイルの呼び出しを許可し、
参照するファイルを格納するディレクトリを指定します。
contents: executables
target: $snap/slot-bin
- Snapのビルドとインストールを行います。
$ snapcraft --destructive-mode
$ sudo snap install slot-snap_0.1_amd64.snap --dangerous --devmode
$ sudo snap install plug-snap_0.1_amd64.snap --dangerous --devmode
Snapの接続
- インストールした2つのSnapを接続します。
$ sudo snap connect plug-snap:test-interface slot-snap:test-interface
- 接続されたかどうかは下記のコマンドで確認することが出来ます。
$ snap connections plug-snap
Interface Plug Slot Notes
content[test-interface] plug-snap:test-interface slot-snap:test-interface manual
$ snap connections slot-snap
Interface Plug Slot Notes
content[test-interface] plug-snap:test-interface slot-snap:test-interface manual
Snapの実行
- consumes側のSnapを実行します。
plug-snap.plug
ls /snap/slot-snap/current
meta slot.sh snap
sh /snap/slot-snap/current/slot.sh
slot.sh is snapped in slot-snap.
- Snapが接続されていないとslot-binがせず、consumes側でslot.shが参照出来ない為、エラーになります。
$ sudo snap disconnect plug-snap:test-interface slot-snap:test-interface
$ snap connections plug-snap
Interface Plug Slot Notes
content plug-snap:test-interface - -
$ snap connections slot-snap
Interface Plug Slot Notes
content - slot-snap:test-interface -
$ /snap/bin/plug-snap.plug
ls /snap/plug-snap/x10
meta plug.sh snap
sh /snap/plug-snap/x10/slot-bin/slot.sh
sh: 0: cannot open /snap/plug-snap/x10/slot-bin/slot.sh: No such file
Discussion