/
main_recover.go
251 lines (199 loc) · 7.3 KB
/
main_recover.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
package main
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/canonical/lxd/client"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/validate"
)
type cmdRecover struct {
global *cmdGlobal
}
func (c *cmdRecover) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = "recover"
cmd.Short = "Recover missing instances and volumes from existing and unknown storage pools"
cmd.Long = `Description:
Recover missing instances and volumes from existing and unknown storage pools
This command is mostly used for disaster recovery. It will ask you about unknown storage pools and attempt to
access them, along with existing storage pools, and identify any missing instances and volumes that exist on the
pools but are not in the LXD database. It will then offer to recreate these database records.
`
cmd.RunE = c.Run
return cmd
}
func (c *cmdRecover) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
if len(args) > 0 {
return fmt.Errorf("Invalid arguments")
}
d, err := lxd.ConnectLXDUnix("", nil)
if err != nil {
return err
}
server, _, err := d.GetServer()
if err != nil {
return err
}
isClustered := d.IsClustered()
// Get list of existing storage pools to scan.
existingPools, err := d.GetStoragePools()
if err != nil {
return fmt.Errorf("Failed getting existing storage pools: %w", err)
}
fmt.Println("This LXD server currently has the following storage pools:")
for _, existingPool := range existingPools {
fmt.Printf(" - %s (backend=%q, source=%q)\n", existingPool.Name, existingPool.Driver, existingPool.Config["source"])
}
unknownPools := make([]api.StoragePoolsPost, 0, len(existingPools))
// Build up a list of unknown pools to scan.
// We don't offer this option if the server is clustered because we don't allow creating storage pools on
// an individual server when clustered.
if !isClustered {
var supportedDriverNames []string
for {
addUnknownPool, err := c.global.asker.AskBool("Would you like to recover another storage pool? (yes/no) [default=no]: ", "no")
if err != nil {
return err
}
if !addUnknownPool {
break
}
// Get available storage drivers if not done already.
if supportedDriverNames == nil {
for _, supportedDriver := range server.Environment.StorageSupportedDrivers {
supportedDriverNames = append(supportedDriverNames, supportedDriver.Name)
}
}
unknownPool := api.StoragePoolsPost{
StoragePoolPut: api.StoragePoolPut{
Config: make(map[string]string),
},
}
unknownPool.Name, err = c.global.asker.AskString("Name of the storage pool: ", "", validate.Required(func(value string) error {
if value == "" {
return fmt.Errorf("Pool name cannot be empty")
}
for _, p := range unknownPools {
if value == p.Name {
return fmt.Errorf("Storage pool %q is already on recover list", value)
}
}
return nil
}))
if err != nil {
return err
}
unknownPool.Driver, err = c.global.asker.AskString(fmt.Sprintf("Name of the storage backend (%s): ", strings.Join(supportedDriverNames, ", ")), "", validate.IsOneOf(supportedDriverNames...))
if err != nil {
return err
}
unknownPool.Config["source"], err = c.global.asker.AskString("Source of the storage pool (block device, volume group, dataset, path, ... as applicable): ", "", validate.IsNotEmpty)
if err != nil {
return err
}
for {
var configKey, configValue string
_, _ = c.global.asker.AskString("Additional storage pool configuration property (KEY=VALUE, empty when done): ", "", validate.Optional(func(value string) error {
configParts := strings.SplitN(value, "=", 2)
if len(configParts) < 2 {
return fmt.Errorf("Config option should be in the format KEY=VALUE")
}
configKey = configParts[0]
configValue = configParts[1]
return nil
}))
if configKey == "" {
break
}
unknownPool.Config[configKey] = configValue
}
unknownPools = append(unknownPools, unknownPool)
}
}
fmt.Println("The recovery process will be scanning the following storage pools:")
for _, p := range existingPools {
fmt.Printf(" - EXISTING: %q (backend=%q, source=%q)\n", p.Name, p.Driver, p.Config["source"])
}
for _, p := range unknownPools {
fmt.Printf(" - NEW: %q (backend=%q, source=%q)\n", p.Name, p.Driver, p.Config["source"])
}
proceed, err := c.global.asker.AskBool("Would you like to continue with scanning for lost volumes? (yes/no) [default=yes]: ", "yes")
if err != nil {
return err
}
if !proceed {
return nil
}
fmt.Println("Scanning for unknown volumes...")
// Send /internal/recover/validate request to LXD.
reqValidate := internalRecoverValidatePost{
Pools: make([]api.StoragePoolsPost, 0, len(existingPools)+len(unknownPools)),
}
// Add existing pools to request.
for _, p := range existingPools {
reqValidate.Pools = append(reqValidate.Pools, api.StoragePoolsPost{
Name: p.Name, // Only send existing pool name, the rest will be looked up on server.
})
}
// Add unknown pools to request.
reqValidate.Pools = append(reqValidate.Pools, unknownPools...)
for {
resp, _, err := d.RawQuery("POST", "/internal/recover/validate", reqValidate, "")
if err != nil {
return fmt.Errorf("Failed validation request: %w", err)
}
var res internalRecoverValidateResult
err = resp.MetadataAsStruct(&res)
if err != nil {
return fmt.Errorf("Failed parsing validation response: %w", err)
}
if len(unknownPools) > 0 {
fmt.Println("The following unknown storage pools have been found:")
for _, unknownPool := range unknownPools {
fmt.Printf(" - Storage pool %q of type %q\n", unknownPool.Name, unknownPool.Driver)
}
}
if len(res.UnknownVolumes) > 0 {
fmt.Println("The following unknown volumes have been found:")
for _, unknownVol := range res.UnknownVolumes {
fmt.Printf(" - %s %q on pool %q in project %q (includes %d snapshots)\n", cases.Title(language.English).String(unknownVol.Type), unknownVol.Name, unknownVol.Pool, unknownVol.Project, unknownVol.SnapshotCount)
}
}
if len(res.DependencyErrors) > 0 {
fmt.Println("You are currently missing the following:")
for _, depErr := range res.DependencyErrors {
fmt.Printf(" - %s\n", depErr)
}
_, _ = c.global.asker.AskString("Please create those missing entries and then hit ENTER: ", "", validate.Optional())
} else {
if len(unknownPools) == 0 && len(res.UnknownVolumes) == 0 {
fmt.Println("No unknown storage pools or volumes found. Nothing to do.")
return nil
}
break // Dependencies met.
}
}
proceed, err = c.global.asker.AskBool("Would you like those to be recovered? (yes/no) [default=no]: ", "no")
if err != nil {
return err
}
if !proceed {
return nil
}
fmt.Println("Starting recovery...")
// Send /internal/recover/import request to LXD.
// Don't lint next line with gosimple. It says we should convert reqValidate directly to an internalRecoverImportPost
// because their types are identical. This is less clear and will not work if either type changes in the future.
reqImport := internalRecoverImportPost{ //nolint:gosimple
Pools: reqValidate.Pools,
}
_, _, err = d.RawQuery("POST", "/internal/recover/import", reqImport, "")
if err != nil {
return fmt.Errorf("Failed import request: %w", err)
}
return nil
}