class Pocketknife::NodeManager
NodeManager
¶ ↑
This class finds, validates and manages {Pocketknife::Node} instances for a {Pocketknife}.
Attributes
@return [Array<Pocketknife::Node>] Known nodes, cached by {#known_nodes}.
@return [Hash{String => Pocketknife::Node
}] Node
instances by their name.
@return [Pocketknife] The Pocketknife
instance to manage.
Public Class Methods
Instantiates a new manager.
@param [Pocketknife] pocketknife
# File lib/pocketknife/node_manager.rb, line 18 def initialize(pocketknife) self.pocketknife = pocketknife self.nodes = {} self.known_nodes_cache = nil end
Public Instance Methods
Asserts that the specified nodes are known to Pocketknife
.
@param [Array<String>] names A list of node names. @return [void] @raise [Pocketknife::NoSuchNode] Couldn’t find a node.
# 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
Returns a node. Uses cached value in {#known_nodes_cache} if available.
@param [String] name A node name to find, can be an abbrevation. @return [Pocketknife::Node]
# 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
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
.
@param [String] abbreviated_name A node name, which may be abbreviated, e.g. henrietta
. @return [String] The complete node name, e.g. henrietta.swa.gov.it
. @raise [NoSuchNode] Couldn’t find this node, either because it doesn’t exist or the abbreviation isn’t unique.
# 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
Returns the known node names for this project.
Caches results to {#known_nodes_cache}.
@return [Array<String>] The node names. @raise [Errno::ENOENT] Can’t find the nodes
directory.
# 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