knife-vsphere 1.2.21 → 1.2.22

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: 75be37e04c524b55d4e49cf27e95b5d689f4588f
4
- data.tar.gz: d68bd0bb8b73ce62437191cf74efec809e1412af
3
+ metadata.gz: c6a2061ebd4d7e9f14eb44fb90f8641e2f811c4c
4
+ data.tar.gz: 179ce77c203ad335c82a872ed55a93e8d4e8751c
5
5
  SHA512:
6
- metadata.gz: aa48a83dc562b4af0798e3faa1bb9cba61b01343bf215f1937950b8e5b13f0f8b3e8a58d6d9065271a3cd9e1ec6fb2e350170b8b5e6fd2d2a6189a53dab13151
7
- data.tar.gz: 84a0f2525640c43902a15ad7685ae91e9949f10dd26887d397ba20791713188387642aa8066862567f10b014bd3131be11113373c98530305711f070dd937d3a
6
+ metadata.gz: c78a89b81505d49aee5040c9863f128437bc55c54d4a7cd36e69e4af48ac700d1b89b7ac646b7f9ff0e5b11ff1c371b8d3d7e4ce407ee66fd2d094f2559658d6
7
+ data.tar.gz: d485ec257d46837ba95cb19ca1ed9148ce3193dafe1dfbf0eea04af3a6f3630286a67a2323349fdd6ce6ae893846ec87518db1da8c8277765af4be97896de0fe
@@ -146,6 +146,19 @@ class Chef
146
146
  false
147
147
  end
148
148
 
149
+ def traverse_folders_for_pools(folder)
150
+ retval = []
151
+ children = folder.children.find_all
152
+ children.each do |child|
153
+ if child.class == RbVmomi::VIM::ResourcePool
154
+ retval << child
155
+ elsif child.class == RbVmomi::VIM::Folder
156
+ retval.concat(traverse_folders_for_pools(child))
157
+ end
158
+ end
159
+ retval
160
+ end
161
+
149
162
  def traverse_folders_for_computeresources(folder)
150
163
  retval = []
151
164
  children = folder.children.find_all
@@ -185,6 +198,13 @@ class Chef
185
198
  false
186
199
  end
187
200
 
201
+ def find_pools_and_clusters(folder, poolname = nil)
202
+ pools = traverse_folders_for_pools(folder)
203
+ clusters = traverse_folders_for_computeresources(folder)
204
+ cluster_pool = clusters + pools
205
+ poolname.nil? ? cluster_pool : cluster_pool.select { |p| p.name == poolname }
206
+ end
207
+
188
208
  def datacenter
189
209
  dcname = get_config(:vsphere_dc)
190
210
  traverse_folders_for_dc(vim_connection.rootFolder, dcname) || abort('datacenter not found')
@@ -293,6 +313,23 @@ class Chef
293
313
  nil
294
314
  end
295
315
 
316
+ def number_to_human_size(number)
317
+ number = number.to_f
318
+ storage_units_fmt = %w(byte kB MB GB TB)
319
+ base = 1024
320
+ if number.to_i < base
321
+ unit = storage_units_fmt[0]
322
+ else
323
+ max_exp = storage_units_fmt.size - 1
324
+ exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
325
+ exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
326
+ number /= base**exponent
327
+ unit = storage_units_fmt[exponent]
328
+ end
329
+
330
+ format('%0.2f %s', number, unit)
331
+ end
332
+
296
333
  def find_device(vm, deviceName)
297
334
  vm.config.hardware.device.each do |device|
298
335
  return device if device.deviceInfo.label == deviceName
@@ -17,11 +17,11 @@ class Chef::Knife::VsphereClusterList < Chef::Knife::BaseVsphereCommand
17
17
 
18
18
  if folder.is_a? RbVmomi::VIM::ClusterComputeResource
19
19
  clusters = folder.path[3..-1].reject { |p| p.last == 'ClusterComputeResource' }
20
- puts "#{ui.color('Cluster', :cyan)}: " + clusters.map(&:last).join('/')
20
+ return { 'Cluster' => clusters.map(&:last).join('/') }
21
21
  end
22
22
 
23
23
  folders = find_all_in_folder(folder, RbVmomi::VIM::ManagedObject) || []
24
- folders.each do |child|
24
+ folders.map do |child|
25
25
  traverse_folders(child)
26
26
  end
27
27
  end
@@ -42,6 +42,6 @@ class Chef::Knife::VsphereClusterList < Chef::Knife::BaseVsphereCommand
42
42
  def run
43
43
  vim_connection
44
44
  base_folder = find_cluster_folder(get_config(:folder))
45
- traverse_folders(base_folder)
45
+ ui.output(traverse_folders(base_folder))
46
46
  end
47
47
  end
@@ -18,23 +18,6 @@
18
18
  require 'chef/knife'
19
19
  require 'chef/knife/base_vsphere_command'
20
20
 
21
- def number_to_human_size(number)
22
- number = number.to_f
23
- storage_units_fmt = %w(byte kB MB GB TB)
24
- base = 1024
25
- if number.to_i < base
26
- unit = storage_units_fmt[0]
27
- else
28
- max_exp = storage_units_fmt.size - 1
29
- exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
30
- exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
31
- number /= base**exponent
32
- unit = storage_units_fmt[exponent]
33
- end
34
-
35
- format('%0.2f %s', number, unit)
36
- end
37
-
38
21
  # Lists all known data stores in datacenter with sizes
39
22
  class Chef::Knife::VsphereDatastoreList < Chef::Knife::BaseVsphereCommand
40
23
  banner 'knife vsphere datastore list'
@@ -46,23 +29,49 @@ class Chef::Knife::VsphereDatastoreList < Chef::Knife::BaseVsphereCommand
46
29
  short: '-L',
47
30
  description: "Indicates whether to list VM's in datastore",
48
31
  boolean: true
32
+ option :pool,
33
+ long: '--pool pool',
34
+ description: 'Target pool'
49
35
 
50
36
  def run
51
37
  $stdout.sync = true
52
38
 
53
39
  vim_connection
54
40
  dc = datacenter
55
- dc.datastore.each do |store|
41
+ folder = dc.hostFolder
42
+ target_pool = get_config(:pool)
43
+
44
+ pools = find_pools_and_clusters(folder, target_pool)
45
+ if target_pool && pools.empty?
46
+ puts "Pool #{target_pool} not found"
47
+ return
48
+ end
49
+
50
+ pool_info = pools.map do |pool|
51
+ datastores = list_ds(pool)
52
+ { 'Pool' => pool.name, 'Datastores' => datastores }
53
+ end
54
+ ui.output(pool_info)
55
+ end
56
+
57
+ private
58
+
59
+ def list_vms(store)
60
+ store.vm.map do |vm|
61
+ host_name = vm.guest[:hostName]
62
+ guest_full_name = vm.guest[:guest_full_name]
63
+ guest_state = vm.guest[:guest_state]
64
+ { 'VM Name' => host_name, 'OS' => guest_full_name, 'State' => guest_state }
65
+ end
66
+ end
67
+
68
+ def list_ds(pool)
69
+ pool.datastore.map do |store|
56
70
  avail = number_to_human_size(store.summary[:freeSpace])
57
71
  cap = number_to_human_size(store.summary[:capacity])
58
- puts "#{ui.color('Datastore', :cyan)}: #{store.name} (#{avail} / #{cap})"
59
- next unless get_config(:list)
60
- store.vm.each do |vms|
61
- host_name = vms.guest[:hostName]
62
- guest_full_name = vms.guest[:guest_full_name]
63
- guest_state = vms.guest[:guest_state]
64
- puts "#{ui.color('VM Name:', :green)} #{host_name} #{ui.color('OS:', :magenta)} #{guest_full_name} #{ui.color('State:', :cyan)} #{guest_state}"
65
- end
72
+ ds_info = { 'Datastore' => store.name, 'Free' => avail, 'Capacity' => cap }
73
+ ds_info['Vms'] = list_vms(store) if get_config(:list)
74
+ ds_info
66
75
  end
67
76
  end
68
77
  end
@@ -39,7 +39,7 @@ class Chef::Knife::VsphereDatastoreMaxfree < Chef::Knife::BaseVsphereCommand
39
39
 
40
40
  vim_connection
41
41
  dcname = get_config(:vsphere_dc)
42
- regex = /#{Regexp.escape(get_config(:regex))}/
42
+ regex = /#{get_config(:regex)}/
43
43
  dc = config[:vim].serviceInstance.find_datacenter(dcname) || abort('datacenter not found')
44
44
  max = nil
45
45
  datastores = if get_config(:vlan)
@@ -13,11 +13,6 @@ class Chef::Knife::VsphereHostsList < Chef::Knife::BaseVsphereCommand
13
13
  short: '-h',
14
14
  description: 'Target pool'
15
15
 
16
- def find_pools(folder, poolname = nil)
17
- pools = folder.children.find_all.select { |p| p.is_a?(RbVmomi::VIM::ComputeResource) || p.is_a?(RbVmomi::VIM::ResourcePool) }
18
- poolname.nil? ? pools : pools.select { |p| p.name == poolname }
19
- end
20
-
21
16
  def run
22
17
  vim_connection
23
18
  dc = datacenter
@@ -25,18 +20,25 @@ class Chef::Knife::VsphereHostsList < Chef::Knife::BaseVsphereCommand
25
20
 
26
21
  target_pool = config[:pool]
27
22
 
28
- pools = find_pools(folder, target_pool)
23
+ pools = find_pools_and_clusters(folder, target_pool)
29
24
  if target_pool && pools.empty?
30
25
  puts "Pool #{target_pool} not found"
31
26
  return
32
27
  end
33
28
 
34
- pools.each do |pool|
35
- puts "#{ui.color('Pool', :cyan)}: #{pool.name}"
36
- hosts = pool.host || []
37
- hosts.each do |hostc|
38
- puts " #{ui.color('Host', :cyan)}: #{hostc.name}"
39
- end
29
+ pool_list = pools.map do |pool|
30
+ host_list = list_hosts(pool)
31
+ { 'Pool' => pool.name, 'Hosts' => host_list }
32
+ end
33
+ ui.output(pool_list)
34
+ end
35
+
36
+ private
37
+
38
+ def list_hosts(pool)
39
+ hosts = pool.host || []
40
+ hosts.map do |hostc|
41
+ { 'Host' => hostc.name }
40
42
  end
41
43
  end
42
44
  end
@@ -23,8 +23,10 @@ class Chef::Knife::VsphereTemplateList < Chef::Knife::BaseVsphereCommand
23
23
  vms = find_all_in_folder(base_folder, RbVmomi::VIM::VirtualMachine)
24
24
  .select { |v| !v.config.nil? && v.config.template == true }
25
25
 
26
- vms.each do |vm|
27
- puts "#{ui.color('Template Name', :cyan)}: #{vm.name}"
26
+ vm_list = vms.map do |vm|
27
+ { 'Template Name' => vm.name }
28
28
  end
29
+
30
+ ui.output(vm_list)
29
31
  end
30
32
  end
@@ -1,4 +1,3 @@
1
- #
2
1
  # Author:: Ezra Pagel (<ezra@cpan.org>)
3
2
  # Contributor:: Jesse Campbell (<hikeit@gmail.com>)
4
3
  # Contributor:: Bethany Erskine (<bethany@paperlesspost.com>)
@@ -44,6 +43,10 @@ class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
44
43
  long: '--datastorecluster STORE',
45
44
  description: 'The datastorecluster into which to put the cloned VM'
46
45
 
46
+ option :host,
47
+ long: '--host HOST',
48
+ description: 'The host into which to put the cloned VM'
49
+
47
50
  option :resource_pool,
48
51
  long: '--resource-pool POOL',
49
52
  description: 'The resource pool or cluster into which to put the cloned VM'
@@ -505,16 +508,40 @@ class Chef::Knife::VsphereVmClone < Chef::Knife::BaseVsphereCommand
505
508
  hosts
506
509
  end
507
510
 
511
+ def all_the_hosts
512
+ hosts = traverse_folders_for_computeresources(datacenter.hostFolder)
513
+ all_hosts = []
514
+ hosts.each do |host|
515
+ if host.is_a? RbVmomi::VIM::ClusterComputeResource
516
+ all_hosts.concat(host.host)
517
+ else
518
+ all_hosts.push host
519
+ end
520
+ end
521
+ all_hosts
522
+ end
523
+
524
+ def find_host(host_name)
525
+ host = all_the_hosts.find { |host| host.name == host_name }
526
+ raise "Can't find #{host_name}. I found #{all_the_hosts.map(&:name)}" unless host
527
+ host
528
+ end
529
+
508
530
  # Builds a CloneSpec
509
531
  def generate_clone_spec(src_config)
510
532
  rspec = RbVmomi::VIM.VirtualMachineRelocateSpec
511
533
 
512
- rspec.pool = if get_config(:resource_pool)
513
- find_pool(get_config(:resource_pool))
514
- else
515
- hosts = find_available_hosts
516
- hosts.first.resourcePool
517
- end
534
+ case
535
+ when get_config(:host)
536
+ rspec.host = find_host(get_config(:host))
537
+ hosts = find_available_hosts
538
+ rspec.pool = hosts.first.resourcePool
539
+ when get_config(:resource_pool)
540
+ rspec.pool = find_pool(get_config(:resource_pool))
541
+ else
542
+ hosts = find_available_hosts
543
+ rspec.pool = hosts.first.resourcePool
544
+ end
518
545
 
519
546
  rspec.diskMoveType = :moveChildMostDiskBacking if get_config(:linked_clone)
520
547
 
@@ -1,3 +1,3 @@
1
1
  module KnifeVsphere
2
- VERSION = '1.2.21'
2
+ VERSION = '1.2.22'
3
3
  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: 1.2.21
4
+ version: 1.2.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ezra Pagel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-06 00:00:00.000000000 Z
11
+ date: 2016-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: filesize
@@ -194,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
194
  version: '0'
195
195
  requirements: []
196
196
  rubyforge_project:
197
- rubygems_version: 2.6.7
197
+ rubygems_version: 2.5.1
198
198
  signing_key:
199
199
  specification_version: 4
200
200
  summary: vSphere Support for Knife