-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize.go
384 lines (349 loc) · 11.9 KB
/
initialize.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
383
384
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/errors"
"github.com/juju/utils"
"github.com/juju/utils/clock"
names "gopkg.in/juju/names.v2"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/txn"
"github.com/juju/juju/cloud"
"github.com/juju/juju/controller"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/mongo"
"github.com/juju/juju/permission"
"github.com/juju/juju/status"
"github.com/juju/juju/storage"
"github.com/juju/juju/storage/poolmanager"
)
// InitializeParams contains the parameters for initializing the state database.
type InitializeParams struct {
// Clock wraps all calls time. Real uses use clock.WallClock,
// tests may override with a testing clock.
Clock clock.Clock
// ControllerModelArgs contains the arguments for creating
// the controller model.
ControllerModelArgs ModelArgs
// Cloud contains the properties of the cloud that the
// controller runs in.
Cloud cloud.Cloud
// CloudCredentials contains the credentials for the owner of
// the controller model to store in the controller.
CloudCredentials map[names.CloudCredentialTag]cloud.Credential
// ControllerConfig contains config attributes for
// the controller.
ControllerConfig controller.Config
// ControllerInheritedConfig contains default config attributes for
// models on the specified cloud.
ControllerInheritedConfig map[string]interface{}
// RegionInheritedConfig contains region specific configuration for
// models running on specific cloud regions.
RegionInheritedConfig cloud.RegionConfig
// NewPolicy is a function that returns the set of state policies
// to apply.
NewPolicy NewPolicyFunc
// MongoInfo contains the information required to address and
// authenticate with Mongo.
MongoInfo *mongo.MongoInfo
// MongoDialOpts contains the dial options for connecting to
// Mongo.
MongoDialOpts mongo.DialOpts
}
// Validate checks that the state initialization parameters are valid.
func (p InitializeParams) Validate() error {
if p.Clock == nil {
return errors.NotValidf("missing clock")
}
if err := p.ControllerModelArgs.Validate(); err != nil {
return errors.Trace(err)
}
if p.ControllerModelArgs.MigrationMode != MigrationModeNone {
return errors.NotValidf("migration mode %q", p.ControllerModelArgs.MigrationMode)
}
uuid := p.ControllerModelArgs.Config.UUID()
controllerUUID := p.ControllerConfig.ControllerUUID()
if uuid == controllerUUID {
return errors.NotValidf("same controller model uuid (%v) and controller-uuid (%v)", uuid, controllerUUID)
}
if p.MongoInfo == nil {
return errors.NotValidf("nil MongoInfo")
}
if err := validateCloud(p.Cloud); err != nil {
return errors.Annotate(err, "validating cloud")
}
if _, err := validateCloudRegion(p.Cloud, p.ControllerModelArgs.CloudRegion); err != nil {
return errors.Annotate(err, "validating controller model cloud region")
}
if _, err := validateCloudCredentials(p.Cloud, p.CloudCredentials); err != nil {
return errors.Annotate(err, "validating cloud credentials")
}
creds := make(map[string]cloud.Credential, len(p.CloudCredentials))
for tag, cred := range p.CloudCredentials {
creds[tag.Id()] = cred
}
if _, err := validateCloudCredential(
p.Cloud,
creds,
p.ControllerModelArgs.CloudCredential,
); err != nil {
return errors.Annotate(err, "validating controller model cloud credential")
}
return nil
}
// InitDatabaseFunc defines a function used to
// create the collections and indices in a Juju database.
type InitDatabaseFunc func(*mgo.Session, string, *controller.Config) error
// Initialize sets up an initial empty state and returns it.
// This needs to be performed only once for the initial controller model.
// It returns unauthorizedError if access is unauthorized.
func Initialize(args InitializeParams) (_ *Controller, _ *State, err error) {
if err := args.Validate(); err != nil {
return nil, nil, errors.Annotate(err, "validating initialization args")
}
controllerTag := names.NewControllerTag(args.ControllerConfig.ControllerUUID())
modelUUID := args.ControllerModelArgs.Config.UUID()
if !names.IsValidModel(modelUUID) {
return nil, nil, errors.New("invalid model UUID")
}
modelTag := names.NewModelTag(modelUUID)
ctlr, err := OpenController(OpenParams{
Clock: args.Clock,
ControllerTag: controllerTag,
ControllerModelTag: modelTag,
MongoInfo: args.MongoInfo,
MongoDialOpts: args.MongoDialOpts,
NewPolicy: args.NewPolicy,
InitDatabaseFunc: InitDatabase,
})
if err != nil {
return nil, nil, errors.Annotate(err, "opening controller")
}
defer func() {
if err != nil {
if closeErr := ctlr.Close(); closeErr != nil {
logger.Errorf("error closing controller while aborting Initialize: %v", closeErr)
}
}
}()
st, err := ctlr.NewState(modelTag)
if err != nil {
return nil, nil, errors.Annotate(err, "opening state")
}
defer func() {
if err != nil {
if closeErr := st.Close(); closeErr != nil {
logger.Errorf("error closing state while aborting Initialize: %v", closeErr)
}
}
}()
st.controllerModelTag = modelTag
// A valid model is used as a signal that the
// state has already been initalized. If this is the case
// do nothing.
if _, err := st.Model(); err == nil {
return nil, nil, errors.New("already initialized")
} else if !errors.IsNotFound(err) {
return nil, nil, errors.Trace(err)
}
logger.Infof("initializing controller model %s", modelTag.Id())
modelOps, modelStatusDoc, err := st.modelSetupOps(
args.ControllerConfig.ControllerUUID(),
args.ControllerModelArgs,
&lineage{
ControllerConfig: args.ControllerInheritedConfig,
RegionConfig: args.RegionInheritedConfig,
})
if err != nil {
return nil, nil, errors.Trace(err)
}
salt, err := utils.RandomSalt()
if err != nil {
return nil, nil, err
}
dateCreated := st.nowToTheSecond()
ops := createInitialUserOps(
args.ControllerConfig.ControllerUUID(),
args.ControllerModelArgs.Owner,
args.MongoInfo.Password,
salt,
dateCreated,
)
ops = append(ops,
txn.Op{
C: controllersC,
Id: modelGlobalKey,
Assert: txn.DocMissing,
Insert: &controllersDoc{
CloudName: args.Cloud.Name,
ModelUUID: st.ModelUUID(),
},
},
createCloudOp(args.Cloud),
txn.Op{
C: controllersC,
Id: apiHostPortsKey,
Assert: txn.DocMissing,
Insert: &apiHostPortsDoc{},
},
txn.Op{
C: controllersC,
Id: stateServingInfoKey,
Assert: txn.DocMissing,
Insert: &StateServingInfo{},
},
txn.Op{
C: controllersC,
Id: hostedModelCountKey,
Assert: txn.DocMissing,
Insert: &hostedModelCountDoc{},
},
createSettingsOp(controllersC, controllerSettingsGlobalKey, args.ControllerConfig),
createSettingsOp(globalSettingsC, controllerInheritedSettingsGlobalKey, args.ControllerInheritedConfig),
)
for k, v := range args.Cloud.RegionConfig {
// Create an entry keyed on cloudname#<key>, value for each region in
// region-config. The values here are themselves
// map[string]interface{}.
ops = append(ops, createSettingsOp(globalSettingsC, regionSettingsGlobalKey(args.Cloud.Name, k), v))
}
for tag, cred := range args.CloudCredentials {
ops = append(ops, createCloudCredentialOp(tag, cred))
}
ops = append(ops, modelOps...)
if err := st.db().RunTransaction(ops); err != nil {
return nil, nil, errors.Trace(err)
}
probablyUpdateStatusHistory(st.db(), modelGlobalKey, modelStatusDoc)
return ctlr, st, nil
}
// InitDatabase creates all the collections and indices in a Juju database.
func InitDatabase(session *mgo.Session, modelUUID string, settings *controller.Config) error {
schema := allCollections()
if err := schema.Create(session.DB(jujuDB), settings); err != nil {
return errors.Trace(err)
}
if err := InitDbLogs(session, modelUUID); err != nil {
return errors.Trace(err)
}
return nil
}
// lineage is a composite of inheritable properties for the extent of
// passing them into modelSetupOps.
type lineage struct {
ControllerConfig map[string]interface{}
RegionConfig cloud.RegionConfig
}
// modelSetupOps returns the transactions necessary to set up a model.
func (st *State) modelSetupOps(controllerUUID string, args ModelArgs, inherited *lineage) ([]txn.Op, statusDoc, error) {
var modelStatusDoc statusDoc
if inherited != nil {
if err := checkControllerInheritedConfig(inherited.ControllerConfig); err != nil {
return nil, modelStatusDoc, errors.Trace(err)
}
}
if err := checkModelConfig(args.Config); err != nil {
return nil, modelStatusDoc, errors.Trace(err)
}
controllerModelUUID := st.controllerModelTag.Id()
modelUUID := args.Config.UUID()
modelStatusDoc = statusDoc{
ModelUUID: modelUUID,
Updated: st.clock().Now().UnixNano(),
Status: status.Available,
}
modelUserOps := createModelUserOps(
modelUUID, args.Owner, args.Owner, args.Owner.Name(), st.nowToTheSecond(), permission.AdminAccess,
)
ops := []txn.Op{
createStatusOp(st, modelGlobalKey, modelStatusDoc),
createConstraintsOp(modelGlobalKey, args.Constraints),
}
// Inc ref count for hosted models.
if controllerModelUUID != modelUUID {
ops = append(ops, incHostedModelCountOp())
}
// Create the default storage pools for the model.
if args.StorageProviderRegistry != nil {
defaultStoragePoolsOps, err := st.createDefaultStoragePoolsOps(args.StorageProviderRegistry)
if err != nil {
return nil, modelStatusDoc, errors.Trace(err)
}
ops = append(ops, defaultStoragePoolsOps...)
}
// Create the final map of config attributes for the model.
// If we have ControllerInheritedConfig passed in, that means state
// is being initialised and there won't be any config sources
// in state.
var configSources []modelConfigSource
if inherited != nil {
configSources = []modelConfigSource{
{
name: config.JujuDefaultSource,
sourceFunc: modelConfigSourceFunc(func() (attrValues, error) {
return config.ConfigDefaults(), nil
})},
{
name: config.JujuControllerSource,
sourceFunc: modelConfigSourceFunc(func() (attrValues, error) {
return inherited.ControllerConfig, nil
})},
{
name: config.JujuRegionSource,
sourceFunc: modelConfigSourceFunc(func() (attrValues, error) {
// We return the values specific to this region for this model.
return attrValues(inherited.RegionConfig[args.CloudRegion]), nil
})},
}
} else {
rspec := &environs.RegionSpec{Cloud: args.CloudName, Region: args.CloudRegion}
configSources = modelConfigSources(st, rspec)
}
modelCfg, err := composeModelConfigAttributes(args.Config.AllAttrs(), configSources...)
if err != nil {
return nil, modelStatusDoc, errors.Trace(err)
}
// Some values require marshalling before storage.
modelCfg = config.CoerceForStorage(modelCfg)
ops = append(ops,
createSettingsOp(settingsC, modelGlobalKey, modelCfg),
createModelEntityRefsOp(modelUUID),
createModelOp(
args.Type,
args.Owner,
args.Config.Name(),
modelUUID, controllerUUID,
args.CloudName, args.CloudRegion, args.CloudCredential,
args.MigrationMode,
args.EnvironVersion,
),
createUniqueOwnerModelNameOp(args.Owner, args.Config.Name()),
)
ops = append(ops, modelUserOps...)
return ops, modelStatusDoc, nil
}
func (st *State) createDefaultStoragePoolsOps(registry storage.ProviderRegistry) ([]txn.Op, error) {
m := poolmanager.MemSettings{make(map[string]map[string]interface{})}
pm := poolmanager.New(m, registry)
providerTypes, err := registry.StorageProviderTypes()
if err != nil {
return nil, errors.Trace(err)
}
for _, providerType := range providerTypes {
p, err := registry.StorageProvider(providerType)
if err != nil {
return nil, errors.Trace(err)
}
if err := poolmanager.AddDefaultStoragePools(p, pm); err != nil {
return nil, errors.Annotatef(
err, "adding default storage pools for %q", providerType,
)
}
}
var ops []txn.Op
for key, settings := range m.Settings {
ops = append(ops, createSettingsOp(settingsC, key, settings))
}
return ops, nil
}