Class: Chef::Knife::VsphereVmFind

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

Overview

find vms belonging to pool that match criteria, display specified fields

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

#runObject

Main entry point to the command



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/chef/knife/vsphere_vm_find.rb', line 123

def run
  property_map("name" => "name")
  property_map("runtime.powerState" => "state") { |value| state_to_english(value) }

  property_map("config.cpuHotAddEnabled" => "cpu_hot_add_enabled") if get_config(:cpu_hot_add_enabled)
  property_map("config.memoryHotAddEnabled" => "memory_hot_add_enabled") if get_config(:memory_hot_add_enabled)
  property_map("guest.guestFullName" => "os") if get_config(:matchos) || get_config(:os)
  property_map("guest.hostName" => "hostname") if get_config(:hostname)
  property_map("guest.ipAddress" => "ip") if get_config(:matchip) || get_config(:ip)
  property_map("guest.toolsStatus" => "tools") if get_config(:matchtools) || get_config(:tools)
  property_map("summary.config.memorySizeMB" => "ram") if get_config(:ram)
  property_map("summary.config.numCpu" => "cpu") if get_config(:cpu)
  property_map("summary.overallStatus" => "alarms") if get_config(:alarms)
  property_map("summary.runtime.host" => "host_name", &:name) if get_config(:host_name)

  # TODO: https://www.vmware.com/support/developer/converter-sdk/conv55_apireference/vim.VirtualMachine.html#field_detail says this is deprecated
  property_map("layout.disk" => "esx_disk") { |disks| disks.map(&:diskFile) } if get_config(:esx_disk)
  property_map("snapshot.rootSnapshotList" => "snapshots") { |snapshots| Array(snapshots).map(&:name) } if get_config(:snapshots)

  if get_config(:networks)
    property_map("guest.net" => "networks") do |nets|
      ipregex = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
      nets.map do |net|
        firstip = net.ipConfig.ipAddress.first { |i| i.ipAddress[ipregex] }
        { "name" => net.network, "ip" => firstip.ipAddress, "prefix" => firstip.prefixLength }
      end
    end
  end

  if get_config(:os_disk)
    property_map("guest.disk" => "disks") do |disks|
      disks.map do |disk|
        { "name" => disk.diskPath,
          "capacity" => disk.capacity / 1024 / 1024,
          "free" => disk.freeSpace / 1024 / 1024 }
      end
    end
  end

  output = matched_vms.map do |vm|
    thisvm = {}
    @output_pairs.each do |property, out_key|
      thisvm[out_key] = if @blocks[out_key]
                          @blocks[out_key].call(vm[property])
                        else
                          vm[property]
                        end
    end

    thisvm["folder"] = full_path_to(vm) if get_config(:full_path)
    thisvm["folder"] = vm.obj.parent.name if get_config(:short_path)

    thisvm
  end
  ui.output(output.compact)
end