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, #get_config, #get_password_from_stdin, #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

#runObject



110
111
112
113
114
115
116
117
118
119
120
121
122
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
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
# File 'lib/chef/knife/vsphere_vm_find.rb', line 110

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

  vim_connection
  dc = datacenter
  folder = dc.hostFolder

  pool = traverse_folders_for_pool_clustercompute(folder, poolname) || abort("Pool #{poolname} not found")

  vm = if pool.class == RbVmomi::VIM::ResourcePool
         pool.vm
       else
         pool.resourcePool.vm
       end

  return if vm.nil?
  vm.each do |vmc|
    state = case vmc.runtime.powerState
            when PS_ON
              ui.color('on', :green)
            when PS_OFF
              ui.color('off', :red)
            when PS_SUSPENDED
              ui.color('suspended', :yellow)
            end

    if get_config(:matchname)
      next unless vmc.name.include? config[:matchname]
    end

    if get_config(:matchtools)
      next unless vmc.guest.toolsStatus == config[:matchtools]
    end

    next if get_config(:soff) && (vmc.runtime.powerState == PS_ON)

    next if get_config(:son) && (vmc.runtime.powerState == PS_OFF)

    if get_config(:matchip)
      if !vmc.guest.ipAddress.nil? && vmc.guest.ipAddress != ''
        next unless vmc.guest.ipAddress.include? config[:matchip]
      else
        next
      end
    end

    unless vmc.guest.guestFullName.nil?
      if get_config(:matchos)
        next unless vmc.guest.guestFullName.include? config[:matchos]
      end
    end

    print "#{ui.color('VM Name:', :cyan)} #{vmc.name}\t"
    if get_config(:hostname)
      print "#{ui.color('Hostname:', :cyan)} #{vmc.guest.hostName}\t"
    end

    if get_config(:full_path)
      actualname = ''
      vmcp = vmc
      while !vmcp.parent.nil? && vmcp.parent.name != 'vm'
        actualname.concat("#{vmcp.parent.name}/")
        vmcp = vmcp.parent
      end
      print ui.color('Folder:', :cyan)
      print '"'
      print actualname.split('/').reverse.join('/')
      print "\"\t"

    else
      print "#{ui.color('Folder', :cyan)}: #{vmc.parent.name}\t"
    end

    if get_config(:ip)
      print "#{ui.color('IP:', :cyan)} #{vmc.guest.ipAddress}\t"
    end
    if get_config(:ips)
      ipregex = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
      networks = vmc.guest.net.map { |net| "#{net.network}:" + net.ipConfig.ipAddress.select { |i| i.ipAddress[ipregex] }[0].ipAddress }
      print "#{ui.color('IPS:', :cyan)} #{networks.join(',')}\t"
    end
    if get_config(:os)
      print "#{ui.color('OS:', :cyan)} #{vmc.guest.guestFullName}\t"
    end
    if get_config(:ram)
      print "#{ui.color('RAM:', :cyan)} #{vmc.summary.config.memorySizeMB}\t"
    end
    if get_config(:cpu)
      print "#{ui.color('CPU:', :cyan)} #{vmc.summary.config.numCpu}\t"
    end
    if get_config(:alarms)
      print "#{ui.color('Alarms:', :cyan)} #{vmc.summary.overallStatus}\t"
    end
    print "#{ui.color('State:', :cyan)} #{state}\t"
    if get_config(:tools)
      print "#{ui.color('Tools:', :cyan)} #{vmc.guest.toolsStatus}\t"
    end

    if get_config(:os_disk)
      print ui.color('OS Disks:', :cyan)
      vmc.guest.disk.each do |disc|
        print "#{disc.diskPath} #{disc.capacity / 1024 / 1024}MB Free:#{disc.freeSpace / 1024 / 1024}MB |"
      end
    end

    if get_config(:esx_disk)
      print ui.color('ESX Disks:', :cyan)
      vmc.layout.disk.each do |dsc|
        print "#{dsc.diskFile} | "
      end
    end

    if get_config(:snapshots)
      unless vmc.snapshot.nil?
        print ui.color('Snapshots:', :cyan)
        vmc.snapshot.rootSnapshotList.each do |snap|
          print " #{snap.name}"
        end
      end
    end
    puts
  end
end

#traverse_folders_for_pool_clustercompute(folder, poolname) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/knife/vsphere_vm_find.rb', line 96

def traverse_folders_for_pool_clustercompute(folder, poolname)
  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 == poolname
      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, poolname)
    end
    return pool if pool
  end
  false
end