forked from canonical/lxd
/
migration.go
246 lines (229 loc) · 6.58 KB
/
migration.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
package db
import (
"database/sql"
"fmt"
"strings"
"github.com/lxc/lxd/lxd/db/query"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/logger"
"github.com/pkg/errors"
)
// LoadPreClusteringData loads all the data that before the introduction of
// LXD clustering used to live in the node-level database.
//
// This is used for performing a one-off data migration when a LXD instance is
// upgraded from a version without clustering to a version that supports
// clustering, since in those version all data lives in the cluster database
// (regardless of whether clustering is actually on or off).
func LoadPreClusteringData(tx *sql.Tx) (*Dump, error) {
// Dump all tables.
dump := &Dump{
Schema: map[string][]string{},
Data: map[string][][]interface{}{},
}
for _, table := range preClusteringTables {
logger.Debugf("Loading data from table %s", table)
data := [][]interface{}{}
stmt := fmt.Sprintf("SELECT * FROM %s", table)
rows, err := tx.Query(stmt)
if err != nil {
return nil, errors.Wrapf(err, "failed to fetch rows from %s", table)
}
columns, err := rows.Columns()
if err != nil {
return nil, errors.Wrapf(err, "failed to get columns of %s", table)
}
dump.Schema[table] = columns
for rows.Next() {
values := make([]interface{}, len(columns))
row := make([]interface{}, len(columns))
for i := range values {
row[i] = &values[i]
}
err := rows.Scan(row...)
if err != nil {
return nil, errors.Wrapf(err, "failed to scan row from %s", table)
}
data = append(data, values)
}
err = rows.Err()
if err != nil {
return nil, errors.Wrapf(err, "error while fetching rows from %s", table)
}
dump.Data[table] = data
}
return dump, nil
}
// ImportPreClusteringData imports the data loaded with LoadPreClusteringData.
func (c *Cluster) ImportPreClusteringData(dump *Dump) error {
tx, err := c.db.Begin()
if err != nil {
return errors.Wrap(err, "failed to start cluster database transaction")
}
// Delete the default profile in the cluster database, which always
// gets created no matter what.
_, err = tx.Exec("DELETE FROM profiles WHERE id=1")
if err != nil {
return errors.Wrap(err, "failed to delete default profile")
}
for _, table := range preClusteringTables {
logger.Debugf("Migrating data for table %s", table)
for i, row := range dump.Data[table] {
for i, element := range row {
// Convert []byte columns to string. This is safe to do since
// the pre-clustering schema only had TEXT fields and no BLOB.
bytes, ok := element.([]byte)
if ok {
row[i] = string(bytes)
}
}
columns := dump.Schema[table]
nullNodeID := false // Whether node-related rows should have a NULL node ID
appendNodeID := func() {
columns = append(columns, "node_id")
if nullNodeID {
row = append(row, nil)
} else {
row = append(row, int64(1))
}
}
switch table {
case "config":
// Don't migrate the core.https_address and maas.machine config keys,
// which is node-specific and must remain in the node
// database.
keys := []string{"core.https_address", "maas.machine"}
skip := false
for i, column := range columns {
value, ok := row[i].(string)
if !ok {
continue
}
if column == "key" && shared.StringInSlice(value, keys) {
skip = true
}
}
if skip {
continue
}
case "containers":
appendNodeID()
case "networks_config":
// The keys listed in NetworkNodeConfigKeys
// are the only ones which are not global to the
// cluster, so all other keys will have a NULL
// node_id.
index := 0
for i, column := range columns {
if column == "key" {
index = i
break
}
}
key := row[index].(string)
if !shared.StringInSlice(key, NetworkNodeConfigKeys) {
nullNodeID = true
break
}
appendNodeID()
case "storage_pools_config":
// The keys listed in StoragePoolNodeConfigKeys
// are the only ones which are not global to the
// cluster, so all other keys will have a NULL
// node_id.
index := 0
for i, column := range columns {
if column == "key" {
index = i
break
}
}
key := row[index].(string)
if !shared.StringInSlice(key, StoragePoolNodeConfigKeys) {
nullNodeID = true
break
}
appendNodeID()
case "networks":
fallthrough
case "storage_pools":
columns = append(columns, "state")
row = append(row, storagePoolCreated)
case "storage_volumes":
appendNodeID()
}
stmt := fmt.Sprintf("INSERT INTO %s(%s)", table, strings.Join(columns, ", "))
stmt += fmt.Sprintf(" VALUES %s", query.Params(len(columns)))
result, err := tx.Exec(stmt, row...)
if err != nil {
return errors.Wrapf(err, "failed to insert row %d into %s", i, table)
}
n, err := result.RowsAffected()
if err != nil {
return errors.Wrapf(err, "no result count for row %d of %s", i, table)
}
if n != 1 {
return fmt.Errorf("could not insert %d int %s", i, table)
}
// Also insert the image ID -> node ID association.
if shared.StringInSlice(table, []string{"images", "networks", "storage_pools"}) {
entity := table[:len(table)-1]
importNodeAssociation(entity, columns, row, tx)
}
}
}
return tx.Commit()
}
// Insert a row in one of the nodes association tables (storage_pools_nodes,
// networks_nodes, images_nodes).
func importNodeAssociation(entity string, columns []string, row []interface{}, tx *sql.Tx) error {
stmt := fmt.Sprintf(
"INSERT INTO %ss_nodes(%s_id, node_id) VALUES(?, 1)", entity, entity)
var id int64
for i, column := range columns {
if column == "id" {
id = row[i].(int64)
break
}
}
if id == 0 {
return fmt.Errorf("entity %s has invalid ID", entity)
}
_, err := tx.Exec(stmt, row...)
if err != nil {
return errors.Wrapf(err, "failed to associate %s to node", entity)
}
return nil
}
// Dump is a dump of all the user data in lxd.db prior the migration to the
// cluster db.
type Dump struct {
// Map table names to the names or their columns.
Schema map[string][]string
// Map a table name to all the rows it contains. Each row is a slice
// of interfaces.
Data map[string][][]interface{}
}
var preClusteringTables = []string{
"certificates",
"config",
"profiles",
"profiles_config",
"profiles_devices",
"profiles_devices_config",
"containers",
"containers_config",
"containers_devices",
"containers_devices_config",
"containers_profiles",
"images",
"images_aliases",
"images_properties",
"images_source",
"networks",
"networks_config",
"storage_pools",
"storage_pools_config",
"storage_volumes",
"storage_volumes_config",
}