Module: Blender::Manifest::Nodes
- Included in:
- Root
- Defined in:
- lib/blender/manifest/nodes.rb
Overview
This module encapsulates nodes handeling Nodes can be declared both on a class level and inside a recipe When defining a node an ‘id’ is associated with its hostname and the node can later be reffered by this id (for example to get its ip)
Constant Summary collapse
- @@internal_hostnames =
this holds the map from host ids to hostnames
{}
- @@external_hostnames =
{}
Class Method Summary collapse
Instance Method Summary collapse
-
#addr(id) ⇒ Object
find out node addr.
-
#addr!(id) ⇒ Object
same as addr but throws exception if IP can’t be found.
-
#current_node ⇒ Object
returns id of the node we are running at Note: will ONLY return non-nil value after (or during) node definition unless node is forced by environment NODE variable or /etc/node file.
-
#current_node?(id) ⇒ Boolean
True if we are running on the node with the given ‘id`.
- #host_ip(name) ⇒ Object
-
#hostname(id = nil, external = false) ⇒ Object
returns hostname by id or local host’s name if the id is nil.
-
#node(id, internal_name = nil, external_name = internal_name) ⇒ Object
define node and conditionally execute code block only on the specific node Note: can be called multiple times without internal_name and external_name parameters in which case will only do the conditional execution.
-
#resolv ⇒ Object
resolves host name using ‘host’ executable Note: cached value from @host_ips will be used if exists.
-
#set_host_ip(name, ip) ⇒ Object
sets cache value for host ip allows resolving hosts that can’t be resolved through the sytem.
Class Method Details
.included(base) ⇒ Object
8 9 10 |
# File 'lib/blender/manifest/nodes.rb', line 8 def self.included(base) base.send :extend, self end |
Instance Method Details
#addr(id) ⇒ Object
find out node addr. try to use external, then internal, then the host id to determine ip
92 93 94 95 96 97 98 99 100 |
# File 'lib/blender/manifest/nodes.rb', line 92 def addr(id) [hostname(id, true).to_s, hostname(id).to_s, id.to_s].each do |name| ip = host_ip(name) return ip if ip end # if all else fails, we should still be able to address the current node current_node?(id) ? "127.0.0.1" : nil end |
#addr!(id) ⇒ Object
same as addr but throws exception if IP can’t be found
103 104 105 |
# File 'lib/blender/manifest/nodes.rb', line 103 def addr!(id) addr(id) or raise "Can't find address for '#{id}'" end |
#current_node ⇒ Object
returns id of the node we are running at Note: will ONLY return non-nil value after (or during) node definition
unless node is forced by environment NODE variable or /etc/node file
28 29 30 31 32 33 34 |
# File 'lib/blender/manifest/nodes.rb', line 28 def current_node node = ENV['NODE'] || (File.exists?("/etc/node") && File.read("/etc/node").strip) || @@internal_hostnames.index(hostname) || @@external_hostnames.index(hostname(nil, true)) node && node.to_sym end |
#current_node?(id) ⇒ Boolean
Returns true if we are running on the node with the given ‘id`.
37 38 39 |
# File 'lib/blender/manifest/nodes.rb', line 37 def current_node?(id) current_node == id.to_sym end |
#host_ip(name) ⇒ Object
45 46 47 48 49 |
# File 'lib/blender/manifest/nodes.rb', line 45 def host_ip(name) return nil unless name && !name.empty? @host_ips ||= {} @host_ips[name] ||= Resolv.getaddress(name) rescue nil end |
#hostname(id = nil, external = false) ⇒ Object
returns hostname by id or local host’s name if the id is nil
17 18 19 20 21 22 23 |
# File 'lib/blender/manifest/nodes.rb', line 17 def hostname(id = nil, external = false) if id (external ? @@external_hostnames : @@internal_hostnames)[id] else external ? Facter.fqdn : Facter.hostname end end |
#node(id, internal_name = nil, external_name = internal_name) ⇒ Object
define node and conditionally execute code block only on the specific node Note: can be called multiple times without internal_name and external_name parameters
in which case will only do the conditional execution
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/blender/manifest/nodes.rb', line 76 def node(id, internal_name = nil, external_name = internal_name) return if false == internal_name # can be used to temporary 'disable' host's definition # like: # host :app2, false do .. end @@internal_hostnames[id] = (internal_name || id).to_s @@external_hostnames[id] = external_name.to_s if external_name if block_given? && current_node?(id) puts "NODE: #{id} / #{current_node}" @node = id yield end end |
#resolv ⇒ Object
resolves host name using ‘host’ executable Note: cached value from @host_ips will be used if exists
44 |
# File 'lib/blender/manifest/nodes.rb', line 44 require 'resolv' |
#set_host_ip(name, ip) ⇒ Object
sets cache value for host ip allows resolving hosts that can’t be resolved through the sytem
56 57 58 59 |
# File 'lib/blender/manifest/nodes.rb', line 56 def set_host_ip(name, ip) @host_ips ||= {} @host_ips[name] = ip end |