knife-vsphere 1.0.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/chef/knife/base_vsphere_command.rb +383 -371
- data/lib/chef/knife/customization_helper.rb +40 -0
- data/lib/chef/knife/vsphere_cluster_list.rb +47 -0
- data/lib/chef/knife/vsphere_cpu_ratio.rb +41 -45
- data/lib/chef/knife/vsphere_customization_list.rb +24 -29
- data/lib/chef/knife/vsphere_datastore_list.rb +68 -72
- data/lib/chef/knife/vsphere_datastore_maxfree.rb +48 -49
- data/lib/chef/knife/vsphere_datastorecluster_list.rb +66 -71
- data/lib/chef/knife/vsphere_datastorecluster_maxfree.rb +75 -84
- data/lib/chef/knife/vsphere_folder_list.rb +28 -30
- data/lib/chef/knife/vsphere_hosts_list.rb +42 -42
- data/lib/chef/knife/vsphere_pool_list.rb +46 -48
- data/lib/chef/knife/vsphere_pool_query.rb +58 -58
- data/lib/chef/knife/vsphere_template_list.rb +30 -32
- data/lib/chef/knife/vsphere_vlan_create.rb +51 -0
- data/lib/chef/knife/vsphere_vlan_list.rb +35 -37
- data/lib/chef/knife/vsphere_vm_clone.rb +834 -581
- data/lib/chef/knife/vsphere_vm_config.rb +48 -46
- data/lib/chef/knife/vsphere_vm_delete.rb +70 -66
- data/lib/chef/knife/vsphere_vm_execute.rb +62 -66
- data/lib/chef/knife/vsphere_vm_list.rb +57 -61
- data/lib/chef/knife/vsphere_vm_markastemplate.rb +48 -54
- data/lib/chef/knife/vsphere_vm_migrate.rb +73 -0
- data/lib/chef/knife/vsphere_vm_move.rb +88 -0
- data/lib/chef/knife/vsphere_vm_net.rb +57 -0
- data/lib/chef/knife/vsphere_vm_property_get.rb +44 -46
- data/lib/chef/knife/vsphere_vm_property_set.rb +83 -84
- data/lib/chef/knife/vsphere_vm_query.rb +48 -48
- data/lib/chef/knife/vsphere_vm_snapshot.rb +124 -130
- data/lib/chef/knife/vsphere_vm_state.rb +122 -127
- data/lib/chef/knife/vsphere_vm_toolsconfig.rb +54 -52
- data/lib/chef/knife/vsphere_vm_vmdk_add.rb +234 -241
- data/lib/chef/knife/vsphere_vm_wait_sysprep.rb +54 -0
- data/lib/knife-vsphere/version.rb +3 -4
- metadata +43 -15
- data/lib/chef/knife/vshpere_vm_migrate.rb +0 -80
- data/lib/chef/knife/vshpere_vm_move.rb +0 -92
- data/lib/chef/knife/vshpere_vm_net.rb +0 -57
@@ -1,48 +1,48 @@
|
|
1
|
-
# Author:: Brian Dupras (<bdupras@rallydev.com>)
|
2
|
-
# License:: Apache License, Version 2.0
|
3
|
-
|
4
|
-
require 'chef/knife'
|
5
|
-
require 'chef/knife/base_vsphere_command'
|
6
|
-
require 'rbvmomi'
|
7
|
-
require 'netaddr'
|
8
|
-
|
9
|
-
class Chef::Knife::VsphereVmQuery < Chef::Knife::BaseVsphereCommand
|
10
|
-
banner "knife vsphere vm query VMNAME QUERY. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html\" for allowed QUERY values."
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def run
|
15
|
-
$stdout.sync = true
|
16
|
-
vmname = @name_args[0]
|
17
|
-
if vmname.nil?
|
18
|
-
show_usage
|
19
|
-
fatal_exit(
|
20
|
-
end
|
21
|
-
|
22
|
-
query_string = @name_args[1]
|
23
|
-
if query_string.nil?
|
24
|
-
show_usage
|
25
|
-
fatal_exit(
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
dc =
|
31
|
-
folder = find_folder(get_config(:folder)) || dc.vmFolder
|
32
|
-
|
33
|
-
vm = traverse_folders_for_vm(folder, vmname)
|
34
|
-
|
35
|
-
# split QUERY by dots, and walk the object model
|
36
|
-
query = query_string.split '.'
|
37
|
-
result = vm
|
38
|
-
query.each do |part|
|
39
|
-
message, index = part.split(/[\[\]]/)
|
40
|
-
unless result.respond_to? message.to_sym
|
41
|
-
fatal_exit("\"#{query_string}\" not recognized.")
|
42
|
-
end
|
43
|
-
|
44
|
-
result = index ? result.send(message)[index.to_i] : result.send(message)
|
45
|
-
end
|
46
|
-
puts result
|
47
|
-
end
|
48
|
-
end
|
1
|
+
# Author:: Brian Dupras (<bdupras@rallydev.com>)
|
2
|
+
# License:: Apache License, Version 2.0
|
3
|
+
|
4
|
+
require 'chef/knife'
|
5
|
+
require 'chef/knife/base_vsphere_command'
|
6
|
+
require 'rbvmomi'
|
7
|
+
require 'netaddr'
|
8
|
+
|
9
|
+
class Chef::Knife::VsphereVmQuery < Chef::Knife::BaseVsphereCommand
|
10
|
+
banner "knife vsphere vm query VMNAME QUERY. See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.VirtualMachine.html\" for allowed QUERY values."
|
11
|
+
|
12
|
+
common_options
|
13
|
+
|
14
|
+
def run
|
15
|
+
$stdout.sync = true
|
16
|
+
vmname = @name_args[0]
|
17
|
+
if vmname.nil?
|
18
|
+
show_usage
|
19
|
+
fatal_exit('You must specify a virtual machine name')
|
20
|
+
end
|
21
|
+
|
22
|
+
query_string = @name_args[1]
|
23
|
+
if query_string.nil?
|
24
|
+
show_usage
|
25
|
+
fatal_exit('You must specify a QUERY value (e.g. guest.ipAddress or network[0].name)')
|
26
|
+
end
|
27
|
+
|
28
|
+
vim_connection
|
29
|
+
|
30
|
+
dc = datacenter
|
31
|
+
folder = find_folder(get_config(:folder)) || dc.vmFolder
|
32
|
+
|
33
|
+
vm = traverse_folders_for_vm(folder, vmname) || abort("VM #{vmname} not found")
|
34
|
+
|
35
|
+
# split QUERY by dots, and walk the object model
|
36
|
+
query = query_string.split '.'
|
37
|
+
result = vm
|
38
|
+
query.each do |part|
|
39
|
+
message, index = part.split(/[\[\]]/)
|
40
|
+
unless result.respond_to? message.to_sym
|
41
|
+
fatal_exit("\"#{query_string}\" not recognized.")
|
42
|
+
end
|
43
|
+
|
44
|
+
result = index ? result.send(message)[index.to_i] : result.send(message)
|
45
|
+
end
|
46
|
+
puts result
|
47
|
+
end
|
48
|
+
end
|
@@ -1,130 +1,124 @@
|
|
1
|
-
#
|
2
|
-
# License:: Apache License, Version 2.0
|
3
|
-
#
|
4
|
-
|
5
|
-
require 'chef/knife'
|
6
|
-
require 'chef/knife/base_vsphere_command'
|
7
|
-
require 'rbvmomi'
|
8
|
-
require 'netaddr'
|
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
|
-
end
|
72
|
-
|
73
|
-
if config[:
|
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
|
-
if !node.childSnapshotList.empty?
|
126
|
-
node.childSnapshotList.each { |item| out << display_node(item, current, shift+1) }
|
127
|
-
end
|
128
|
-
out
|
129
|
-
end
|
130
|
-
end
|
1
|
+
#
|
2
|
+
# License:: Apache License, Version 2.0
|
3
|
+
#
|
4
|
+
|
5
|
+
require 'chef/knife'
|
6
|
+
require 'chef/knife/base_vsphere_command'
|
7
|
+
require 'rbvmomi'
|
8
|
+
require 'netaddr'
|
9
|
+
|
10
|
+
# Manage snapshots of a virtual machine
|
11
|
+
class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
|
12
|
+
banner 'knife vsphere vm snapshot VMNAME (options)'
|
13
|
+
|
14
|
+
common_options
|
15
|
+
|
16
|
+
option :list,
|
17
|
+
long: '--list',
|
18
|
+
description: 'The current tree of snapshots'
|
19
|
+
|
20
|
+
option :create_new_snapshot,
|
21
|
+
long: '--create SNAPSHOT',
|
22
|
+
description: 'Create a new snapshot off of the current snapshot.'
|
23
|
+
|
24
|
+
option :remove_named_snapshot,
|
25
|
+
long: '--remove SNAPSHOT',
|
26
|
+
description: 'Remove a named snapshot.'
|
27
|
+
|
28
|
+
option :revert_snapshot,
|
29
|
+
long: '--revert SNAPSHOT',
|
30
|
+
description: 'Revert to a named snapshot.'
|
31
|
+
|
32
|
+
option :revert_current_snapshot,
|
33
|
+
long: '--revert-current',
|
34
|
+
description: 'Revert to current snapshot.',
|
35
|
+
boolean: false
|
36
|
+
|
37
|
+
option :power,
|
38
|
+
long: '--start',
|
39
|
+
description: 'Indicates whether to start the VM after a successful revert',
|
40
|
+
boolean: false
|
41
|
+
|
42
|
+
def run
|
43
|
+
$stdout.sync = true
|
44
|
+
|
45
|
+
vmname = @name_args[0]
|
46
|
+
if vmname.nil?
|
47
|
+
show_usage
|
48
|
+
ui.fatal('You must specify a virtual machine name')
|
49
|
+
exit 1
|
50
|
+
end
|
51
|
+
|
52
|
+
vim_connection
|
53
|
+
|
54
|
+
base_folder = find_folder(get_config(:folder))
|
55
|
+
|
56
|
+
vm = find_in_folder(base_folder, RbVmomi::VIM::VirtualMachine, vmname) || abort("VM #{vmname} not found")
|
57
|
+
|
58
|
+
if vm.snapshot
|
59
|
+
snapshot_list = vm.snapshot.rootSnapshotList
|
60
|
+
current_snapshot = vm.snapshot.currentSnapshot
|
61
|
+
end
|
62
|
+
|
63
|
+
if config[:list] && vm.snapshot
|
64
|
+
puts 'Current snapshot tree: '
|
65
|
+
puts "#{vmname}"
|
66
|
+
snapshot_list.each { |i| puts display_node(i, current_snapshot) }
|
67
|
+
end
|
68
|
+
|
69
|
+
if config[:create_new_snapshot]
|
70
|
+
vm.CreateSnapshot_Task(name: config[:create_new_snapshot], description: '', memory: false, quiesce: false)
|
71
|
+
end
|
72
|
+
|
73
|
+
if config[:remove_named_snapshot]
|
74
|
+
ss_name = config[:remove_named_snapshot]
|
75
|
+
snapshot = find_node(snapshot_list, ss_name)
|
76
|
+
puts "Found snapshot #{ss_name} removing."
|
77
|
+
snapshot.RemoveSnapshot_Task(removeChildren: false)
|
78
|
+
end
|
79
|
+
|
80
|
+
if config[:revert_current_snapshot]
|
81
|
+
puts 'Reverting to Current Snapshot'
|
82
|
+
vm.RevertToCurrentSnapshot_Task(suppressPowerOn: false).wait_for_completion
|
83
|
+
if get_config(:power)
|
84
|
+
vm.PowerOnVM_Task.wait_for_completion
|
85
|
+
puts "Powered on virtual machine #{vmname}"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
return unless config[:revert_snapshot]
|
90
|
+
ss_name = config[:revert_snapshot]
|
91
|
+
snapshot = find_node(snapshot_list, ss_name)
|
92
|
+
snapshot.RevertToSnapshot_Task(suppressPowerOn: false).wait_for_completion
|
93
|
+
return unless get_config(:power)
|
94
|
+
vm.PowerOnVM_Task.wait_for_completion
|
95
|
+
puts "Powered on virtual machine #{vmname}"
|
96
|
+
end
|
97
|
+
|
98
|
+
def find_node(tree, name)
|
99
|
+
snapshot = nil
|
100
|
+
tree.each do |node|
|
101
|
+
if node.name == name
|
102
|
+
snapshot = node.snapshot
|
103
|
+
break
|
104
|
+
elsif !node.childSnapshotList.empty?
|
105
|
+
snapshot = find_node(node.childSnapshotList, name)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
snapshot
|
109
|
+
end
|
110
|
+
|
111
|
+
def display_node(node, current, shift = 1)
|
112
|
+
out = ''
|
113
|
+
out << '+--' * shift
|
114
|
+
if node.snapshot == current
|
115
|
+
out << "#{ui.color(node.name, :cyan)}" << "\n"
|
116
|
+
else
|
117
|
+
out << "#{node.name}" << "\n"
|
118
|
+
end
|
119
|
+
unless node.childSnapshotList.empty?
|
120
|
+
node.childSnapshotList.each { |item| out << display_node(item, current, shift + 1) }
|
121
|
+
end
|
122
|
+
out
|
123
|
+
end
|
124
|
+
end
|
@@ -1,127 +1,122 @@
|
|
1
|
-
#
|
2
|
-
# Author:: Ezra Pagel (<ezra@cpan.org>)
|
3
|
-
# License:: Apache License, Version 2.0
|
4
|
-
#
|
5
|
-
|
6
|
-
require 'chef/knife'
|
7
|
-
require 'chef/knife/base_vsphere_command'
|
8
|
-
require 'rbvmomi'
|
9
|
-
require 'netaddr'
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
}
|
20
|
-
|
21
|
-
# Manage power state of a virtual machine
|
22
|
-
class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
|
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
|
-
puts "done"
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
1
|
+
#
|
2
|
+
# Author:: Ezra Pagel (<ezra@cpan.org>)
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
#
|
5
|
+
|
6
|
+
require 'chef/knife'
|
7
|
+
require 'chef/knife/base_vsphere_command'
|
8
|
+
require 'rbvmomi'
|
9
|
+
require 'netaddr'
|
10
|
+
|
11
|
+
PS_ON = 'poweredOn'
|
12
|
+
PS_OFF = 'poweredOff'
|
13
|
+
PS_SUSPENDED = 'suspended'
|
14
|
+
|
15
|
+
POWER_STATES = {
|
16
|
+
PS_ON => 'powered on',
|
17
|
+
PS_OFF => 'powered off',
|
18
|
+
PS_SUSPENDED => 'suspended'
|
19
|
+
}
|
20
|
+
|
21
|
+
# Manage power state of a virtual machine
|
22
|
+
class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
|
23
|
+
banner 'knife vsphere vm state VMNAME (options)'
|
24
|
+
|
25
|
+
common_options
|
26
|
+
|
27
|
+
option :state,
|
28
|
+
short: '-s STATE',
|
29
|
+
long: '--state STATE',
|
30
|
+
description: 'The power state to transition the VM into; one of on|off|suspended'
|
31
|
+
|
32
|
+
option :wait_port,
|
33
|
+
short: '-w PORT',
|
34
|
+
long: '--wait-port PORT',
|
35
|
+
description: 'Wait for VM to be accessible on a port'
|
36
|
+
|
37
|
+
option :shutdown,
|
38
|
+
short: '-g',
|
39
|
+
long: '--shutdown',
|
40
|
+
description: 'Guest OS shutdown'
|
41
|
+
|
42
|
+
option :recursive,
|
43
|
+
short: '-r',
|
44
|
+
long: '--recursive',
|
45
|
+
description: 'Search all folders'
|
46
|
+
|
47
|
+
def run
|
48
|
+
$stdout.sync = true
|
49
|
+
|
50
|
+
vmname = @name_args[0]
|
51
|
+
if vmname.nil?
|
52
|
+
show_usage
|
53
|
+
ui.fatal('You must specify a virtual machine name')
|
54
|
+
exit 1
|
55
|
+
end
|
56
|
+
|
57
|
+
vim_connection
|
58
|
+
|
59
|
+
if get_config(:recursive)
|
60
|
+
vms = get_vms(vmname)
|
61
|
+
if vms.length > 1
|
62
|
+
abort "More than one VM with name #{vmname} found:\n" + vms.map { |vm| get_path_to_object(vm) }.join("\n")
|
63
|
+
end
|
64
|
+
abort "VM #{vmname} not found" if vms.length == 0
|
65
|
+
vm = vms[0]
|
66
|
+
else
|
67
|
+
base_folder = find_folder(get_config(:folder))
|
68
|
+
|
69
|
+
vm = find_in_folder(base_folder, RbVmomi::VIM::VirtualMachine, vmname) || abort("VM #{vmname} not found")
|
70
|
+
end
|
71
|
+
|
72
|
+
state = vm.runtime.powerState
|
73
|
+
|
74
|
+
if config[:state].nil?
|
75
|
+
puts "VM #{vmname} is currently " + POWER_STATES[vm.runtime.powerState]
|
76
|
+
else
|
77
|
+
|
78
|
+
case config[:state]
|
79
|
+
when 'on'
|
80
|
+
if state == PS_ON
|
81
|
+
puts "Virtual machine #{vmname} was already powered on"
|
82
|
+
else
|
83
|
+
vm.PowerOnVM_Task.wait_for_completion
|
84
|
+
puts "Powered on virtual machine #{vmname}"
|
85
|
+
end
|
86
|
+
when 'off'
|
87
|
+
if state == PS_OFF
|
88
|
+
puts "Virtual machine #{vmname} was already powered off"
|
89
|
+
else
|
90
|
+
if get_config(:shutdown)
|
91
|
+
vm.ShutdownGuest
|
92
|
+
print "Waiting for virtual machine #{vmname} to shut down..."
|
93
|
+
until vm.runtime.powerState == PS_OFF
|
94
|
+
sleep 2
|
95
|
+
print '.'
|
96
|
+
end
|
97
|
+
puts 'done'
|
98
|
+
else
|
99
|
+
vm.PowerOffVM_Task.wait_for_completion
|
100
|
+
puts "Powered off virtual machine #{vmname}"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
when 'suspend'
|
104
|
+
if state == POWER_STATES['suspended']
|
105
|
+
puts "Virtual machine #{vmname} was already suspended"
|
106
|
+
else
|
107
|
+
vm.SuspendVM_Task.wait_for_completion
|
108
|
+
puts "Suspended virtual machine #{vmname}"
|
109
|
+
end
|
110
|
+
when 'reset'
|
111
|
+
vm.ResetVM_Task.wait_for_completion
|
112
|
+
puts "Reset virtual machine #{vmname}"
|
113
|
+
end
|
114
|
+
|
115
|
+
if get_config(:wait_port)
|
116
|
+
print "Waiting for port #{get_config(:wait_port)}..."
|
117
|
+
print '.' until tcp_test_port_vm(vm, get_config(:wait_port))
|
118
|
+
puts 'done'
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|