Module: OpenNebula::WaitExt

Defined in:
lib/opennebula/wait_ext.rb

Overview

Module to decorate Wait classes with the following methods:

- Wait

rubocop:disable Style/ClassAndModuleChildren

Constant Summary collapse

WAIT =

Wait classes and the name published in ZMQ/STATE

{
    OpenNebula::VirtualNetwork  => {
        :event => lambda {|o, s1, _s2|
            "EVENT STATE NET/#{s1}//#{o['ID']}"
        },

        :in_state => lambda {|o, s1, _s2|
            obj_s = Integer(o['STATE'])
            inx_s = OpenNebula::VirtualNetwork::VN_STATES.index(s1)

            obj_s == inx_s
        },

        :in_state_e => lambda {|s1, _s2, content|
            xml   = Nokogiri::XML(Base64.decode64(content))

            obj_s = Integer(xml.xpath('//VNET/STATE').text)
            inx_s = OpenNebula::VirtualNetwork::VN_STATES.index(s1)

            obj_s == inx_s
        }
    },

    OpenNebula::Host  => {
        :event => lambda {|o, s1, _s2|
            "EVENT STATE HOST/#{s1}//#{o['ID']}"
        },

        :in_state => lambda {|o, s1, _s2|
            obj_s = Integer(o['STATE'])
            inx_s = OpenNebula::Host::HOST_STATES.index(s1)

            obj_s == inx_s
        },

        :in_state_e => lambda {|s1, _s2, content|
            xml   = Nokogiri::XML(Base64.decode64(content))

            obj_s = Integer(xml.xpath('//HOST/STATE').text)
            inx_s = OpenNebula::Host::HOST_STATES.index(s1)

            obj_s == inx_s
        }
    },

    OpenNebula::Image  => {
        :event => lambda {|o, s1, _s2|
            "EVENT STATE IMAGE/#{s1}//#{o['ID']}"
        },

        :in_state => lambda {|o, s1, _s2|
            obj_s = Integer(o['STATE'])
            inx_s = OpenNebula::Image::IMAGE_STATES.index(s1)

            obj_s == inx_s
        },

        :in_state_e => lambda {|s1, _s2, content|
            xml   = Nokogiri::XML(Base64.decode64(content))

            obj_s = Integer(xml.xpath('//IMAGE/STATE').text)
            inx_s = OpenNebula::Image::IMAGE_STATES.index(s1)

            obj_s == inx_s
        }
    },

    OpenNebula::VirtualMachine => {
        :event => lambda {|o, s1, s2|
            "EVENT STATE VM/#{s1}/#{s2}/#{o['ID']}"
        },

        :in_state => lambda {|o, s1, s2|
            obj_s1 = Integer(o['STATE'])
            inx_s1 = OpenNebula::VirtualMachine::VM_STATE.index(s1)

            obj_s2 = Integer(o['LCM_STATE'])
            inx_s2 = OpenNebula::VirtualMachine::LCM_STATE.index(s2)

            obj_s1 == inx_s1 && obj_s2 == inx_s2
        },

        :in_state_e => lambda {|s1, s2, content|
            xml = Nokogiri::XML(Base64.decode64(content))

            obj_s1 = Integer(xml.xpath('//VM/STATE').text)
            inx_s1 = OpenNebula::VirtualMachine::VM_STATE.index(s1)

            obj_s2 = Integer(xml.xpath('//VM/LCM_STATE').text)
            inx_s2 = OpenNebula::VirtualMachine::LCM_STATE.index(s2)

            obj_s1 == inx_s1 && obj_s2 == inx_s2
        }
    }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extend_object(obj) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/opennebula/wait_ext.rb', line 236

def self.extend_object(obj)
    wait?(obj)

    class << obj

        begin
            require 'ffi-rzmq'

            include OpenNebula::WaitExtEvent
        rescue LoadError
            include OpenNebula::WaitExtPolling
        end

    end

    super
end

.wait?(obj) ⇒ Boolean

Check if object has the method wait or not

Parameters:

  • obj (Object or Class)

    Object to check class

Returns:

  • (Boolean)

Raises:

  • (StandardError)


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/opennebula/wait_ext.rb', line 269

def self.wait?(obj)
    # Get obj class to find parents in wait class
    # rubocop:disable Style/TernaryParentheses
    (obj.is_a? Class) ? o_class = obj : o_class = obj.class
    # rubocop:enable Style/TernaryParentheses

    found   = false
    i_class = o_class

    while i_class
        if WAIT.keys.include?(i_class)
            found = true
            break
        end

        i_class = i_class.superclass
    end

    return if found

    raise StandardError, "Cannot extend #{o_class} with WaitExt"
end

Instance Method Details

#wait(state_str, timeout = 60, cycles = -1)) ⇒ Object

Wait until the element reaches some specific state It waits until the state can be found in ZMQ event message

Parameters:

  • state_str (String)

    State name to wait

  • timeout (Integer) (defaults to: 60)

    Number of seconds to timeout event recv

  • cycles (Integer) (defaults to: -1))

    Number of recv cycles. After each one object status is checked in OpenNebula. Use -1 (default) to wait forever.



262
263
264
# File 'lib/opennebula/wait_ext.rb', line 262

def wait(state_str, timeout = 60, cycles = -1)
    wait2(state_str, '', timeout, cycles)
end