Class: VCenterDriver::VmmImporter

Inherits:
VcImporter show all
Defined in:
lib/vmm_importer.rb

Overview

Functionality to import vCenter VMs into OpenNebula

Instance Method Summary collapse

Methods inherited from VcImporter

#get_indexes, import_clusters, new_child, #one_str, #output, #process_import, register_hooks, #retrieve_resources, sanitize, #stdout

Constructor Details

#initialize(one_client, vi_client) ⇒ VmmImporter

Returns a new instance of VmmImporter.



6
7
8
9
10
# File 'lib/vmm_importer.rb', line 6

def initialize(one_client, vi_client)
    super(one_client, vi_client)
    @one_class = OpenNebula::VirtualMachine
    @defaults = {}
end

Instance Method Details

#buildObject



38
39
40
41
# File 'lib/vmm_importer.rb', line 38

def build
    xml = OpenNebula::VirtualMachine.build_xml
    OpenNebula::VirtualMachine.new(xml, @one_client)
end

#import(selected) ⇒ Object



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
77
78
79
80
81
82
83
84
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
112
113
114
115
116
117
# File 'lib/vmm_importer.rb', line 43

def import(selected)
    import_tmplt = selected['IMPORT_TEMPLATE']
    import_tmplt = Base64.decode64(import_tmplt) if import_tmplt
    vm_ref     = selected['DEPLOY_ID'] || selected[:wild]['DEPLOY_ID']
    vm         = selected[:one_item]   || build
    template   = selected[:template]   || import_tmplt
    host_id    = selected[:host]       || @list.keys[0]

    vc_uuid    = @vi_client.vim.serviceContent.about.instanceUuid
    vc_name    = @vi_client.vim.host
    dpool, ipool, npool, hpool = create_pools

    vc_vm = VCenterDriver::VirtualMachine.new_without_id(@vi_client,
                                                         vm_ref)
    vname = vc_vm['name']

    type = { :object => 'VM', :id => vname }
    error, template_disks = vc_vm.import_vcenter_disks(vc_uuid, dpool,
                                                       ipool, type)
    raise error unless error.empty?

    template << template_disks

    opts = {
        :vi_client => @vi_client,
        :vc_uuid => vc_uuid,
        :npool => npool,
        :hpool => hpool,
        :vcenter => vc_name,
        :template_moref => vm_ref,
        :vm_object => vc_vm
    }

    # Create images or get nics information for template
    error, template_nics, ar_ids =
        vc_vm.import_vcenter_nics(opts)
    opts = { :uuid => vc_uuid, :npool => npool, :error => error }
    Raction.delete_ars(ar_ids, opts) unless error.empty?

    template << template_nics
    template << "VCENTER_ESX_HOST = #{vc_vm['runtime.host.name']}\n"

    # Get DS_ID for the deployment, the wild VM needs a System DS
    dc_ref = vc_vm.datacenter.item._ref
    ds_ref = template.match(/^VCENTER_DS_REF *= *"(.*)" *$/)[1]

    ds_one = dpool.select do |e|
        e['TEMPLATE/TYPE'] == 'SYSTEM_DS' &&
            e['TEMPLATE/VCENTER_DS_REF']      == ds_ref &&
            e['TEMPLATE/VCENTER_DC_REF']      == dc_ref &&
            e['TEMPLATE/VCENTER_INSTANCE_ID'] == vc_uuid
    end.first
    opts[:error] = "ds with ref #{ds_ref} is not imported, aborting"
    Raction.delete_ars(ar_ids, opts) unless ds_one

    rc = vm.allocate(template)
    if OpenNebula.is_error?(rc)
        Raction.delete_ars(ar_ids, opts.merge(:error => rc.message))
    end

    rc = vm.deploy(host_id, false, ds_one.id)
    if OpenNebula.is_error?(rc)
        Raction.delete_ars(ar_ids, opts.merge(:error => rc.message))
    end

    # Set reference to template disks and nics in VM template
    vc_vm.one_item = vm

    request_vnc(vc_vm)

    # Sync disks with extraConfig
    vc_vm.reference_all_disks

    vm.id
end

#list(key, list) ⇒ Object



12
13
14
# File 'lib/vmm_importer.rb', line 12

def list(key, list)
    @list = { key => list }
end

#request_vnc(vc_vm) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vmm_importer.rb', line 16

def request_vnc(vc_vm)
    one_vm = vc_vm.one_item
    vnc_port = one_vm['TEMPLATE/GRAPHICS/PORT']
    elapsed_seconds = 0

    # Let's update the info to gather VNC port
    until vnc_port || elapsed_seconds > 30
        sleep(1)
        one_vm.info
        vnc_port = one_vm['TEMPLATE/GRAPHICS/PORT']
        elapsed_seconds += 1
    end

    return unless vnc_port

    extraconfig   = []
    extraconfig  += vc_vm.extraconfig_vnc
    spec_hash     = { :extraConfig => extraconfig }
    spec = RbVmomi::VIM.VirtualMachineConfigSpec(spec_hash)
    vc_vm.item.ReconfigVM_Task(:spec => spec).wait_for_completion
end