-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
365 lines (288 loc) · 10.3 KB
/
main.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
package main
import (
"context"
"errors"
"net/url"
"os"
"time"
"github.com/erikgeiser/promptkit/confirmation"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/flavors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/thediveo/enumflag/v2"
"github.com/vexxhost/migratekit/cmd"
"github.com/vexxhost/migratekit/internal/nbdkit"
"github.com/vexxhost/migratekit/internal/openstack"
"github.com/vexxhost/migratekit/internal/target"
"github.com/vexxhost/migratekit/internal/vmware"
"github.com/vexxhost/migratekit/internal/vmware_nbdkit"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/session"
"github.com/vmware/govmomi/session/keepalive"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)
type BusTypeOpts enumflag.Flag
const (
Virtio BusTypeOpts = iota
Scsi
)
var BusTypeOptsIds = map[BusTypeOpts][]string{
Virtio: {"virtio"},
Scsi: {"scsi"},
}
type CompressionMethodOpts enumflag.Flag
const (
None CompressionMethodOpts = iota
Zlib
Fastlz
Skipz
)
var CompressionMethodOptsIds = map[CompressionMethodOpts][]string{
None: {"none"},
Zlib: {"zlib"},
Fastlz: {"fastlz"},
Skipz: {"skipz"},
}
var (
debug bool
endpoint string
username string
password string
path string
compressionMethod CompressionMethodOpts = Skipz
flavorId string
networkMapping cmd.NetworkMappingFlag
availabilityZone string
volumeType string
securityGroups []string
enablev2v bool
busType BusTypeOpts
vzUnsafeVolumeByName bool
)
var rootCmd = &cobra.Command{
Use: "migratekit",
Short: "Near-live migration toolkit for VMware to OpenStack",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if debug {
log.SetLevel(log.DebugLevel)
}
endpointUrl := &url.URL{
Scheme: "https",
Host: endpoint,
User: url.UserPassword(username, password),
Path: "sdk",
}
var err error
// validBuses := []string{"scsi", "virtio"}
// if !slices.Contains(validBuses, busType) {
// log.Fatal("Invalid bus type: ", busType, ". Valid options are: ", validBuses)
// }
thumbprint, err := vmware.GetEndpointThumbprint(endpointUrl)
if err != nil {
return err
}
ctx := context.TODO()
soapClient := soap.NewClient(endpointUrl, true)
vimClient, err := vim25.NewClient(ctx, soapClient)
if err != nil {
log.WithError(err).Error("Failed to create VMware client")
return err
}
vimClient.RoundTripper = keepalive.NewHandlerSOAP(
vimClient.RoundTripper,
15*time.Second,
nil,
)
mgr := session.NewManager(vimClient)
err = mgr.Login(ctx, endpointUrl.User)
if err != nil {
log.WithError(err).Error("Failed to login to VMware")
return err
}
finder := find.NewFinder(vimClient)
vm, err := finder.VirtualMachine(ctx, path)
if err != nil {
switch err.(type) {
case *find.NotFoundError:
log.WithError(err).Error("Virtual machine not found, list of all virtual machines:")
vms, err := finder.VirtualMachineList(ctx, "*")
if err != nil {
return err
}
for _, vm := range vms {
log.Info(" - ", vm.InventoryPath)
}
os.Exit(1)
default:
return err
}
}
var o mo.VirtualMachine
err = vm.Properties(ctx, vm.Reference(), []string{"config"}, &o)
if err != nil {
return err
}
if o.Config.ChangeTrackingEnabled == nil || !*o.Config.ChangeTrackingEnabled {
return errors.New("change tracking is not enabled on the virtual machine")
}
if snapshotRef, _ := vm.FindSnapshot(ctx, "migratekit"); snapshotRef != nil {
log.Info("Snapshot already exists")
input := confirmation.New("Delete existing snapshot?", confirmation.Undecided)
delete, err := input.RunPrompt()
if err != nil {
return err
}
if delete {
consolidate := true
_, err := vm.RemoveSnapshot(ctx, snapshotRef.Value, false, &consolidate)
if err != nil {
return err
}
} else {
return errors.New("unable to continue without deleting existing snapshot")
}
}
ctx = context.WithValue(ctx, "vm", vm)
ctx = context.WithValue(ctx, "vddkConfig", &vmware_nbdkit.VddkConfig{
Debug: debug,
Endpoint: endpointUrl,
Thumbprint: thumbprint,
Compression: nbdkit.CompressionMethod(CompressionMethodOptsIds[compressionMethod][0]),
})
log.Info("Setting Disk Bus: ", BusTypeOptsIds[busType][0])
v := target.VolumeCreateOpts{
AvailabilityZone: availabilityZone,
VolumeType: volumeType,
BusType: BusTypeOptsIds[busType][0],
}
ctx = context.WithValue(ctx, "volumeCreateOpts", &v)
ctx = context.WithValue(ctx, "vzUnsafeVolumeByName", vzUnsafeVolumeByName)
cmd.SetContext(ctx)
return nil
},
}
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Run a migration cycle",
Long: `This command will run a migration cycle on the virtual machine without shutting off the source virtual machine.
- If no data for this virtual machine exists on the target, it will do a full copy.
- If data exists on the target, it will only copy the changed blocks.
It handles the following additional cases as well:
- If VMware indicates the change tracking has reset, it will do a full copy.`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
vm := ctx.Value("vm").(*object.VirtualMachine)
vddkConfig := ctx.Value("vddkConfig").(*vmware_nbdkit.VddkConfig)
servers := vmware_nbdkit.NewNbdkitServers(vddkConfig, vm)
err := servers.MigrationCycle(ctx, false)
if err != nil {
return err
}
log.Info("Migration completed")
return nil
},
}
var cutoverCmd = &cobra.Command{
Use: "cutover",
Short: "Cutover to the new virtual machine",
Long: `This commands will cutover into the OpenStack virtual machine from VMware by executing the following steps:
- Run a migration cycle
- Shut down the source virtual machine
- Run a final migration cycle to capture missing changes & run virt-v2v-in-place
- Spin up the new OpenStack virtual machine with the migrated disk`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
vm := ctx.Value("vm").(*object.VirtualMachine)
vddkConfig := ctx.Value("vddkConfig").(*vmware_nbdkit.VddkConfig)
clients, err := openstack.NewClientSet(ctx)
if err != nil {
return err
}
log.Info("Ensuring OpenStack resources exist")
flavor, err := flavors.Get(ctx, clients.Compute, flavorId).Extract()
if err != nil {
return err
}
log.WithFields(log.Fields{
"flavor": flavor.Name,
}).Info("Flavor exists, ensuring network resources exist")
v := openstack.PortCreateOpts{}
if len(securityGroups) > 0 {
v.SecurityGroups = &securityGroups
}
ctx = context.WithValue(ctx, "portCreateOpts", &v)
networks, err := clients.EnsurePortsForVirtualMachine(ctx, vm, &networkMapping)
if err != nil {
return err
}
log.Info("Starting migration cycle")
servers := vmware_nbdkit.NewNbdkitServers(vddkConfig, vm)
err = servers.MigrationCycle(ctx, false)
if err != nil {
return err
}
log.Info("Completed migration cycle, shutting down source VM")
powerState, err := vm.PowerState(ctx)
if err != nil {
return err
}
if powerState == types.VirtualMachinePowerStatePoweredOff {
log.Warn("Source VM is already off, skipping shutdown")
} else {
err := vm.ShutdownGuest(ctx)
if err != nil {
return err
}
err = vm.WaitForPowerState(ctx, types.VirtualMachinePowerStatePoweredOff)
if err != nil {
return err
}
log.Info("Source VM shut down, starting final migration cycle")
}
servers = vmware_nbdkit.NewNbdkitServers(vddkConfig, vm)
err = servers.MigrationCycle(ctx, enablev2v)
if err != nil {
return err
}
log.Info("Final migration cycle completed, spinning up new OpenStack VM")
err = clients.CreateResourcesForVirtualMachine(ctx, vm, flavorId, networks)
if err != nil {
return err
}
log.Info("Cutover completed")
return nil
},
}
func init() {
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Enable debug logging")
rootCmd.PersistentFlags().StringVar(&endpoint, "vmware-endpoint", "", "VMware endpoint (hostname or IP only)")
rootCmd.MarkPersistentFlagRequired("vmware-endpoint")
rootCmd.PersistentFlags().StringVar(&username, "vmware-username", "", "VMware username")
rootCmd.MarkPersistentFlagRequired("vmware-username")
rootCmd.PersistentFlags().StringVar(&password, "vmware-password", "", "VMware password")
rootCmd.MarkPersistentFlagRequired("vmware-password")
rootCmd.PersistentFlags().StringVar(&path, "vmware-path", "", "VMware VM path (e.g. '/Datacenter/vm/VM')")
rootCmd.MarkPersistentFlagRequired("vmware-path")
rootCmd.PersistentFlags().Var(enumflag.New(&compressionMethod, "compression-method", CompressionMethodOptsIds, enumflag.EnumCaseInsensitive), "compression-method", "Specifies the compression method to use for the disk")
rootCmd.PersistentFlags().StringVar(&availabilityZone, "availability-zone", "", "Openstack availability zone for blockdevice & server")
rootCmd.PersistentFlags().StringVar(&volumeType, "volume-type", "", "Openstack volume type")
rootCmd.PersistentFlags().Var(enumflag.New(&busType, "disk-bus-type", BusTypeOptsIds, enumflag.EnumCaseInsensitive), "disk-bus-type", "Specifies the type of disk controller to attach disk devices to.")
rootCmd.PersistentFlags().BoolVar(&vzUnsafeVolumeByName, "vz-unsafe-volume-by-name", false, "Only use the name to find a volume - workaround for virtuozzu - dangerous option")
cutoverCmd.Flags().StringVar(&flavorId, "flavor", "", "OpenStack Flavor ID")
cutoverCmd.MarkFlagRequired("flavor")
cutoverCmd.Flags().Var(&networkMapping, "network-mapping", "Network mapping (e.g. 'mac=00:11:22:33:44:55,network-id=6bafb3d3-9d4d-4df1-86bb-bb7403403d24,subnet-id=47ed1da7-82d4-4e67-9bdd-5cb4993e06ff[,ip=1.2.3.4]')")
cutoverCmd.MarkFlagRequired("network-mapping")
cutoverCmd.Flags().StringSliceVar(&securityGroups, "security-groups", nil, "Openstack security groups, comma separated (e.g. '42c5a89e-4034-4f2a-adea-b33adc9614f4,6647122c-2d46-42f1-bb26-f38007730fdc')")
cutoverCmd.Flags().BoolVar(&enablev2v, "run-v2v", true, "Run virt2v-inplace on destination VM")
rootCmd.AddCommand(migrateCmd)
rootCmd.AddCommand(cutoverCmd)
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}