Class: Pocketknife::NodeManager
- Inherits:
-
Object
- Object
- Pocketknife::NodeManager
- Defined in:
- lib/pocketknife/node_manager.rb
Overview
NodeManager
This class finds, validates and manages Node instances for a Pocketknife.
Instance Attribute Summary collapse
-
#known_nodes_cache ⇒ Array<Pocketknife::Node>
Known nodes, cached by #known_nodes.
-
#nodes ⇒ Hash{String => Pocketknife::Node}
Node instances by their name.
-
#pocketknife ⇒ Pocketknife
The Pocketknife instance to manage.
Instance Method Summary collapse
-
#assert_known(names) ⇒ void
Asserts that the specified nodes are known to Pocketknife.
-
#find(name) ⇒ Pocketknife::Node
Returns a node.
-
#hostname_for(abbreviated_name) ⇒ String
Returns a node’s hostname based on its abbreviated node name.
-
#initialize(pocketknife) ⇒ NodeManager
constructor
Instantiates a new manager.
-
#known_nodes ⇒ Array<String>
Returns the known node names for this project.
Constructor Details
#initialize(pocketknife) ⇒ NodeManager
Instantiates a new manager.
18 19 20 21 22 |
# File 'lib/pocketknife/node_manager.rb', line 18 def initialize(pocketknife) self.pocketknife = pocketknife self.nodes = {} self.known_nodes_cache = nil end |
Instance Attribute Details
#known_nodes_cache ⇒ Array<Pocketknife::Node>
Returns Known nodes, cached by #known_nodes.
13 14 15 |
# File 'lib/pocketknife/node_manager.rb', line 13 def known_nodes_cache @known_nodes_cache end |
#nodes ⇒ Hash{String => Pocketknife::Node}
Returns Node instances by their name.
10 11 12 |
# File 'lib/pocketknife/node_manager.rb', line 10 def nodes @nodes end |
#pocketknife ⇒ Pocketknife
Returns The Pocketknife instance to manage.
7 8 9 |
# File 'lib/pocketknife/node_manager.rb', line 7 def pocketknife @pocketknife end |
Instance Method Details
#assert_known(names) ⇒ void
This method returns an undefined value.
Asserts that the specified nodes are known to Pocketknife.
65 66 67 68 69 70 |
# File 'lib/pocketknife/node_manager.rb', line 65 def assert_known(names) for name in names # This will raise a NoSuchNode exception if there's a problem. self.hostname_for(name) end end |
#find(name) ⇒ Pocketknife::Node
Returns a node. Uses cached value in #known_nodes_cache if available.
28 29 30 31 32 33 |
# File 'lib/pocketknife/node_manager.rb', line 28 def find(name) hostname = self.hostname_for(name) return self.nodes[hostname] ||= begin node = Node.new(hostname, self.pocketknife) end end |
#hostname_for(abbreviated_name) ⇒ String
Returns a node’s hostname based on its abbreviated node name.
The hostname is derived from the filename that defines it. For example, the nodes/henrietta.swa.gov.it.json
file defines a node with the hostname henrietta.swa.gov.it
. This node can can be also be referred to as henrietta.swa.gov
, henrietta.swa
, or henrietta
.
The abbreviated node name given must match only one node exactly. For example, you’ll get a Pocketknife::NoSuchNode if you ask for an abbreviated node by the name of giovanni
when there are nodes called giovanni.boldini.it
and giovanni.bellini.it
– you’d need to ask using a more specific name, such as giovanni.boldini
.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pocketknife/node_manager.rb', line 44 def hostname_for(abbreviated_name) if self.known_nodes.include?(abbreviated_name) return abbreviated_name else matches = self.known_nodes.grep(/^#{abbreviated_name}\./) case matches.length when 1 return matches.first when 0 raise NoSuchNode.new("Can't find node named '#{abbreviated_name}'", abbreviated_name) else raise NoSuchNode.new("Can't find unique node named '#{abbreviated_name}', this matches nodes: #{matches.join(', ')}", abbreviated_name) end end end |
#known_nodes ⇒ Array<String>
Returns the known node names for this project.
Caches results to #known_nodes_cache.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/pocketknife/node_manager.rb', line 78 def known_nodes return(self.known_nodes_cache ||= begin dir = Pathname.new("nodes") json_extension = /\.json$/ if dir.directory? dir.entries.select do |path| path.to_s =~ json_extension end.map do |path| path.to_s.sub(json_extension, "") end else raise Errno::ENOENT, "Can't find 'nodes' directory." end end) end |