/
move.go
361 lines (299 loc) · 10.2 KB
/
move.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
package lxc
import (
"fmt"
"github.com/spf13/cobra"
"github.com/bketelsen/dlx/config"
"github.com/bketelsen/dlx/state"
"github.com/lxc/lxd/lxc/utils"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
cli "github.com/lxc/lxd/shared/cmd"
"github.com/lxc/lxd/shared/i18n"
"github.com/pkg/errors"
)
type cmdMove struct {
global *state.Global
flagNoProfiles bool
flagProfile []string
flagConfig []string
flagInstanceOnly bool
flagDevice []string
flagMode string
flagStateless bool
flagStorage string
flagTarget string
flagTargetProject string
}
func (c *cmdMove) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("move", i18n.G("[<remote>:]<instance>[/<snapshot>] [<remote>:][<instance>[/<snapshot>]]"))
cmd.Aliases = []string{"mv"}
cmd.Short = i18n.G("Move instances within or in between LXD servers")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Move instances within or in between LXD servers`))
cmd.Example = cli.FormatSection("", i18n.G(
`lxc move [<remote>:]<source instance> [<remote>:][<destination instance>] [--instance-only]
Move an instance between two hosts, renaming it if destination name differs.
lxc move <old name> <new name> [--instance-only]
Rename a local instance.
lxc move <instance>/<old snapshot name> <instance>/<new snapshot name>
Rename a snapshot.`))
cmd.RunE = c.Run
cmd.Flags().StringArrayVarP(&c.flagConfig, "config", "c", nil, i18n.G("Config key/value to apply to the target instance")+"``")
cmd.Flags().StringArrayVarP(&c.flagDevice, "device", "d", nil, i18n.G("New key/value to apply to a specific device")+"``")
cmd.Flags().StringArrayVarP(&c.flagProfile, "profile", "p", nil, i18n.G("Profile to apply to the target instance")+"``")
cmd.Flags().BoolVar(&c.flagNoProfiles, "no-profiles", false, i18n.G("Unset all profiles on the target instance"))
cmd.Flags().BoolVar(&c.flagInstanceOnly, "instance-only", false, i18n.G("Move the instance without its snapshots"))
cmd.Flags().StringVar(&c.flagMode, "mode", moveDefaultMode, i18n.G("Transfer mode. One of pull (default), push or relay.")+"``")
cmd.Flags().BoolVar(&c.flagStateless, "stateless", false, i18n.G("Copy a stateful instance stateless"))
cmd.Flags().StringVarP(&c.flagStorage, "storage", "s", "", i18n.G("Storage pool name")+"``")
cmd.Flags().StringVar(&c.flagTarget, "target", "", i18n.G("Cluster member name")+"``")
cmd.Flags().StringVar(&c.flagTargetProject, "target-project", "", i18n.G("Copy to a project different from the source")+"``")
return cmd
}
func (c *cmdMove) Run(cmd *cobra.Command, args []string) error {
conf := c.global.Conf
// Quick checks.
if c.flagTarget == "" && c.flagTargetProject == "" && c.flagStorage == "" {
exit, err := c.global.CheckArgs(cmd, args, 2, 2)
if exit {
return err
}
} else {
exit, err := c.global.CheckArgs(cmd, args, 1, 2)
if exit {
return err
}
}
// Parse the mode
mode := moveDefaultMode
if c.flagMode != "" {
mode = c.flagMode
}
sourceRemote, sourceName, err := conf.ParseRemote(args[0])
if err != nil {
return err
}
destRemote := sourceRemote
destName := ""
if len(args) == 2 {
var err error
destRemote, destName, err = conf.ParseRemote(args[1])
if err != nil {
return err
}
}
// As an optimization, if the source an destination are the same, do
// this via a simple rename. This only works for instances that aren't
// running, instances that are running should be live migrated (of
// course, this changing of hostname isn't supported right now, so this
// simply won't work).
if sourceRemote == destRemote && c.flagTarget == "" && c.flagStorage == "" && c.flagTargetProject == "" {
if c.flagConfig != nil || c.flagDevice != nil || c.flagProfile != nil || c.flagNoProfiles {
return fmt.Errorf(i18n.G("Can't override configuration or profiles in local rename"))
}
source, err := conf.GetInstanceServer(sourceRemote)
if err != nil {
return err
}
if shared.IsSnapshot(sourceName) {
// Snapshot rename
srcParent, srcSnap, _ := shared.InstanceGetParentAndSnapshotName(sourceName)
dstParent, dstSnap, dstIsSnap := shared.InstanceGetParentAndSnapshotName(destName)
if srcParent != dstParent {
return fmt.Errorf("Invalid new snapshot name, parent must be the same as source")
}
if !dstIsSnap {
return fmt.Errorf("Invalid new snapshot name")
}
op, err := source.RenameInstanceSnapshot(srcParent, srcSnap, api.InstanceSnapshotPost{Name: dstSnap})
if err != nil {
return err
}
return op.Wait()
}
// Instance rename
op, err := source.RenameInstance(sourceName, api.InstancePost{Name: destName})
if err != nil {
return err
}
return op.Wait()
}
sourceResource := args[0]
destResource := sourceResource
if len(args) == 2 {
destResource = args[1]
}
if c.flagTarget != "" {
// If the target option was specified, we're moving an instance from a
// cluster member to another, let's use the dedicated API.
if sourceRemote == destRemote {
if c.flagStateless {
return fmt.Errorf(i18n.G("The --stateless flag can't be used with --target"))
}
if c.flagInstanceOnly {
return fmt.Errorf(i18n.G("The --instance-only flag can't be used with --target"))
}
if c.flagStorage != "" {
return fmt.Errorf(i18n.G("The --storage flag can't be used with --target"))
}
if c.flagMode != moveDefaultMode {
return fmt.Errorf(i18n.G("The --mode flag can't be used with --target"))
}
return moveClusterInstance(conf, sourceResource, destResource, c.flagTarget, c.global.FlagQuiet)
}
dest, err := conf.GetInstanceServer(destRemote)
if err != nil {
return err
}
if !dest.IsClustered() {
return fmt.Errorf(i18n.G("The destination LXD server is not clustered"))
}
}
// Support for server-side pool move.
if c.flagStorage != "" && sourceRemote == destRemote {
source, err := conf.GetInstanceServer(sourceRemote)
if err != nil {
return err
}
if source.HasExtension("instance_pool_move") {
if c.flagStateless {
return fmt.Errorf(i18n.G("The --stateless flag can't be used with --storage"))
}
if c.flagMode != moveDefaultMode {
return fmt.Errorf(i18n.G("The --mode flag can't be used with --storage"))
}
return moveInstancePool(conf, sourceResource, destResource, c.flagInstanceOnly, c.flagStorage)
}
}
cpy := cmdCopy{}
cpy.global = c.global
cpy.flagTarget = c.flagTarget
cpy.flagTargetProject = c.flagTargetProject
cpy.flagConfig = c.flagConfig
cpy.flagDevice = c.flagDevice
cpy.flagProfile = c.flagProfile
cpy.flagNoProfiles = c.flagNoProfiles
stateful := !c.flagStateless
instanceOnly := c.flagInstanceOnly
// A move is just a copy followed by a delete; however, we want to
// keep the volatile entries around since we are moving the instance.
err = cpy.copyInstance(conf, sourceResource, destResource, true, -1, stateful, instanceOnly, mode, c.flagStorage, true)
if err != nil {
return err
}
del := cmdDelete{global: c.global}
del.flagForce = true
del.flagForceProtected = true
err = del.Run(cmd, args[:1])
if err != nil {
return errors.Wrap(err, "Failed to delete original instance after copying it")
}
return nil
}
// Move an instance using special POST /instances/<name>?target=<member> API.
func moveClusterInstance(conf *config.Config, sourceResource string, destResource string, target string, quiet bool) error {
// Parse the source.
sourceRemote, sourceName, err := conf.ParseRemote(sourceResource)
if err != nil {
return err
}
// Parse the destination.
_, destName, err := conf.ParseRemote(destResource)
if err != nil {
return err
}
// Make sure we have an instance or snapshot name.
if sourceName == "" {
return fmt.Errorf(i18n.G("You must specify a source instance name"))
}
// The destination name is optional.
if destName == "" {
destName = sourceName
}
// Connect to the source host
source, err := conf.GetInstanceServer(sourceRemote)
if err != nil {
return errors.Wrap(err, i18n.G("Failed to connect to cluster member"))
}
// Check that it's a cluster
if !source.IsClustered() {
return fmt.Errorf(i18n.G("The source LXD server is not clustered"))
}
// The migrate API will do the right thing when passed a target.
source = source.UseTarget(target)
req := api.InstancePost{
Name: destName,
Migration: true,
}
op, err := source.MigrateInstance(sourceName, req)
if err != nil {
return errors.Wrap(err, i18n.G("Migration API failure"))
}
// Watch the background operation
progress := utils.ProgressRenderer{
Format: i18n.G("Transferring instance: %s"),
Quiet: quiet,
}
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
// Wait for the move to complete
err = utils.CancelableWait(op, &progress)
if err != nil {
progress.Done("")
return err
}
progress.Done("")
err = op.Wait()
if err != nil {
return errors.Wrap(err, i18n.G("Migration operation failure"))
}
return nil
}
// Move an instance between pools using special POST /instances/<name> API.
func moveInstancePool(conf *config.Config, sourceResource string, destResource string, instanceOnly bool, storage string) error {
// Parse the source.
sourceRemote, sourceName, err := conf.ParseRemote(sourceResource)
if err != nil {
return err
}
// Parse the destination.
_, destName, err := conf.ParseRemote(destResource)
if err != nil {
return err
}
// Make sure we have an instance or snapshot name.
if sourceName == "" {
return fmt.Errorf(i18n.G("You must specify a source instance name"))
}
// The destination name is optional.
if destName == "" {
destName = sourceName
}
// Connect to the source host
source, err := conf.GetInstanceServer(sourceRemote)
if err != nil {
return errors.Wrap(err, i18n.G("Failed to connect to cluster member"))
}
// Pass the new pool to the migration API.
req := api.InstancePost{
Name: destName,
Migration: true,
Pool: storage,
InstanceOnly: instanceOnly,
}
op, err := source.MigrateInstance(sourceName, req)
if err != nil {
return errors.Wrap(err, i18n.G("Migration API failure"))
}
err = op.Wait()
if err != nil {
return errors.Wrap(err, i18n.G("Migration operation failure"))
}
return nil
}
// Default migration mode when moving an instance.
const moveDefaultMode = "pull"