Service

class
Superclass
Arborist::Node
Included Modules
Arborist::HashUtilities
Arborist::NetworkUtilities

A node type for Arborist trees that represent services running on hosts.

Constants

DEFAULT_PROTOCOL

The default transport layer protocol to use for services that don't specify one

Public Class Methods

anchor
new( identifier, host, attributes={}, &block )

Create a new Service node.

# File lib/arborist/node/service.rb, line 28
def initialize( identifier, host, attributes={}, &block )
        raise Arborist::NodeError, "no host given" unless host.is_a?( Arborist::Node::Host )
        qualified_identifier = "%s-%s" % [ host.identifier, identifier ]

        @host         = host
        @addresses    = nil
        @app_protocol = nil
        @protocol     = nil
        @port         = nil

        attributes[ :app_protocol ] ||= identifier
        attributes[ :protocol ] ||= DEFAULT_PROTOCOL

        super( qualified_identifier, host, attributes, &block )

        unless @port
                service_port = default_port_for( @app_protocol, @protocol ) or
                        raise ArgumentError, "can't determine the port for %s/%s" %
                                [ @app_protocol, @protocol ]
                @port = Integer( service_port )
        end
end

Public Instance Methods

anchor
==( other_host )

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

# File lib/arborist/node/service.rb, line 178
def ==( other_host )
        return super &&
                other_host.addresses == self.addresses &&
                other_host.protocol == self.protocol &&
                other_host.app_protocol == self.app_protocol &&
                other_host.port == self.port
end
anchor
address( new_address )

Set an IP address of the service. This must be one of the addresses of its containing host.

# File lib/arborist/node/service.rb, line 83
def address( new_address )
        self.log.debug "Adding address %p to %p" % [ new_address, self ]
        normalized_addresses = normalize_address( new_address )

        unless normalized_addresses.all? {|addr| @host.addresses.include?(addr) }
                raise Arborist::ConfigError, "%s is not one of %s's addresses" %
                        [ new_address, @host.identifier ]
        end

        @addresses ||= []
        @addresses += normalized_addresses
end
anchor
addresses()

Delegate the service's address to its host.

# File lib/arborist/node/service.rb, line 98
def addresses
        return @addresses || @host.addresses
end
anchor
app_protocol()

Get/set the application protocol the service uses

# File lib/arborist/node/service.rb, line 62
dsl_accessor :app_protocol
anchor
hostname()

Delegate the service's hostname to it's parent host.

# File lib/arborist/node/service.rb, line 104
def hostname
        return @host.hostname
end
anchor
match_criteria?( key, val )

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

# File lib/arborist/node/service.rb, line 110
def match_criteria?( key, val )
        self.log.debug "Matching %p: %p against %p" % [ key, val, self ]
        array_val = Array( val )
        return case key
                when 'port'
                        vals = array_val.collect do |port|
                                port = default_port_for( port, @protocol ) unless port.is_a?( Integer )
                                port.to_i
                        end
                        vals.include?( self.port )
                when 'address'
                        search_addr = IPAddr.new( val )
                        self.addresses.any? {|a| search_addr.include?(a) }
                when 'protocol' then array_val.include?( self.protocol )
                when 'app', 'app_protocol' then array_val.include?( self.app_protocol )
                else
                        super
                end
end
anchor
modify( attributes )

Set service attributes.

# File lib/arborist/node/service.rb, line 70
def modify( attributes )
        attributes = stringify_keys( attributes )

        super

        self.port( attributes['port'] )
        self.app_protocol( attributes['app_protocol'] )
        self.protocol( attributes['protocol'] )
end
anchor
node_description()

Return service-node-specific information for inspect.

# File lib/arborist/node/service.rb, line 145
def node_description
        return "{listening on %s port %d}" % [
                self.protocol,
                self.port,
        ]
end
anchor
operational_values()

Return a Hash of the operational values that are included with the node's monitor state.

# File lib/arborist/node/service.rb, line 133
def operational_values
        return super.merge(
                addresses: self.addresses.map( &:to_s ),
                hostname: self.hostname,
                port: self.port,
                protocol: self.protocol,
                app_protocol: self.app_protocol,
        )
end
anchor
parent( new_parent=nil )

Overridden to disallow modification of a Service's parent, as it needs a reference to the Host node for delegation.

# File lib/arborist/node/service.rb, line 155
def parent( new_parent=nil )
        return super unless new_parent
        raise "Can't reparent a service; replace the node instead"
end
anchor
port()

Get/set the port the service binds to

# File lib/arborist/node/service.rb, line 58
dsl_accessor :port
anchor
protocol()

Get/set the network protocol the service uses

# File lib/arborist/node/service.rb, line 66
dsl_accessor :protocol
anchor
to_h( * )

Return a Hash of the host node's state.

# File lib/arborist/node/service.rb, line 166
def to_h( * )
        return super.merge(
                addresses: self.addresses.map(&:to_s),
                protocol: self.protocol,
                app_protocol: self.app_protocol,
                port: self.port
        )
end