Class: Arborist::Node::Host

Inherits:
Arborist::Node show all
Includes:
Arborist::NetworkUtilities
Defined in:
lib/arborist/node/host.rb

Overview

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

Constant Summary

Constants included from Arborist::NetworkUtilities

Arborist::NetworkUtilities::IPADDR_RE

Constants inherited from Arborist::Node

LOADED_INSTANCE_KEY, OPERATIONAL_ATTRIBUTES, UNREACHABLE_STATES, VALID_IDENTIFIER

Instance Attribute Summary collapse

Attributes inherited from Arborist::Node

#ack, #children, #dependencies, #errors, #identifier, #last_contacted, #pending_change_events, #previous_ack, #properties, #quieted_reasons, #source, #status_changed, #status_history, #status_last_changed, #subscriptions, #update_delta, #warnings

Instance Method Summary collapse

Methods included from Arborist::NetworkUtilities

#normalize_address

Methods inherited from Arborist::Node

#<<, #acked_description, #acknowledge, #add_child, add_loaded_instance, add_subnode_factory_method, #add_subscription, #all_of, #any_of, #broadcast_events, #clear_transition_temp_vars, #config, curried_create, #dependencies_down?, #dependencies_up?, #depends_on, #description, #each, each_in, #fetch_values, #find_matching_subscriptions, #flap_threshold, #flapping, from_hash, #handle_event, #handle_node_disabled_event, #handle_node_down_event, #handle_node_quieted_event, #handle_node_up_event, #has_children?, #has_dependencies?, #has_quieted_reason?, inherited, #inspect, load, #make_delta_event, #make_update_event, #marshal_dump, #matches?, #merge_and_record_delta, #merge_new_properties, new, #node_subscribers, #operational?, #parent, parent_types, #publish_events, #reachable?, #register_secondary_dependencies, #remove_child, #remove_subscription, #reparent, #restore, #state_has_changed?, #status, #status_description, #status_history_size, subnode_type?, #tags, #type, #unacknowledge, #unreachable?, #update, #update_errors, #update_properties, #update_warnings

Methods included from MethodUtilities

#attr_predicate, #attr_predicate_accessor, #dsl_accessor, #singleton_attr_accessor, #singleton_attr_reader, #singleton_attr_writer, #singleton_method_alias, #singleton_predicate_accessor, #singleton_predicate_reader

Methods included from HashUtilities

compact_hash, hash_matches, merge_recursively, stringify_keys, symbolify_keys

Constructor Details

#initialize(identifier, attributes = {}, &block) ⇒ Host

Create a new Host node.



37
38
39
40
41
# File 'lib/arborist/node/host.rb', line 37

def initialize( identifier, attributes={}, &block )
	@addresses = []
	@hostname = nil
	super
end

Instance Attribute Details

#addressesObject (readonly)

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



50
51
52
# File 'lib/arborist/node/host.rb', line 50

def addresses
  @addresses
end

Instance Method Details

#==(other_host) ⇒ Object

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



148
149
150
151
152
# File 'lib/arborist/node/host.rb', line 148

def ==( other_host )
	return super &&
		other_host.addresses == self.addresses &&
		other_host.hostname == @hostname
end

#address(new_address) ⇒ Object

Set an IP address of the host.



92
93
94
95
96
97
98
99
100
101
# File 'lib/arborist/node/host.rb', line 92

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

#familyObject

Return the node family, so observers can know ancestry after serialization for custom node types that inherit from this class.



59
60
61
# File 'lib/arborist/node/host.rb', line 59

def family
	return :host
end

#hostnameObject

An optional hostname.



54
# File 'lib/arborist/node/host.rb', line 54

dsl_accessor :hostname

#marshal_load(hash) ⇒ Object

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.



139
140
141
142
143
# File 'lib/arborist/node/host.rb', line 139

def marshal_load( hash )
	super
	@addresses = hash[:addresses].map {|addr| IPAddr.new(addr) }
	@hostname = hash[:hostname]
end

#match_criteria?(key, val) ⇒ Boolean

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

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
# File 'lib/arborist/node/host.rb', line 105

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) ⇒ Object

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



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/arborist/node/host.rb', line 66

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_descriptionObject

Return host-node-specific information for #inspect.



118
119
120
121
# File 'lib/arborist/node/host.rb', line 118

def node_description
	return "{no addresses}" if self.addresses.empty?
	return "{addresses: %s}" % [ self.addresses.map(&:to_s).join(', ') ]
end

#operational_valuesObject

Return the host’s operational attributes.



82
83
84
85
86
87
88
# File 'lib/arborist/node/host.rb', line 82

def operational_values
	properties = super
	return properties.merge(
		hostname: @hostname,
		addresses: self.addresses.map(&:to_s)
	)
end

#to_hObject

Return a Hash of the host node’s state.



129
130
131
132
133
134
# File 'lib/arborist/node/host.rb', line 129

def to_h( ** )
	return super.merge(
		hostname:  @hostname,
		addresses: self.addresses.map(&:to_s)
	)
end