knife-vsphere 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f069c4c03cfac41d16940d9a0001ce6e5782baf
4
- data.tar.gz: 747a52f56b215dfbee8b1c5260422513f0fcc5f6
3
+ metadata.gz: 7da50d689bdd4164cea3df391395eca1a9acb4e5
4
+ data.tar.gz: 6682cf4d67ef8da7290a9fda877813b8ab33a8e5
5
5
  SHA512:
6
- metadata.gz: ee2324c39ef703042d4aa565c9af42c18f9dd91a582ac6af875c8d691b17662a6df07162ad7a6265672eeecacd3ca9a6b53f3238a19773c5bb3bb74fcfba57a0
7
- data.tar.gz: d9ef842ef5b50f283086b6a28efdbc8c5df0d0f6d259498f51c149b9c7c7576ca189328640568c47c443032b0665af03b03767e57f7e3193ec989635ccb39a84
6
+ metadata.gz: e1f0c1e19b98f0292b9596ed8df1fae872f88c17d8fb3e0ba0d4f0d78a115a54e18c0b224165d19fd6b0e22bd02c492d151312e7a932708636f96c7b305b19fd
7
+ data.tar.gz: 2f286acc8610eeba75e6edadb76a3c6282b2243eec4fbd7c1582962b5f3b5baea2a57041db9f6e94ee3308035f80a526c08fe0d448ab07717aacddb0f31939fc
@@ -0,0 +1,41 @@
1
+ # Some helpers for faster searching of the inventory
2
+ module SearchHelper
3
+ # Retrieves all the VM objects and returns their ObjectContents
4
+ # Note that since it's a ObjectContent coming back, the individual
5
+ # object's [] will return only the properties you asked for
6
+ # and `#obj` will return the actual object (but make a call to the server)
7
+ # param [Array<String>] properties to retrieve
8
+ # @return [Array<RbVmomi::VIM::ObjectContent>]
9
+ def get_all_vm_objects(properties = ['name'])
10
+ pc = vim_connection.serviceInstance.content.propertyCollector
11
+ viewmgr = vim_connection.serviceInstance.content.viewManager
12
+ root_folder = vim_connection.serviceInstance.content.rootFolder
13
+ vmview = viewmgr.CreateContainerView(container: root_folder,
14
+ type: ['VirtualMachine'],
15
+ recursive: true)
16
+
17
+ filter_spec = RbVmomi::VIM.PropertyFilterSpec(
18
+ objectSet: [
19
+ obj: vmview,
20
+ skip: true,
21
+ selectSet: [
22
+ RbVmomi::VIM.TraversalSpec(
23
+ name: 'traverseEntities',
24
+ type: 'ContainerView',
25
+ path: 'view',
26
+ skip: false
27
+ )
28
+ ]
29
+ ],
30
+ propSet: [
31
+ { type: 'VirtualMachine', pathSet: properties }
32
+ ]
33
+ )
34
+ pc.RetrieveProperties(specSet: [filter_spec])
35
+ end
36
+
37
+ def get_vm_by_name(vmname)
38
+ vm = get_all_vm_objects.detect { |r| r['name'] == vmname }
39
+ vm ? vm.obj : nil
40
+ end
41
+ end
@@ -3,11 +3,11 @@
3
3
 
4
4
  require 'chef/knife'
5
5
  require 'chef/knife/base_vsphere_command'
6
- require 'rbvmomi'
7
- require 'netaddr'
6
+ require 'chef/knife/search_helper'
8
7
 
9
8
  # VsphereVMconfig extends the BaseVspherecommand
10
9
  class Chef::Knife::VsphereVmConfig < Chef::Knife::BaseVsphereCommand
10
+ include SearchHelper
11
11
  banner "knife vsphere vm config VMNAME PROPERTY VALUE.
12
12
  See \"http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html\"
13
13
  for allowed ATTRIBUTE values (any property of type xs:string is supported)."
@@ -39,10 +39,7 @@ class Chef::Knife::VsphereVmConfig < Chef::Knife::BaseVsphereCommand
39
39
 
40
40
  vim_connection
41
41
 
42
- dc = datacenter
43
- folder = find_folder(get_config(:folder)) || dc.vmFolder
44
-
45
- vm = traverse_folders_for_vm(folder, vmname) || abort("VM #{vmname} not found")
42
+ vm = get_vm_by_name(vmname) || fatal_exit("Could not find #{vmname}")
46
43
 
47
44
  properties = {}
48
45
  properties[property_name] = property_value
@@ -1,9 +1,12 @@
1
1
  require 'chef/knife'
2
2
  require 'chef/knife/base_vsphere_command'
3
+ require 'chef/knife/search_helper'
3
4
 
4
5
  # List the disks attached to a VM
5
6
  # VsphereVmdisklist extends the BaseVspherecommand
6
7
  class Chef::Knife::VsphereVmDiskList < Chef::Knife::BaseVsphereCommand
8
+ include SearchHelper
9
+
7
10
  banner 'knife vsphere vm disk list VMNAME'
8
11
 
9
12
  common_options
@@ -18,9 +21,7 @@ class Chef::Knife::VsphereVmDiskList < Chef::Knife::BaseVsphereCommand
18
21
  fatal_exit 'You must specify a virtual machine name'
19
22
  end
20
23
 
21
- vim_connection
22
- vm = get_vm(vmname)
23
- fatal_exit "Could not find #{vmname}" unless vm
24
+ vm = get_vm_by_name(vmname) || fatal_exit("Could not find #{vmname}")
24
25
 
25
26
  disks = vm.config.hardware.device.select do |device|
26
27
  device.is_a? RbVmomi::VIM::VirtualDisk
@@ -39,6 +39,14 @@ class Chef::Knife::VsphereVmFind < Chef::Knife::BaseVsphereCommand
39
39
  long: '--cpu',
40
40
  description: 'Show cpu'
41
41
 
42
+ option :cpu_hot_add_enabled,
43
+ long: '--cpu_hot_add_enabled',
44
+ description: 'Show cpu hot add enabled'
45
+
46
+ option :memory_hot_add_enabled,
47
+ long: '--memory_hot_add_enabled',
48
+ description: 'Show memory hot add enabled'
49
+
42
50
  option :ram,
43
51
  long: '--ram',
44
52
  description: 'Show ram'
@@ -235,6 +243,14 @@ class Chef::Knife::VsphereVmFind < Chef::Knife::BaseVsphereCommand
235
243
  thisvm['ram'] = vm.summary.config.memorySizeMB
236
244
  end
237
245
 
246
+ if get_config(:cpu_hot_add_enabled)
247
+ thisvm['cpu_hot_add_enabled'] = vm.config.cpuHotAddEnabled
248
+ end
249
+
250
+ if get_config(:memory_hot_add_enabled)
251
+ thisvm['memory_hot_add_enabled'] = vm.config.memoryHotAddEnabled
252
+ end
253
+
238
254
  if get_config(:cpu)
239
255
  thisvm['cpu'] = vm.summary.config.numCpu
240
256
  end
@@ -3,11 +3,11 @@
3
3
 
4
4
  require 'chef/knife'
5
5
  require 'chef/knife/base_vsphere_command'
6
- require 'rbvmomi'
7
- require 'netaddr'
6
+ require 'chef/knife/search_helper'
8
7
 
9
8
  # VsphereVMPropertySet extends Basevspherecommand
10
9
  class Chef::Knife::VsphereVmPropertySet < Chef::Knife::BaseVsphereCommand
10
+ include SearchHelper
11
11
  banner 'knife vsphere vm property set VMNAME PROPERTY VALUE. Sets a vApp Property on VMNAME.'
12
12
 
13
13
  common_options
@@ -42,9 +42,8 @@ class Chef::Knife::VsphereVmPropertySet < Chef::Knife::BaseVsphereCommand
42
42
  vim_connection
43
43
 
44
44
  dc = datacenter
45
- folder = find_folder(get_config(:folder)) || dc.vmFolder
46
45
 
47
- vm = find_in_folder(folder, RbVmomi::VIM::VirtualMachine, vmname) || abort("VM #{vmname} not found")
46
+ vm = get_vm_by_name(vmname) || fatal_exit("Could not find #{vmname}")
48
47
 
49
48
  if vm.config.vAppConfig && vm.config.vAppConfig.property
50
49
  existing_property = vm.config.vAppConfig.property.find { |p| p.props[:id] == property_name.to_s }
@@ -4,11 +4,11 @@
4
4
 
5
5
  require 'chef/knife'
6
6
  require 'chef/knife/base_vsphere_command'
7
- require 'rbvmomi'
8
- require 'netaddr'
7
+ require 'chef/knife/search_helper'
9
8
 
10
9
  # Manage snapshots of a virtual machine
11
10
  class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
11
+ include SearchHelper
12
12
  banner 'knife vsphere vm snapshot VMNAME (options)'
13
13
 
14
14
  common_options
@@ -44,7 +44,7 @@ class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
44
44
  description: 'Indicates whether to wait for creation/removal to complete',
45
45
  boolean: false
46
46
 
47
- option :find,
47
+ option :find, # imma deprecate this
48
48
  long: '--find',
49
49
  description: 'Finds the virtual machine by searching all folders'
50
50
 
@@ -77,14 +77,7 @@ class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
77
77
 
78
78
  vim_connection
79
79
 
80
- vm = if get_config(:find)
81
- puts "No folder entered, searching for #{vmname}"
82
- src_folder = find_folder(get_config(:folder))
83
- traverse_folders_for_vm(src_folder, vmname)
84
- else
85
- base_folder = find_folder get_config(:folder)
86
- find_in_folder(base_folder, RbVmomi::VIM::VirtualMachine, vmname) || abort("VM #{vmname} not found")
87
- end
80
+ vm = get_vm_by_name(vmname) || fatal_exit("Could not find #{vmname}")
88
81
 
89
82
  if vm.snapshot
90
83
  snapshot_list = vm.snapshot.rootSnapshotList
@@ -92,9 +85,7 @@ class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
92
85
  end
93
86
 
94
87
  if get_config(:list) && vm.snapshot
95
- puts 'Current snapshot tree: '
96
- puts "#{vmname}"
97
- snapshot_list.each { |i| puts display_node(i, current_snapshot) }
88
+ ui.output(snapshot_list.map { |i| display_node(i, current_snapshot) })
98
89
  end
99
90
 
100
91
  if get_config(:create_new_snapshot)
@@ -146,18 +137,14 @@ class Chef::Knife::VsphereVmSnapshot < Chef::Knife::BaseVsphereCommand
146
137
  snapshot
147
138
  end
148
139
 
149
- def display_node(node, current, shift = 1)
150
- descr = node.name + ' ' + node.createTime.iso8601
151
- out = ''
152
- out << '+--' * shift
153
- if node.snapshot == current
154
- out << ui.color(descr, :cyan) << '\n'
155
- else
156
- out << descr << '\n'
157
- end
158
- unless node.childSnapshotList.empty?
159
- node.childSnapshotList.each { |item| out << display_node(item, current, shift + 1) }
160
- end
161
- out
140
+ def display_node(node, current)
141
+ children = node.childSnapshotList.map { |item| display_node(item, current) }
142
+ snapshot_tree = { 'SnapshotName' => node.name,
143
+ 'SnapshotId' => node.id,
144
+ 'SnapshotDescription' => node.description,
145
+ 'SnapshotCreationDate' => node.createTime.iso8601,
146
+ 'Children' => children }
147
+ snapshot_tree.merge!({ 'IsCurrentSnapshot' => true }) if node.snapshot == current
148
+ snapshot_tree
162
149
  end
163
150
  end
@@ -5,12 +5,12 @@
5
5
 
6
6
  require 'chef/knife'
7
7
  require 'chef/knife/base_vsphere_command'
8
- require 'rbvmomi'
9
- require 'netaddr'
8
+ require 'chef/knife/search_helper'
10
9
 
11
10
  # Manage power state of a virtual machine
12
11
  # VsphereVmState extends the BaseVspherecommand
13
12
  class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
13
+ include SearchHelper
14
14
  # The Different power states that vSphere reports
15
15
  POWER_STATES = {
16
16
  PS_ON => 'powered on',
@@ -56,18 +56,7 @@ class Chef::Knife::VsphereVmState < Chef::Knife::BaseVsphereCommand
56
56
 
57
57
  vim_connection
58
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
59
+ vm = get_vm_by_name(vmname) || fatal_exit("Could not find #{vmname}")
71
60
 
72
61
  state = vm.runtime.powerState
73
62
 
@@ -1,5 +1,5 @@
1
1
  # The main knife-vsphere module.
2
2
  module KnifeVsphere
3
3
  # The version of this gem.
4
- VERSION = '2.0.3'
4
+ VERSION = '2.0.4'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-vsphere
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ezra Pagel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-22 00:00:00.000000000 Z
11
+ date: 2018-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: knife-windows
@@ -172,6 +172,7 @@ extra_rdoc_files: []
172
172
  files:
173
173
  - lib/chef/knife/base_vsphere_command.rb
174
174
  - lib/chef/knife/customization_helper.rb
175
+ - lib/chef/knife/search_helper.rb
175
176
  - lib/chef/knife/vsphere_cluster_list.rb
176
177
  - lib/chef/knife/vsphere_cpu_ratio.rb
177
178
  - lib/chef/knife/vsphere_customization_list.rb
@@ -236,7 +237,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
236
237
  version: '0'
237
238
  requirements: []
238
239
  rubyforge_project:
239
- rubygems_version: 2.6.14
240
+ rubygems_version: 2.6.11
240
241
  signing_key:
241
242
  specification_version: 4
242
243
  summary: vSphere Support for Knife