Class: VCenterDriver::Storage

Inherits:
Object
  • Object
show all
Includes:
Memoize
Defined in:
lib/datastore.rb

Overview

Class Storage

Direct Known Subclasses

Datastore, StoragePod

Constant Summary collapse

CURLBIN =
'curl'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Memoize

#[], #[]=, #clear, #clear_all

Instance Attribute Details

#itemObject

Returns the value of attribute item.



105
106
107
# File 'lib/datastore.rb', line 105

def item
  @item
end

Class Method Details

.disk_or_iso?(device) ⇒ Boolean

Checks if a RbVmomi::VIM::VirtualDevice is a disk or an iso file

Returns:

  • (Boolean)


219
220
221
222
223
224
225
226
# File 'lib/datastore.rb', line 219

def self.disk_or_iso?(device)
    is_disk = !device.class.ancestors.index(
        RbVmomi::VIM::VirtualDisk
    ).nil?
    is_iso = device
             .backing.is_a? RbVmomi::VIM::VirtualCdromIsoBackingInfo
    is_disk || is_iso
end

.exists_one_by_ref_dc_and_type?(ref, dc_ref, vcenter_uuid, type, pool = nil) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/datastore.rb', line 238

def self.exists_one_by_ref_dc_and_type?(
    ref,
    dc_ref,
    vcenter_uuid,
    type,
    pool = nil
)
    if pool.nil?
        pool = VCenterDriver::VIHelper.one_pool(
            OpenNebula::DatastorePool,
            false
        )
        if pool.respond_to?(:message)
            raise "Could not get OpenNebula \
            DatastorePool: #{pool.message}"
        end
    end
    elements = pool.select do |e|
        e['TEMPLATE/TYPE'] == type &&
            e['TEMPLATE/VCENTER_DS_REF'] == ref &&
            e['TEMPLATE/VCENTER_DC_REF'] == dc_ref &&
            e['TEMPLATE/VCENTER_INSTANCE_ID'] == vcenter_uuid
    end

    elements.size == 1
end

.get_image_import_template(params) ⇒ Object



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
# File 'lib/datastore.rb', line 119

def self.get_image_import_template(params)
    disk = params[:disk]
    ipool = params[:ipool]
    _type = params[:_type]
    ds_id = params[:ds_id]
    opts = params[:opts]
    images = params[:images]

    VCenterDriver::VIHelper.check_opts(opts, [:persistent])

    ds_name      = disk[:datastore].name
    image_path   = disk[:path_wo_ds]
    image_type   = disk[:type]
    image_prefix = disk[:prefix]

    image_name = nil

    one_image = {}
    one_image[:template] = ''

    # Get image name
    file_name = File.basename(image_path).gsub(/\.vmdk$/, '')

    # Check if the image has already been imported
    image = VIHelper
            .find_image_by(
                'SOURCE',
                OpenNebula::ImagePool,
                image_path,
                ds_id,
                ipool
            )

    if image.nil?
        key = "#{file_name}#{ds_name}#{image_path}"
        byte = 0
        image_name = VCenterDriver::VIHelper
                     .one_name(
                         OpenNebula::ImagePool,
                         file_name,
                         key,
                         ipool,
                         byte
                     )
        while images.include?(image_name)
            byte += 2
            image_name = VCenterDriver::VIHelper
                         .one_name(
                             OpenNebula::ImagePool,
                             file_name,
                             key,
                             ipool,
                             byte
                         )
        end

        # Set template
        one_image[:template] << "NAME=\"#{image_name}\"\n"
        one_image[:template] << "PATH=\"vcenter://#{image_path}\"\n"
        one_image[:template] << "TYPE=\"#{image_type}\"\n"
        one_image[:template] << "PERSISTENT=\"#{opts[:persistent]}\"\n"
        unless CONFIG[:delete_images]
            one_image[:template] << "VCENTER_IMPORTED=\"YES\"\n"
        end
        one_image[:template] << "DEV_PREFIX=\"#{image_prefix}\"\n"
    else
        # Return the image XML if it already exists
        one_image[:one] = image
    end

    [one_image, image_name]
end

.get_one_image_ds_by_ref_and_dc(ref, dc_ref, vcenter_uuid, pool = nil) ⇒ Object



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
# File 'lib/datastore.rb', line 192

def self.get_one_image_ds_by_ref_and_dc(
    ref,
    dc_ref,
    vcenter_uuid,
    pool = nil
)
    if pool.nil?
        pool = VCenterDriver::VIHelper
               .one_pool(
                   OpenNebula::DatastorePool,
                   false
               )
        if pool.respond_to?(:message)
            raise "Could not get \
            OpenNebula DatastorePool: #{pool.message}"
        end
    end

    pool.select do |e|
        e['TEMPLATE/TYPE'] == 'IMAGE_DS' &&
            e['TEMPLATE/VCENTER_DS_REF']      == ref &&
            e['TEMPLATE/VCENTER_DC_REF']      == dc_ref &&
            e['TEMPLATE/VCENTER_INSTANCE_ID'] == vcenter_uuid
    end.first rescue nil
end

.new_from_ref(ref, vi_client) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/datastore.rb', line 111

def self.new_from_ref(ref, vi_client)
    if ref.start_with?('group-')
        VCenterDriver::StoragePod.new_from_ref(ref, vi_client)
    else
        VCenterDriver::Datastore.new_from_ref(ref, vi_client)
    end
end

Instance Method Details

#create_directory(directory) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/datastore.rb', line 316

def create_directory(directory)
    ds_name = self['name']

    return if self.class == VCenterDriver::StoragePod

    directory_name = "[#{ds_name}] #{directory}"

    create_directory_params = {
        :name                     => directory_name,
        :datacenter               => obtain_dc.item,
        :createParentDirectories  => true
    }

    obtain_fm.MakeDirectory(create_directory_params) rescue nil
end

#create_virtual_disk(img_name, size, adapter_type, disk_type) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/datastore.rb', line 293

def create_virtual_disk(img_name, size, adapter_type, disk_type)
    leading_dirs = img_name.split('/')[0..-2]
    if !leading_dirs.empty?
        create_directory(leading_dirs.join('/'))
    end

    ds_name = self['name']

    vmdk_spec = RbVmomi::VIM::FileBackedVirtualDiskSpec(
        :adapterType => adapter_type,
        :capacityKb  => size.to_i*1024,
        :diskType    => disk_type
    )

    obtain_vdm.CreateVirtualDisk_Task(
        :datacenter => obtain_dc.item,
        :name       => "[#{ds_name}] #{img_name}.vmdk",
        :spec       => vmdk_spec
    ).wait_for_completion

    "#{img_name}.vmdk"
end

#monitorObject



228
229
230
231
232
233
234
235
236
# File 'lib/datastore.rb', line 228

def monitor
    summary = self['summary']

    total_mb = (summary.capacity.to_i / 1024) / 1024
    free_mb  = (summary.freeSpace.to_i / 1024) / 1024
    used_mb  = total_mb - free_mb

    "USED_MB=#{used_mb}\nFREE_MB=#{free_mb} \nTOTAL_MB=#{total_mb}"
end

#obtain_dcObject



340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/datastore.rb', line 340

def obtain_dc
    item = @item

    until item.instance_of? RbVmomi::VIM::Datacenter
        item = item.parent
        if item.nil?
            raise 'Could not find the parent Datacenter'
        end
    end

    Datacenter.new(item)
end

#obtain_fmObject



332
333
334
# File 'lib/datastore.rb', line 332

def obtain_fm
    self['_connection.serviceContent.fileManager']
end

#obtain_vdmObject



336
337
338
# File 'lib/datastore.rb', line 336

def obtain_vdm
    self['_connection.serviceContent.virtualDiskManager']
end

#to_one(ds_hash, vcenter_uuid, dc_name, dc_ref) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/datastore.rb', line 265

def to_one(ds_hash, vcenter_uuid, dc_name, dc_ref)
    one = ''
    one << "DRIVER=\"vcenter\"\n"
    one << "NAME=\"#{ds_hash[:name]}\"\n"
    one << "TM_MAD=vcenter\n"
    one << "VCENTER_INSTANCE_ID=\"#{vcenter_uuid}\"\n"
    one << "VCENTER_DC_REF=\"#{dc_ref}\"\n"
    one << "VCENTER_DC_NAME=\"#{dc_name}\"\n"
    one << "VCENTER_DS_NAME=\"#{ds_hash[:simple_name]}\"\n"
    one << "VCENTER_DS_REF=\"#{self['_ref']}\"\n"
    one
end

#to_one_template(ds_hash, vcenter_uuid, dc_name, dc_ref, type) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/datastore.rb', line 278

def to_one_template(ds_hash, vcenter_uuid, dc_name, dc_ref, type)
    one_tmp = {
        :one  => to_one(ds_hash, vcenter_uuid, dc_name, dc_ref)
    }

    if type == 'SYSTEM_DS'
        one_tmp[:one] << "TYPE=SYSTEM_DS\n"
    else
        one_tmp[:one] << "DS_MAD=vcenter\n"
        one_tmp[:one] << "TYPE=IMAGE_DS\n"
    end

    one_tmp
end