Class: DeleteNICCommand

Inherits:
BaseCommand show all
Defined in:
bin/esx

Instance Method Summary collapse

Instance Method Details

#executeObject



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
# File 'bin/esx', line 161

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}
      if hd
        spec = RbVmomi::VIM.VirtualMachineConfigSpec({:deviceChange => [{:operation => :remove,:device => hd}]})
        unless free_license?
          vm.vm_object.ReconfigVM_Task(:spec => spec).wait_for_completion
        else
          #TODO: This is horrible! Find a way to reconfigure the VM without downtime
          key = hd.key
          vmx_file = vm.vm_object.config.files.vmPathName.gsub("[","/vmfs/volumes/").gsub(/]\s*/,"/")
          puts "VMX file: #{vmx_file}" if debug?
          eth_name = host.remote_command("fgrep #{mac_address} #{vmx_file} | cut -d. -f1")
          if eth_name
            eth_name.strip!
            puts "#{eth_name} found!" if debug?
            host.remote_command "sed -i -e '/.*#{eth_name}\..*/d' #{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."
      else
        puts "No NIC with #{mac_address} mac address was found!"
      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