Class: VCenterDriver::VIHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/vi_helper.rb

Overview

Class VIHelper

Constant Summary collapse

ETC_LOCATION =
'/etc/one/'
VCENTER_DRIVER_DEFAULT =
"#{ETC_LOCATION}/vcenter_driver.default"
VM_PREFIX_DEFAULT =
'one-$i-'

Class Method Summary collapse

Class Method Details

.add_ref_hash(attr, one_object) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/vi_helper.rb', line 206

def self.add_ref_hash(attr, one_object)
    raise 'cache is empty!' unless @ref_hash

    refkey = get_ref_key(one_object, attr)

    return unless @ref_hash[attr]

    @ref_hash[attr][refkey] = one_object
end

.check_error(rc, message, exit_condition = false) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/vi_helper.rb', line 66

def self.check_error(rc, message, exit_condition = false)
    return unless OpenNebula.is_error?(rc)

    OpenNebula.error_message("\n    Error #{message}: #{rc.message}\n")
    exit 1 if exit_condition

    raise rc.message
end

.check_opts(opts, att_list) ⇒ Object



92
93
94
95
96
# File 'lib/vi_helper.rb', line 92

def self.check_opts(opts, att_list)
    att_list.each do |att|
        raise "#{att} option is mandatory" if opts[att].nil?
    end
end

.clean_ref_hashObject



202
203
204
# File 'lib/vi_helper.rb', line 202

def self.clean_ref_hash
    @ref_hash = {}
end

.clientObject

rubocop:disable Style/GlobalVars rubocop:disable Style/ClassVars



33
34
35
36
37
38
39
40
41
42
# File 'lib/vi_helper.rb', line 33

def self.client
    if $conf.nil?
        @@client ||= OpenNebula::Client.new # rubocop:disable Style/ClassVars
    else
        @@client ||= OpenNebula::Client.new(
            nil,
            $conf[:one_xmlrpc]
        )
    end
end

.create_ref_hash(attribute, pool, vcenter_uuid = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/vi_helper.rb', line 191

def self.create_ref_hash(attribute, pool, vcenter_uuid = nil)
    hash = {}

    pool.each_element(proc do |e|
        refkey = get_ref_key(e, attribute, vcenter_uuid)
        hash[refkey] = e
    end)

    hash
end

.find_by_name(the_class, name, pool = nil, raise_if_fail = true) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/vi_helper.rb', line 136

def self.find_by_name(the_class, name, pool = nil, raise_if_fail = true)
    pool = one_pool(the_class, raise_if_fail) if pool.nil?
    element = pool.find {|e| e['NAME'] == name.to_s }
    if element.nil? && raise_if_fail
        raise "Could not find element '#{name}' in pool '#{the_class}'"
    end

    element
end

.find_by_ref(the_class, attribute, ref, vcenter_uuid, pool = nil) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/vi_helper.rb', line 226

def self.find_by_ref(
    the_class,
    attribute,
    ref,
    vcenter_uuid,
    pool = nil
)
    pool = one_pool(the_class, false) if pool.nil?
    @ref_hash ||= {}

    if @ref_hash[attribute].nil? || @ref_hash[attribute] == {}
        @ref_hash[attribute] = create_ref_hash(attribute,
                                               pool,
                                               vcenter_uuid)
    end

    refkey = ''
    refkey = ref if ref
    refkey += vcenter_uuid if vcenter_uuid

    @ref_hash[attribute][refkey]
end

.find_image_by(att, the_class, path, ds_id, pool = nil) ⇒ Object



250
251
252
253
254
255
256
# File 'lib/vi_helper.rb', line 250

def self.find_image_by(att, the_class, path, ds_id, pool = nil)
    pool = one_pool(the_class, false) if pool.nil?
    pool.find do |e|
        e[att] == Addressable::URI.escape(path) &&
            e['DATASTORE_ID'] == ds_id
    end
end

.find_persistent_image_by_source(source, pool) ⇒ Object



258
259
260
261
262
263
# File 'lib/vi_helper.rb', line 258

def self.find_persistent_image_by_source(source, pool)
    pool.find do |e|
        e['SOURCE'] == source &&
            e['PERSISTENT'] == '1'
    end
end

.find_vcenter_vm_by_name(one_vm, host, vi_client) ⇒ Object



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

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

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

    if !view.view.nil? && !view.view.empty?
        vcenter_vm = view
                     .view
                     .find {|v| v.name == vm_name }
    end

    view.DestroyView # Destroy the view

    vcenter_vm
end

.generate_name(opts, nbytes) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/vi_helper.rb', line 146

def self.generate_name(opts, nbytes)
    return opts[:name] if nbytes <= 0

    @sha256 ||= Digest::SHA256.new
    chain = opts[:key]
    hash  = @sha256.hexdigest(chain)[0..nbytes-1]

    "#{opts[:name]}-#{hash}"
end

.get_cluster_id(clusters) ⇒ Object



75
76
77
78
79
80
# File 'lib/vi_helper.rb', line 75

def self.get_cluster_id(clusters)
    clusters.each do |id|
        return id unless id == -1
    end
    -1
end

.get_default(xpath) ⇒ Object



298
299
300
301
302
303
304
305
306
# File 'lib/vi_helper.rb', line 298

def self.get_default(xpath)
    begin
        xml = OpenNebula::XMLElement.new
        xml.initialize_xml(File.read(VCENTER_DRIVER_DEFAULT), 'VCENTER')
        xml[xpath]
    rescue StandardError
        nil
    end
end

.get_deploy_id(deploy_id) ⇒ Object

Since github.com/OpenNebula/one/issues/5689 there two deploy_ids allowed:

* moref, eg: vm-567
* moref +"_" + vcenter uuid, eg:
       2499952a-6c85-480e-b7df-4cbd2137eb69_vm-456

This function will always return the moref



132
133
134
# File 'lib/vi_helper.rb', line 132

def self.get_deploy_id(deploy_id)
    deploy_id.split('_')[0]
end

.get_location(item) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/vi_helper.rb', line 308

def self.get_location(item)
    folders = []
    until item.instance_of? RbVmomi::VIM::Datacenter
        item = item.parent
        if !item.instance_of?(RbVmomi::VIM::Datacenter) &&
           item.name != 'host'
            folders << item.name
        end
        raise 'Could not find the location' if item.nil?
    end
    location   = folders.reverse.join('/')
    location = '/' if location.empty?

    location
end

.get_ref_key(element, attribute, vcenter_uuid = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vi_helper.rb', line 172

def self.get_ref_key(element, attribute, vcenter_uuid = nil)
    key = element[attribute]

    return if key.nil?

    tvid = element['TEMPLATE/VCENTER_INSTANCE_ID']
    uvid = element['USER_TEMPLATE/VCENTER_INSTANCE_ID']

    if tvid
        key += tvid
    elsif uvid
        key += uvid
    elsif vcenter_uuid
        key += vcenter_uuid
    end

    key
end

.new_one_item(the_class) ⇒ Object



109
110
111
# File 'lib/vi_helper.rb', line 109

def self.new_one_item(the_class)
    the_class.new(the_class.build_xml, client)
end

.one_item(the_class, id, exit_if_fail = true) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/vi_helper.rb', line 98

def self.one_item(the_class, id, exit_if_fail = true)
    item = the_class.new_with_id(id, client)
    rc=nil
    if the_class == OpenNebula::VirtualMachine
        rc = item.info(true)
    else
        rc = item.info
    end
    return_if_error(rc, item, exit_if_fail)
end

.one_managed?(object) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
# File 'lib/vi_helper.rb', line 82

def self.one_managed?(object)
    if object.class.ancestors.include?(OpenNebula::XMLElement)
        managed =
            object['TEMPLATE/OPENNEBULA_MANAGED'] ||
            object['USER_TEMPLATE/OPENNEBULA_MANAGED']
        return managed != 'NO'
    end
    false
end

.one_name(the_class, name, key, pool = nil, bytes = 0) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/vi_helper.rb', line 156

def self.one_name(the_class, name, key, pool = nil, bytes = 0)
    # Remove \u007F character that comes from vcenter
    name = name.tr("\u007F", '')
    pool = one_pool(the_class) if pool.nil?

    import_name = generate_name({ :name => name, :key => key }, bytes)

    begin
        find_by_name(the_class, import_name, pool)
    rescue StandardError
        return import_name
    end

    one_name(the_class, name, key, pool, bytes+2)
end

.one_pool(the_class, exit_if_fail = true) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/vi_helper.rb', line 113

def self.one_pool(the_class, exit_if_fail = true)
    item = the_class.new(client)

    rc = nil
    begin
        rc = item.info_all
    rescue StandardError
        rc = item.info
    end

    return_if_error(rc, item, exit_if_fail)
end

.remove_ref_hash(attr, one_object) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/vi_helper.rb', line 216

def self.remove_ref_hash(attr, one_object)
    raise 'cache is empty!' unless @ref_hash

    refkey = get_ref_key(one_object, attr)

    return unless @ref_hash[attr]

    @ref_hash[attr].delete(refkey)
end

.return_if_error(rc, item, exit_if_fail) ⇒ Object

rubocop:enable Style/ClassVars



54
55
56
57
58
59
60
61
62
63
# File 'lib/vi_helper.rb', line 54

def self.return_if_error(rc, item, exit_if_fail)
    if OpenNebula.is_error?(rc)
        raise rc.message unless exit_if_fail

        STDERR.puts rc.message
        exit 1
    else
        item
    end
end

.set_client(options, client = nil) ⇒ Object

rubocop:enable Style/GlobalVars



45
46
47
48
49
50
51
# File 'lib/vi_helper.rb', line 45

def self.set_client(options, client = nil)
    if client.nil?
        @@client=OpenNebulaHelper::OneHelper.get_client(options, true)
    else
        @@client = client
    end
end