Class: Chef::Knife::VsphereVmList

Inherits:
BaseVsphereCommand show all
Defined in:
lib/chef/knife/vsphere_vm_list.rb

Overview

Lists all known virtual machines in the configured datacenter

Instance Method Summary collapse

Methods inherited from BaseVsphereCommand

#choose_datastore, common_options, #datacenter, #fatal_exit, #find_all_in_folder, #find_datastore, #find_datastorecluster, #find_datastores_regex, #find_device, #find_folder, #find_in_folder, #find_network, #find_pool, #get_config, #get_path_to_object, #get_vm, #get_vms, #linux?, #password, #tcp_test_port, #tcp_test_port_vm, #traverse_folders_for_computeresources, #traverse_folders_for_dc, #traverse_folders_for_vm, #traverse_folders_for_vms, #vim_connection, #windows?

Methods inherited from Chef::Knife

#log_verbose?

Instance Method Details



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/knife/vsphere_vm_list.rb', line 35

def print_vm(vm)
  state = case vm.runtime.powerState
          when PS_ON
            ui.color('on', :green)
          when PS_OFF
            ui.color('off', :red)
          when PS_SUSPENDED
            ui.color('suspended', :yellow)
          end
  puts "\t#{ui.color('VM Name:', :cyan)} #{vm.name}"
  puts "\t\t#{ui.color('IP:', :magenta)} #{vm.guest.ipAddress}"
  puts "\t\t#{ui.color('RAM:', :magenta)} #{vm.summary.config.memorySizeMB}"
  puts "\t\t#{ui.color('State:', :magenta)} #{state}"
end

#runObject



50
51
52
53
54
55
56
# File 'lib/chef/knife/vsphere_vm_list.rb', line 50

def run
  vim_connection
  base_folder = find_folder(get_config(:folder))
  recurse =  get_config(:recursive)
  is_top = true
  traverse_folders(base_folder, is_top, recurse)
end

#traverse_folders(folder, is_top = false, recurse = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chef/knife/vsphere_vm_list.rb', line 19

def traverse_folders(folder, is_top = false, recurse = false)
  vms = find_all_in_folder(folder, RbVmomi::VIM::VirtualMachine).select { |v| v.config && !v.config.template }
  if vms.any?
    puts "#{ui.color('Folder', :cyan)}: " + (folder.path[3..-1].map { |x| x[1] }.* '/')
    vms.each { |v|  print_vm(v) }
  elsif is_top
    puts "#{ui.color('No VMs', :cyan)}"
  end

  return unless recurse
  folders = find_all_in_folder(folder, RbVmomi::VIM::Folder)
  folders.each do |child|
    traverse_folders(child, false, recurse)
  end
end