class Arborist::Node::Host
A node type for Arborist
trees that represent network-connected hosts.
host_node = Arborist::Node.create( :host, 'acme' ) do description "Public-facing webserver" address '93.184.216.34' tags :public, :dmz resource 'disk' resource 'memory' do config hwm: '3.4G' end resource 'loadavg' resource 'processes' do config expect: { nginx: 2 } end service 'ssh' service 'www' end
Attributes
The network address(es) of this Host
as an Array of IPAddr objects
Public Class Methods
Create a new Host
node.
Arborist::Node::new
# File lib/arborist/node/host.rb, line 36 def initialize( identifier, attributes={}, &block ) @addresses = [] @hostname = nil super end
Public Instance Methods
Equality operator – returns true
if other_node
is equal to the receiver. Overridden to also compare addresses.
# File lib/arborist/node/host.rb, line 147 def ==( other_host ) return super && other_host.addresses == self.addresses && other_host.hostname == @hostname end
Set an IP address of the host.
# File lib/arborist/node/host.rb, line 91 def address( new_address ) self.log.debug "Adding address %p to %p" % [ new_address, self ] if new_address.to_s =~ /^[[:alnum:]][a-z0-9\-]+/i && ! @hostname @hostname = new_address end @addresses += normalize_address( new_address ) @addresses.uniq! end
Return the node family, so observers can know ancestry after serialization for custom node types that inherit from this class.
# File lib/arborist/node/host.rb, line 58 def family return :host end
An optional hostname.
# File lib/arborist/node/host.rb, line 53 dsl_accessor :hostname
Marshal API – set up the object's state using the hash
from a previously-marshalled node. Overridden to turn the addresses back into IPAddr objects.
Arborist::Node#marshal_load
# File lib/arborist/node/host.rb, line 138 def marshal_load( hash ) super @addresses = hash[:addresses].map {|addr| IPAddr.new(addr) } @hostname = hash[:hostname] end
Returns true
if the node matches the specified key
and val
criteria.
Arborist::Node#match_criteria?
# File lib/arborist/node/host.rb, line 104 def match_criteria?( key, val ) return case key when 'hostname' then @hostname == val when 'address' search_addr = IPAddr.new( val ) @addresses.any? {|a| search_addr.include?(a) } else super end end
Set one or more node attributes
. Supported attributes (in addition to those supported by Node) are: addresses
.
Arborist::Node#modify
# File lib/arborist/node/host.rb, line 65 def modify( attributes ) attributes = stringify_keys( attributes ) super self.hostname( attributes['hostname'] ) if attributes[ 'hostname' ] if attributes[ 'addresses' ] self.addresses.clear Array( attributes['addresses'] ).each do |addr| self.address( addr ) end end end
Return host-node-specific information for inspect
.
# File lib/arborist/node/host.rb, line 117 def node_description return "{no addresses}" if self.addresses.empty? return "{addresses: %s}" % [ self.addresses.map(&:to_s).join(', ') ] end
Return the host's operational attributes.
Arborist::Node#operational_values
# File lib/arborist/node/host.rb, line 81 def operational_values properties = super return properties.merge( hostname: @hostname, addresses: self.addresses.map(&:to_s) ) end
Return a Hash of the host node's state.
Arborist::Node#to_h
# File lib/arborist/node/host.rb, line 128 def to_h( ** ) return super.merge( hostname: @hostname, addresses: self.addresses.map(&:to_s) ) end