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, #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_common_options, #get_config, #get_datacenter, #get_password, #get_path_to_object, #get_vim_connection, #get_vm, #get_vms, #tcp_test_port, #tcp_test_port_vm, #traverse_folders_for_dc, #traverse_folders_for_vm, #traverse_folders_for_vms

Instance Method Details



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chef/knife/vsphere_vm_list.rb', line 39

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

#runObject



54
55
56
57
58
59
60
# File 'lib/chef/knife/vsphere_vm_list.rb', line 54

def run
  vim = get_vim_connection
  baseFolder = find_folder(get_config(:folder));
  recurse =  get_config(:recursive)
  is_top = true
  traverse_folders(baseFolder, is_top, recurse)
end

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



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

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

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

end