forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environ.go
446 lines (388 loc) · 12.6 KB
/
environ.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package lxd
import (
stdcontext "context"
"strings"
"sync"
"github.com/juju/errors"
"github.com/lxc/lxd/shared/api"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/core/lxdprofile"
"github.com/juju/juju/core/network"
"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/environs/tags"
"github.com/juju/juju/provider/common"
)
const bootstrapMessage = `To configure your system to better support LXD containers, please see: https://github.com/lxc/lxd/blob/master/doc/production-setup.md`
type baseProvider interface {
// BootstrapEnv bootstraps a Juju environment.
BootstrapEnv(environs.BootstrapContext, context.ProviderCallContext, environs.BootstrapParams) (*environs.BootstrapResult, error)
// DestroyEnv destroys the provided Juju environment.
DestroyEnv(ctx context.ProviderCallContext) error
}
type environ struct {
environs.NoSpaceDiscoveryEnviron
cloud environscloudspec.CloudSpec
provider *environProvider
name string
uuid string
base baseProvider
// namespace is used to create the machine and device hostnames.
namespace instance.Namespace
// lock protects the *Unlocked fields below.
lock sync.Mutex
ecfgUnlocked *environConfig
serverUnlocked Server
// profileMutex is used when writing profiles via the server.
profileMutex sync.Mutex
}
func newEnviron(
p *environProvider,
spec environscloudspec.CloudSpec,
cfg *config.Config,
) (*environ, error) {
ecfg, err := newValidConfig(cfg)
if err != nil {
return nil, errors.Annotate(err, "invalid config")
}
namespace, err := instance.NewNamespace(cfg.UUID())
if err != nil {
return nil, errors.Trace(err)
}
env := &environ{
provider: p,
cloud: spec,
name: ecfg.Name(),
uuid: ecfg.UUID(),
namespace: namespace,
ecfgUnlocked: ecfg,
}
env.base = common.DefaultProvider{Env: env}
err = env.SetCloudSpec(stdcontext.TODO(), spec)
if err != nil {
return nil, errors.Trace(err)
}
return env, nil
}
func (env *environ) initProfile() error {
pName := env.profileName()
hasProfile, err := env.serverUnlocked.HasProfile(pName)
if err != nil {
return errors.Trace(err)
}
if hasProfile {
return nil
}
cfg := map[string]string{
"boot.autostart": "true",
"security.nesting": "true",
}
// In ci, perhaps other places, there can be a race if more than one
// controller is starting up, where we try to create the profile more
// than once and get: The profile already exists. LXD does not have
// typed errors. Therefore if CreateProfile fails, check to see if the
// profile exists. No need to fail if it does.
err = env.serverUnlocked.CreateProfileWithConfig(pName, cfg)
if err == nil {
return nil
}
hasProfile, hasErr := env.serverUnlocked.HasProfile(pName)
if hasErr != nil {
logger.Errorf("%s", err)
return errors.Trace(hasErr)
}
if hasProfile {
logger.Debugf("received %q, but no need to fail", err)
return nil
}
return err
}
func (env *environ) profileName() string {
return "juju-" + env.Name()
}
// Name returns the name of the environ.
func (env *environ) Name() string {
return env.name
}
// Provider returns the provider that created this environ.
func (env *environ) Provider() environs.EnvironProvider {
return env.provider
}
// SetConfig updates the environ's configuration.
func (env *environ) SetConfig(cfg *config.Config) error {
env.lock.Lock()
defer env.lock.Unlock()
ecfg, err := newValidConfig(cfg)
if err != nil {
return errors.Trace(err)
}
env.ecfgUnlocked = ecfg
return nil
}
// SetCloudSpec is specified in the environs.Environ interface.
func (env *environ) SetCloudSpec(_ stdcontext.Context, spec environscloudspec.CloudSpec) error {
env.lock.Lock()
defer env.lock.Unlock()
serverFactory := env.provider.serverFactory
server, err := serverFactory.RemoteServer(spec)
if err != nil {
return errors.Trace(err)
}
if project := env.ecfgUnlocked.project(); project != "" {
server.UseProject(project)
}
env.serverUnlocked = server
return env.initProfile()
}
func (env *environ) server() Server {
env.lock.Lock()
defer env.lock.Unlock()
return env.serverUnlocked
}
// Config returns the configuration data with which the env was created.
func (env *environ) Config() *config.Config {
env.lock.Lock()
defer env.lock.Unlock()
cfg := env.ecfgUnlocked.Config
return cfg
}
// PrepareForBootstrap implements environs.Environ.
func (env *environ) PrepareForBootstrap(_ environs.BootstrapContext, _ string) error {
return nil
}
// Create implements environs.Environ.
func (env *environ) Create(context.ProviderCallContext, environs.CreateParams) error {
return nil
}
// Bootstrap implements environs.Environ.
func (env *environ) Bootstrap(ctx environs.BootstrapContext, callCtx context.ProviderCallContext, params environs.BootstrapParams) (*environs.BootstrapResult, error) {
ctx.Infof("%s", bootstrapMessage)
return env.base.BootstrapEnv(ctx, callCtx, params)
}
// Destroy shuts down all known machines and destroys the rest of the
// known environment.
func (env *environ) Destroy(ctx context.ProviderCallContext) error {
if err := env.base.DestroyEnv(ctx); err != nil {
common.HandleCredentialError(IsAuthorisationFailure, err, ctx)
return errors.Trace(err)
}
if env.storageSupported() {
if err := destroyModelFilesystems(env); err != nil {
common.HandleCredentialError(IsAuthorisationFailure, err, ctx)
return errors.Annotate(err, "destroying LXD filesystems for model")
}
}
return nil
}
// DestroyController implements the Environ interface.
func (env *environ) DestroyController(ctx context.ProviderCallContext, controllerUUID string) error {
if err := env.Destroy(ctx); err != nil {
return errors.Trace(err)
}
if err := env.destroyHostedModelResources(controllerUUID); err != nil {
common.HandleCredentialError(IsAuthorisationFailure, err, ctx)
return errors.Trace(err)
}
if env.storageSupported() {
if err := destroyControllerFilesystems(env, controllerUUID); err != nil {
common.HandleCredentialError(IsAuthorisationFailure, err, ctx)
return errors.Annotate(err, "destroying LXD filesystems for controller")
}
}
return nil
}
func (env *environ) destroyHostedModelResources(controllerUUID string) error {
// Destroy all instances with juju-controller-uuid
// matching the specified UUID.
const prefix = "juju-"
instances, err := env.prefixedInstances(prefix)
if err != nil {
return errors.Annotate(err, "listing instances")
}
var names []string
for _, inst := range instances {
if inst.container.Metadata(tags.JujuModel) == env.uuid {
continue
}
if inst.container.Metadata(tags.JujuController) != controllerUUID {
continue
}
names = append(names, string(inst.Id()))
}
logger.Debugf("removing instances: %v", names)
return errors.Trace(env.server().RemoveContainers(names))
}
// lxdAvailabilityZone wraps a LXD cluster member as an availability zone.
type lxdAvailabilityZone struct {
api.ClusterMember
}
// Name implements AvailabilityZone.
func (z *lxdAvailabilityZone) Name() string {
return z.ServerName
}
// Available implements AvailabilityZone.
func (z *lxdAvailabilityZone) Available() bool {
return strings.ToLower(z.Status) == "online"
}
// AvailabilityZones (ZonedEnviron) returns all availability zones in the
// environment. For LXD, this means the cluster node names.
func (env *environ) AvailabilityZones(ctx context.ProviderCallContext) (network.AvailabilityZones, error) {
// If we are not using a clustered server (which includes those not
// supporting the clustering API) just represent the single server as the
// only availability zone.
server := env.server()
if !server.IsClustered() {
return network.AvailabilityZones{
&lxdAvailabilityZone{
ClusterMember: api.ClusterMember{
ServerName: server.Name(),
Status: "ONLINE",
},
},
}, nil
}
nodes, err := server.GetClusterMembers()
if err != nil {
common.HandleCredentialError(IsAuthorisationFailure, err, ctx)
return nil, errors.Annotate(err, "listing cluster members")
}
aZones := make(network.AvailabilityZones, len(nodes))
for i, n := range nodes {
aZones[i] = &lxdAvailabilityZone{n}
}
return aZones, nil
}
// InstanceAvailabilityZoneNames (ZonedEnviron) returns the names of the
// availability zones for the specified instances.
// For containers, this means the LXD server node names where they reside.
func (env *environ) InstanceAvailabilityZoneNames(
ctx context.ProviderCallContext, ids []instance.Id,
) (map[instance.Id]string, error) {
instances, err := env.Instances(ctx, ids)
if err != nil && err != environs.ErrPartialInstances {
return nil, err
}
// If not clustered, just report all input IDs as being in the zone
// represented by the single server.
server := env.server()
if !server.IsClustered() {
zones := make(map[instance.Id]string, len(ids))
n := server.Name()
for _, id := range ids {
zones[id] = n
}
return zones, nil
}
zones := make(map[instance.Id]string, len(instances))
for _, ins := range instances {
if ei, ok := ins.(*environInstance); ok {
zones[ins.Id()] = ei.container.Location
}
}
return zones, nil
}
// DeriveAvailabilityZones (ZonedEnviron) attempts to derive availability zones
// from the specified StartInstanceParams.
func (env *environ) DeriveAvailabilityZones(
ctx context.ProviderCallContext, args environs.StartInstanceParams,
) ([]string, error) {
p, err := env.parsePlacement(ctx, args.Placement)
if err != nil {
return nil, errors.Trace(err)
}
if p.nodeName == "" {
return nil, nil
}
return []string{p.nodeName}, nil
}
// TODO: HML 2-apr-2019
// When provisioner_task processProfileChanges() is
// removed, maybe change to take an lxdprofile.ProfilePost as
// an arg.
// MaybeWriteLXDProfile implements environs.LXDProfiler.
func (env *environ) MaybeWriteLXDProfile(pName string, put lxdprofile.Profile) error {
env.profileMutex.Lock()
defer env.profileMutex.Unlock()
server := env.server()
hasProfile, err := server.HasProfile(pName)
if err != nil {
return errors.Trace(err)
}
if hasProfile {
logger.Debugf("lxd profile %q already exists, not written again", pName)
return nil
}
logger.Debugf("attempting to write lxd profile %q %+v", pName, put)
post := api.ProfilesPost{
Name: pName,
ProfilePut: api.ProfilePut{
Description: put.Description,
Config: put.Config,
Devices: put.Devices,
},
}
if err = server.CreateProfile(post); err != nil {
return errors.Trace(err)
}
logger.Debugf("wrote lxd profile %q", pName)
if err := env.verifyProfile(pName); err != nil {
return errors.Trace(err)
}
return nil
}
// verifyProfile gets the actual profile from lxd for the name provided
// and logs the result. For informational purposes only. Returns an error
// if the call to GetProfile fails.
func (env *environ) verifyProfile(pName string) error {
// As there are configs where we do not have the option of looking at
// the profile on the machine to verify, verify here that what we thought
// was written, is what was written.
profile, _, err := env.server().GetProfile(pName)
if err != nil {
return err
}
logger.Debugf("lxd profile %q: received %+v ", pName, profile.ProfilePut)
return nil
}
// LXDProfileNames implements environs.LXDProfiler.
func (env *environ) LXDProfileNames(containerName string) ([]string, error) {
return env.server().GetContainerProfiles(containerName)
}
// AssignLXDProfiles implements environs.LXDProfiler.
func (env *environ) AssignLXDProfiles(instID string, profilesNames []string, profilePosts []lxdprofile.ProfilePost) (current []string, err error) {
report := func(err error) ([]string, error) {
// Always return the current profiles assigned to the instance.
currentProfiles, err2 := env.LXDProfileNames(instID)
if err != nil && err2 != nil {
logger.Errorf("retrieving profile names for %q: %s", instID, err2)
}
return currentProfiles, err
}
// Write any new profilePosts and gather a slice of profile
// names to be deleted, after removal.
var deleteProfiles []string
for _, p := range profilePosts {
if p.Profile != nil {
if err := env.MaybeWriteLXDProfile(p.Name, *p.Profile); err != nil {
return report(err)
}
} else {
deleteProfiles = append(deleteProfiles, p.Name)
}
}
server := env.server()
if err := server.UpdateContainerProfiles(instID, profilesNames); err != nil {
return report(errors.Trace(err))
}
for _, name := range deleteProfiles {
if err := server.DeleteProfile(name); err != nil {
// most likely the failure is because the profile is already in use
logger.Debugf("failed to delete profile %q: %s", name, err)
}
}
return report(nil)
}