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
|
# File 'bin/esx', line 217
def execute
begin
host = ESX::Host.connect address, user, password, true, {:free_license=>free_license?}
vm = host.virtual_machines.find{|x| x.name.eql? vm_name}
if vm
hd = vm.vm_object.config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).find{|nic| nic.props[:macAddress].eql?mac_address}
s = vm.vm_object.config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).size
if hd
puts "NIC with #{mac_address} mac address already exists!"
else
if mac_address
nics = {:macAddress => mac_address, :addressType => 'manual'}
else
nics = {:addressType => 'generated'}
end
spec = RbVmomi::VIM.VirtualMachineConfigSpec({:deviceChange => [{
:operation => :add,
:device => RbVmomi::VIM.VirtualE1000({
:key => s,
:deviceInfo => {
:label => "Network Adapter #{s}",
:summary => network || 'VM Network'
},
:backing => RbVmomi::VIM.VirtualEthernetCardNetworkBackingInfo(
:deviceName => network || 'VM Network'
)
}.merge(nics))
}]})
unless free_license?
vm.vm_object.ReconfigVM_Task(:spec => spec).wait_for_completion
else
vmx_file = vm.vm_object.config.files.vmPathName.gsub("[","/vmfs/volumes/").gsub(/]\s*/,"/")
puts "VMX file: #{vmx_file}" if debug?
host.remote_command " echo 'ethernet#{s}.present = \"true\"' >> #{vmx_file}"
host.remote_command " echo 'ethernet#{s}.virtualDev = \"e1000\"' >> #{vmx_file}"
host.remote_command " echo 'ethernet#{s}.wakeOnPcktRcv = \"false\"' >> #{vmx_file}"
host.remote_command " echo 'ethernet#{s}.networkName = \"#{network || 'VM Network'}\"' >> #{vmx_file}"
if mac_address
host.remote_command " echo 'ethernet#{s}.addressType = \"static\"' >> #{vmx_file}"
host.remote_command " echo 'ethernet#{s}.address = \"#{mac_address}\"' >> #{vmx_file}"
else
host.remote_command " echo 'ethernet#{s}.addressType = \"generated\"' >> #{vmx_file}"
end
if force?
vm.power_off
host.remote_command "vim-cmd vmsvc/unregister #{vm.vmid}"
id = host.remote_command "vim-cmd solo/registervm #{vmx_file}"
host.remote_command "vim-cmd vmsvc/power.on #{id}"
else
puts "You have to power-off the vm unregister it and register it agin."
end
end
puts "Done."
end
else
puts "No Virtual Machine found!"
end
rescue Exception => e
$stderr.puts "Can't connect to the host #{address}."
if debug?
$stderr.puts e.message
end
exit 1
end
end
|