forked from UbuntuEvangelist/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charm.go
293 lines (257 loc) · 8.43 KB
/
charm.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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"net/url"
"regexp"
"github.com/juju/errors"
"github.com/juju/names"
"gopkg.in/juju/charm.v6-unstable"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
"github.com/juju/juju/mongo"
)
// charmDoc represents the internal state of a charm in MongoDB.
type charmDoc struct {
DocID string `bson:"_id"`
URL *charm.URL `bson:"url"` // DANGEROUS see below
EnvUUID string `bson:"env-uuid"`
// TODO(fwereade) 2015-06-18 lp:1467964
// DANGEROUS: our schema can change any time the charm package changes,
// and we have no automated way to detect when that happens. We *must*
// not depend upon serializations we cannot control from inside this
// package. What's in a *charm.Meta? What will be tomorrow? What logic
// will we be writing on the assumption that all stored Metas have set
// some field? What fields might lose precision when they go into the
// database?
Meta *charm.Meta `bson:"meta"`
Config *charm.Config `bson:"config"`
Actions *charm.Actions `bson:"actions"`
Metrics *charm.Metrics `bson:"metrics"`
// DEPRECATED: BundleURL is deprecated, and exists here
// only for migration purposes. We should remove this
// when migrations are no longer necessary.
BundleURL *url.URL `bson:"bundleurl,omitempty"`
BundleSha256 string `bson:"bundlesha256"`
StoragePath string `bson:"storagepath"`
PendingUpload bool `bson:"pendingupload"`
Placeholder bool `bson:"placeholder"`
}
// insertCharmOps returns the txn operations necessary to insert the supplied
// charm data.
func insertCharmOps(
st *State, ch charm.Charm, curl *charm.URL, storagePath, bundleSha256 string,
) ([]txn.Op, error) {
return insertAnyCharmOps(&charmDoc{
DocID: curl.String(),
URL: curl,
EnvUUID: st.EnvironTag().Id(),
Meta: ch.Meta(),
Config: safeConfig(ch),
Metrics: ch.Metrics(),
Actions: ch.Actions(),
BundleSha256: bundleSha256,
StoragePath: storagePath,
})
}
// insertPlaceholderCharmOps returns the txn operations necessary to insert a
// charm document referencing a store charm that is not yet directly accessible
// within the environment.
func insertPlaceholderCharmOps(st *State, curl *charm.URL) ([]txn.Op, error) {
return insertAnyCharmOps(&charmDoc{
DocID: curl.String(),
URL: curl,
EnvUUID: st.EnvironTag().Id(),
Placeholder: true,
})
}
// insertPendingCharmOps returns the txn operations necessary to insert a charm
// document referencing a charm that has yet to be uploaded to the environment.
func insertPendingCharmOps(st *State, curl *charm.URL) ([]txn.Op, error) {
return insertAnyCharmOps(&charmDoc{
DocID: curl.String(),
URL: curl,
EnvUUID: st.EnvironTag().Id(),
PendingUpload: true,
})
}
// insertAnyCharmOps returns the txn operations necessary to insert the supplied
// charm document.
func insertAnyCharmOps(cdoc *charmDoc) ([]txn.Op, error) {
return []txn.Op{{
C: charmsC,
Id: cdoc.DocID,
Assert: txn.DocMissing,
Insert: cdoc,
}}, nil
}
// updateCharmOps returns the txn operations necessary to update the charm
// document with the supplied data, so long as the supplied assert still holds
// true.
func updateCharmOps(
st *State, ch charm.Charm, curl *charm.URL, storagePath, bundleSha256 string, assert bson.D,
) ([]txn.Op, error) {
updateFields := bson.D{{"$set", bson.D{
{"meta", ch.Meta()},
{"config", safeConfig(ch)},
{"actions", ch.Actions()},
{"metrics", ch.Metrics()},
{"storagepath", storagePath},
{"bundlesha256", bundleSha256},
{"pendingupload", false},
{"placeholder", false},
}}}
return []txn.Op{{
C: charmsC,
Id: curl.String(),
Assert: assert,
Update: updateFields,
}}, nil
}
// convertPlaceholderCharmOps returns the txn operations necessary to convert
// the charm with the supplied docId from a placeholder to one marked for
// pending upload.
func convertPlaceholderCharmOps(docID string) ([]txn.Op, error) {
return []txn.Op{{
C: charmsC,
Id: docID,
Assert: bson.D{
{"bundlesha256", ""},
{"pendingupload", false},
{"placeholder", true},
},
Update: bson.D{{"$set", bson.D{
{"pendingupload", true},
{"placeholder", false},
}}},
}}, nil
}
// deleteOldPlaceholderCharmsOps returns the txn ops required to delete all placeholder charm
// records older than the specified charm URL.
func deleteOldPlaceholderCharmsOps(st *State, charms mongo.Collection, curl *charm.URL) ([]txn.Op, error) {
// Get a regex with the charm URL and no revision.
noRevURL := curl.WithRevision(-1)
curlRegex := "^" + regexp.QuoteMeta(st.docID(noRevURL.String()))
var docs []charmDoc
query := bson.D{{"_id", bson.D{{"$regex", curlRegex}}}, {"placeholder", true}}
err := charms.Find(query).Select(bson.D{{"_id", 1}, {"url", 1}}).All(&docs)
if err != nil {
return nil, errors.Trace(err)
}
var ops []txn.Op
for _, doc := range docs {
if doc.URL.Revision >= curl.Revision {
continue
}
ops = append(ops, txn.Op{
C: charmsC,
Id: doc.DocID,
Assert: stillPlaceholder,
Remove: true,
})
}
return ops, nil
}
// safeConfig is a travesty which attempts to work around our continued failure
// to properly insulate our database from code changes; it escapes mongo-
// significant characters in config options. See lp:1467964.
func safeConfig(ch charm.Charm) *charm.Config {
// Make sure we escape any "$" and "." in config option names
// first. See http://pad.lv/1308146.
cfg := ch.Config()
escapedConfig := charm.NewConfig()
for optionName, option := range cfg.Options {
escapedName := escapeReplacer.Replace(optionName)
escapedConfig.Options[escapedName] = option
}
return escapedConfig
}
// Charm represents the state of a charm in the environment.
type Charm struct {
st *State
doc charmDoc
}
func newCharm(st *State, cdoc *charmDoc) *Charm {
// Because we probably just read the doc from state, make sure we
// unescape any config option names for "$" and ".". See
// http://pad.lv/1308146
if cdoc != nil && cdoc.Config != nil {
unescapedConfig := charm.NewConfig()
for optionName, option := range cdoc.Config.Options {
unescapedName := unescapeReplacer.Replace(optionName)
unescapedConfig.Options[unescapedName] = option
}
cdoc.Config = unescapedConfig
}
return &Charm{st: st, doc: *cdoc}
}
// Tag returns a tag identifying the charm.
// Implementing state.GlobalEntity interface.
func (c *Charm) Tag() names.Tag {
return names.NewCharmTag(c.URL().String())
}
// charmGlobalKey returns the global database key for the charm
// with the given url.
func charmGlobalKey(charmURL *charm.URL) string {
return "c#" + charmURL.String()
}
// GlobalKey returns the global database key for the charm.
// Implementing state.GlobalEntity interface.
func (c *Charm) globalKey() string {
return charmGlobalKey(c.doc.URL)
}
func (c *Charm) String() string {
return c.doc.URL.String()
}
// URL returns the URL that identifies the charm.
func (c *Charm) URL() *charm.URL {
clone := *c.doc.URL
return &clone
}
// Revision returns the monotonically increasing charm
// revision number.
func (c *Charm) Revision() int {
return c.doc.URL.Revision
}
// Meta returns the metadata of the charm.
func (c *Charm) Meta() *charm.Meta {
return c.doc.Meta
}
// Config returns the configuration of the charm.
func (c *Charm) Config() *charm.Config {
return c.doc.Config
}
// Metrics returns the metrics declared for the charm.
func (c *Charm) Metrics() *charm.Metrics {
return c.doc.Metrics
}
// Actions returns the actions definition of the charm.
func (c *Charm) Actions() *charm.Actions {
return c.doc.Actions
}
// StoragePath returns the storage path of the charm bundle.
func (c *Charm) StoragePath() string {
return c.doc.StoragePath
}
// BundleURL returns the url to the charm bundle in
// the provider storage.
//
// DEPRECATED: this is only to be used for migrating
// charm archives to environment storage.
func (c *Charm) BundleURL() *url.URL {
return c.doc.BundleURL
}
// BundleSha256 returns the SHA256 digest of the charm bundle bytes.
func (c *Charm) BundleSha256() string {
return c.doc.BundleSha256
}
// IsUploaded returns whether the charm has been uploaded to the
// environment storage.
func (c *Charm) IsUploaded() bool {
return !c.doc.PendingUpload
}
// IsPlaceholder returns whether the charm record is just a placeholder
// rather than representing a deployed charm.
func (c *Charm) IsPlaceholder() bool {
return c.doc.Placeholder
}