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

Instantiates 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_cacheArray<Pocketknife::Node>

Returns Known nodes, cached by #known_nodes.

Returns:



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

def known_nodes_cache
  @known_nodes_cache
end

#nodesHash{String => Pocketknife::Node}

Returns Node instances by their name.

Returns:



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

def nodes
  @nodes
end

#pocketknifePocketknife

Returns The Pocketknife instance to manage.

Returns:

  • (Pocketknife)

    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.

Parameters:

  • names (Array<String>)

    A list of node names.

Raises:



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.

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)

    Couldn’t find this node, either because it doesn’t exist or the abbreviation isn’t unique.



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)

    Can’t find the nodes directory.



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