-
Notifications
You must be signed in to change notification settings - Fork 907
/
init.go
483 lines (404 loc) · 14.1 KB
/
init.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package main
import (
"fmt"
"github.com/canonical/lxd/client"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/revert"
)
// Helper to initialize node-specific entities on a LXD instance using the
// definitions from the given api.InitLocalPreseed object.
//
// It's used both by the 'lxd init' command and by the PUT /1.0/cluster API.
//
// In case of error, the returned function can be used to revert the changes.
func initDataNodeApply(d lxd.InstanceServer, config api.InitLocalPreseed) (func(), error) {
revert := revert.New()
defer revert.Fail()
// Apply server configuration.
if config.Config != nil && len(config.Config) > 0 {
// Get current config.
currentServer, etag, err := d.GetServer()
if err != nil {
return nil, fmt.Errorf("Failed to retrieve current server configuration: %w", err)
}
// Setup reverter.
revert.Add(func() { _ = d.UpdateServer(currentServer.Writable(), "") })
// Prepare the update.
newServer := api.ServerPut{}
err = shared.DeepCopy(currentServer.Writable(), &newServer)
if err != nil {
return nil, fmt.Errorf("Failed to copy server configuration: %w", err)
}
for k, v := range config.Config {
newServer.Config[k] = fmt.Sprintf("%v", v)
}
// Apply it.
err = d.UpdateServer(newServer, etag)
if err != nil {
return nil, fmt.Errorf("Failed to update server configuration: %w", err)
}
}
// Apply storage configuration.
if config.StoragePools != nil && len(config.StoragePools) > 0 {
// Get the list of storagePools.
storagePoolNames, err := d.GetStoragePoolNames()
if err != nil {
return nil, fmt.Errorf("Failed to retrieve list of storage pools: %w", err)
}
// StoragePool creator
createStoragePool := func(storagePool api.StoragePoolsPost) error {
// Create the storagePool if doesn't exist.
err := d.CreateStoragePool(storagePool)
if err != nil {
return fmt.Errorf("Failed to create storage pool %q: %w", storagePool.Name, err)
}
// Setup reverter.
revert.Add(func() { _ = d.DeleteStoragePool(storagePool.Name) })
return nil
}
// StoragePool updater.
updateStoragePool := func(storagePool api.StoragePoolsPost) error {
// Get the current storagePool.
currentStoragePool, etag, err := d.GetStoragePool(storagePool.Name)
if err != nil {
return fmt.Errorf("Failed to retrieve current storage pool %q: %w", storagePool.Name, err)
}
// Quick check.
if currentStoragePool.Driver != storagePool.Driver {
return fmt.Errorf("Storage pool %q is of type %q instead of %q", currentStoragePool.Name, currentStoragePool.Driver, storagePool.Driver)
}
// Setup reverter.
revert.Add(func() { _ = d.UpdateStoragePool(currentStoragePool.Name, currentStoragePool.Writable(), "") })
// Prepare the update.
newStoragePool := api.StoragePoolPut{}
err = shared.DeepCopy(currentStoragePool.Writable(), &newStoragePool)
if err != nil {
return fmt.Errorf("Failed to copy configuration of storage pool %q: %w", storagePool.Name, err)
}
// Description override.
if storagePool.Description != "" {
newStoragePool.Description = storagePool.Description
}
// Config overrides.
for k, v := range storagePool.Config {
newStoragePool.Config[k] = fmt.Sprintf("%v", v)
}
// Apply it.
err = d.UpdateStoragePool(currentStoragePool.Name, newStoragePool, etag)
if err != nil {
return fmt.Errorf("Failed to update storage pool %q: %w", storagePool.Name, err)
}
return nil
}
for _, storagePool := range config.StoragePools {
// New storagePool.
if !shared.ValueInSlice(storagePool.Name, storagePoolNames) {
err := createStoragePool(storagePool)
if err != nil {
return nil, err
}
continue
}
// Existing storagePool.
err := updateStoragePool(storagePool)
if err != nil {
return nil, err
}
}
}
// Apply network configuration function.
applyNetwork := func(network api.InitNetworksProjectPost) error {
currentNetwork, etag, err := d.UseProject(network.Project).GetNetwork(network.Name)
if err != nil {
// Create the network if doesn't exist.
err := d.UseProject(network.Project).CreateNetwork(network.NetworksPost)
if err != nil {
return fmt.Errorf("Failed to create local member network %q in project %q: %w", network.Name, network.Project, err)
}
// Setup reverter.
revert.Add(func() { _ = d.UseProject(network.Project).DeleteNetwork(network.Name) })
} else {
// Prepare the update.
newNetwork := api.NetworkPut{}
err = shared.DeepCopy(currentNetwork.Writable(), &newNetwork)
if err != nil {
return fmt.Errorf("Failed to copy configuration of network %q in project %q: %w", network.Name, network.Project, err)
}
// Description override.
if network.Description != "" {
newNetwork.Description = network.Description
}
// Config overrides.
for k, v := range network.Config {
newNetwork.Config[k] = fmt.Sprintf("%v", v)
}
// Apply it.
err = d.UseProject(network.Project).UpdateNetwork(currentNetwork.Name, newNetwork, etag)
if err != nil {
return fmt.Errorf("Failed to update local member network %q in project %q: %w", network.Name, network.Project, err)
}
// Setup reverter.
revert.Add(func() {
_ = d.UseProject(network.Project).UpdateNetwork(currentNetwork.Name, currentNetwork.Writable(), "")
})
}
return nil
}
// Apply networks in the default project before other projects config applied (so that if the projects
// depend on a network in the default project they can have their config applied successfully).
for i := range config.Networks {
// Populate default project if not specified for backwards compatbility with earlier
// preseed dump files.
if config.Networks[i].Project == "" {
config.Networks[i].Project = api.ProjectDefaultName
}
if config.Networks[i].Project != api.ProjectDefaultName {
continue
}
err := applyNetwork(config.Networks[i])
if err != nil {
return nil, err
}
}
// Apply project configuration.
if config.Projects != nil && len(config.Projects) > 0 {
// Get the list of projects.
projectNames, err := d.GetProjectNames()
if err != nil {
return nil, fmt.Errorf("Failed to retrieve list of projects: %w", err)
}
// Project creator.
createProject := func(project api.ProjectsPost) error {
// Create the project if doesn't exist.
err := d.CreateProject(project)
if err != nil {
return fmt.Errorf("Failed to create local member project %q: %w", project.Name, err)
}
// Setup reverter.
revert.Add(func() { _ = d.DeleteProject(project.Name) })
return nil
}
// Project updater.
updateProject := func(project api.ProjectsPost) error {
// Get the current project.
currentProject, etag, err := d.GetProject(project.Name)
if err != nil {
return fmt.Errorf("Failed to retrieve current project %q: %w", project.Name, err)
}
// Setup reverter.
revert.Add(func() { _ = d.UpdateProject(currentProject.Name, currentProject.Writable(), "") })
// Prepare the update.
newProject := api.ProjectPut{}
err = shared.DeepCopy(currentProject.Writable(), &newProject)
if err != nil {
return fmt.Errorf("Failed to copy configuration of project %q: %w", project.Name, err)
}
// Description override.
if project.Description != "" {
newProject.Description = project.Description
}
// Config overrides.
for k, v := range project.Config {
newProject.Config[k] = fmt.Sprintf("%v", v)
}
// Apply it.
err = d.UpdateProject(currentProject.Name, newProject, etag)
if err != nil {
return fmt.Errorf("Failed to update local member project %q: %w", project.Name, err)
}
return nil
}
for _, project := range config.Projects {
// New project.
if !shared.ValueInSlice(project.Name, projectNames) {
err := createProject(project)
if err != nil {
return nil, err
}
continue
}
// Existing project.
err := updateProject(project)
if err != nil {
return nil, err
}
}
}
// Apply networks in non-default projects after project config applied (so that their projects exist).
for i := range config.Networks {
if config.Networks[i].Project == api.ProjectDefaultName {
continue
}
err := applyNetwork(config.Networks[i])
if err != nil {
return nil, err
}
}
// Apply storage volumes configuration.
applyStorageVolume := func(storageVolume api.InitStorageVolumesProjectPost) error {
// Get the current storageVolume.
currentStorageVolume, etag, err := d.UseProject(storageVolume.Project).GetStoragePoolVolume(storageVolume.Pool, storageVolume.Type, storageVolume.Name)
if err != nil {
// Create the storage volume if it doesn't exist.
err := d.UseProject(storageVolume.Project).CreateStoragePoolVolume(storageVolume.Pool, storageVolume.StorageVolumesPost)
if err != nil {
return fmt.Errorf("Failed to create storage volume %q in project %q on pool %q: %w", storageVolume.Name, storageVolume.Project, storageVolume.Pool, err)
}
// Setup reverter.
revert.Add(func() {
_ = d.UseProject(storageVolume.Project).DeleteStoragePoolVolume(storageVolume.Pool, storageVolume.Type, storageVolume.Name)
})
} else {
// Quick check.
if currentStorageVolume.Type != storageVolume.Type {
return fmt.Errorf("Storage volume %q in project %q is of type %q instead of %q", currentStorageVolume.Name, storageVolume.Project, currentStorageVolume.Type, storageVolume.Type)
}
// Setup reverter.
revert.Add(func() {
_ = d.UseProject(storageVolume.Project).UpdateStoragePoolVolume(storageVolume.Pool, currentStorageVolume.Type, currentStorageVolume.Name, currentStorageVolume.Writable(), "")
})
// Prepare the update.
newStorageVolume := api.StorageVolumePut{}
err = shared.DeepCopy(currentStorageVolume.Writable(), &newStorageVolume)
if err != nil {
return fmt.Errorf("Failed to copy configuration of storage volume %q in project %q: %w", storageVolume.Name, storageVolume.Project, err)
}
// Description override.
if storageVolume.Description != "" {
newStorageVolume.Description = storageVolume.Description
}
// Config overrides.
for k, v := range storageVolume.Config {
newStorageVolume.Config[k] = fmt.Sprintf("%v", v)
}
// Apply it.
err = d.UseProject(storageVolume.Project).UpdateStoragePoolVolume(storageVolume.Pool, storageVolume.Type, currentStorageVolume.Name, newStorageVolume, etag)
if err != nil {
return fmt.Errorf("Failed to update storage volume %q in project %q: %w", storageVolume.Name, storageVolume.Project, err)
}
}
return nil
}
// Apply storage volumes in the default project before other projects config.
for i := range config.StorageVolumes {
// Populate default project if not specified.
if config.StorageVolumes[i].Project == "" {
config.StorageVolumes[i].Project = api.ProjectDefaultName
}
// Populate default type if not specified.
if config.StorageVolumes[i].Type == "" {
config.StorageVolumes[i].Type = "custom"
}
err := applyStorageVolume(config.StorageVolumes[i])
if err != nil {
return nil, err
}
}
// Apply profile configuration.
if config.Profiles != nil && len(config.Profiles) > 0 {
// Get the list of profiles.
profileNames, err := d.GetProfileNames()
if err != nil {
return nil, fmt.Errorf("Failed to retrieve list of profiles: %w", err)
}
// Profile creator.
createProfile := func(profile api.ProfilesPost) error {
// Create the profile if doesn't exist.
err := d.CreateProfile(profile)
if err != nil {
return fmt.Errorf("Failed to create profile %q: %w", profile.Name, err)
}
// Setup reverter.
revert.Add(func() { _ = d.DeleteProfile(profile.Name) })
return nil
}
// Profile updater.
updateProfile := func(profile api.ProfilesPost) error {
// Get the current profile.
currentProfile, etag, err := d.GetProfile(profile.Name)
if err != nil {
return fmt.Errorf("Failed to retrieve current profile %q: %w", profile.Name, err)
}
// Setup reverter.
revert.Add(func() { _ = d.UpdateProfile(currentProfile.Name, currentProfile.Writable(), "") })
// Prepare the update.
newProfile := api.ProfilePut{}
err = shared.DeepCopy(currentProfile.Writable(), &newProfile)
if err != nil {
return fmt.Errorf("Failed to copy configuration of profile %q: %w", profile.Name, err)
}
// Description override.
if profile.Description != "" {
newProfile.Description = profile.Description
}
// Config overrides.
for k, v := range profile.Config {
newProfile.Config[k] = fmt.Sprintf("%v", v)
}
// Device overrides.
for k, v := range profile.Devices {
// New device.
_, ok := newProfile.Devices[k]
if !ok {
newProfile.Devices[k] = v
continue
}
// Existing device.
for configKey, configValue := range v {
newProfile.Devices[k][configKey] = fmt.Sprintf("%v", configValue)
}
}
// Apply it.
err = d.UpdateProfile(currentProfile.Name, newProfile, etag)
if err != nil {
return fmt.Errorf("Failed to update profile %q: %w", profile.Name, err)
}
return nil
}
for _, profile := range config.Profiles {
// New profile.
if !shared.ValueInSlice(profile.Name, profileNames) {
err := createProfile(profile)
if err != nil {
return nil, err
}
continue
}
// Existing profile.
err := updateProfile(profile)
if err != nil {
return nil, err
}
}
}
cleanup := revert.Clone().Fail // Clone before calling revert.Success() so we can return the Fail func.
revert.Success()
return cleanup, nil
}
// Helper to initialize LXD clustering.
//
// Used by the 'lxd init' command.
func initDataClusterApply(d lxd.InstanceServer, config *api.InitClusterPreseed) error {
if config == nil || !config.Enabled {
return nil
}
// Get the current cluster configuration
currentCluster, etag, err := d.GetCluster()
if err != nil {
return fmt.Errorf("Failed to retrieve current cluster config: %w", err)
}
// Check if already enabled
if !currentCluster.Enabled {
// Configure the cluster
op, err := d.UpdateCluster(config.ClusterPut, etag)
if err != nil {
return fmt.Errorf("Failed to configure cluster: %w", err)
}
err = op.Wait()
if err != nil {
return fmt.Errorf("Failed to configure cluster: %w", err)
}
}
return nil
}