Class: Pocketknife::NodeManager

Inherits:
Object
  • Object
show all
Defined in:
lib/pocketknife/node_manager.rb

Overview

NodeManager

This class finds, validates and manages Node instances for a Pocketknife.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pocketknife) ⇒ NodeManager

Instantiate a new manager.

Parameters:



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_cacheObject

Array of known nodes, used as cache by #known_nodes.



13
14
15
# File 'lib/pocketknife/node_manager.rb', line 13

def known_nodes_cache
  @known_nodes_cache
end

#nodesObject

Hash of Node instances by their name.



10
11
12
# File 'lib/pocketknife/node_manager.rb', line 10

def nodes
  @nodes
end

#pocketknifeObject

Instance of a Pocketknife.



7
8
9
# File 'lib/pocketknife/node_manager.rb', line 7

def pocketknife
  @pocketknife
end

Instance Method Details

#assert_known(names) ⇒ Object

Asserts that the specified nodes are known to Pocketknife.

Parameters:

  • nodes (Array<String>)

    A list of node names.

Raises:



64
65
66
67
68
69
# File 'lib/pocketknife/node_manager.rb', line 64

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

Return a node. Uses cached value in #known_nodes_cache if available.

Parameters:

  • name (String)

    A node name to find, can be an abbrevation.

Returns:



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.

Parameters:

  • abbreviated_name (String)

    A node name, which may be abbreviated, e.g. “henrietta”.

Returns:

  • (String)

    The complete node name, e.g. “henrietta.swa.gov.it”

Raises:

  • (NoSuchNode)

    A hostname could not be found for this node, either because the node doesn’t exist or the abbreviated form isn’t unique enough.



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_nodesArray<String>

Returns the known node names for this project.

Caches results to #known_nodes_cache.

Returns:

  • (Array<String>)

    The node names.

Raises:

  • (Errno::ENOENT)

    Raised if can’t find the nodes directory.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pocketknife/node_manager.rb', line 77

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