forked from canonical/lxd
/
storage_pools.go
658 lines (548 loc) · 19.5 KB
/
storage_pools.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"sync"
"github.com/gorilla/mux"
"github.com/pkg/errors"
lxd "github.com/lxc/lxd/client"
"github.com/lxc/lxd/lxd/cluster"
"github.com/lxc/lxd/lxd/cluster/request"
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/lxd/project"
"github.com/lxc/lxd/lxd/response"
storagePools "github.com/lxc/lxd/lxd/storage"
"github.com/lxc/lxd/lxd/util"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
log "github.com/lxc/lxd/shared/log15"
"github.com/lxc/lxd/shared/logger"
"github.com/lxc/lxd/shared/version"
)
// Lock to prevent concurent storage pools creation
var storagePoolCreateLock sync.Mutex
var storagePoolsCmd = APIEndpoint{
Path: "storage-pools",
Get: APIEndpointAction{Handler: storagePoolsGet, AccessHandler: allowAuthenticated},
Post: APIEndpointAction{Handler: storagePoolsPost},
}
var storagePoolCmd = APIEndpoint{
Path: "storage-pools/{name}",
Delete: APIEndpointAction{Handler: storagePoolDelete},
Get: APIEndpointAction{Handler: storagePoolGet, AccessHandler: allowAuthenticated},
Patch: APIEndpointAction{Handler: storagePoolPatch},
Put: APIEndpointAction{Handler: storagePoolPut},
}
// /1.0/storage-pools
// List all storage pools.
func storagePoolsGet(d *Daemon, r *http.Request) response.Response {
recursion := util.IsRecursionRequest(r)
pools, err := d.cluster.GetStoragePoolNames()
if err != nil && err != db.ErrNoSuchObject {
return response.SmartError(err)
}
resultString := []string{}
resultMap := []api.StoragePool{}
for _, pool := range pools {
if !recursion {
resultString = append(resultString, fmt.Sprintf("/%s/storage-pools/%s", version.APIVersion, pool))
} else {
_, pl, _, err := d.cluster.GetStoragePoolInAnyState(pool)
if err != nil {
continue
}
// Get all users of the storage pool.
poolUsedBy := []string{}
err = d.cluster.Transaction(func(tx *db.ClusterTx) error {
poolUsedBy, err = tx.GetStoragePoolUsedBy(pool)
return err
})
if err != nil {
return response.SmartError(err)
}
pl.UsedBy = project.FilterUsedBy(r, poolUsedBy)
resultMap = append(resultMap, *pl)
}
}
if !recursion {
return response.SyncResponse(true, resultString)
}
return response.SyncResponse(true, resultMap)
}
// /1.0/storage-pools
// Create a storage pool.
func storagePoolsPost(d *Daemon, r *http.Request) response.Response {
storagePoolCreateLock.Lock()
defer storagePoolCreateLock.Unlock()
req := api.StoragePoolsPost{}
// Parse the request.
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
return response.BadRequest(err)
}
// Sanity checks.
if req.Name == "" {
return response.BadRequest(fmt.Errorf("No name provided"))
}
if strings.Contains(req.Name, "/") {
return response.BadRequest(fmt.Errorf("Storage pool names may not contain slashes"))
}
if req.Driver == "" {
return response.BadRequest(fmt.Errorf("No driver provided"))
}
url := fmt.Sprintf("/%s/storage-pools/%s", version.APIVersion, req.Name)
resp := response.SyncResponseLocation(true, nil, url)
clientType := request.UserAgentClientType(r.Header.Get("User-Agent"))
if isClusterNotification(r) {
// This is an internal request which triggers the actual
// creation of the pool across all nodes, after they have been
// previously defined.
err = storagePoolValidate(req.Name, req.Driver, req.Config)
if err != nil {
return response.BadRequest(err)
}
poolID, err := d.cluster.GetStoragePoolID(req.Name)
if err != nil {
return response.NotFound(err)
}
_, err = storagePoolCreateLocal(d.State(), poolID, req, clientType)
if err != nil {
return response.SmartError(err)
}
return resp
}
targetNode := queryParam(r, "target")
if targetNode != "" {
// A targetNode was specified, let's just define the node's storage without actually creating it.
// The only legal key values for the storage config are the ones in StoragePoolNodeConfigKeys.
for key := range req.Config {
if !shared.StringInSlice(key, db.StoragePoolNodeConfigKeys) {
return response.SmartError(fmt.Errorf("Config key %q may not be used as node-specific key", key))
}
}
err = storagePoolValidate(req.Name, req.Driver, req.Config)
if err != nil {
return response.BadRequest(err)
}
err = d.cluster.Transaction(func(tx *db.ClusterTx) error {
return tx.CreatePendingStoragePool(targetNode, req.Name, req.Driver, req.Config)
})
if err != nil {
if err == db.ErrAlreadyDefined {
return response.BadRequest(fmt.Errorf("The storage pool already defined on node %q", targetNode))
}
return response.SmartError(err)
}
return resp
}
// Load existing pool if exists, if not don't fail.
_, pool, _, err := d.cluster.GetStoragePoolInAnyState(req.Name)
if err != nil && err != db.ErrNoSuchObject {
return response.InternalError(err)
}
// Check if we're clustered.
count, err := cluster.Count(d.State())
if err != nil {
return response.SmartError(err)
}
// No targetNode was specified and we're clustered or there is an existing partially created single node
// pool, either way finalize the config in the db and actually create the pool on all node in the cluster.
if count > 1 || (pool != nil && pool.Status != api.StoragePoolStatusCreated) {
err = storagePoolsPostCluster(d, pool, req, clientType)
if err != nil {
return response.InternalError(err)
}
return resp
}
// Create new single node storage pool.
err = storagePoolCreateGlobal(d.State(), req, clientType)
if err != nil {
return response.InternalError(err)
}
return resp
}
// storagePoolPartiallyCreated returns true of supplied storage pool has properties that indicate it has had
// previous create attempts run on it but failed on one or more nodes.
func storagePoolPartiallyCreated(pool *api.StoragePool) bool {
// If the pool status is StoragePoolStatusErrored, this means create has been run in the past and has
// failed on one or more nodes. Hence it is partially created.
if pool.Status == api.StoragePoolStatusErrored {
return true
}
// If the pool has global config keys, then it has previously been created by having its global config
// inserted, and this means it is partialled created.
for key := range pool.Config {
if !shared.StringInSlice(key, db.StoragePoolNodeConfigKeys) {
return true
}
}
return false
}
// storagePoolsPostCluster handles creating storage pools after the per-node config records have been created.
// Accepts an optional existing pool record, which will exist when performing subsequent re-create attempts.
func storagePoolsPostCluster(d *Daemon, pool *api.StoragePool, req api.StoragePoolsPost, clientType request.ClientType) error {
// Check that no node-specific config key has been defined.
for key := range req.Config {
if shared.StringInSlice(key, db.StoragePoolNodeConfigKeys) {
return fmt.Errorf("Config key %q is node-specific", key)
}
}
// Perform sanity checks if pool already exists.
if pool != nil {
// Check pool isn't already created.
if pool.Status == api.StoragePoolStatusCreated {
return fmt.Errorf("The storage pool is already created")
}
// Check the requested pool type matches the type created when adding the local node config.
if req.Driver != pool.Driver {
return fmt.Errorf("Requested storage pool driver %q doesn't match driver in existing database record %q", req.Driver, pool.Driver)
}
}
// Check that the pool is properly defined, fetch the node-specific configs and insert the global config.
var configs map[string]map[string]string
var nodeName string
var poolID int64
err := d.cluster.Transaction(func(tx *db.ClusterTx) error {
var err error
// Check that the pool was defined at all. Must come before partially created checks.
poolID, err = tx.GetStoragePoolID(req.Name)
if err != nil {
return err
}
// Check if any global config exists already, if so we should not create global config again.
if pool != nil && storagePoolPartiallyCreated(pool) {
if len(req.Config) > 0 {
return fmt.Errorf("Storage pool already partially created. Please do not specify any global config when re-running create")
}
logger.Debug("Skipping global storage pool create as global config already partially created", log.Ctx{"pool": req.Name})
return nil
}
// Fetch the node-specific configs and check the pool is defined for all nodes.
configs, err = tx.GetStoragePoolNodeConfigs(poolID)
if err != nil {
return err
}
// Take note of the name of this node
nodeName, err = tx.GetLocalNodeName()
if err != nil {
return err
}
// Insert the global config keys.
err = tx.CreateStoragePoolConfig(poolID, 0, req.Config)
if err != nil {
return err
}
// Assume failure unless we succeed later on.
return tx.StoragePoolErrored(req.Name)
})
if err != nil {
if err == db.ErrNoSuchObject {
return fmt.Errorf("Pool not pending on any node (use --target <node> first)")
}
return err
}
// Create notifier for other nodes to create the storage pool.
notifier, err := cluster.NewNotifier(d.State(), d.endpoints.NetworkCert(), cluster.NotifyAll)
if err != nil {
return err
}
// Create the pool on this node.
nodeReq := req
// Merge node specific config items into global config.
for key, value := range configs[nodeName] {
nodeReq.Config[key] = value
}
err = storagePoolValidate(req.Name, req.Driver, nodeReq.Config)
if err != nil {
return err
}
updatedConfig, err := storagePoolCreateLocal(d.State(), poolID, req, clientType)
if err != nil {
return err
}
req.Config = updatedConfig
logger.Debug("Created storage pool on local cluster member", log.Ctx{"pool": req.Name})
// Strip node specific config keys from config. Very important so we don't forward node-specific config.
for _, k := range db.StoragePoolNodeConfigKeys {
delete(req.Config, k)
}
// Notify all other nodes to create the pool.
err = notifier(func(client lxd.InstanceServer) error {
server, _, err := client.GetServer()
if err != nil {
return err
}
nodeReq := req
// Merge node specific config items into global config.
for key, value := range configs[server.Environment.ServerName] {
nodeReq.Config[key] = value
}
err = client.CreateStoragePool(nodeReq)
if err != nil {
return err
}
logger.Debug("Created storage pool on cluster member", log.Ctx{"pool": req.Name, "member": server.Environment.ServerName})
return nil
})
if err != nil {
return err
}
// Finally update the storage pool state.
err = d.cluster.Transaction(func(tx *db.ClusterTx) error {
return tx.StoragePoolCreated(req.Name)
})
if err != nil {
return err
}
logger.Debug("Marked storage pool global status as created", log.Ctx{"pool": req.Name})
return nil
}
// /1.0/storage-pools/{name}
// Get a single storage pool.
func storagePoolGet(d *Daemon, r *http.Request) response.Response {
// If a target was specified, forward the request to the relevant node.
resp := forwardedResponseIfTargetIsRemote(d, r)
if resp != nil {
return resp
}
poolName := mux.Vars(r)["name"]
// Get the existing storage pool.
_, pool, _, err := d.cluster.GetStoragePoolInAnyState(poolName)
if err != nil {
return response.SmartError(err)
}
// Get all users of the storage pool.
poolUsedBy := []string{}
err = d.cluster.Transaction(func(tx *db.ClusterTx) error {
poolUsedBy, err = tx.GetStoragePoolUsedBy(poolName)
return err
})
if err != nil {
return response.SmartError(err)
}
pool.UsedBy = project.FilterUsedBy(r, poolUsedBy)
targetNode := queryParam(r, "target")
clustered, err := cluster.Enabled(d.db)
if err != nil {
return response.SmartError(err)
}
// If no target node is specified and the daemon is clustered, we omit
// the node-specific fields.
if targetNode == "" && clustered {
for _, key := range db.StoragePoolNodeConfigKeys {
delete(pool.Config, key)
}
}
etag := []interface{}{pool.Name, pool.Driver, pool.Config}
return response.SyncResponseETag(true, &pool, etag)
}
// /1.0/storage-pools/{name}
// Replace pool properties.
func storagePoolPut(d *Daemon, r *http.Request) response.Response {
// If a target was specified, forward the request to the relevant node.
resp := forwardedResponseIfTargetIsRemote(d, r)
if resp != nil {
return resp
}
poolName := mux.Vars(r)["name"]
// Get the existing storage pool.
pool, err := storagePools.GetPoolByName(d.State(), poolName)
if err != nil {
return response.SmartError(err)
}
targetNode := queryParam(r, "target")
clustered, err := cluster.Enabled(d.db)
if err != nil {
return response.SmartError(err)
}
if targetNode == "" && pool.Status() != api.StoragePoolStatusCreated {
return response.BadRequest(fmt.Errorf("Cannot update storage pool global config when not in created state"))
}
// Duplicate config for etag modification and generation.
etagConfig := util.CopyConfig(pool.Driver().Config())
// If no target node is specified and the daemon is clustered, we omit the node-specific fields so that
// the e-tag can be generated correctly. This is because the GET request used to populate the request
// will also remove node-specific keys when no target is specified.
if targetNode == "" && clustered {
for _, key := range db.StoragePoolNodeConfigKeys {
delete(etagConfig, key)
}
}
// Validate the ETag.
etag := []interface{}{pool.Name(), pool.Driver().Info().Name, etagConfig}
err = util.EtagCheck(r, etag)
if err != nil {
return response.PreconditionFailed(err)
}
// Decode the request.
req := api.StoragePoolPut{}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return response.BadRequest(err)
}
// In clustered mode, we differentiate between node specific and non-node specific config keys based on
// whether the user has specified a target to apply the config to.
if clustered {
if targetNode == "" {
// If no target is specified, then ensure only non-node-specific config keys are changed.
for k := range req.Config {
if shared.StringInSlice(k, db.StoragePoolNodeConfigKeys) {
return response.BadRequest(fmt.Errorf("Config key %q is node-specific", k))
}
}
} else {
curConfig := pool.Driver().Config()
// If a target is specified, then ensure only node-specific config keys are changed.
for k, v := range req.Config {
if !shared.StringInSlice(k, db.StoragePoolNodeConfigKeys) && curConfig[k] != v {
return response.BadRequest(fmt.Errorf("Config key %q may not be used as node-specific key", k))
}
}
}
}
clientType := request.UserAgentClientType(r.Header.Get("User-Agent"))
return doStoragePoolUpdate(d, pool, req, targetNode, clientType, r.Method, clustered)
}
// /1.0/storage-pools/{name}
// Change pool properties.
func storagePoolPatch(d *Daemon, r *http.Request) response.Response {
return storagePoolPut(d, r)
}
// doStoragePoolUpdate takes the current local storage pool config, merges with the requested storage pool config,
// validates and applies the changes. Will also notify other cluster nodes of non-node specific config if needed.
func doStoragePoolUpdate(d *Daemon, pool storagePools.Pool, req api.StoragePoolPut, targetNode string, clientType request.ClientType, httpMethod string, clustered bool) response.Response {
if req.Config == nil {
req.Config = map[string]string{}
}
// Normally a "put" request will replace all existing config, however when clustered, we need to account
// for the node specific config keys and not replace them when the request doesn't specify a specific node.
if targetNode == "" && httpMethod != http.MethodPatch && clustered {
// If non-node specific config being updated via "put" method in cluster, then merge the current
// node-specific network config with the submitted config to allow validation.
// This allows removal of non-node specific keys when they are absent from request config.
for k, v := range pool.Driver().Config() {
if shared.StringInSlice(k, db.StoragePoolNodeConfigKeys) {
req.Config[k] = v
}
}
} else if httpMethod == http.MethodPatch {
// If config being updated via "patch" method, then merge all existing config with the keys that
// are present in the request config.
for k, v := range pool.Driver().Config() {
_, ok := req.Config[k]
if !ok {
req.Config[k] = v
}
}
}
// Validate the configuration.
err := storagePoolValidateConfig(pool.Name(), pool.Driver().Info().Name, req.Config, pool.Driver().Config())
if err != nil {
return response.BadRequest(err)
}
// Notify the other nodes, unless this is itself a notification.
if clustered && clientType != request.ClientTypeNotifier && targetNode == "" {
cert := d.endpoints.NetworkCert()
notifier, err := cluster.NewNotifier(d.State(), cert, cluster.NotifyAll)
if err != nil {
return response.SmartError(err)
}
sendPool := req
sendPool.Config = make(map[string]string)
for k, v := range req.Config {
// Don't forward node specific keys (these will be merged in on recipient node).
if shared.StringInSlice(k, db.StoragePoolNodeConfigKeys) {
continue
}
sendPool.Config[k] = v
}
err = notifier(func(client lxd.InstanceServer) error {
return client.UpdateStoragePool(pool.Name(), sendPool, "")
})
if err != nil {
return response.SmartError(err)
}
}
err = pool.Update(clientType, req.Description, req.Config, nil)
if err != nil {
return response.InternalError(err)
}
return response.EmptySyncResponse
}
// /1.0/storage-pools/{name}
// Delete storage pool.
func storagePoolDelete(d *Daemon, r *http.Request) response.Response {
poolName := mux.Vars(r)["name"]
pool, err := storagePools.GetPoolByName(d.State(), poolName)
if err != nil {
return response.SmartError(err)
}
clientType := request.UserAgentClientType(r.Header.Get("User-Agent"))
clusterNotification := isClusterNotification(r)
if !clusterNotification {
// Sanity checks.
inUse, err := pool.IsUsed()
if err != nil {
return response.SmartError(err)
}
if inUse {
return response.BadRequest(fmt.Errorf("The storage pool is currently in use"))
}
}
// Only delete images if locally stored or running on initial member.
if !clusterNotification || !pool.Driver().Info().Remote {
// Only image volumes should remain now.
volumeNames, err := d.cluster.GetStoragePoolVolumesNames(pool.ID())
if err != nil {
return response.InternalError(err)
}
for _, volume := range volumeNames {
_, imgInfo, err := d.cluster.GetImage(projectParam(r), volume, false)
if err != nil {
return response.InternalError(errors.Wrapf(err, "Failed getting image info for %q", volume))
}
err = doDeleteImageFromPool(d.State(), imgInfo.Fingerprint, pool.Name())
if err != nil {
return response.InternalError(errors.Wrapf(err, "Error deleting image %q from pool", volume))
}
}
}
if pool.LocalStatus() != api.StoragePoolStatusPending {
err = pool.Delete(clientType, nil)
if err != nil {
return response.InternalError(err)
}
}
// If this is a cluster notification, we're done, any database work will be done by the node that is
// originally serving the request.
if clusterNotification {
return response.EmptySyncResponse
}
// If we are clustered, also notify all other nodes, if any.
clustered, err := cluster.Enabled(d.db)
if err != nil {
return response.SmartError(err)
}
if clustered {
notifier, err := cluster.NewNotifier(d.State(), d.endpoints.NetworkCert(), cluster.NotifyAll)
if err != nil {
return response.SmartError(err)
}
err = notifier(func(client lxd.InstanceServer) error {
_, _, err := client.GetServer()
if err != nil {
return err
}
return client.DeleteStoragePool(pool.Name())
})
if err != nil {
return response.SmartError(err)
}
}
err = dbStoragePoolDeleteAndUpdateCache(d.State(), pool.Name())
if err != nil {
return response.SmartError(err)
}
return response.EmptySyncResponse
}