forked from canonical/lxd
/
images.go
657 lines (543 loc) · 16.3 KB
/
images.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
package db
import (
"database/sql"
"fmt"
"time"
"github.com/lxc/lxd/lxd/db/query"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/osarch"
)
var ImageSourceProtocol = map[int]string{
0: "lxd",
1: "direct",
2: "simplestreams",
}
func (c *Cluster) ImagesGet(public bool) ([]string, error) {
q := "SELECT fingerprint FROM images"
if public == true {
q = "SELECT fingerprint FROM images WHERE public=1"
}
var fp string
inargs := []interface{}{}
outfmt := []interface{}{fp}
dbResults, err := queryScan(c.db, q, inargs, outfmt)
if err != nil {
return []string{}, err
}
results := []string{}
for _, r := range dbResults {
results = append(results, r[0].(string))
}
return results, nil
}
func (c *Cluster) ImagesGetExpired(expiry int64) ([]string, error) {
q := `SELECT fingerprint, last_use_date, upload_date FROM images WHERE cached=1`
var fpStr string
var useStr string
var uploadStr string
inargs := []interface{}{}
outfmt := []interface{}{fpStr, useStr, uploadStr}
dbResults, err := queryScan(c.db, q, inargs, outfmt)
if err != nil {
return []string{}, err
}
results := []string{}
for _, r := range dbResults {
// Figure out the expiry
timestamp := r[2]
if r[1] != "" {
timestamp = r[1]
}
var imageExpiry time.Time
err = imageExpiry.UnmarshalText([]byte(timestamp.(string)))
if err != nil {
return []string{}, err
}
imageExpiry = imageExpiry.Add(time.Duration(expiry*24) * time.Hour)
// Check if expired
if imageExpiry.After(time.Now()) {
continue
}
results = append(results, r[0].(string))
}
return results, nil
}
func (c *Cluster) ImageSourceInsert(imageId int, server string, protocol string, certificate string, alias string) error {
stmt := `INSERT INTO images_source (image_id, server, protocol, certificate, alias) values (?, ?, ?, ?, ?)`
protocolInt := -1
for protoInt, protoString := range ImageSourceProtocol {
if protoString == protocol {
protocolInt = protoInt
}
}
if protocolInt == -1 {
return fmt.Errorf("Invalid protocol: %s", protocol)
}
_, err := exec(c.db, stmt, imageId, server, protocolInt, certificate, alias)
return err
}
func (c *Cluster) ImageSourceGet(imageId int) (int, api.ImageSource, error) {
q := `SELECT id, server, protocol, certificate, alias FROM images_source WHERE image_id=?`
id := 0
protocolInt := -1
result := api.ImageSource{}
arg1 := []interface{}{imageId}
arg2 := []interface{}{&id, &result.Server, &protocolInt, &result.Certificate, &result.Alias}
err := dbQueryRowScan(c.db, q, arg1, arg2)
if err != nil {
if err == sql.ErrNoRows {
return -1, api.ImageSource{}, NoSuchObjectError
}
return -1, api.ImageSource{}, err
}
protocol, found := ImageSourceProtocol[protocolInt]
if !found {
return -1, api.ImageSource{}, fmt.Errorf("Invalid protocol: %d", protocolInt)
}
result.Protocol = protocol
return id, result, nil
}
// Try to find a source entry of a locally cached image that matches
// the given remote details (server, protocol and alias). Return the
// fingerprint linked to the matching entry, if any.
func (c *Cluster) ImageSourceGetCachedFingerprint(server string, protocol string, alias string) (string, error) {
protocolInt := -1
for protoInt, protoString := range ImageSourceProtocol {
if protoString == protocol {
protocolInt = protoInt
}
}
if protocolInt == -1 {
return "", fmt.Errorf("Invalid protocol: %s", protocol)
}
q := `SELECT images.fingerprint
FROM images_source
INNER JOIN images
ON images_source.image_id=images.id
WHERE server=? AND protocol=? AND alias=? AND auto_update=1
ORDER BY creation_date DESC`
fingerprint := ""
arg1 := []interface{}{server, protocolInt, alias}
arg2 := []interface{}{&fingerprint}
err := dbQueryRowScan(c.db, q, arg1, arg2)
if err != nil {
if err == sql.ErrNoRows {
return "", NoSuchObjectError
}
return "", err
}
return fingerprint, nil
}
// Whether an image with the given fingerprint exists.
func (c *Cluster) ImageExists(fingerprint string) (bool, error) {
var exists bool
var err error
query := "SELECT COUNT(*) > 0 FROM images WHERE fingerprint=?"
inargs := []interface{}{fingerprint}
outargs := []interface{}{&exists}
err = dbQueryRowScan(c.db, query, inargs, outargs)
return exists, err
}
// ImageGet gets an Image object from the database.
// If strictMatching is false, The fingerprint argument will be queried with a LIKE query, means you can
// pass a shortform and will get the full fingerprint.
// There can never be more than one image with a given fingerprint, as it is
// enforced by a UNIQUE constraint in the schema.
func (c *Cluster) ImageGet(fingerprint string, public bool, strictMatching bool) (int, *api.Image, error) {
var err error
var create, expire, used, upload *time.Time // These hold the db-returned times
// The object we'll actually return
image := api.Image{}
id := -1
arch := -1
// These two humongous things will be filled by the call to DbQueryRowScan
outfmt := []interface{}{&id, &image.Fingerprint, &image.Filename,
&image.Size, &image.Cached, &image.Public, &image.AutoUpdate, &arch,
&create, &expire, &used, &upload}
var inargs []interface{}
query := `
SELECT
id, fingerprint, filename, size, cached, public, auto_update, architecture,
creation_date, expiry_date, last_use_date, upload_date
FROM images`
if strictMatching {
inargs = []interface{}{fingerprint}
query += " WHERE fingerprint = ?"
} else {
inargs = []interface{}{fingerprint + "%"}
query += " WHERE fingerprint LIKE ?"
}
if public {
query += " AND public=1"
}
err = dbQueryRowScan(c.db, query, inargs, outfmt)
if err != nil {
return -1, nil, err // Likely: there are no rows for this fingerprint
}
// Validate we only have a single match
if !strictMatching {
query = "SELECT COUNT(id) FROM images WHERE fingerprint LIKE ?"
count := 0
outfmt := []interface{}{&count}
err = dbQueryRowScan(c.db, query, inargs, outfmt)
if err != nil {
return -1, nil, err
}
if count > 1 {
return -1, nil, fmt.Errorf("Partial fingerprint matches more than one image")
}
}
// Some of the dates can be nil in the DB, let's process them.
if create != nil {
image.CreatedAt = *create
} else {
image.CreatedAt = time.Time{}
}
if expire != nil {
image.ExpiresAt = *expire
} else {
image.ExpiresAt = time.Time{}
}
if used != nil {
image.LastUsedAt = *used
} else {
image.LastUsedAt = time.Time{}
}
image.Architecture, _ = osarch.ArchitectureName(arch)
// The upload date is enforced by NOT NULL in the schema, so it can never be nil.
image.UploadedAt = *upload
// Get the properties
q := "SELECT key, value FROM images_properties where image_id=?"
var key, value, name, desc string
inargs = []interface{}{id}
outfmt = []interface{}{key, value}
results, err := queryScan(c.db, q, inargs, outfmt)
if err != nil {
return -1, nil, err
}
properties := map[string]string{}
for _, r := range results {
key = r[0].(string)
value = r[1].(string)
properties[key] = value
}
image.Properties = properties
// Get the aliases
q = "SELECT name, description FROM images_aliases WHERE image_id=?"
inargs = []interface{}{id}
outfmt = []interface{}{name, desc}
results, err = queryScan(c.db, q, inargs, outfmt)
if err != nil {
return -1, nil, err
}
aliases := []api.ImageAlias{}
for _, r := range results {
name = r[0].(string)
desc = r[1].(string)
a := api.ImageAlias{Name: name, Description: desc}
aliases = append(aliases, a)
}
image.Aliases = aliases
_, source, err := c.ImageSourceGet(id)
if err == nil {
image.UpdateSource = &source
}
return id, &image, nil
}
// ImageLocate returns the address of an online node that has a local copy of
// the given image, or an empty string if the image is already available on this
// node.
//
// If the image is not available on any online node, an error is returned.
func (c *Cluster) ImageLocate(fingerprint string) (string, error) {
stmt := `
SELECT nodes.address FROM nodes
LEFT JOIN images_nodes ON images_nodes.node_id = nodes.id
LEFT JOIN images ON images_nodes.image_id = images.id
WHERE images.fingerprint = ?
`
var localAddress string // Address of this node
var addresses []string // Addresses of online nodes with the image
err := c.Transaction(func(tx *ClusterTx) error {
offlineThreshold, err := tx.NodeOfflineThreshold()
if err != nil {
return err
}
localAddress, err = tx.NodeAddress()
if err != nil {
return err
}
allAddresses, err := query.SelectStrings(tx.tx, stmt, fingerprint)
if err != nil {
return err
}
for _, address := range allAddresses {
node, err := tx.NodeByAddress(address)
if err != nil {
return err
}
if address != localAddress && node.IsOffline(offlineThreshold) {
continue
}
addresses = append(addresses, address)
}
return err
})
if err != nil {
return "", err
}
if len(addresses) == 0 {
return "", fmt.Errorf("image not available on any online node")
}
for _, address := range addresses {
if address == localAddress {
return "", nil
}
}
return addresses[0], nil
}
// ImageAssociateNode creates a new entry in the images_nodes table for
// tracking that the current node has the given image.
func (c *Cluster) ImageAssociateNode(fingerprint string) error {
imageID, _, err := c.ImageGet(fingerprint, false, true)
if err != nil {
return err
}
err = c.Transaction(func(tx *ClusterTx) error {
_, err := tx.tx.Exec("INSERT INTO images_nodes(image_id, node_id) VALUES(?, ?)", imageID, c.nodeID)
return err
})
return err
}
func (c *Cluster) ImageDelete(id int) error {
_, err := exec(c.db, "DELETE FROM images WHERE id=?", id)
if err != nil {
return err
}
return nil
}
func (c *Cluster) ImageAliasesGet() ([]string, error) {
q := "SELECT name FROM images_aliases"
var name string
inargs := []interface{}{}
outfmt := []interface{}{name}
results, err := queryScan(c.db, q, inargs, outfmt)
if err != nil {
return nil, err
}
names := []string{}
for _, res := range results {
names = append(names, res[0].(string))
}
return names, nil
}
func (c *Cluster) ImageAliasGet(name string, isTrustedClient bool) (int, api.ImageAliasesEntry, error) {
q := `SELECT images_aliases.id, images.fingerprint, images_aliases.description
FROM images_aliases
INNER JOIN images
ON images_aliases.image_id=images.id
WHERE images_aliases.name=?`
if !isTrustedClient {
q = q + ` AND images.public=1`
}
var fingerprint, description string
id := -1
entry := api.ImageAliasesEntry{}
arg1 := []interface{}{name}
arg2 := []interface{}{&id, &fingerprint, &description}
err := dbQueryRowScan(c.db, q, arg1, arg2)
if err != nil {
if err == sql.ErrNoRows {
return -1, entry, NoSuchObjectError
}
return -1, entry, err
}
entry.Name = name
entry.Target = fingerprint
entry.Description = description
return id, entry, nil
}
func (c *Cluster) ImageAliasRename(id int, name string) error {
_, err := exec(c.db, "UPDATE images_aliases SET name=? WHERE id=?", name, id)
return err
}
func (c *Cluster) ImageAliasDelete(name string) error {
_, err := exec(c.db, "DELETE FROM images_aliases WHERE name=?", name)
return err
}
func (c *Cluster) ImageAliasesMove(source int, destination int) error {
_, err := exec(c.db, "UPDATE images_aliases SET image_id=? WHERE image_id=?", destination, source)
return err
}
// Insert an alias ento the database.
func (c *Cluster) ImageAliasAdd(name string, imageID int, desc string) error {
stmt := `INSERT INTO images_aliases (name, image_id, description) values (?, ?, ?)`
_, err := exec(c.db, stmt, name, imageID, desc)
return err
}
func (c *Cluster) ImageAliasUpdate(id int, imageID int, desc string) error {
stmt := `UPDATE images_aliases SET image_id=?, description=? WHERE id=?`
_, err := exec(c.db, stmt, imageID, desc, id)
return err
}
func (c *Cluster) ImageLastAccessUpdate(fingerprint string, date time.Time) error {
stmt := `UPDATE images SET last_use_date=? WHERE fingerprint=?`
_, err := exec(c.db, stmt, date, fingerprint)
return err
}
func (c *Cluster) ImageLastAccessInit(fingerprint string) error {
stmt := `UPDATE images SET cached=1, last_use_date=strftime("%s") WHERE fingerprint=?`
_, err := exec(c.db, stmt, fingerprint)
return err
}
func (c *Cluster) ImageUpdate(id int, fname string, sz int64, public bool, autoUpdate bool, architecture string, createdAt time.Time, expiresAt time.Time, properties map[string]string) error {
arch, err := osarch.ArchitectureId(architecture)
if err != nil {
arch = 0
}
tx, err := begin(c.db)
if err != nil {
return err
}
publicInt := 0
if public {
publicInt = 1
}
autoUpdateInt := 0
if autoUpdate {
autoUpdateInt = 1
}
stmt, err := tx.Prepare(`UPDATE images SET filename=?, size=?, public=?, auto_update=?, architecture=?, creation_date=?, expiry_date=? WHERE id=?`)
if err != nil {
tx.Rollback()
return err
}
defer stmt.Close()
_, err = stmt.Exec(fname, sz, publicInt, autoUpdateInt, arch, createdAt, expiresAt, id)
if err != nil {
tx.Rollback()
return err
}
_, err = tx.Exec(`DELETE FROM images_properties WHERE image_id=?`, id)
if err != nil {
tx.Rollback()
return err
}
stmt2, err := tx.Prepare(`INSERT INTO images_properties (image_id, type, key, value) VALUES (?, ?, ?, ?)`)
if err != nil {
tx.Rollback()
return err
}
defer stmt2.Close()
for key, value := range properties {
_, err = stmt2.Exec(id, 0, key, value)
if err != nil {
tx.Rollback()
return err
}
}
if err := TxCommit(tx); err != nil {
return err
}
return nil
}
func (c *Cluster) ImageInsert(fp string, fname string, sz int64, public bool, autoUpdate bool, architecture string, createdAt time.Time, expiresAt time.Time, properties map[string]string) error {
arch, err := osarch.ArchitectureId(architecture)
if err != nil {
arch = 0
}
tx, err := begin(c.db)
if err != nil {
return err
}
publicInt := 0
if public {
publicInt = 1
}
autoUpdateInt := 0
if autoUpdate {
autoUpdateInt = 1
}
stmt, err := tx.Prepare(`INSERT INTO images (fingerprint, filename, size, public, auto_update, architecture, creation_date, expiry_date, upload_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
if err != nil {
tx.Rollback()
return err
}
defer stmt.Close()
result, err := stmt.Exec(fp, fname, sz, publicInt, autoUpdateInt, arch, createdAt, expiresAt, time.Now().UTC())
if err != nil {
tx.Rollback()
return err
}
id64, err := result.LastInsertId()
if err != nil {
tx.Rollback()
return err
}
id := int(id64)
if len(properties) > 0 {
pstmt, err := tx.Prepare(`INSERT INTO images_properties (image_id, type, key, value) VALUES (?, 0, ?, ?)`)
if err != nil {
tx.Rollback()
return err
}
defer pstmt.Close()
for k, v := range properties {
// we can assume, that there is just one
// value per key
_, err = pstmt.Exec(id, k, v)
if err != nil {
tx.Rollback()
return err
}
}
}
_, err = tx.Exec("INSERT INTO images_nodes(image_id, node_id) VALUES(?, ?)", id, c.nodeID)
if err != nil {
tx.Rollback()
return err
}
if err := TxCommit(tx); err != nil {
return err
}
return nil
}
// Get the names of all storage pools on which a given image exists.
func (c *Cluster) ImageGetPools(imageFingerprint string) ([]int64, error) {
poolID := int64(-1)
query := "SELECT storage_pool_id FROM storage_volumes WHERE node_id=? AND name=? AND type=?"
inargs := []interface{}{c.nodeID, imageFingerprint, StoragePoolVolumeTypeImage}
outargs := []interface{}{poolID}
result, err := queryScan(c.db, query, inargs, outargs)
if err != nil {
return []int64{}, err
}
poolIDs := []int64{}
for _, r := range result {
poolIDs = append(poolIDs, r[0].(int64))
}
return poolIDs, nil
}
// Get the names of all storage pools on which a given image exists.
func (c *Cluster) ImageGetPoolNamesFromIDs(poolIDs []int64) ([]string, error) {
var poolName string
query := "SELECT name FROM storage_pools WHERE id=?"
poolNames := []string{}
for _, poolID := range poolIDs {
inargs := []interface{}{poolID}
outargs := []interface{}{poolName}
result, err := queryScan(c.db, query, inargs, outargs)
if err != nil {
return []string{}, err
}
for _, r := range result {
poolNames = append(poolNames, r[0].(string))
}
}
return poolNames, nil
}
// ImageUploadedAt updates the upload_date column and an image row.
func (c *Cluster) ImageUploadedAt(id int, uploadedAt time.Time) error {
_, err := exec(c.db, "UPDATE images SET upload_date=? WHERE id=?", uploadedAt, id)
return err
}