class OpenNebula::Host

Constants

HOST_METHODS

Constants and Class Methods

HOST_STATES
HOST_STATUS
SHORT_HOST_STATES

Public Class Methods

build_xml(pe_id=nil) click to toggle source

Creates a Host description with just its identifier this method should be used to create plain Host objects. id the id of the host

Example:

host = Host.new(Host.build_xml(3),rpc_client)
# File lib/opennebula/host.rb, line 66
def Host.build_xml(pe_id=nil)
    if pe_id
        host_xml = "<HOST><ID>#{pe_id}</ID></HOST>"
    else
        host_xml = "<HOST></HOST>"
    end

    XMLElement.build_xml(host_xml, 'HOST')
end
new(xml, client) click to toggle source

Class constructor

Calls superclass method
# File lib/opennebula/host.rb, line 77
def initialize(xml, client)
    super(xml,client)

    @client = client
    @pe_id  = self['ID'].to_i if self['ID']
end

Public Instance Methods

allocate(hostname, im, vmm, cluster_id=ClusterPool::NONE_CLUSTER_ID) click to toggle source

Allocates a new Host in OpenNebula

@param hostname [String] Name of the new Host. @param im [String] Name of the im_driver (information/monitoring) @param vmm [String] Name of the vmm_driver (hypervisor) @param cluster_id [String] Id of the cluster, -1 to use default

@return [Integer, OpenNebula::Error] the new ID in case of

success, error otherwise
Calls superclass method
# File lib/opennebula/host.rb, line 104
def allocate(hostname, im, vmm, cluster_id=ClusterPool::NONE_CLUSTER_ID)
    super(HOST_METHODS[:allocate], hostname, im, vmm, cluster_id)
end
delete() click to toggle source

Deletes the Host

Calls superclass method
# File lib/opennebula/host.rb, line 109
def delete()
    super(HOST_METHODS[:delete])
end
disable() click to toggle source

Disables the Host

# File lib/opennebula/host.rb, line 119
def disable()
    set_status("DISABLED")
end
enable() click to toggle source

Enables the Host

# File lib/opennebula/host.rb, line 114
def enable()
    set_status("ENABLED")
end
flush(action) click to toggle source
# File lib/opennebula/host.rb, line 136
def flush(action)
    self.disable

    vm_pool = OpenNebula::VirtualMachinePool.new(@client,
                                        VirtualMachinePool::INFO_ALL_VM)

    rc = vm_pool.info
    if OpenNebula.is_error?(rc)
         puts rc.message
         exit(-1)
    end

    vm_pool.each do |vm|
        hid = vm['HISTORY_RECORDS/HISTORY[last()]/HID']
        if hid == self['ID']
            case action
            when "resched"
                vm.resched
            when "delete-recreate"
                vm.recover(4)
            else
                vm.resched
            end
        end
    end
end
forceupdate() click to toggle source

Resets monitoring forcing an update

# File lib/opennebula/host.rb, line 129
def forceupdate()
    rc = offline
    return rc if OpenNebula.is_error?(rc)

    enable
end
import_wild(name) click to toggle source

Imports a wild VM from the host and puts it in running state

@param name [String] Name of the VM to import

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
# File lib/opennebula/host.rb, line 223
def import_wild(name)
    vms = importable_wilds.select {|vm| vm['VM_NAME'] == name }

    if vms.length == 0
        return OpenNebula::Error.new("No importable wilds with name " <<
            "'#{name}' found.")
    elsif vms.length > 1
        return OpenNebula::Error.new("More than one importable wild " <<
            "with name '#{name}' found.")
    end

    wild = vms.first

    template = Base64.decode64(wild['IMPORT_TEMPLATE'])

    xml = OpenNebula::VirtualMachine.build_xml
    vm = OpenNebula::VirtualMachine.new(xml, @client)

    # vCenter wild VMs has a different process
    # image and vnets objects representing existing nics and disks
    # must be created and referenced
    vcenter_wild_vm = wild.key? "VCENTER_TEMPLATE"
    if vcenter_wild_vm
        require 'vcenter_driver'
        vi_client = VCenterDriver::VIClient.new_from_host(self["ID"])
        importer  = VCenterDriver::VmmImporter.new(@client, vi_client)

        return importer.import({wild: wild, template: template,
                                one_item: vm, host: self['ID']})
    else
        rc = vm.allocate(template)

        return rc if OpenNebula.is_error?(rc)

        vm.deploy(id, false)
        return vm.id
    end
end
importable_wilds() click to toggle source

Get importable wild VMs in the host

# File lib/opennebula/host.rb, line 293
def importable_wilds
    wilds.select {|w| Hash === w && w['IMPORT_TEMPLATE'] }
end
info(decrypt = false) click to toggle source

Retrieves the information of the given Host.

Calls superclass method
# File lib/opennebula/host.rb, line 89
def info(decrypt = false)
    super(HOST_METHODS[:info], 'HOST', decrypt)
end
Also aliased as: info!
info!(decrypt = false)
Alias for: info
monitoring(xpath_expressions) click to toggle source

Retrieves this Host’s monitoring data from OpenNebula

@param [Array<String>] xpath_expressions Elements to retrieve.

@return [Hash<String, Array<Array<int>>>, OpenNebula::Error] Hash with

the requested xpath expressions, and an Array of 'timestamp, value'.

@example

host.monitoring( ['HOST_SHARE/FREE_CPU', 'HOST_SHARE/RUNNING_VMS'] )

{ "HOST_SHARE/RUNNING_VMS" =>
    [["1337266000", "1"],
     ["1337266044", "1"],
     ["1337266088", "3"]],
  "HOST_SHARE/FREE_CPU" =>
    [["1337266000", "800"],
     ["1337266044", "800"],
     ["1337266088", "800"]]
}
Calls superclass method
# File lib/opennebula/host.rb, line 194
def monitoring(xpath_expressions)
    return super(HOST_METHODS[:monitoring], xpath_expressions)
end
monitoring_xml() click to toggle source

Retrieves this Host’s monitoring data from OpenNebula, in XML

@return [String] Monitoring data, in XML

# File lib/opennebula/host.rb, line 201
def monitoring_xml()
    return Error.new('ID not defined') if !@pe_id

    return @client.call(HOST_METHODS[:monitoring], @pe_id)
end
offline() click to toggle source

Sets the Host offline

# File lib/opennebula/host.rb, line 124
def offline()
    set_status("OFFLINE")
end
rename(name) click to toggle source

Renames this Host

@param name [String] New name for the Host.

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
# File lib/opennebula/host.rb, line 213
def rename(name)
    return call(HOST_METHODS[:rename], @pe_id, name)
end
short_state_str() click to toggle source

Returns the state of the Host (string value)

# File lib/opennebula/host.rb, line 277
def short_state_str
    SHORT_HOST_STATES[state_str]
end
state() click to toggle source

Returns the state of the Host (numeric value)

# File lib/opennebula/host.rb, line 267
def state
    self['STATE'].to_i
end
state_str() click to toggle source

Returns the state of the Host (string value)

# File lib/opennebula/host.rb, line 272
def state_str
    HOST_STATES[state]
end
template_str(indent=true) click to toggle source

Returns the <TEMPLATE> element in text form

indent

Boolean indents the resulting string, default true

# File lib/opennebula/host.rb, line 283
def template_str(indent=true)
    template_like_str('TEMPLATE', indent)
end
update(new_template, append=false) click to toggle source

Replaces the template contents

@param new_template [String] New template contents @param append [true, false] True to append new attributes instead of

replace the whole template

@return [nil, OpenNebula::Error] nil in case of success, Error

otherwise
Calls superclass method
# File lib/opennebula/host.rb, line 171
def update(new_template, append=false)
    super(HOST_METHODS[:update], new_template, append ? 1 : 0)
end
wilds() click to toggle source

Get wild VMs in the host

# File lib/opennebula/host.rb, line 288
def wilds
    [self.to_hash['HOST']['TEMPLATE']['VM']].flatten.compact
end

Private Instance Methods

set_status(status) click to toggle source
# File lib/opennebula/host.rb, line 298
def set_status(status)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(HOST_METHODS[:status], @pe_id, HOST_STATUS[status])
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end