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

addresses[R]

The network address(es) of this Host as an Array of IPAddr objects

Public Class Methods

new( identifier, attributes={}, &block ) click to toggle source

Create a new Host node.

Calls superclass method Arborist::Node::new
# File lib/arborist/node/host.rb, line 36
def initialize( identifier, attributes={}, &block )
        @addresses = []
        @hostname = nil
        super
end

Public Instance Methods

==( other_host ) click to toggle source

Equality operator – returns true if other_node is equal to the receiver. Overridden to also compare addresses.

Calls superclass method Arborist::Node#==
# File lib/arborist/node/host.rb, line 147
def ==( other_host )
        return super &&
                other_host.addresses == self.addresses &&
                other_host.hostname == @hostname
end
address( new_address ) click to toggle source

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
family() click to toggle source

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
hostname() click to toggle source

An optional hostname.

# File lib/arborist/node/host.rb, line 53
dsl_accessor :hostname
marshal_load( hash ) click to toggle source

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.

Calls superclass method 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
match_criteria?( key, val ) click to toggle source

Returns true if the node matches the specified key and val criteria.

Calls superclass method 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
modify( attributes ) click to toggle source

Set one or more node attributes. Supported attributes (in addition to those supported by Node) are: addresses.

Calls superclass method 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
node_description() click to toggle source

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
operational_values() click to toggle source

Return the host's operational attributes.

Calls superclass method 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
to_h( ** ) click to toggle source

Return a Hash of the host node's state.

Calls superclass method 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