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, #conn_opts, #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, #find_pool_folder, #find_pools_and_clusters, #get_config, #get_password_from_stdin, #get_path_to_object, #linux?, #number_to_human_size, #password, #tcp_test_port, #tcp_test_port_vm, #traverse_folders_for_computeresources, #traverse_folders_for_dc, #traverse_folders_for_pools, #vim_connection, #windows?

Instance Method Details



64
65
66
# File 'lib/chef/knife/vsphere_vm_list.rb', line 64

def print_folder(folder)
  puts "#{ui.color("Folder", :cyan)}: " + (folder.path[3..-1].map { |x| x[1] }.* "/")
end


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef/knife/vsphere_vm_list.rb', line 49

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



68
69
70
71
72
73
74
75
# File 'lib/chef/knife/vsphere_vm_list.rb', line 68

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

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef/knife/vsphere_vm_list.rb', line 28

def traverse_folders(folder, is_top = false, recurse = false, only_folders = false)
  if only_folders
    print_folder(folder)
  else
    vms = find_all_in_folder(folder, RbVmomi::VIM::VirtualMachine).select { |v| v.config && !v.config.template }
    if vms.any?
      print_folder(folder)
      vms.each { |v| print_vm(v) }
    elsif is_top
      puts "#{ui.color("No VMs", :cyan)}"
    end
  end

  return unless recurse

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