forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.go
382 lines (353 loc) · 10 KB
/
meta.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charm
import (
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/juju/charm/hooks"
"github.com/juju/schema"
"launchpad.net/goyaml"
)
// RelationScope describes the scope of a relation.
type RelationScope string
// Note that schema doesn't support custom string types,
// so when we use these values in a schema.Checker,
// we must store them as strings, not RelationScopes.
const (
ScopeGlobal RelationScope = "global"
ScopeContainer RelationScope = "container"
)
// RelationRole defines the role of a relation.
type RelationRole string
const (
RoleProvider RelationRole = "provider"
RoleRequirer RelationRole = "requirer"
RolePeer RelationRole = "peer"
)
// Relation represents a single relation defined in the charm
// metadata.yaml file.
type Relation struct {
Name string
Role RelationRole
Interface string
Optional bool
Limit int
Scope RelationScope
}
// ImplementedBy returns whether the relation is implemented by the supplied charm.
func (r Relation) ImplementedBy(ch Charm) bool {
if r.IsImplicit() {
return true
}
var m map[string]Relation
switch r.Role {
case RoleProvider:
m = ch.Meta().Provides
case RoleRequirer:
m = ch.Meta().Requires
case RolePeer:
m = ch.Meta().Peers
default:
panic(fmt.Errorf("unknown relation role %q", r.Role))
}
rel, found := m[r.Name]
if !found {
return false
}
if rel.Interface == r.Interface {
switch r.Scope {
case ScopeGlobal:
return rel.Scope != ScopeContainer
case ScopeContainer:
return true
default:
panic(fmt.Errorf("unknown relation scope %q", r.Scope))
}
}
return false
}
// IsImplicit returns whether the relation is supplied by juju itself,
// rather than by a charm.
func (r Relation) IsImplicit() bool {
return (r.Name == "juju-info" &&
r.Interface == "juju-info" &&
r.Role == RoleProvider)
}
// Meta represents all the known content that may be defined
// within a charm's metadata.yaml file.
type Meta struct {
Name string
Summary string
Description string
Subordinate bool
Provides map[string]Relation `bson:",omitempty"`
Requires map[string]Relation `bson:",omitempty"`
Peers map[string]Relation `bson:",omitempty"`
Format int `bson:",omitempty"`
OldRevision int `bson:",omitempty"` // Obsolete
Categories []string `bson:",omitempty"`
Series string `bson:",omitempty"`
}
func generateRelationHooks(relName string, allHooks map[string]bool) {
for _, hookName := range hooks.RelationHooks() {
allHooks[fmt.Sprintf("%s-%s", relName, hookName)] = true
}
}
// Hooks returns a map of all possible valid hooks, taking relations
// into account. It's a map to enable fast lookups, and the value is
// always true.
func (m Meta) Hooks() map[string]bool {
allHooks := make(map[string]bool)
// Unit hooks
for _, hookName := range hooks.UnitHooks() {
allHooks[string(hookName)] = true
}
// Relation hooks
for hookName := range m.Provides {
generateRelationHooks(hookName, allHooks)
}
for hookName := range m.Requires {
generateRelationHooks(hookName, allHooks)
}
for hookName := range m.Peers {
generateRelationHooks(hookName, allHooks)
}
return allHooks
}
func parseCategories(categories interface{}) []string {
if categories == nil {
return nil
}
slice := categories.([]interface{})
result := make([]string, 0, len(slice))
for _, cat := range slice {
result = append(result, cat.(string))
}
return result
}
// ReadMeta reads the content of a metadata.yaml file and returns
// its representation.
func ReadMeta(r io.Reader) (meta *Meta, err error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return
}
raw := make(map[interface{}]interface{})
err = goyaml.Unmarshal(data, raw)
if err != nil {
return
}
v, err := charmSchema.Coerce(raw, nil)
if err != nil {
return nil, errors.New("metadata: " + err.Error())
}
m := v.(map[string]interface{})
meta = &Meta{}
meta.Name = m["name"].(string)
// Schema decodes as int64, but the int range should be good
// enough for revisions.
meta.Summary = m["summary"].(string)
meta.Description = m["description"].(string)
meta.Provides = parseRelations(m["provides"], RoleProvider)
meta.Requires = parseRelations(m["requires"], RoleRequirer)
meta.Peers = parseRelations(m["peers"], RolePeer)
meta.Format = int(m["format"].(int64))
meta.Categories = parseCategories(m["categories"])
if subordinate := m["subordinate"]; subordinate != nil {
meta.Subordinate = subordinate.(bool)
}
if rev := m["revision"]; rev != nil {
// Obsolete
meta.OldRevision = int(m["revision"].(int64))
}
if series, ok := m["series"]; ok && series != nil {
meta.Series = series.(string)
}
if err := meta.Check(); err != nil {
return nil, err
}
return meta, nil
}
// Check checks that the metadata is well-formed.
func (meta Meta) Check() error {
// Check for duplicate or forbidden relation names or interfaces.
names := map[string]bool{}
checkRelations := func(src map[string]Relation, role RelationRole) error {
for name, rel := range src {
if rel.Name != name {
return fmt.Errorf("charm %q has mismatched relation name %q; expected %q", meta.Name, rel.Name, name)
}
if rel.Role != role {
return fmt.Errorf("charm %q has mismatched role %q; expected %q", meta.Name, rel.Role, role)
}
// Container-scoped require relations on subordinates are allowed
// to use the otherwise-reserved juju-* namespace.
if !meta.Subordinate || role != RoleRequirer || rel.Scope != ScopeContainer {
if reservedName(name) {
return fmt.Errorf("charm %q using a reserved relation name: %q", meta.Name, name)
}
}
if role != RoleRequirer {
if reservedName(rel.Interface) {
return fmt.Errorf("charm %q relation %q using a reserved interface: %q", meta.Name, name, rel.Interface)
}
}
if names[name] {
return fmt.Errorf("charm %q using a duplicated relation name: %q", meta.Name, name)
}
names[name] = true
}
return nil
}
if err := checkRelations(meta.Provides, RoleProvider); err != nil {
return err
}
if err := checkRelations(meta.Requires, RoleRequirer); err != nil {
return err
}
if err := checkRelations(meta.Peers, RolePeer); err != nil {
return err
}
// Subordinate charms must have at least one relation that
// has container scope, otherwise they can't relate to the
// principal.
if meta.Subordinate {
valid := false
if meta.Requires != nil {
for _, relationData := range meta.Requires {
if relationData.Scope == ScopeContainer {
valid = true
break
}
}
}
if !valid {
return fmt.Errorf("subordinate charm %q lacks \"requires\" relation with container scope", meta.Name)
}
}
if meta.Series != "" {
if !IsValidSeries(meta.Series) {
return fmt.Errorf("charm %q declares invalid series: %q", meta.Name, meta.Series)
}
}
return nil
}
func reservedName(name string) bool {
return name == "juju" || strings.HasPrefix(name, "juju-")
}
func parseRelations(relations interface{}, role RelationRole) map[string]Relation {
if relations == nil {
return nil
}
result := make(map[string]Relation)
for name, rel := range relations.(map[string]interface{}) {
relMap := rel.(map[string]interface{})
relation := Relation{
Name: name,
Role: role,
Interface: relMap["interface"].(string),
Optional: relMap["optional"].(bool),
}
if scope := relMap["scope"]; scope != nil {
relation.Scope = RelationScope(scope.(string))
}
if relMap["limit"] != nil {
// Schema defaults to int64, but we know
// the int range should be more than enough.
relation.Limit = int(relMap["limit"].(int64))
}
result[name] = relation
}
return result
}
// Schema coercer that expands the interface shorthand notation.
// A consistent format is easier to work with than considering the
// potential difference everywhere.
//
// Supports the following variants::
//
// provides:
// server: riak
// admin: http
// foobar:
// interface: blah
//
// provides:
// server:
// interface: mysql
// limit:
// optional: false
//
// In all input cases, the output is the fully specified interface
// representation as seen in the mysql interface description above.
func ifaceExpander(limit interface{}) schema.Checker {
return ifaceExpC{limit}
}
type ifaceExpC struct {
limit interface{}
}
var (
stringC = schema.String()
mapC = schema.StringMap(schema.Any())
)
func (c ifaceExpC) Coerce(v interface{}, path []string) (newv interface{}, err error) {
s, err := stringC.Coerce(v, path)
if err == nil {
newv = map[string]interface{}{
"interface": s,
"limit": c.limit,
"optional": false,
"scope": string(ScopeGlobal),
}
return
}
v, err = mapC.Coerce(v, path)
if err != nil {
return
}
m := v.(map[string]interface{})
if _, ok := m["limit"]; !ok {
m["limit"] = c.limit
}
return ifaceSchema.Coerce(m, path)
}
var ifaceSchema = schema.FieldMap(
schema.Fields{
"interface": schema.String(),
"limit": schema.OneOf(schema.Const(nil), schema.Int()),
"scope": schema.OneOf(schema.Const(string(ScopeGlobal)), schema.Const(string(ScopeContainer))),
"optional": schema.Bool(),
},
schema.Defaults{
"scope": string(ScopeGlobal),
"optional": false,
},
)
var charmSchema = schema.FieldMap(
schema.Fields{
"name": schema.String(),
"summary": schema.String(),
"description": schema.String(),
"peers": schema.StringMap(ifaceExpander(int64(1))),
"provides": schema.StringMap(ifaceExpander(nil)),
"requires": schema.StringMap(ifaceExpander(int64(1))),
"revision": schema.Int(), // Obsolete
"format": schema.Int(),
"subordinate": schema.Bool(),
"categories": schema.List(schema.String()),
"series": schema.String(),
},
schema.Defaults{
"provides": schema.Omit,
"requires": schema.Omit,
"peers": schema.Omit,
"revision": schema.Omit,
"format": 1,
"subordinate": schema.Omit,
"categories": schema.Omit,
"series": schema.Omit,
},
)