/
config.go
410 lines (353 loc) · 11.3 KB
/
config.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// +build go1.3
package lxd
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/schema"
"gopkg.in/juju/environschema.v1"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/provider/lxd/lxdclient"
)
// TODO(ericsnow) Support providing cert/key file.
// The LXD-specific config keys.
const (
cfgNamespace = "namespace"
cfgRemoteURL = "remote-url"
cfgClientCert = "client-cert"
cfgClientKey = "client-key"
)
// TODO(ericsnow) Use configSchema.ExampleYAML (once it is implemented)
// to generate boilerplaceConfig.
// boilerplateConfig will be shown in help output, so please keep it up to
// date when you change environment configuration below.
var boilerplateConfig = `
lxd:
type: lxd
# namespace identifies the namespace to associate with containers
# created by the provider. It is prepended to the container names.
# By default the model's name is used as the namespace.
#
# Setting the namespace is useful when more than one model
# is using the same remote (e.g. the local LXD socket).
#
# namespace: lxd
# remote-url is the URL to the LXD API server to use for managing
# containers, if any. If not specified then the locally running LXD
# server is used.
#
# Note: Juju does not set up remotes for you. Run the following
# commands on an LXD remote's host to install LXD:
#
# add-apt-repository ppa:ubuntu-lxc/lxd-stable
# apt-get update
# apt-get install lxd
#
# Before using a locally running LXD (the default for this provider)
# after installing it, either through Juju or the LXD CLI ("lxc"),
# you must either log out and back in or run this command:
#
# newgrp lxd
#
# You will also need to prepare the "ubuntu" images that Juju uses:
#
# lxc remote add images images.linuxcontainers.org
# lxd-images import ubuntu --alias ubuntu-wily wily
#
# (Also consider the --stream and --sync options.)
#
# You will need to prepare an image for each Ubuntu series for which
# you want to create instances. The alias must match the series:
#
# lxd-images import ubuntu --alias ubuntu-trusty trusty
# lxd-images import ubuntu --alias ubuntu-wily wily
# lxd-images import ubuntu --alias ubuntu-xenial xenial
#
# See: https://linuxcontainers.org/lxd/getting-started-cli/
#
# Note: the LXD provider does not support using any series older
# than wily for a controller instance. However, non-controller
# instances may be provisioned on earler series (e.g. trusty).
#
# remote-url:
# The cert and key the client should use to connect to the remote
# may also be provided. If not then they are auto-generated.
#
# client-cert:
# client-key:
`[1:]
// configSchema defines the schema for the configuration attributes
// defined by the LXD provider.
var configSchema = environschema.Fields{
cfgNamespace: {
Description: `Identifies the namespace to associate with containers created by the provider. It is prepended to the container names. By default the model's name is used as the namespace.`,
Type: environschema.Tstring,
Immutable: true,
},
cfgRemoteURL: {
Description: `Identifies the LXD API server to use for managing containers, if any.`,
Type: environschema.Tstring,
Immutable: true,
},
cfgClientKey: {
Description: `The client key used for connecting to a LXD host machine.`,
Type: environschema.Tstring,
Immutable: true,
},
cfgClientCert: {
Description: `The client cert used for connecting to a LXD host machine.`,
Type: environschema.Tstring,
Immutable: true,
},
}
var (
// TODO(ericsnow) Extract the defaults from configSchema as soon as
// (or if) environschema.Attr supports defaults.
configBaseDefaults = schema.Defaults{
cfgNamespace: "",
cfgRemoteURL: "",
cfgClientCert: "",
cfgClientKey: "",
}
configFields, configDefaults = func() (schema.Fields, schema.Defaults) {
fields, defaults, err := configSchema.ValidationSchema()
if err != nil {
panic(err)
}
defaults = updateDefaults(defaults, configBaseDefaults)
return fields, defaults
}()
configImmutableFields = func() []string {
var names []string
for name, attr := range configSchema {
if attr.Immutable {
names = append(names, name)
}
}
return names
}()
configSecretFields = []string{}
)
func updateDefaults(defaults schema.Defaults, updates schema.Defaults) schema.Defaults {
updated := schema.Defaults{}
for k, v := range defaults {
updated[k] = v
}
for k, v := range updates {
// TODO(ericsnow) Delete the item if v is nil?
updated[k] = v
}
return updated
}
func adjustDefaults(cfg *config.Config, defaults map[string]interface{}) (map[string]interface{}, []string) {
var unset []string
updated := make(map[string]interface{})
for k, v := range defaults {
updated[k] = v
}
// Set the proper default namespace.
raw := updated[cfgNamespace]
if raw == nil || raw.(string) == "" {
raw = cfg.Name()
updated[cfgNamespace] = raw
}
if val, ok := cfg.UnknownAttrs()[cfgNamespace]; ok && val == "" {
unset = append(unset, cfgNamespace)
}
return updated, unset
}
// TODO(ericsnow) environschema.Fields should have this...
func ensureImmutableFields(oldAttrs, newAttrs map[string]interface{}) error {
for name, attr := range configSchema {
if !attr.Immutable {
continue
}
if newAttrs[name] != oldAttrs[name] {
return errors.Errorf("%s: cannot change from %v to %v", name, oldAttrs[name], newAttrs[name])
}
}
return nil
}
type environConfig struct {
*config.Config
attrs map[string]interface{}
}
// newConfig builds a new environConfig from the provided Config and
// returns it.
func newConfig(cfg *config.Config) *environConfig {
return &environConfig{
Config: cfg,
attrs: cfg.UnknownAttrs(),
}
}
// newValidConfig builds a new environConfig from the provided Config
// and returns it. This includes applying the provided defaults
// values, if any. The resulting config values are validated.
func newValidConfig(cfg *config.Config, defaults map[string]interface{}) (*environConfig, error) {
// Any auth credentials handling should happen first...
// Ensure that the provided config is valid.
if err := config.Validate(cfg, nil); err != nil {
return nil, errors.Trace(err)
}
// Apply the defaults and coerce/validate the custom config attrs.
fixedDefaults, unset := adjustDefaults(cfg, defaults)
cfg, err := cfg.Remove(unset)
if err != nil {
return nil, errors.Trace(err)
}
validated, err := cfg.ValidateUnknownAttrs(configFields, fixedDefaults)
if err != nil {
return nil, errors.Trace(err)
}
validCfg, err := cfg.Apply(validated)
if err != nil {
return nil, errors.Trace(err)
}
// Build the config.
ecfg := newConfig(validCfg)
// Update to defaults set via client config.
clientCfg, err := ecfg.clientConfig()
if err != nil {
return nil, errors.Trace(err)
}
ecfg, err = ecfg.updateForClientConfig(clientCfg)
if err != nil {
return nil, errors.Trace(err)
}
// Do final (more complex, provider-specific) validation.
if err := ecfg.validate(); err != nil {
return nil, errors.Trace(err)
}
return ecfg, nil
}
func (c *environConfig) namespace() string {
raw := c.attrs[cfgNamespace]
return raw.(string)
}
func (c *environConfig) dirname() string {
// TODO(ericsnow) Put it under one of the juju/paths.*() directories.
return ""
}
func (c *environConfig) remoteURL() string {
raw := c.attrs[cfgRemoteURL]
return raw.(string)
}
func (c *environConfig) clientCert() string {
raw := c.attrs[cfgClientCert]
return raw.(string)
}
func (c *environConfig) clientKey() string {
raw := c.attrs[cfgClientKey]
return raw.(string)
}
// clientConfig builds a LXD Config based on the env config and returns it.
func (c *environConfig) clientConfig() (lxdclient.Config, error) {
remote := lxdclient.Remote{
Name: "juju-remote",
Host: c.remoteURL(),
}
if c.clientCert() != "" {
certPEM := []byte(c.clientCert())
keyPEM := []byte(c.clientKey())
cert := lxdclient.NewCert(certPEM, keyPEM)
cert.Name = fmt.Sprintf("juju cert for env %q", c.Name())
remote.Cert = &cert
}
cfg := lxdclient.Config{
Namespace: c.namespace(),
Dirname: lxdclient.ConfigPath("juju-" + c.namespace()),
Remote: remote,
}
cfg, err := cfg.WithDefaults()
if err != nil {
return cfg, errors.Trace(err)
}
return cfg, nil
}
// TODO(ericsnow) Switch to a DI testing approach and eliminiate this var.
var asNonLocal = lxdclient.Config.UsingTCPRemote
func (c *environConfig) updateForClientConfig(clientCfg lxdclient.Config) (*environConfig, error) {
nonlocal, err := asNonLocal(clientCfg)
if err != nil {
return nil, errors.Trace(err)
}
clientCfg = nonlocal
c.attrs[cfgNamespace] = clientCfg.Namespace
c.attrs[cfgRemoteURL] = clientCfg.Remote.Host
var cert lxdclient.Cert
if clientCfg.Remote.Cert != nil {
cert = *clientCfg.Remote.Cert
}
c.attrs[cfgClientCert] = string(cert.CertPEM)
c.attrs[cfgClientKey] = string(cert.KeyPEM)
// Apply the updates.
cfg, err := c.Config.Apply(c.attrs)
if err != nil {
return nil, errors.Trace(err)
}
return newConfig(cfg), nil
}
// secret gathers the "secret" config values and returns them.
func (c *environConfig) secret() map[string]string {
if len(configSecretFields) == 0 {
return nil
}
secretAttrs := make(map[string]string, len(configSecretFields))
for _, key := range configSecretFields {
secretAttrs[key] = c.attrs[key].(string)
}
return secretAttrs
}
// validate checks more complex LCD-specific config values.
func (c environConfig) validate() error {
// All fields must be populated, even with just the default.
// TODO(ericsnow) Shouldn't configSchema support this?
for field := range configFields {
if dflt, ok := configDefaults[field]; ok && dflt == "" {
continue
}
if c.attrs[field].(string) == "" {
return errors.Errorf("%s: must not be empty", field)
}
}
// If cert is provided then key must be (and vice versa).
if c.clientCert() == "" && c.clientKey() != "" {
return errors.Errorf("missing %s (got %s value %q)", cfgClientCert, cfgClientKey, c.clientKey())
}
if c.clientCert() != "" && c.clientKey() == "" {
return errors.Errorf("missing %s (got %s value %q)", cfgClientKey, cfgClientCert, c.clientCert())
}
// Check sanity of complex provider-specific fields.
cfg, err := c.clientConfig()
if err != nil {
return errors.Trace(err)
}
if err := cfg.Validate(); err != nil {
return errors.Trace(err)
}
return nil
}
// update applies changes from the provided config to the env config.
// Changes to any immutable attributes result in an error.
func (c *environConfig) update(cfg *config.Config) error {
// Validate the updates. newValidConfig does not modify the "known"
// config attributes so it is safe to call Validate here first.
if err := config.Validate(cfg, c.Config); err != nil {
return errors.Trace(err)
}
updates, err := newValidConfig(cfg, configDefaults)
if err != nil {
return errors.Trace(err)
}
// Check that no immutable fields have changed.
attrs := updates.UnknownAttrs()
if err := ensureImmutableFields(c.attrs, attrs); err != nil {
return errors.Trace(err)
}
// Apply the updates.
// TODO(ericsnow) Should updates.Config be set in instead of cfg?
c.Config = cfg
c.attrs = cfg.UnknownAttrs()
return nil
}