forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
452 lines (411 loc) · 13.5 KB
/
provider.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package lxd
import (
stdcontext "context"
"io/ioutil"
"os"
"path/filepath"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/jsonschema"
"github.com/juju/schema"
"gopkg.in/juju/environschema.v1"
"gopkg.in/yaml.v2"
"github.com/juju/juju/cloud"
"github.com/juju/juju/container/lxd"
"github.com/juju/juju/environs"
environscloudspec "github.com/juju/juju/environs/cloudspec"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/provider/lxd/lxdnames"
)
// LXCConfigReader reads files required for the LXC configuration.
type LXCConfigReader interface {
// ReadConfig takes a path and returns a LXCConfig.
// Returns an error if there is an error with the location of the config
// file, or there was an error parsing the file.
ReadConfig(path string) (LXCConfig, error)
// ReadCert takes a path and returns a raw certificate, there is no
// validation of the certificate.
// Returns an error if there is an error with the location of the
// certificate.
ReadCert(path string) ([]byte, error)
}
// LXCConfig represents a configuration setup of a LXC configuration file.
// The LXCConfig expects the configuration file to be in a yaml representation.
type LXCConfig struct {
DefaultRemote string `yaml:"local"`
Remotes map[string]LXCRemoteConfig `yaml:"remotes"`
}
// LXCRemoteConfig defines a the remote servers of a LXC configuration.
type LXCRemoteConfig struct {
Addr string `yaml:"addr"`
Public bool `yaml:"public"`
Protocol string `yaml:"protocol"`
AuthType string `yaml:"auth_type"`
}
type environProvider struct {
environs.ProviderCredentials
environs.RequestFinalizeCredential
environs.ProviderCredentialsRegister
serverFactory ServerFactory
lxcConfigReader LXCConfigReader
Clock clock.Clock
}
var cloudSchema = &jsonschema.Schema{
Type: []jsonschema.Type{jsonschema.ObjectType},
Required: []string{cloud.EndpointKey, cloud.AuthTypesKey},
Order: []string{cloud.EndpointKey, cloud.AuthTypesKey, cloud.RegionsKey},
Properties: map[string]*jsonschema.Schema{
cloud.EndpointKey: {
Singular: "the API endpoint url for the remote LXD server",
Type: []jsonschema.Type{jsonschema.StringType},
Format: jsonschema.FormatURI,
},
cloud.AuthTypesKey: {
Singular: "auth type",
Plural: "auth types",
Type: []jsonschema.Type{jsonschema.ArrayType},
UniqueItems: jsonschema.Bool(true),
Default: string(cloud.CertificateAuthType),
Items: &jsonschema.ItemSpec{
Schemas: []*jsonschema.Schema{{
Type: []jsonschema.Type{jsonschema.StringType},
Enum: []interface{}{
string(cloud.CertificateAuthType),
},
}},
},
},
cloud.RegionsKey: {
Type: []jsonschema.Type{jsonschema.ObjectType},
Singular: "region",
Plural: "regions",
Default: "default",
AdditionalProperties: &jsonschema.Schema{
Type: []jsonschema.Type{jsonschema.ObjectType},
Required: []string{cloud.EndpointKey},
MaxProperties: jsonschema.Int(1),
Properties: map[string]*jsonschema.Schema{
cloud.EndpointKey: {
Singular: "the API endpoint url for the region",
Type: []jsonschema.Type{jsonschema.StringType},
Format: jsonschema.FormatURI,
Default: "",
PromptDefault: "use cloud api url",
},
},
},
},
},
}
// NewProvider returns a new LXD EnvironProvider.
func NewProvider() environs.CloudEnvironProvider {
configReader := lxcConfigReader{}
factory := NewServerFactory()
credentials := environProviderCredentials{
certReadWriter: certificateReadWriter{},
certGenerator: certificateGenerator{},
lookup: netLookup{},
serverFactory: factory,
lxcConfigReader: configReader,
}
return &environProvider{
ProviderCredentials: credentials,
RequestFinalizeCredential: credentials,
ProviderCredentialsRegister: credentials,
serverFactory: factory,
lxcConfigReader: configReader,
}
}
// Version is part of the EnvironProvider interface.
func (*environProvider) Version() int {
return 0
}
// Open implements environs.EnvironProvider.
func (p *environProvider) Open(_ stdcontext.Context, args environs.OpenParams) (environs.Environ, error) {
if err := p.validateCloudSpec(args.Cloud); err != nil {
return nil, errors.Annotate(err, "validating cloud spec")
}
env, err := newEnviron(
p,
args.Cloud,
args.Config,
)
return env, errors.Trace(err)
}
// CloudSchema returns the schema used to validate input for add-cloud.
func (p *environProvider) CloudSchema() *jsonschema.Schema {
return cloudSchema
}
// Ping tests the connection to the cloud, to verify the endpoint is valid.
func (p *environProvider) Ping(ctx context.ProviderCallContext, endpoint string) error {
// if the endpoint is empty, then don't ping, as we can assume we're using
// local lxd
if endpoint == "" {
return nil
}
// Ensure the Port on the Host, if we get an error it is reasonable to
// assume that the address in the spec is invalid.
lxdEndpoint, err := lxd.EnsureHostPort(endpoint)
if err != nil {
return errors.Trace(err)
}
// Make sure we have an https url
if lxdEndpoint != endpoint {
return errors.Errorf("invalid URL %q: only HTTPS is supported", endpoint)
}
// Connect to the remote server anonymously so we can just verify it exists
// as we're not sure that the certificates are loaded in time for when the
// ping occurs i.e. interactive add-cloud
_, err = lxd.ConnectRemote(lxd.NewInsecureServerSpec(lxdEndpoint))
if err != nil {
return errors.Errorf("no lxd server running at %s", lxdEndpoint)
}
return nil
}
// PrepareConfig implements environs.EnvironProvider.
func (p *environProvider) PrepareConfig(args environs.PrepareConfigParams) (*config.Config, error) {
if err := p.validateCloudSpec(args.Cloud); err != nil {
return nil, errors.Annotate(err, "validating cloud spec")
}
// Set the default filesystem-storage source.
attrs := make(map[string]interface{})
if _, ok := args.Config.StorageDefaultFilesystemSource(); !ok {
attrs[config.StorageDefaultFilesystemSourceKey] = lxdStorageProviderType
}
if len(attrs) == 0 {
return args.Config, nil
}
cfg, err := args.Config.Apply(attrs)
return cfg, errors.Trace(err)
}
// Validate implements environs.EnvironProvider.
func (*environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
if _, err := newValidConfig(cfg); err != nil {
return nil, errors.Annotate(err, "invalid base config")
}
return cfg, nil
}
// DetectClouds implements environs.CloudDetector.
func (p *environProvider) DetectClouds() ([]cloud.Cloud, error) {
usedNames := map[string]bool{lxdnames.ProviderType: true, lxdnames.DefaultCloud: true}
clouds := []cloud.Cloud{localhostCloud}
for _, dir := range configDirs() {
configPath := filepath.Join(dir, "config.yml")
config, err := p.lxcConfigReader.ReadConfig(configPath)
if err != nil {
logger.Errorf("unable to read/parse LXC config file (%s): %s", configPath, err)
}
for name, remote := range config.Remotes {
if remote.Protocol != lxdnames.ProviderType {
continue
}
if usedNames[name] {
logger.Warningf("ignoring ambigious cloud %s", name)
continue
}
usedNames[name] = true
clouds = append(clouds, cloud.Cloud{
Name: name,
Type: lxdnames.ProviderType,
Endpoint: remote.Addr,
Description: "LXD Cluster",
AuthTypes: []cloud.AuthType{
cloud.CertificateAuthType,
},
Regions: []cloud.Region{{
Name: lxdnames.DefaultRemoteRegion,
Endpoint: remote.Addr,
}},
})
}
}
return clouds, nil
}
// DetectCloud implements environs.CloudDetector.
func (p *environProvider) DetectCloud(name string) (cloud.Cloud, error) {
clouds, err := p.DetectClouds()
if err != nil {
return cloud.Cloud{}, errors.Trace(err)
}
match := name
if match == lxdnames.DefaultCloudAltName {
match = lxdnames.DefaultCloud
}
for _, cloud := range clouds {
if cloud.Name == match {
return cloud, nil
}
}
return cloud.Cloud{}, errors.NotFoundf("cloud %s", name)
}
func (p *environProvider) detectCloud(name, path string) (cloud.Cloud, error) {
config, err := p.lxcConfigReader.ReadConfig(path)
if err != nil {
return cloud.Cloud{}, err
}
if remote, ok := config.Remotes[name]; ok {
return cloud.Cloud{
Name: name,
Type: lxdnames.ProviderType,
Endpoint: remote.Addr,
Description: cloud.DefaultCloudDescription(lxdnames.ProviderType),
AuthTypes: []cloud.AuthType{
cloud.CertificateAuthType,
},
Regions: []cloud.Region{{
Name: lxdnames.DefaultRemoteRegion,
Endpoint: remote.Addr,
}},
}, nil
}
return cloud.Cloud{}, errors.NotFoundf("cloud %s", name)
}
// FinalizeCloud is part of the environs.CloudFinalizer interface.
func (p *environProvider) FinalizeCloud(
ctx environs.FinalizeCloudContext,
in cloud.Cloud,
) (cloud.Cloud, error) {
var endpoint string
resolveEndpoint := func(name string, ep *string) error {
// If the name doesn't equal "localhost" then we shouldn't resolve
// the end point, instead we should just accept what we already have.
if name != lxdnames.DefaultCloud || *ep != "" {
return nil
}
if endpoint == "" {
// The cloud endpoint is empty, which means
// that we should connect to the local LXD.
hostAddress, err := p.getLocalHostAddress(ctx)
if err != nil {
return errors.Trace(err)
}
endpoint = hostAddress
}
*ep = endpoint
return nil
}
// If any of the endpoints point to localhost, go through and backfill the
// cloud spec with local host addresses.
if err := resolveEndpoint(in.Name, &in.Endpoint); err != nil {
return cloud.Cloud{}, errors.Trace(err)
}
for i, k := range in.Regions {
if err := resolveEndpoint(k.Name, &in.Regions[i].Endpoint); err != nil {
return cloud.Cloud{}, errors.Trace(err)
}
}
// If the provider type is not named localhost and there is no region, set the
// region to be a default region
if in.Name != lxdnames.DefaultCloud && len(in.Regions) == 0 {
in.Regions = append(in.Regions, cloud.Region{
Name: lxdnames.DefaultRemoteRegion,
Endpoint: in.Endpoint,
})
}
return in, nil
}
func (p *environProvider) getLocalHostAddress(ctx environs.FinalizeCloudContext) (string, error) {
svr, err := p.serverFactory.LocalServer()
if err != nil {
return "", errors.Trace(err)
}
bridgeName := svr.LocalBridgeName()
hostAddress, err := p.serverFactory.LocalServerAddress()
if err != nil {
return "", errors.Trace(err)
}
ctx.Verbosef(
"Resolved LXD host address on bridge %s: %s",
bridgeName, hostAddress,
)
return hostAddress, nil
}
// localhostCloud is the predefined "localhost" LXD cloud. We leave the
// endpoints empty to indicate that LXD is on the local host. When the
// cloud is finalized (see FinalizeCloud), we resolve the bridge address
// of the LXD host, and use that as the endpoint.
var localhostCloud = cloud.Cloud{
Name: lxdnames.DefaultCloud,
Type: lxdnames.ProviderType,
AuthTypes: []cloud.AuthType{
cloud.CertificateAuthType,
},
Endpoint: "",
Regions: []cloud.Region{{
Name: lxdnames.DefaultLocalRegion,
Endpoint: "",
}},
Description: cloud.DefaultCloudDescription(lxdnames.ProviderType),
}
// DetectRegions implements environs.CloudRegionDetector.
func (*environProvider) DetectRegions() ([]cloud.Region, error) {
// For now we just return a hard-coded "localhost" region,
// i.e. the local LXD daemon. We may later want to detect
// locally-configured remotes.
return []cloud.Region{{Name: lxdnames.DefaultLocalRegion}}, nil
}
// Schema returns the configuration schema for an environment.
func (*environProvider) Schema() environschema.Fields {
fields, err := config.Schema(configSchema)
if err != nil {
panic(err)
}
return fields
}
func (p *environProvider) validateCloudSpec(spec environscloudspec.CloudSpec) error {
if err := spec.Validate(); err != nil {
return errors.Trace(err)
}
if spec.Credential == nil {
return errors.NotValidf("missing credential")
}
// Always validate the spec.Endpoint, to ensure that it's valid.
if _, err := endpointURL(spec.Endpoint); err != nil {
return errors.Trace(err)
}
switch authType := spec.Credential.AuthType(); authType {
case cloud.CertificateAuthType:
if spec.Credential == nil {
return errors.NotFoundf("credentials")
}
if _, _, ok := getCertificates(*spec.Credential); !ok {
return errors.NotValidf("certificate credentials")
}
default:
return errors.NotSupportedf("%q auth-type", authType)
}
return nil
}
// ConfigSchema returns extra config attributes specific
// to this provider only.
func (p *environProvider) ConfigSchema() schema.Fields {
return configFields
}
// ConfigDefaults returns the default values for the
// provider specific config attributes.
func (p *environProvider) ConfigDefaults() schema.Defaults {
return configDefaults
}
// lxcConfigReader is the default implementation for reading files from disk.
type lxcConfigReader struct{}
func (lxcConfigReader) ReadConfig(path string) (LXCConfig, error) {
configFile, err := ioutil.ReadFile(path)
if err != nil {
if cause := errors.Cause(err); os.IsNotExist(cause) {
return LXCConfig{}, nil
}
return LXCConfig{}, errors.Trace(err)
}
var config LXCConfig
if err := yaml.Unmarshal(configFile, &config); err != nil {
return LXCConfig{}, errors.Trace(err)
}
return config, nil
}
func (lxcConfigReader) ReadCert(path string) ([]byte, error) {
certFile, err := ioutil.ReadFile(path)
return certFile, errors.Trace(err)
}