File: snapshots.go

package info (click to toggle)
incus 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,392 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (98 lines) | stat: -rw-r--r-- 2,716 bytes parent folder | download | duplicates (6)
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
//go:build linux && cgo && !agent

package db

import (
	"context"
	"database/sql"
	"time"

	"github.com/lxc/incus/v6/internal/server/db/cluster"
	"github.com/lxc/incus/v6/internal/server/db/query"
)

// UpdateInstanceSnapshotConfig inserts/updates/deletes the provided config keys.
func (c *ClusterTx) UpdateInstanceSnapshotConfig(id int, values map[string]string) error {
	insertSQL := "INSERT OR REPLACE INTO instances_snapshots_config (instance_snapshot_id, key, value) VALUES"
	deleteSQL := "DELETE FROM instances_snapshots_config WHERE key IN %s AND instance_snapshot_id=?"
	return c.configUpdate(id, values, insertSQL, deleteSQL)
}

// UpdateInstanceSnapshot updates the description and expiry date of the
// instance snapshot with the given ID.
func (c *ClusterTx) UpdateInstanceSnapshot(id int, description string, expiryDate time.Time) error {
	str := "UPDATE instances_snapshots SET description=?, expiry_date=? WHERE id=?"
	var err error
	if expiryDate.IsZero() {
		_, err = c.tx.Exec(str, description, "", id)
	} else {
		_, err = c.tx.Exec(str, description, expiryDate, id)
	}

	if err != nil {
		return err
	}

	return nil
}

// GetLocalExpiredInstanceSnapshots returns a list of expired snapshots.
func (c *ClusterTx) GetLocalExpiredInstanceSnapshots(ctx context.Context) ([]cluster.InstanceSnapshot, error) {
	q := `
	SELECT
		instances_snapshots.id,
		instances_snapshots.expiry_date
	FROM instances_snapshots
	JOIN instances ON instances.id=instances_snapshots.instance_id
	WHERE instances.node_id=? AND instances_snapshots.expiry_date != '0001-01-01T00:00:00Z'
	`

	snapshotIDs := []int{}
	err := query.Scan(ctx, c.Tx(), q, func(scan func(dest ...any) error) error {
		var id int
		var expiry sql.NullTime

		// Read the row.
		err := scan(&id, &expiry)
		if err != nil {
			return err
		}

		// Skip if not expired.
		if !expiry.Valid || expiry.Time.Unix() <= 0 {
			return nil
		}

		if time.Now().Before(expiry.Time) {
			return nil
		}

		// Add the snapshot.
		snapshotIDs = append(snapshotIDs, id)

		return nil
	}, c.nodeID)
	if err != nil {
		return nil, err
	}

	// Fetch all the expired snapshot details.
	snapshots := make([]cluster.InstanceSnapshot, len(snapshotIDs))

	for i, id := range snapshotIDs {
		snap, err := cluster.GetInstanceSnapshots(ctx, c.tx, cluster.InstanceSnapshotFilter{ID: &id})
		if err != nil {
			return nil, err
		}

		snapshots[i] = snap[0]
	}

	return snapshots, nil
}

// GetInstanceSnapshotID returns the ID of the snapshot with the given name.
func (c *ClusterTx) GetInstanceSnapshotID(ctx context.Context, project, instance, name string) (int, error) {
	id, err := cluster.GetInstanceSnapshotID(ctx, c.tx, project, instance, name)
	return int(id), err
}