class VirtualMachineDriver

This class provides basic messaging and logging functionality to implement OpenNebula Drivers. A driver is a program that specialize the OpenNebula behavior by interfacing with specific infrastructure functionalities.

A Driver inherits this class and only has to provide methods for each action it wants to receive. The method must be associated with the action name through the register_action function

Constants

ACTION

Virtual Machine Driver Protocol constants

HOST_ARG
POLL_ATTRIBUTE
VM_STATE

Public Class Methods

new(directory, options={}) click to toggle source

Register default actions for the protocol.

@param [String] directory path inside remotes path where the scripts

reside

@param [Hash] options options for OpenNebula driver (check the available

options in {OpenNebulaDriver#initialize})

@option options [Boolean] :threaded (true) enables or disables threads

Calls superclass method OpenNebulaDriver.new
# File lib/VirtualMachineDriver.rb, line 69
def initialize(directory, options={})
    @options={
        :threaded    => true,
        :single_host => true
    }.merge!(options)

    super(directory, @options)

    @hosts   = Array.new

    register_action(ACTION[:deploy].to_sym,      method("deploy"))
    register_action(ACTION[:shutdown].to_sym,    method("shutdown"))
    register_action(ACTION[:reboot].to_sym,      method("reboot"))
    register_action(ACTION[:reset].to_sym,       method("reset"))
    register_action(ACTION[:cancel].to_sym,      method("cancel"))
    register_action(ACTION[:save].to_sym,        method("save"))
    register_action(ACTION[:restore].to_sym,     method("restore"))
    register_action(ACTION[:migrate].to_sym,     method("migrate"))
    register_action(ACTION[:poll].to_sym,        method("poll"))
    register_action(ACTION[:attach_disk].to_sym, method("attach_disk"))
    register_action(ACTION[:detach_disk].to_sym, method("detach_disk"))
    register_action(ACTION[:snapshot_create].to_sym, method("snapshot_create"))
    register_action(ACTION[:snapshot_revert].to_sym, method("snapshot_revert"))
    register_action(ACTION[:snapshot_delete].to_sym, method("snapshot_delete"))
    register_action(ACTION[:cleanup].to_sym,    method("cleanup"))
    register_action(ACTION[:attach_nic].to_sym, method("attach_nic"))
    register_action(ACTION[:detach_nic].to_sym, method("detach_nic"))
    register_action(ACTION[:disk_snapshot_create].to_sym, method("disk_snapshot_create"))
    register_action(ACTION[:resize_disk].to_sym, method("resize_disk"))
    register_action(ACTION[:update_sg].to_sym, method("update_sg"))
end

Public Instance Methods

attach_disk(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 168
def attach_disk(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:attach_disk],RESULT[:failure],id,error)
end
attach_nic(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 178
def attach_nic(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:attach_nic],RESULT[:failure],id,error)
end
cancel(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 143
def cancel(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:cancel],RESULT[:failure],id,error)
end
cleanup(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 218
def cleanup(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:cleanup],RESULT[:failure],id,error)
end
decode(drv_message) click to toggle source

Decodes the encoded XML driver message received from the core

@param [String] drv_message the driver message @return [REXML::Element] the root element of the decoded XML message

# File lib/VirtualMachineDriver.rb, line 105
def decode(drv_message)
    message = Base64.decode64(drv_message)
    xml_doc = REXML::Document.new(message)

    xml_doc.root
end
deploy(id, drv_message) click to toggle source

Virtual Machine Manager Protocol Actions (generic implementation)

# File lib/VirtualMachineDriver.rb, line 123
def deploy(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:deploy],RESULT[:failure],id,error)
end
detach_disk(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 173
def detach_disk(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:detach_disk],RESULT[:failure],id,error)
end
detach_nic(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 183
def detach_nic(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:detach_nic],RESULT[:failure],id,error)
end
disk_snapshot_create(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 203
def disk_snapshot_create(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:disk_snapshot_create],RESULT[:failure],id,error)
end
local_action(command, id, action) click to toggle source

Execute a command associated to an action and id on localhost

Calls superclass method
# File lib/VirtualMachineDriver.rb, line 118
def local_action(command, id, action)
    super(command,id,ACTION[action])
end
migrate(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 158
def migrate(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:migrate],RESULT[:failure],id,error)
end
poll(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 163
def poll(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:poll],RESULT[:failure],id,error)
end
reboot(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 133
def reboot(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:reboot],RESULT[:failure],id,error)
end
remotes_action(command, id, host, action, remote_dir, std_in=nil) click to toggle source

Execute a command associated to an action and id in a remote host.

Calls superclass method
# File lib/VirtualMachineDriver.rb, line 113
def remotes_action(command, id, host, action, remote_dir, std_in=nil)
    super(command,id,host,ACTION[action],remote_dir,std_in)
end
reset(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 138
def reset(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:reset],RESULT[:failure],id,error)
end
resize_disk(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 208
def resize_disk(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:resize_disk],RESULT[:failure],id,error)
end
restore(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 153
def restore(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:restore],RESULT[:failure],id,error)
end
save(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 148
def save(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:save],RESULT[:failure],id,error)
end
shutdown(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 128
def shutdown(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:shutdown],RESULT[:failure],id,error)
end
snapshot_create(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 188
def snapshot_create(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:snapshot_create],RESULT[:failure],id,error)
end
snapshot_delete(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 198
def snapshot_delete(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:snapshot_delete],RESULT[:failure],id,error)
end
snapshot_revert(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 193
def snapshot_revert(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:snapshot_revert],RESULT[:failure],id,error)
end
update_sg(id, drv_message) click to toggle source
# File lib/VirtualMachineDriver.rb, line 213
def update_sg(id, drv_message)
    error = "Action not implemented by driver #{self.class}"
    send_message(ACTION[:update_sg],RESULT[:failure],id,error)
end

Private Instance Methods

delete_running_action(action_id) click to toggle source

Interface to handle the pending events from the ActionManager Interface

Calls superclass method ActionManager#delete_running_action
# File lib/VirtualMachineDriver.rb, line 226
def delete_running_action(action_id)
    if @options[:single_host]
        delete_running_action_single_host(action_id)
    else
        super(action_id)
    end
end
delete_running_action_single_host(action_id) click to toggle source
# File lib/VirtualMachineDriver.rb, line 234
def delete_running_action_single_host(action_id)
    action=@action_running[action_id]
    if action
        @hosts.delete(action[:host])
        @action_running.delete(action_id)
    end
end
empty_queue() click to toggle source
Calls superclass method ActionManager#empty_queue
# File lib/VirtualMachineDriver.rb, line 306
def empty_queue
    if @options[:single_host]
        empty_queue_single_host
    else
        super
    end
end
empty_queue_single_host() click to toggle source
# File lib/VirtualMachineDriver.rb, line 314
def empty_queue_single_host
    get_first_runable==nil
end
get_first_runable() click to toggle source
Calls superclass method
# File lib/VirtualMachineDriver.rb, line 242
def get_first_runable
    if @options[:single_host]
        get_first_runable_single_host
    else
        super
    end
end
get_first_runable_single_host() click to toggle source
# File lib/VirtualMachineDriver.rb, line 250
def get_first_runable_single_host
    action_index=nil
    @action_queue.each_with_index do |action, index|
        if !action.keys.include?(:host)
            if action[:args].length == 2
                begin
                    xml=decode(action[:args].last)
                    host=xml.elements['HOST']
                    action[:host]=host.text if host
                rescue
                    action[:host]=nil
                end
            else
                action[:host]=nil
            end
        end

        if action.keys.include?(:host) && action[:host]
            if !@hosts.include?(action[:host])
                action_index=index
                break
            end
       else
            action_index=index
            break
       end
    end

    return action_index
end
get_runable_action() click to toggle source
Calls superclass method ActionManager#get_runable_action
# File lib/VirtualMachineDriver.rb, line 281
def get_runable_action
    if @options[:single_host]
        get_runable_action_single_host
    else
        super
    end
end
get_runable_action_single_host() click to toggle source
# File lib/VirtualMachineDriver.rb, line 289
def get_runable_action_single_host
    action_index=get_first_runable

    if action_index
        action=@action_queue[action_index]
    else
        action=nil
    end

    if action
        @hosts << action[:host] if action[:host]
        @action_queue.delete_at(action_index)
    end

    return action
end