Class: Chef::Knife::VsphereVmFind
- Inherits:
-
BaseVsphereCommand
- Object
- Chef::Knife
- BaseVsphereCommand
- Chef::Knife::VsphereVmFind
- 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
-
#run ⇒ Object
Main entry point to the command.
-
#traverse_folders_for_pool_clustercompute(folder, objectname) ⇒ RbVmomi::VIM::ClusterComputeResource, ...
Find the given pool or compute resource.
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?
Methods inherited from Chef::Knife
Instance Method Details
#run ⇒ Object
Main entry point to the command
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/chef/knife/vsphere_vm_find.rb', line 135 def run poolname = config[:pool] if poolname.nil? show_usage fatal_exit('You must specify a resource pool or cluster name (see knife vsphere pool list)') end abort '--ips has been removed. Please use --networks' if get_config(:ips) vim_connection dc = datacenter folder = dc.hostFolder pool = if get_config(:poolpath) find_pool(poolname) || abort("Pool #{poolname} not found") else traverse_folders_for_pool_clustercompute(folder, poolname) || abort("Pool #{poolname} not found") end vm_list = if pool.class == RbVmomi::VIM::ResourcePool pool.vm else pool.resourcePool.vm end return if vm_list.nil? output = vm_list.map do |vm| thisvm = {} if get_config(:matchname) next unless vm.name.include? config[:matchname] end if get_config(:matchtools) next unless vm.guest.toolsStatus == config[:matchtools] end power_state = vm.runtime.powerState thisvm['state'] = case power_state when PS_ON 'on' when PS_OFF 'off' when PS_SUSPENDED 'suspended' end next if get_config(:soff) && (power_state == PS_ON) next if get_config(:son) && (power_state == PS_OFF) if get_config(:matchip) if !vm.guest.ipAddress.nil? && vm.guest.ipAddress != '' next unless vm.guest.ipAddress.include? config[:matchip] else next end end unless vm.guest.guestFullName.nil? if get_config(:matchos) next unless vm.guest.guestFullName.include? config[:matchos] end end thisvm['name'] = vm.name if get_config(:hostname) thisvm['hostname'] = vm.guest.hostName end if get_config(:host_name) # TODO: Why vm.summary.runtime vs vm.runtime? thisvm['host_name'] = vm.summary.runtime.host.name end if get_config(:full_path) fullpath = '' iterator = vm while iterator = iterator.parent break if iterator.name == 'vm' fullpath = fullpath.empty? ? iterator.name : "#{iterator.name}/#{fullpath}" end thisvm['folder'] = fullpath else thisvm['folder'] = vm.parent.name end if get_config(:ip) thisvm['ip'] = vm.guest.ipAddress end if get_config(:networks) ipregex = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ thisvm['networks'] = vm.guest.net.map do |net| firstip = net.ipConfig.ipAddress.first { |i| i.ipAddress[ipregex] } { 'name' => net.network, 'ip' => firstip.ipAddress, 'prefix' => firstip.prefixLength } end end if get_config(:os) thisvm['os'] = vm.guest.guestFullName end if get_config(:ram) thisvm['ram'] = vm.summary.config.memorySizeMB end if get_config(:cpu_hot_add_enabled) thisvm['cpu_hot_add_enabled'] = vm.config.cpuHotAddEnabled end if get_config(:memory_hot_add_enabled) thisvm['memory_hot_add_enabled'] = vm.config.memoryHotAddEnabled end if get_config(:cpu) thisvm['cpu'] = vm.summary.config.numCpu end if get_config(:alarms) thisvm['alarms'] = vm.summary.overallStatus end if get_config(:tools) thisvm['tools'] = vm.guest.toolsStatus end if get_config(:os_disk) thisvm['disks'] = vm.guest.disk.map do |disk| { 'name' => disk.diskPath, 'capacity' => disk.capacity / 1024 / 1024, 'free' => disk.freeSpace / 1024 / 1024 } end end if get_config(:esx_disk) # TODO: https://www.vmware.com/support/developer/converter-sdk/conv55_apireference/vim.VirtualMachine.html#field_detail says this is deprecated thisvm['esx_disks'] = vm.layout.disk.map(&:diskFile) end if get_config(:snapshots) thisvm['snapshots'] = if vm.snapshot vm.snapshot.rootSnapshotList.map(&:name) else [] end end thisvm end ui.output(output.compact) end |
#traverse_folders_for_pool_clustercompute(folder, objectname) ⇒ RbVmomi::VIM::ClusterComputeResource, ...
Find the given pool or compute resource
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/chef/knife/vsphere_vm_find.rb', line 120 def traverse_folders_for_pool_clustercompute(folder, objectname) children = find_all_in_folder(folder, RbVmomi::VIM::ManagedObject) children.each do |child| next unless child.class == RbVmomi::VIM::ClusterComputeResource || child.class == RbVmomi::VIM::ComputeResource || child.class == RbVmomi::VIM::ResourcePool if child.name == objectname return child elsif child.class == RbVmomi::VIM::Folder || child.class == RbVmomi::VIM::ComputeResource || child.class == RbVmomi::VIM::ClusterComputeResource || child.class == RbVmomi::VIM::ResourcePool pool = traverse_folders_for_pool_clustercompute(child, objectname) end return pool if pool end false end |