Class: OpenNebula::Host
- Inherits:
-
PoolElement
- Object
- XMLElement
- PoolElement
- OpenNebula::Host
- Defined in:
- lib/opennebula/host.rb
Constant Summary collapse
- HOST_METHODS =
Constants and Class Methods
{ :info => "host.info", :allocate => "host.allocate", :delete => "host.delete", :status => "host.status", :update => "host.update", :monitoring => "host.monitoring", :rename => "host.rename" }
- HOST_STATES =
%w{INIT MONITORING_MONITORED MONITORED ERROR DISABLED MONITORING_ERROR MONITORING_INIT MONITORING_DISABLED OFFLINE}
- SHORT_HOST_STATES =
{ "INIT" => "init", "MONITORING_MONITORED" => "update", "MONITORED" => "on", "ERROR" => "err", "DISABLED" => "dsbl", "MONITORING_ERROR" => "retry", "MONITORING_INIT" => "init", "MONITORING_DISABLED" => "dsbl", "OFFLINE" => "off" }
- HOST_STATUS =
{ "ENABLED" => 0, "DISABLED" => 1, "OFFLINE" => 2 }
Instance Attribute Summary
Attributes inherited from PoolElement
Class Method Summary collapse
-
.build_xml(pe_id = nil) ⇒ Object
Creates a Host description with just its identifier this method should be used to create plain Host objects.
Instance Method Summary collapse
-
#allocate(hostname, im, vmm, cluster_id = ClusterPool::NONE_CLUSTER_ID) ⇒ Integer, OpenNebula::Error
Allocates a new Host in OpenNebula.
-
#delete ⇒ Object
Deletes the Host.
-
#disable ⇒ Object
Disables the Host.
-
#enable ⇒ Object
Enables the Host.
- #flush(action) ⇒ Object
-
#forceupdate ⇒ Object
Resets monitoring forcing an update.
-
#import_wild(name, ipv4 = nil, ipv6 = nil) ⇒ nil, OpenNebula::Error
Imports a wild VM from the host and puts it in running state.
-
#importable_wilds ⇒ Object
Get importable wild VMs in the host.
-
#info(decrypt = false) ⇒ Object
(also: #info!)
Retrieves the information of the given Host.
-
#initialize(xml, client) ⇒ Host
constructor
Class constructor.
-
#monitoring(xpath_expressions) ⇒ Hash<String, Array<Array<int>>>, OpenNebula::Error
Retrieves this Host’s monitoring data from OpenNebula.
-
#monitoring_xml ⇒ String
Retrieves this Host’s monitoring data from OpenNebula, in XML.
-
#offline ⇒ Object
Sets the Host offline.
-
#rename(name) ⇒ nil, OpenNebula::Error
Renames this Host.
-
#short_state_str ⇒ Object
Returns the state of the Host (string value).
-
#state ⇒ Object
Returns the state of the Host (numeric value).
-
#state_str ⇒ Object
Returns the state of the Host (string value).
-
#template_str(indent = true) ⇒ Object
- Returns the <TEMPLATE> element in text form indent
-
Boolean indents the resulting string, default true.
-
#update(new_template, append = false) ⇒ nil, OpenNebula::Error
Replaces the template contents.
-
#wilds ⇒ Object
Get wild VMs in the host.
Methods inherited from PoolElement
#id, new_with_id, #replace, #to_str
Methods inherited from XMLElement
#[], #add_element, #attr, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize_xml, #name, #retrieve_elements, #retrieve_xmlelements, #set_content, #template_like_str, #template_xml, #text, #to_hash, #to_xml, #xml_nil?
Constructor Details
#initialize(xml, client) ⇒ Host
Class constructor
76 77 78 79 80 81 |
# File 'lib/opennebula/host.rb', line 76 def initialize(xml, client) super(xml,client) @client = client @pe_id = self['ID'].to_i if self['ID'] end |
Class Method Details
.build_xml(pe_id = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/opennebula/host.rb', line 65 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 |
Instance Method Details
#allocate(hostname, im, vmm, cluster_id = ClusterPool::NONE_CLUSTER_ID) ⇒ Integer, OpenNebula::Error
Allocates a new Host in OpenNebula
103 104 105 |
# File 'lib/opennebula/host.rb', line 103 def allocate(hostname, im, vmm, cluster_id=ClusterPool::NONE_CLUSTER_ID) super(HOST_METHODS[:allocate], hostname, im, vmm, cluster_id) end |
#delete ⇒ Object
Deletes the Host
108 109 110 |
# File 'lib/opennebula/host.rb', line 108 def delete() super(HOST_METHODS[:delete]) end |
#disable ⇒ Object
Disables the Host
118 119 120 |
# File 'lib/opennebula/host.rb', line 118 def disable() set_status("DISABLED") end |
#enable ⇒ Object
Enables the Host
113 114 115 |
# File 'lib/opennebula/host.rb', line 113 def enable() set_status("ENABLED") end |
#flush(action) ⇒ Object
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 |
# File 'lib/opennebula/host.rb', line 135 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. 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 ⇒ Object
Resets monitoring forcing an update
128 129 130 131 132 133 |
# File 'lib/opennebula/host.rb', line 128 def forceupdate() rc = offline return rc if OpenNebula.is_error?(rc) enable end |
#import_wild(name, ipv4 = nil, ipv6 = nil) ⇒ nil, OpenNebula::Error
Imports a wild VM from the host and puts it in running state
224 225 226 227 228 229 230 231 232 233 234 235 236 237 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 264 265 266 267 268 |
# File 'lib/opennebula/host.rb', line 224 def import_wild(name, ipv4 = nil, ipv6 = nil) 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'], :ipv4 => ipv4, :ipv6 => ipv6 } ) else rc = vm.allocate(template) return rc if OpenNebula.is_error?(rc) vm.deploy(id, false) return vm.id end end |
#importable_wilds ⇒ Object
Get importable wild VMs in the host
301 302 303 |
# File 'lib/opennebula/host.rb', line 301 def importable_wilds wilds.select {|w| Hash === w && w['IMPORT_TEMPLATE'] } end |
#info(decrypt = false) ⇒ Object Also known as: info!
Retrieves the information of the given Host.
88 89 90 |
# File 'lib/opennebula/host.rb', line 88 def info(decrypt = false) super(HOST_METHODS[:info], 'HOST', decrypt) end |
#monitoring(xpath_expressions) ⇒ Hash<String, Array<Array<int>>>, OpenNebula::Error
Retrieves this Host’s monitoring data from OpenNebula
193 194 195 |
# File 'lib/opennebula/host.rb', line 193 def monitoring(xpath_expressions) return super(HOST_METHODS[:monitoring], xpath_expressions) end |
#monitoring_xml ⇒ String
Retrieves this Host’s monitoring data from OpenNebula, in XML
200 201 202 203 204 |
# File 'lib/opennebula/host.rb', line 200 def monitoring_xml() return Error.new('ID not defined') if !@pe_id return @client.call(HOST_METHODS[:monitoring], @pe_id) end |
#offline ⇒ Object
Sets the Host offline
123 124 125 |
# File 'lib/opennebula/host.rb', line 123 def offline() set_status("OFFLINE") end |
#rename(name) ⇒ nil, OpenNebula::Error
Renames this Host
212 213 214 |
# File 'lib/opennebula/host.rb', line 212 def rename(name) return call(HOST_METHODS[:rename], @pe_id, name) end |
#short_state_str ⇒ Object
Returns the state of the Host (string value)
285 286 287 |
# File 'lib/opennebula/host.rb', line 285 def short_state_str SHORT_HOST_STATES[state_str] end |
#state ⇒ Object
Returns the state of the Host (numeric value)
275 276 277 |
# File 'lib/opennebula/host.rb', line 275 def state self['STATE'].to_i end |
#state_str ⇒ Object
Returns the state of the Host (string value)
280 281 282 |
# File 'lib/opennebula/host.rb', line 280 def state_str HOST_STATES[state] end |
#template_str(indent = true) ⇒ Object
Returns the <TEMPLATE> element in text form
- indent
-
Boolean indents the resulting string, default true
291 292 293 |
# File 'lib/opennebula/host.rb', line 291 def template_str(indent=true) template_like_str('TEMPLATE', indent) end |
#update(new_template, append = false) ⇒ nil, OpenNebula::Error
Replaces the template contents
170 171 172 |
# File 'lib/opennebula/host.rb', line 170 def update(new_template, append=false) super(HOST_METHODS[:update], new_template, append ? 1 : 0) end |
#wilds ⇒ Object
Get wild VMs in the host
296 297 298 |
# File 'lib/opennebula/host.rb', line 296 def wilds [self.to_hash['HOST']['TEMPLATE']['VM']].flatten.compact end |