Revision d4095312

View differences:

src/tm_mad/vcenter/delete
36 36

  
37 37
require 'vcenter_driver'
38 38

  
39
VM_PREFIX_DEFAULT = "one-$i-"
40

  
39 41
path = ARGV[0]
40 42
vmid = ARGV[1]
41 43
dsid = ARGV[2]
......
54 56
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid)
55 57
vm_ref = one_vm['DEPLOY_ID']
56 58

  
59
vm = nil
60

  
57 61
begin
58 62
    vi_client = VCenterDriver::VIClient.new_from_host(host_id)
59
    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
63

  
64
    if !!vm_ref && !vm_ref.empty?
65
        vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
66
    else
67
        vcenter_vm = VCenterDriver::VIHelper.find_vcenter_vm_by_name(one_vm, host, vi_client)
68

  
69
        # If no VM object retrieved, raise an exception
70
        raise "Could not find the undeployed VM in vCenter's inventory using it's name" if !vcenter_vm
71

  
72
        vm_ref = vcenter_vm._ref
73
        vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
74
    end
60 75
rescue Exception => e
61 76
    vi_client.close_connection if vi_client
62 77

  
......
199 214

  
200 215
        end
201 216

  
217
        vm.poweroff_hard if vm.is_powered_on?
218

  
202 219
        vm.destroy
203 220
    rescue Exception => e
204 221
        STDERR.puts "Error unregistering vm #{vmid} (#{vm_ref})."\
src/vmm_mad/remotes/lib/vcenter_driver/vi_helper.rb
57 57
        return element
58 58
    end
59 59

  
60
    def self.find_vcenter_vm_by_name(one_vm, host, vi_client)
61
        # Let's try to find the VM object only by its name
62
        # Let's build the VM name
63
        vm_prefix = host['TEMPLATE/VM_PREFIX']
64
        vm_prefix = VM_PREFIX_DEFAULT if vm_prefix.nil? || vm_prefix.empty?
65
        vm_prefix.gsub!("$i", one_vm['ID'])
66
        vm_name =  vm_prefix + one_vm['NAME']
67

  
68
        # We have no DEPLOY_ID, the VM has never been deployed
69
        # let's use a view to try to find the VM from the root folder
70
        view = vi_client.vim.serviceContent.viewManager.CreateContainerView({
71
            container: vi_client.vim.rootFolder,
72
            type:      ['VirtualMachine'],
73
            recursive: true
74
        })
75

  
76
        vcenter_vm = view.view.find{ |v| v.name == vm_name } if view.view
77

  
78
        view.DestroyView # Destroy the view
79

  
80
        return vcenter_vm
81
    end
82

  
60 83
    def self.get_default(xpath)
61 84
        begin
62 85
            xml = OpenNebula::XMLElement.new
src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb
1834 1834
        @item.PowerOnVM_Task.wait_for_completion
1835 1835
    end
1836 1836

  
1837
    def is_powered_on?
1838
        return @item.runtime.powerState == "poweredOn"
1839
    end
1840

  
1837 1841
    def poweroff_hard
1838 1842
        @item.PowerOffVM_Task.wait_for_completion
1839 1843
    end

Also available in: Unified diff