-
Notifications
You must be signed in to change notification settings - Fork 562
/
component.go
286 lines (244 loc) · 9.17 KB
/
component.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright (C) 2023 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package snap
import (
"fmt"
"path/filepath"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/snap/naming"
"gopkg.in/yaml.v2"
)
// ComponentInfo contains information about a snap component.
type ComponentInfo struct {
Component naming.ComponentRef `yaml:"component"`
Type ComponentType `yaml:"type"`
Version string `yaml:"version"`
Summary string `yaml:"summary"`
Description string `yaml:"description"`
// Hooks contains information about implicit and explicit hooks that this
// component has. This information is derived from a combination of the
// component itself and the snap.Info that represents the snap this
// component is associated with. This field may be empty if the
// ComponentInfo was not created with the help of a snap.Info.
Hooks map[string]*HookInfo `yaml:"-"`
}
// NewComponentInfo creates a new ComponentInfo.
func NewComponentInfo(cref naming.ComponentRef, ctype ComponentType, version, summary, description string) *ComponentInfo {
return &ComponentInfo{
Component: cref,
Type: ctype,
Version: version,
Summary: summary,
Description: description,
}
}
// ComponentSideInfo is the equivalent of SideInfo for components, and
// includes relevant information for which the canonical source is a
// snap store.
type ComponentSideInfo struct {
Component naming.ComponentRef `json:"component"`
Revision Revision `json:"revision"`
}
// NewComponentSideInfo creates a new ComponentSideInfo.
func NewComponentSideInfo(cref naming.ComponentRef, rev Revision) *ComponentSideInfo {
return &ComponentSideInfo{
Component: cref,
Revision: rev,
}
}
// Equal compares two ComponentSideInfo.
func (csi *ComponentSideInfo) Equal(other *ComponentSideInfo) bool {
return *csi == *other
}
// ComponentBaseDir returns where components are to be found for the
// snap with name instanceName.
func ComponentsBaseDir(instanceName string) string {
return filepath.Join(BaseDir(instanceName), "components")
}
// componentPlaceInfo holds information about where to put a component in the
// system. It implements ContainerPlaceInfo and should be used only via this
// interface.
type componentPlaceInfo struct {
// Name and revision for the component
compName string
compRevision Revision
// snapInstance identifies the snap that uses this component.
snapInstance string
}
var _ ContainerPlaceInfo = (*componentPlaceInfo)(nil)
// MinimalComponentContainerPlaceInfo returns a ContainerPlaceInfo with just
// the location information for a component of the given name and revision that
// is used by a snapInstance.
func MinimalComponentContainerPlaceInfo(compName string, compRev Revision, snapInstance string) ContainerPlaceInfo {
return &componentPlaceInfo{
compName: compName,
compRevision: compRev,
snapInstance: snapInstance,
}
}
// ContainerName returns the component name.
func (c *componentPlaceInfo) ContainerName() string {
return fmt.Sprintf("%s+%s", c.snapInstance, c.compName)
}
// Filename returns the container file name.
func (c *componentPlaceInfo) Filename() string {
return filepath.Base(c.MountFile())
}
// MountDir returns the directory where a component gets mounted, which
// will be of the form:
// /snaps/<snap_instance>/components/mnt/<component_name>/<component_revision>
func (c *componentPlaceInfo) MountDir() string {
return ComponentMountDir(c.compName, c.compRevision, c.snapInstance)
}
// MountFile returns the path of the file to be mounted for a component,
// which will be of the form /var/lib/snaps/snaps/<snap>+<comp>_<rev>.comp
func (c *componentPlaceInfo) MountFile() string {
return filepath.Join(dirs.SnapBlobDir,
fmt.Sprintf("%s_%s.comp", c.ContainerName(), c.compRevision))
}
// MountDescription returns the mount unit Description field.
func (c *componentPlaceInfo) MountDescription() string {
return fmt.Sprintf("Mount unit for %s, revision %s", c.ContainerName(), c.compRevision)
}
// ReadComponentInfoFromContainer reads ComponentInfo from a snap component
// container. If snapInfo is not nil, it is used to complete the ComponentInfo
// information about the component's implicit and explicit hooks, and their
// associated plugs. If snapInfo is not nil, consistency checks are performed
// to ensure that the component is a component of the provided snap.
func ReadComponentInfoFromContainer(compf Container, snapInfo *Info) (*ComponentInfo, error) {
yamlData, err := compf.ReadFile("meta/component.yaml")
if err != nil {
return nil, err
}
componentInfo, err := InfoFromComponentYaml(yamlData)
if err != nil {
return nil, err
}
// if snapInfo is nil, then we can't complete the component info with
// implicit and explicit hooks, so we return the component info as is.
//
// we could technically create the hooks, but would be unable to bind plugs
// to them, so it is probably best to just leave them out.
if snapInfo == nil {
return componentInfo, nil
}
if snapInfo.SnapName() != componentInfo.Component.SnapName {
return nil, fmt.Errorf(
"component %q is not a component for snap %q", componentInfo.Component, snapInfo.SnapName())
}
componentName := componentInfo.Component.ComponentName
component, ok := snapInfo.Components[componentName]
if !ok {
return nil, fmt.Errorf("%q is not a component for snap %q", componentName, snapInfo.SnapName())
}
if component.Type != componentInfo.Type {
return nil, fmt.Errorf("inconsistent component type (%q in snap, %q in component)", component.Type, componentInfo.Type)
}
// attach the explicit hooks, these are defined in the snap.yaml. plugs are
// already bound to the hooks.
componentInfo.Hooks = component.ExplicitHooks
// attach the implicit hooks, these are not defined in the snap.yaml.
// unscoped plugs are bound to the implicit hooks here.
addAndBindImplicitComponentHooksFromContainer(compf, componentInfo, component, snapInfo)
return componentInfo, nil
}
func addAndBindImplicitComponentHooksFromContainer(compf Container, componentInfo *ComponentInfo, component *Component, info *Info) {
hooks, err := compf.ListDir("meta/hooks")
if err != nil {
return
}
for _, hook := range hooks {
addAndBindImplicitComponentHook(componentInfo, info, component, hook)
}
}
func addAndBindImplicitComponentHook(componentInfo *ComponentInfo, snapInfo *Info, component *Component, hook string) {
// don't overwrite a hook that has already been loaded from the snap.yaml
if _, ok := componentInfo.Hooks[hook]; ok {
return
}
// TODO: ignore unsupported implicit component hooks, or return an error?
if !IsComponentHookSupported(hook) {
logger.Noticef("ignoring unsupported implicit hook %q for component %q", componentInfo.Component, hook)
return
}
// implicit hooks get all unscoped plugs
unscopedPlugs := make(map[string]*PlugInfo)
for name, plug := range snapInfo.Plugs {
if plug.Unscoped {
unscopedPlugs[name] = plug
}
}
// TODO: if hooks ever get slots, then unscoped slots will need to be
// bound here
componentInfo.Hooks[hook] = &HookInfo{
Snap: snapInfo,
Component: component,
Name: hook,
Plugs: unscopedPlugs,
Explicit: false,
}
}
// InfoFromComponentYaml parses a ComponentInfo from the raw yaml data.
func InfoFromComponentYaml(compYaml []byte) (*ComponentInfo, error) {
var ci ComponentInfo
if err := yaml.UnmarshalStrict(compYaml, &ci); err != nil {
return nil, fmt.Errorf("cannot parse component.yaml: %s", err)
}
if err := ci.validate(); err != nil {
return nil, err
}
return &ci, nil
}
// FullName returns the full name of the component, which is composed
// by snap name and component name.
func (ci *ComponentInfo) FullName() string {
return ci.Component.String()
}
// HooksForPlug returns the component hooks that are associated with the given
// plug.
func (ci *ComponentInfo) HooksForPlug(plug *PlugInfo) []*HookInfo {
return hooksForPlug(plug, ci.Hooks)
}
// Validate performs some basic validations on component.yaml values.
func (ci *ComponentInfo) validate() error {
if ci.Component.SnapName == "" {
return fmt.Errorf("snap name for component cannot be empty")
}
if ci.Component.ComponentName == "" {
return fmt.Errorf("component name cannot be empty")
}
if err := ci.Component.Validate(); err != nil {
return err
}
if ci.Type == "" {
return fmt.Errorf("component type cannot be empty")
}
// version is optional
if ci.Version != "" {
if err := ValidateVersion(ci.Version); err != nil {
return err
}
}
if err := ValidateSummary(ci.Summary); err != nil {
return err
}
if err := ValidateDescription(ci.Description); err != nil {
return err
}
return nil
}