Module: OpenNebula::VirtualMachineExt

Defined in:
lib/opennebula/virtual_machine_ext.rb

Overview

Module to decorate VirtualMachine class with additional helpers not directly exposed through the OpenNebula XMLRPC API. The extensions include

- mp_import helper that imports a template into a marketplace

rubocop:disable Style/ClassAndModuleChildren

Class Method Summary collapse

Class Method Details

.extend_object(obj) ⇒ Object



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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
207
208
209
210
211
212
213
214
215
216
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/opennebula/virtual_machine_ext.rb', line 26

def self.extend_object(obj)
    if !obj.is_a?(OpenNebula::VirtualMachine)
        raise StandardError, "Cannot extended #{obj.class} "\
          ' with MarketPlaceAppExt'
    end

    class << obj

        ####################################################################
        # Public extended interface
        ####################################################################

        #-------------------------------------------------------------------
        # Clones the VM's source Template, replacing the disks with live
        # snapshots of the current disks. The VM capacity and NICs are also
        # preserved
        #
        # @param name [String] Name for the new Template
        # @param name [true,false,nil] Optional, true to make the saved
        # images persistent, false make them non-persistent
        #
        # @return [Integer, OpenNebula::Error] the new Template ID in case
        #   of success, error otherwise
        #-------------------------------------------------------------------
        REMOVE_VNET_ATTRS = ['AR_ID', 'BRIDGE', 'CLUSTER_ID', 'IP', 'MAC', 'TARGET', 'NIC_ID',
                             'NETWORK_ID', 'VN_MAD', 'SECURITY_GROUPS', 'VLAN_ID',
                             'BRIDGE_TYPE']

        REMOVE_IMAGE_ATTRS = ['DEV_PREFIX', 'SOURCE', 'ORIGINAL_SIZE', 'SIZE',
                              'DISK_SNAPSHOT_TOTAL_SIZE', 'DRIVER', 'IMAGE_STATE', 'SAVE',
                              'CLONE', 'READONLY', 'PERSISTENT', 'TARGET', 'ALLOW_ORPHANS',
                              'CLONE_TARGET', 'CLUSTER_ID', 'DATASTORE', 'DATASTORE_ID',
                              'DISK_ID', 'DISK_TYPE', 'IMAGE_ID', 'IMAGE', 'IMAGE_UNAME',
                              'IMAGE_UID', 'LN_TARGET', 'TM_MAD', 'TYPE', 'OPENNEBULA_MANAGED']

        def save_as_template(name, desc, opts = {})
            opts = {
                :persistent => false,
                :poweroff   => false,
                :logger     => nil
            }.merge(opts)

            img_ids = []
            ntid    = nil
            logger  = opts[:logger]
            poweron = false

            rc = info

            raise rc.message if OpenNebula.is_error?(rc)

            tid = self['TEMPLATE/TEMPLATE_ID']

            raise 'VM has no associated template' unless valid?(tid)

            # --------------------------------------------------------------
            # Check VM state and poweroff it if needed
            # --------------------------------------------------------------
            if state_str != 'POWEROFF'
                raise 'VM must be POWEROFF' unless opts[:poweroff]

                logger.info 'Powering off VM' if logger

                poweron = true

                rc = poweroff

                raise rc.message if OpenNebula.is_error?(rc)
            end

            # --------------------------------------------------------------
            # Ask if source VM is linked clone
            # --------------------------------------------------------------
            use_linked_clones = self['USER_TEMPLATE/VCENTER_LINKED_CLONES']

            if use_linked_clones && use_linked_clones.downcase == 'yes'
                # Delay the require until it is strictly needed
                # This way we can avoid the vcenter driver dependency
                # in no vCenter deployments
                require 'vcenter_driver'

                deploy_id = self['DEPLOY_ID']
                vm_id = self['ID']
                host_id = self['HISTORY_RECORDS/HISTORY[last()]/HID']
                vi_client = VCenterDriver::VIClient.new_from_host(host_id)

                vm = VCenterDriver::VirtualMachine.new(
                    vi_client,
                    deploy_id,
                    vm_id
                )

                error, vm_template_ref = vm.save_as_linked_clones(name)

                raise error unless error.nil?

                return vm_template_ref
            end

            # --------------------------------------------------------------
            # Clone the source template
            # --------------------------------------------------------------
            vm_template = OpenNebula::Template.new_with_id(tid, @client)

            ntid = vm_template.clone(name)

            raise ntid.message if OpenNebula.is_error?(ntid)

            # --------------------------------------------------------------
            # Replace the original template's capacity with VM values
            # --------------------------------------------------------------
            cpu  = self['TEMPLATE/CPU']
            vcpu = self['TEMPLATE/VCPU']
            mem  = self['TEMPLATE/MEMORY']

            replace = ''
            replace << "DESCRIPTION = \"#{desc}\"\n" if valid?(desc)
            replace << "CPU = #{cpu}\n" if valid?(cpu)
            replace << "VCPU = #{vcpu}\n" if valid?(vcpu)
            replace << "MEMORY = #{mem}\n" if valid?(mem)

            # --------------------------------------------------------------
            # Process VM DISKs
            # --------------------------------------------------------------
            logger.info 'Processing VM disks' if logger

            each('TEMPLATE/DISK') do |disk|
                # Wait for any pending operation
                rc = wait_state2('POWEROFF', 'LCM_INIT')

                raise rc.message if OpenNebula.is_error?(rc)

                disk_id  = disk['DISK_ID']
                image_id = disk['IMAGE_ID']

                omng = disk['OPENNEBULA_MANAGED']
                type = disk['TYPE']

                raise 'Missing DISK_ID' unless valid?(disk_id)

                REMOVE_IMAGE_ATTRS.each do |attr|
                    disk.delete_element(attr)
                end

                # Volatile disks cannot be saved, copy definition
                if !valid?(image_id)
                    logger.info 'Adding volatile disk' if logger

                    disk_str = template_like_str(
                        'TEMPLATE',
                        true,
                        "DISK [ DISK_ID = #{disk_id} ]"
                    )

                    replace << "#{disk_str}\n"

                    next
                end

                # CDROM disk, copy definition
                if type == 'CDROM'
                    logger.info 'Adding CDROM disk' if logger

                    replace << "DISK = [ IMAGE_ID = #{image_id}"
                    replace << ", OPENNEBULA_MANAGED=#{omng}" if omng
                    replace << " ]\n"

                    next
                end

                # Regular disk, saveas it
                logger.info 'Adding and saving regular disk' if logger

                ndisk_name = "#{name}-disk-#{disk_id}"

                rc = disk_saveas(disk_id.to_i, ndisk_name, '', -1)

                raise rc.message if OpenNebula.is_error?(rc)

                if opts[:persistent]
                    logger.info 'Making disk persistent' if logger

                    nimg = OpenNebula::Image.new_with_id(rc.to_i, @client)
                    nimg.persistent
                end

                img_ids << rc.to_i

                disk_tmpl = disk.template_like_str('.').tr("\n", ",\n")

                replace << "DISK = [ IMAGE_ID = #{rc} "
                replace << ", #{disk_tmpl}" unless disk_tmpl.empty?
                replace << " ]\n"
            end

            # --------------------------------------------------------------
            # Process VM NICs
            # --------------------------------------------------------------
            logger.info 'Processing VM NICs' if logger

            each('TEMPLATE/NIC') do |nic|
                nic_id = nic['NIC_ID']

                raise 'Missing NIC_ID' unless valid?(nic_id)

                REMOVE_VNET_ATTRS.each do |attr|
                    nic.delete_element(attr)
                end

                replace << 'NIC = [ '
                replace << nic.template_like_str('.').tr("\n", ",\n")
                replace << " ] \n"
            end

            # --------------------------------------------------------------
            # Extra. Required by the Sunstone Cloud View
            # --------------------------------------------------------------
            replace << "SAVED_TEMPLATE_ID = #{tid}\n"

            new_tmpl = OpenNebula::Template.new_with_id(ntid, @client)

            logger.info 'Updating VM Template' if logger

            rc = new_tmpl.update(replace, true)

            raise rc.message if OpenNebula.is_error?(rc)

            # --------------------------------------------------------------
            # Resume VM if needed
            # --------------------------------------------------------------
            if poweron
                logger.info 'Powering on VM' if logger

                rc = wait_state2('POWEROFF', 'LCM_INIT')

                raise rc.message if OpenNebula.is_error?(rc)

                resume

                rc = wait_state2('ACTIVE', 'RUNNING')

                raise rc.message if OpenNebula.is_error?(rc)
            end

            ntid
        rescue StandardError
            # --------------------------------------------------------------
            # Rollback. Delete the template and the images created
            # --------------------------------------------------------------
            if ntid
                ntmpl = OpenNebula::Template.new_with_id(ntid, @client)
                ntmpl.delete
            end

            img_ids.each do |id|
                img = OpenNebula::Image.new_with_id(id, @client)
                img.delete
            end

            raise
        end

        ####################################################################
        # Private extended interface
        ####################################################################

        private

        #-------------------------------------------------------------------
        # Check an attribute is defined and valid
        #-------------------------------------------------------------------
        def valid?(att)
            return false if att.nil?

            return !att.nil? && !att.empty? if att.is_a? String

            !att.nil?
        end

    end
end