Class: Chef::Knife::BaseVsphereCommand

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/base_vsphere_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Chef::Knife

#log_verbose?

Class Method Details

.common_optionsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chef/knife/base_vsphere_command.rb', line 25

def self.common_options
  option :vsphere_user,
         short: '-u USERNAME',
         long: '--vsuser USERNAME',
         description: 'The username for vsphere'

  option :vsphere_pass,
         short: '-p PASSWORD',
         long: '--vspass PASSWORD',
         description: 'The password for vsphere'

  option :vsphere_host,
         long: '--vshost HOST',
         description: 'The vsphere host'

  option :vsphere_dc,
         short: '-D DATACENTER',
         long: '--vsdc DATACENTER',
         description: 'The Datacenter for vsphere'

  option :vsphere_path,
         long: '--vspath SOAP_PATH',
         description: 'The vsphere SOAP endpoint path',
         default: '/sdk'

  option :vsphere_port,
         long: '--vsport PORT',
         description: 'The VI SDK port number to use',
         default: '443'

  option :vsphere_nossl,
         long: '--vsnossl',
         description: 'Disable SSL connectivity'

  option :vsphere_insecure,
         long: '--vsinsecure',
         description: 'Disable SSL certificate verification'

  option :folder,
         short: '-f FOLDER',
         long: '--folder FOLDER',
         description: 'The folder to get VMs from',
         default: ''

  option :proxy_host,
         long: '--proxyhost PROXY_HOSTNAME',
         description: 'Proxy hostname'

  option :proxy_port,
         long: '--proxyport PROXY_PORT',
         description: 'Proxy port'
end

Instance Method Details

#choose_datastore(dstores, size) ⇒ Object



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
# File 'lib/chef/knife/base_vsphere_command.rb', line 229

def choose_datastore(dstores, size)
  vmdk_size_b = size.to_i * 1024 * 1024 * 1024

  candidates = []
  dstores.each do |store|
    avail = number_to_human_size(store.summary[:freeSpace])
    cap = number_to_human_size(store.summary[:capacity])
    puts "#{ui.color('Datastore', :cyan)}: #{store.name} (#{avail}(#{store.summary[:freeSpace]}) / #{cap})"

    # vm's can span multiple datastores, so instead of grabbing the first one
    # let's find the first datastore with the available space on a LUN the vm
    # is already using, or use a specified LUN (if given)

    next unless (store.summary[:freeSpace] - vmdk_size_b) > 0
    # also let's not use more than 90% of total space to save room for snapshots.
    cap_remains = 100 * ((store.summary[:freeSpace].to_f - vmdk_size_b.to_f) / store.summary[:capacity].to_f)
    candidates.push(store) if cap_remains.to_i > 10
  end
  if candidates.length > 0
    vmdk_datastore = candidates[0]
  else
    puts 'Insufficient space on all LUNs designated or assigned to the virtual machine. Please specify a new target.'
    vmdk_datastore = nil
  end
  vmdk_datastore
end

#datacenterObject



181
182
183
184
# File 'lib/chef/knife/base_vsphere_command.rb', line 181

def datacenter
  dcname = get_config(:vsphere_dc)
  traverse_folders_for_dc(config[:vim].rootFolder, dcname) || abort('datacenter not found')
end

#fatal_exit(msg) ⇒ Object



327
328
329
330
# File 'lib/chef/knife/base_vsphere_command.rb', line 327

def fatal_exit(msg)
  ui.fatal(msg)
  exit 1
end

#find_all_in_folder(folder, type) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/chef/knife/base_vsphere_command.rb', line 296

def find_all_in_folder(folder, type)
  if folder.instance_of?(RbVmomi::VIM::ClusterComputeResource) || folder.instance_of?(RbVmomi::VIM::ComputeResource)
    folder = folder.resourcePool
  end
  if folder.instance_of?(RbVmomi::VIM::ResourcePool)
    folder.resourcePool.grep(type)
  elsif folder.instance_of?(RbVmomi::VIM::Folder)
    folder.childEntity.grep(type)
  else
    puts "Unknown type #{folder.class}, not enumerating"
    nil
  end
end

#find_datastore(dsName) ⇒ Object



267
268
269
270
271
# File 'lib/chef/knife/base_vsphere_command.rb', line 267

def find_datastore(dsName)
  dc = datacenter
  base_entity = dc.datastore
  base_entity.find { |f| f.info.name == dsName } || abort("no such datastore #{dsName}")
end

#find_datastorecluster(dsName, folder = nil) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/chef/knife/base_vsphere_command.rb', line 273

def find_datastorecluster(dsName, folder = nil)
  unless folder
    dc = datacenter
    folder = dc.datastoreFolder
  end
  folder.childEntity.each do |child|
    if child.class.to_s == 'Folder'
      ds = find_datastorecluster(dsName, child)
      return ds if ds
    elsif child.class.to_s == 'StoragePod' && child.name == dsName
      return child
    end
  end
  nil
end

#find_datastores_regex(regex) ⇒ Object



256
257
258
259
260
261
262
263
264
265
# File 'lib/chef/knife/base_vsphere_command.rb', line 256

def find_datastores_regex(regex)
  stores = []
  puts "Looking for all datastores that match /#{regex}/"
  dc = datacenter
  base_entity = dc.datastore
  base_entity.each do |ds|
    stores.push ds if ds.name.match(/#{regex}/)
  end
  stores
end

#find_device(vm, deviceName) ⇒ Object



289
290
291
292
293
294
# File 'lib/chef/knife/base_vsphere_command.rb', line 289

def find_device(vm, deviceName)
  vm.config.hardware.device.each do |device|
    return device if device.deviceInfo.label == deviceName
  end
  nil
end

#find_folder(folderName) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/chef/knife/base_vsphere_command.rb', line 186

def find_folder(folderName)
  dc = datacenter
  base_entity = dc.vmFolder
  entity_array = folderName.split('/')
  entity_array.each do |entityArrItem|
    if entityArrItem != ''
      base_entity = base_entity.childEntity.grep(RbVmomi::VIM::Folder).find { |f| f.name == entityArrItem } ||
                    abort("no such folder #{folderName} while looking for #{entityArrItem}")
    end
  end
  base_entity
end

#find_in_folder(folder, type, name) ⇒ Object



323
324
325
# File 'lib/chef/knife/base_vsphere_command.rb', line 323

def find_in_folder(folder, type, name)
  folder.childEntity.grep(type).find { |o| o.name == name }
end

#find_network(networkName) ⇒ Object



199
200
201
202
203
# File 'lib/chef/knife/base_vsphere_command.rb', line 199

def find_network(networkName)
  dc = datacenter
  base_entity = dc.network
  base_entity.find { |f| f.name == networkName } || abort("no such network #{networkName}")
end

#find_pool(poolName) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/chef/knife/base_vsphere_command.rb', line 205

def find_pool(poolName)
  dc = datacenter
  base_entity = dc.hostFolder
  entity_array = poolName.split('/')
  entity_array.each do |entityArrItem|
    next if entityArrItem == ''
    if base_entity.is_a? RbVmomi::VIM::Folder
      base_entity = base_entity.childEntity.find { |f| f.name == entityArrItem } ||
                    abort("no such pool #{poolName} while looking for #{entityArrItem}")
    elsif base_entity.is_a?(RbVmomi::VIM::ClusterComputeResource) || base_entity.is_a?(RbVmomi::VIM::ComputeResource)
      base_entity = base_entity.resourcePool.resourcePool.find { |f| f.name == entityArrItem } ||
                    abort("no such pool #{poolName} while looking for #{entityArrItem}")
    elsif base_entity.is_a? RbVmomi::VIM::ResourcePool
      base_entity = base_entity.resourcePool.find { |f| f.name == entityArrItem } ||
                    abort("no such pool #{poolName} while looking for #{entityArrItem}")
    else
      abort "Unexpected Object type encountered #{base_entity.type} while finding resourcePool"
    end
  end

  base_entity = base_entity.resourcePool if !base_entity.is_a?(RbVmomi::VIM::ResourcePool) && base_entity.respond_to?(:resourcePool)
  base_entity
end

#get_config(key) ⇒ Object



78
79
80
81
82
83
# File 'lib/chef/knife/base_vsphere_command.rb', line 78

def get_config(key)
  key = key.to_sym
  rval = config[key] || Chef::Config[:knife][key]
  Chef::Log.debug("value for config item #{key}: #{rval}")
  rval
end

#get_path_to_object(object) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/chef/knife/base_vsphere_command.rb', line 310

def get_path_to_object(object)
  if object.is_a?(RbVmomi::VIM:: ManagedEntity)
    if object.parent.is_a?(RbVmomi::VIM:: ManagedEntity)
      return get_path_to_object(object.parent) + '/' + object.parent.name
    else
      return ''
    end
  else
    puts "Unknown type #{object.class}, not enumerating"
    nil
  end
end

#get_vm(vmname) ⇒ Object



117
118
119
120
121
# File 'lib/chef/knife/base_vsphere_command.rb', line 117

def get_vm(vmname)
  vim_connection
  base_folder = find_folder(get_config(:folder))
  traverse_folders_for_vm(base_folder, vmname)
end

#get_vms(vmname) ⇒ Object



123
124
125
126
127
# File 'lib/chef/knife/base_vsphere_command.rb', line 123

def get_vms(vmname)
  vim_connection
  base_folder = find_folder(get_config(:folder))
  traverse_folders_for_vms(base_folder, vmname)
end

#linux?(config) ⇒ Boolean

Returns:

  • (Boolean)


370
371
372
373
374
375
376
# File 'lib/chef/knife/base_vsphere_command.rb', line 370

def linux?(config)
  gid = config.guestId.downcase
  # This makes the assumption that if it isn't mac or windows it's linux
  is_linux_bool = !gid.include?('windows') && !gid.include?('darwin')
  Chef::Log.debug('Identified os as linux.') if is_linux_bool
  is_linux_bool
end

#passwordObject



113
114
115
# File 'lib/chef/knife/base_vsphere_command.rb', line 113

def password
  @password ||= ui.ask('Enter your password: ') { |q| q.echo = false }
end

#tcp_test_port(hostname, port) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/chef/knife/base_vsphere_command.rb', line 341

def tcp_test_port(hostname, port)
  tcp_socket = TCPSocket.new(hostname, port)
  readable = IO.select([tcp_socket], nil, nil, 5)
  if readable
    Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}") if port == 22
    true
  else
    false
  end
rescue Errno::ETIMEDOUT
  false
rescue Errno::EPERM
  false
rescue Errno::ECONNREFUSED
  sleep 2
  false
rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end

#tcp_test_port_vm(vm, port) ⇒ Object



332
333
334
335
336
337
338
339
# File 'lib/chef/knife/base_vsphere_command.rb', line 332

def tcp_test_port_vm(vm, port)
  ip = vm.guest.ipAddress
  if ip.nil?
    sleep 2
    return false
  end
  tcp_test_port(ip, port)
end

#traverse_folders_for_computeresources(folder) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/chef/knife/base_vsphere_command.rb', line 142

def traverse_folders_for_computeresources(folder)
  retval = []
  children = folder.children.find_all
  children.each do |child|
    if child.class == RbVmomi::VIM::ComputeResource || child.class == RbVmomi::VIM::ClusterComputeResource
      retval << child
    elsif child.class == RbVmomi::VIM::Folder
      retval.concat(traverse_folders_for_computeresources(child))
    end
  end
  retval
end

#traverse_folders_for_dc(folder, dcname) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/chef/knife/base_vsphere_command.rb', line 168

def traverse_folders_for_dc(folder, dcname)
  children = folder.children.find_all
  children.each do |child|
    if child.class == RbVmomi::VIM::Datacenter && child.name == dcname
      return child
    elsif child.class == RbVmomi::VIM::Folder
      dc = traverse_folders_for_dc(child, dcname)
      return dc if dc
    end
  end
  false
end

#traverse_folders_for_vm(folder, vmname) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/chef/knife/base_vsphere_command.rb', line 129

def traverse_folders_for_vm(folder, vmname)
  children = folder.children.find_all
  children.each do |child|
    if child.class == RbVmomi::VIM::VirtualMachine && child.name == vmname
      return child
    elsif child.class == RbVmomi::VIM::Folder
      vm = traverse_folders_for_vm(child, vmname)
      return vm if vm
    end
  end
  false
end

#traverse_folders_for_vms(folder, vmname) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/chef/knife/base_vsphere_command.rb', line 155

def traverse_folders_for_vms(folder, vmname)
  retval = []
  children = folder.children.find_all
  children.each do |child|
    if child.class == RbVmomi::VIM::VirtualMachine && child.name == vmname
      retval << child
    elsif child.class == RbVmomi::VIM::Folder
      retval.concat(traverse_folders_for_vms(child, vmname))
    end
  end
  retval
end

#vim_connectionObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chef/knife/base_vsphere_command.rb', line 85

def vim_connection
  conn_opts = {
    host: get_config(:vsphere_host),
    path: get_config(:vsphere_path),
    port: get_config(:vsphere_port),
    use_ssl: !get_config(:vsphere_nossl),
    user: get_config(:vsphere_user),
    password: get_config(:vsphere_pass),
    insecure: get_config(:vsphere_insecure),
    proxyHost: get_config(:proxy_host),
    proxyPort: get_config(:proxy_port)
  }

  if !conn_opts[:password]
    # Password is not in the config file - grab it
    # from the command line
    conn_opts[:password] = password
  elsif conn_opts[:password].start_with?('base64:')
    conn_opts[:password] = Base64.decode64(conn_opts[:password][7..-1]).chomp
  end

  #    opt :debug, "Log SOAP messages", :short => 'd', :default => (ENV['RBVMOMI_DEBUG'] || false)

  vim = RbVmomi::VIM.connect conn_opts
  config[:vim] = vim
  vim
end

#windows?(config) ⇒ Boolean

Returns:

  • (Boolean)


364
365
366
367
368
# File 'lib/chef/knife/base_vsphere_command.rb', line 364

def windows?(config)
  is_win_bool = config.guestId.downcase.include?('windows')
  Chef::Log.debug('Identified os as windows.') if is_win_bool
  is_win_bool
end