forked from OpenNebula/one-swap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneswap
536 lines (447 loc) · 18 KB
/
oneswap
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2024, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
ONE_LOCATION = ENV['ONE_LOCATION']
if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems'
else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems'
end
# %%RUBYGEMS_SETUP_BEGIN%%
if File.directory?(GEMS_LOCATION)
real_gems_path = File.realpath(GEMS_LOCATION)
if !defined?(Gem) || Gem.path != [real_gems_path]
$LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
# Suppress warnings from Rubygems
# https://github.com/OpenNebula/one/issues/5379
begin
verb = $VERBOSE
$VERBOSE = nil
require 'rubygems'
Gem.use_paths(real_gems_path)
ensure
$VERBOSE = verb
end
end
end
# %%RUBYGEMS_SETUP_END%%
$LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << RUBY_LIB_LOCATION + '/cli'
require 'command_parser'
require 'rbvmomi'
require 'one_helper/oneswap_helper'
require 'fileutils'
require 'openssl'
require 'socket'
require 'ipaddr'
require 'open3'
require 'rexml'
require 'yaml'
require 'cgi'
CommandParser::CmdParser.new(ARGV) do
usage '`oneswap` <command> [<args>] [<options>]'
version OpenNebulaHelper::ONE_VERSION
helper = OneSwapHelper.new
before_proc do
helper.set_client(options)
end
####################
# Global Arguments #
####################
VCENTER = {
:name => 'vcenter',
:large => '--vcenter vCenter',
:description => 'The vCenter hostname',
:format => String
}
USER = {
:name => 'vuser',
:large => '--vuser username',
:description => 'The username to interact with vCenter',
:format => String
}
PASS = {
:name => 'vpass',
:large => '--vpass password',
:description => 'The password for the user',
:format => String
}
PORT = {
:name => 'port',
:short => '-p port',
:large => '--port port',
:format => String,
:description => 'vCenter API port, defaults to 443 (SSL) or 80'
}
AUTH_OPTS = [VCENTER, USER, PASS, PORT]
#####################
# Command Arguments #
#####################
NAME = {
:name => 'name',
:short => '-n text',
:large => '--name text',
:description => 'Filter by name containing text',
:format => String
}
DATACENTER = {
:name => 'datacenter',
:short => '-d text',
:large => '--datacenter text',
:description => 'Filter by Datacenter name containing text',
:format => String
}
CLUSTER = {
:name => 'cluster',
:short => '-c text',
:large => '--cluster text',
:description => 'Filter by Cluster name containing text',
:format => String
}
STATE = {
:name => 'state',
:short => '-s vm_state',
:large => '--state vm_state',
:description => 'Filter VMs by their power state: [poweroff, running, suspended]',
:format => String
}
NETWORK = {
:name => 'network',
:large => '--network id',
:description => 'If a VCENTER_NETWORK_MATCH attribute in an OpenNebula does not match the name of a vCenter network, assign to this OpenNebula Network ID instead.',
:format => Integer
}
NO_IP = {
:name => 'skip_ip',
:large => '--skip-ip',
:description => 'Do not pull IP from vCenter to be created in OpenNebula'
}
NO_MAC = {
:name => 'skip_mac',
:large => '--skip-mac',
:description => 'Do not pull MAC from vCenter to be created in OpenNebula'
}
PERSISTENT_IMG = {
:name => 'persistent_img',
:large => '--persistent-img',
:description => 'Make the image disk persistent in OpenNebula',
}
GRAPHICS_TYPE = {
:name => 'graphics_type',
:large => '--graphics-type type',
:description => 'The Graphics type to enable in OpenNebula. Options: [vnc, sdl, spice]',
:format => String
}
GRAPHICS_LISTEN = {
:name => 'graphics_listen',
:large => '--graphics-listen ip',
:description => 'The Graphics Listen on IP in OpenNebula. Graphics type required.',
:format => String
}
GRAPHICS_PORT = {
:name => 'graphics_port',
:large => '--graphics-port port',
:description => 'The Graphics Server port in OpenNebula. Graphics type required.',
:format => String
}
GRAPHICS_KEYMAP = {
:name => 'graphics_keymap',
:large => '--graphics-keymap keymap',
:description => 'The Graphics Keymap in OpenNebula. Graphics type required.',
:format => String
}
GRAPHICS_PASSWORD = {
:name => 'graphics_password',
:large => '--graphics-password password',
:description => 'The Graphics Password in OpenNebula. Graphics type required.',
:format => String
}
GRAPHICS_COMMAND = {
:name => 'graphics_command',
:large => '--graphics-command command',
:description => 'The Graphics Command in OpenNebula. Graphics type required.',
:format => String
}
MEMORY_MAX = {
:name => 'memory_max',
:large => '--memory-max memorymb',
:description => 'The Max Memory allowed in MB in OpenNebula. Memory Hot Add required in VMware.',
:format => Integer
}
VCPU_MAX = {
:name => 'vcpu_max',
:large => '--vcpu-max vcpumax',
:description => 'The Max vCPU allowed in integer format in OpenNebula. CPU Hot Add required in VMware.',
:format => Integer
}
CPU = {
:name => 'cpu',
:large => '--cpu cpus',
:description => 'The Physical CPU allowed in integer format in OpenNebula. Default is to match CPU cores in vCenter.',
:format => Integer
}
VCPU = {
:name => 'vcpu',
:large => '--vcpu vcpus',
:description => 'The vCPU allowed in integer format in OpenNebula. Default is to match CPU cores in vCenter',
:format => Integer
}
IMG_WAIT = {
:name => 'img_wait',
:large => '--img-wait sec',
:description => 'The amount of time to wait in seconds for the image to be created in OpenNebula. Default is 120 seconds',
:format => Integer
}
DEV_PREFIX = {
:name => 'dev_prefix',
:large => '--dev-prefix sd',
:description => 'The Dev Prefix to use on the disks in OpenNebula. ex: sd, hd, vd, xvd. Default is none',
:format => String
}
DISABLE_CONTEXTUALIZATION = {
:name => 'disable_contextualization',
:large => '--disable-contextualization',
:description => 'Remove default contextualization options in OpenNebula. Default is for Network and SSH contextualization to be enabled.'
}
CPU_MODEL = {
:name => 'cpu_model',
:large => '--cpu-model model_type',
:description => 'Set a Specific CPU model in OpenNebula. Default is None',
:format => String
}
DATASTORE = {
:name => 'datastore',
:large => '--datastore id',
:description => 'Create images in the Image Datastore with this ID, Default: 1',
:format => Integer
}
WORK_DIR = {
:name => 'work_dir',
:short => '-w directory',
:large => '--work-dir directory',
:description => 'Directory where disk conversion takes place, will make subdir for each VM, Default: /tmp/',
:format => String
}
FORMAT = {
:name => 'format',
:short => '-f type',
:large => '--format type',
:description => 'Disk format [ qcow2 | raw ], Default: qcow2',
:format => String
}
DELETE = {
:name => 'delete',
:large => '--delete-after',
:description => 'Removes the leftover conversion directory in the working directory which contains the converted VM disk'\
's and descriptor files'
}
CONTEXT = {
:name => 'context',
:large => '--context-package /path/to/context/',
:description => 'Directory with the context packages for guest OS injection, will search by default in /var/lib/one/context/',
:format => String
}
ESXI = {
:name => 'esxi_ip',
:large => '--esxi ip',
:description => "Transfer directly from ESXi host instead using provided IP, may be useful if vCenter download is slow. "\
"Requires SSH access to ESXi host. The vCenter credentials are required to gather other information abou"\
"t the virtual machine.",
:format => String
}
ESXI_USER = {
:name => 'esxi_user',
:large => '--esxi-user username',
:description => "ESXi username for SSH login, default: root",
:format => String
}
ESXI_PASS = {
:name => 'esxi_pass',
:large => '--esxi-pass password',
:description => "ESXi password for SSH login. Required for direct ESXi transfer.",
:format => String
}
VDDK = {
:name => 'vddk_path',
:large => '--vddk /path/to/vddk/',
:description => 'Full path to the VDDK library, required for VDDK based transfer',
:format => String
}
CUSTOM = { # not sure what to call this actually
:name => 'custom_convert',
:large => '--custom',
:description => 'Forces the use of OpenNebula\'s custom conversion process. This will skip virt-v2v conversion attempts.'
}
FALLBACK = {
:name => 'fallback',
:large => '--fallback',
:description => 'If the virt-v2v conversion fails, attempt the OpenNebula Custom Conversion process.'
}
HYBRID = {
:name => 'hybrid',
:large => '--hybrid',
}
WIN_GA = {
:name => 'qemu_ga_win',
:large => '--win-qemu-ga /path/to/iso',
:description => 'Install QEMU Guest Agent to a Windows guest',
:format => String
}
LNX_GA = {
:name => 'qemu_ga_linux',
:large => '--qemu-ga',
:description => 'Install qemu-guest-agent package to a Linux guest, useful with --custom or --fallback'
}
VIRTIO = {
:name => 'virtio_path',
:large => '--virtio /path/to/iso',
:description => 'Full path of the win-virtio ISO file. Required to inject virtio drivers to Windows Guests',
:format => String
}
RHSRVANY = {
:name => 'virt_tools',
:large => '--virt-tools /path/to/virt-tools',
:description => 'Path to the directory containing rhsrvany.exe, defaults to /usr/share/virt-tools. See https://github.com/rwmjones/rhsrvany.',
:format => String
}
ONE_CLUSTER = {
:name => 'one_cluster',
:large => '--one-cluster id',
:description => 'ID of the Cluster in OpenNebula the VM Template should be scheduled to',
:format => Integer
}
ONE_HOST = {
:name => 'one_host',
:large => '--one-host id',
:description => 'ID of the Host in OpenNebula the VM Template should be scheduled to',
:format => Integer
}
ONE_DS = {
:name => 'one_datastore',
:large => '--one-sys-ds id',
:description => 'ID of the System Datastore in OpenNebula the VM should be scheduled to',
:format => Integer
}
ONE_DS_CLUSTER = {
:name => 'one_datastore_cluster',
:large => '--one-ds-cluster id',
:description => 'ID of the Cluster in OpenNebula the System Datastore should be scheduled to',
:format => Integer
}
V2V_PATH = {
:name => 'v2v_path',
:large => '--v2v-path /path/to/custom/v2v',
:description => 'Path to custom virt-v2v executable if necessary',
:format => String
}
CONF_FILE = {
:name => 'config_file',
:large => '--config-file /path/to/custom/config.yaml',
:description => 'Path to custom Configuration File, default is /var/lib/one/oneswap.yaml',
:format => String
}
ESXI_OPTS = [ESXI, ESXI_USER, ESXI_PASS]
V2V_OPTS = [WORK_DIR, FORMAT, VIRTIO, WIN_GA, LNX_GA, DELETE, VDDK, RHSRVANY]
LIST_OPTS = [NAME, DATACENTER, CLUSTER, STATE] + AUTH_OPTS
CONVERT_OPTS = [NETWORK, NO_IP, NO_MAC, DATASTORE, CONTEXT, FALLBACK, CUSTOM, HYBRID, V2V_PATH, CONF_FILE, IMG_WAIT, DEV_PREFIX,
CPU_MODEL, GRAPHICS_TYPE, GRAPHICS_LISTEN, GRAPHICS_PORT, GRAPHICS_KEYMAP, GRAPHICS_PASSWORD, GRAPHICS_COMMAND, DISABLE_CONTEXTUALIZATION,
PERSISTENT_IMG, MEMORY_MAX, VCPU_MAX, CPU, VCPU, ONE_DS, ONE_DS_CLUSTER, ONE_CLUSTER, ONE_HOST] + AUTH_OPTS + ESXI_OPTS + V2V_OPTS
set :option, CommandParser::OPTIONS + OpenNebulaHelper::CLIENT_OPTIONS
############################################################################
# list resources
############################################################################
list_desc = <<-EOT.unindent
Show a list with vCenter objects, default to VM
Examples:
- listing all VMs:
oneswap list vms
- listing available Clusters:
oneswap list clusters
- listing available vms in a Datacenter and Cluster:
oneswap list vms --datacenter DCName --cluster Cluster2
EOT
command :list, list_desc, :object, :options => LIST_OPTS do
begin
options[:object] = args[0]
args = helper.parse_opts(options)
helper.list(options)
rescue StandardError => e
STDERR.puts e.message
exit 1
end
0
end
conv_desc = <<-EOT.unindent
Convert a vCenter Virtual Machine
Examples:
VOPTS='--vcenter 12.34.56.78 --vuser [email protected] --vpass changeme123'
- Convert a virtual machine:
oneswap convert vm-1234 $VOPTS [--fallback|--custom] [--network ID] [--datacenter ID]
- Convert a virtual machine from ESXi directly:
oneswap convert vm-1234 $VOPTS --esxi 12.34.56.79 --esxi_pass changeme123 [--esxi_user root]
- Convert a vCenter virtual machine utilizing the proprietary VDDK library(faster transfer usually):
oneswap convert vm-1234 $VOPTS --vddk /path/to/vddk-lib
- Convert using OpenNebula Custom Conversion (useful for distributions which are not supported or fail to convert)
You can also define --fallback instead of --custom, which will first attempt virt-v2v style, then fallback to custom.
oneswap convert vm-1234 $VOPTS --custom
EOT
command :convert, conv_desc, :vm_name, :options => CONVERT_OPTS do
begin
if options[:config_file] && !File.exist?(options[:config_file])
raise "Unable to find the config file at #{options[:config_file]}"
end
raise 'ESXi Transfer requires at least IP and Password' if options[:esxi_ip] && options[:esxi_pass].nil?
raise 'ESXI and VDDK cannot be used at same time, recommend VDDK.' if options[:esxi_ip] && options[:vddk]
if options[:hybrid] && ( options[:custom] || options[:esxi_ip] || options[:vddk] )
raise 'Cannot use Hybrid option with Custom, ESXi, or VDDK options.'
end
if options[:virtio_path] && !File.exist?(options[:virtio_path])
raise "Windows VirtIO ISO cannot be found at #{options[:virtio_path]}"
end
if options[:qemu_ga_win] && !File.exist?(options[:qemu_ga_win])
raise "Windows QEMU Guest Agent ISO cannot be found at #{options[:qemu_ga_win]}"
end
if (options[:virtio_path] || options[:qemu_ga_win]) && options[:custom_convert]
vcv = `virt-customize --version`.split(' ')[1]
if Gem::Version.new(vcv) <= Gem::Version.new('1.49.9')
puts 'Windows VirtIO and QEMU Guest Agent require virt-customize >= 1.49.9.'
options[:virtio_path] = options[:qemu_ga_win] = false
end
end
if options[:fallback] && options[:custom_convert]
raise "Cannot define both --fallback and --custom."
end
options[:work_dir] ||= '/tmp'
options[:format] ||= 'qcow2'
options[:max_retries] ||= 0
options[:context_path] ||= '/var/lib/one/context/'
options[:esxi_user] ||= 'root'
options[:datastore] ||= 1
options[:virt_tools] ||= '/usr/share/virt-tools'
options[:v2v_path] ||= 'virt-v2v'
options[:config_file] ||= '/var/lib/one/oneswap.yaml'
cfg_file = YAML.load_file(options[:config_file]) if File.exist?(options[:config_file])
options.merge!(cfg_file) if cfg_file
helper.convert(args[0], options)
rescue StandardError => e
STDERR.puts e.message
exit 1
end
0
end
end