Class: Souffle::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/souffle/node.rb,
lib/souffle/node.rb

Overview

A node object that’s part of a given system.

Defined Under Namespace

Classes: RunList, RunListItem, RunListParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_multiplier = 5) ⇒ Node

Creates a new souffle node with bare dependencies and run_list.

Parameters:

  • parent_multiplier (Fixnum) (defaults to: 5)

    The multiplier for parent nodes.



15
16
17
18
19
20
21
22
23
24
# File 'lib/souffle/node.rb', line 15

def initialize(parent_multiplier=5)
  @dependencies = Souffle::Node::RunList.new
  @run_list = Souffle::Node::RunList.new
  @parents = []
  @children = []
  @options = {
    :attributes => Hash.new
  }
  @parent_multiplier = parent_multiplier
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



9
10
11
# File 'lib/souffle/node.rb', line 9

def children
  @children
end

#dependenciesObject

Returns the value of attribute dependencies.



9
10
11
# File 'lib/souffle/node.rb', line 9

def dependencies
  @dependencies
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/souffle/node.rb', line 9

def name
  @name
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/souffle/node.rb', line 9

def options
  @options
end

#parentsObject

Returns the value of attribute parents.



9
10
11
# File 'lib/souffle/node.rb', line 9

def parents
  @parents
end

#provisionerObject

Returns the value of attribute provisioner.



9
10
11
# File 'lib/souffle/node.rb', line 9

def provisioner
  @provisioner
end

#run_listObject

Returns the value of attribute run_list.



9
10
11
# File 'lib/souffle/node.rb', line 9

def run_list
  @run_list
end

#systemObject

Returns the value of attribute system.



9
10
11
# File 'lib/souffle/node.rb', line 9

def system
  @system
end

Instance Method Details

#add_child(node) ⇒ Object

Adds a child node to the current node.

Parameters:

Raises:

  • (InvaidChild)

    Children must have dependencies and a run_list.



55
56
57
58
59
60
61
62
63
64
# File 'lib/souffle/node.rb', line 55

def add_child(node)
  unless node.respond_to?(:dependencies) && node.respond_to?(:run_list)
    raise Souffle::Exceptions::InvalidChild,
      "Child must act as a Souffle::Node"
  end
  unless @children.include? node
    node.parents << self
    @children.push(node)
  end
end

#depends_on?(node) ⇒ Array

Check whether or not a given node depends on another node.

Examples:


n1 = Souffle::Node.new
n2 = Souffle::Node.new

n1.run_list     << "role[dns_server]"
n2.dependencies << "role[dns_server]"
n2.depends_on?(n1)

> [ true, [role[dns_server]] ]

Parameters:

  • node (Souffle::Node)

    Check to see whether this node depends

Returns:

  • (Array)

    The tuple of [depends_on, dependency_list].



42
43
44
45
46
47
48
# File 'lib/souffle/node.rb', line 42

def depends_on?(node)
  dependency_list = []
  @dependencies.each do |d|
    dependency_list << d if node.run_list.include? d
  end
  [dependency_list.any?, dependency_list]
end

#domainString

The top-level domain name for the given node.

Returns:

  • (String)

    The top-level domain name for the given node.



126
127
128
# File 'lib/souffle/node.rb', line 126

def domain
  try_opt(:domain)
end

#each_child {|Souffle::Node, NilClass| ... } ⇒ Object

Iterator method for children.

Yields:



69
70
71
# File 'lib/souffle/node.rb', line 69

def each_child
  @children.each { |child| yield child }
end

#eql?(other) ⇒ Boolean

Equality comparator for nodes.

Parameters:

Returns:

  • (Boolean)


76
77
78
# File 'lib/souffle/node.rb', line 76

def eql?(other)
  @dependencies == other.dependencies && @run_list == other.run_list
end

#fqdnString

The fully qualified domain name for the given node.

Returns:

  • (String)

    The fully qualified domain name for the given node.



133
134
135
# File 'lib/souffle/node.rb', line 133

def fqdn
  [name, tag, domain].compact.join('.')
end

#log_prefixString

The logging prefix for the given node.

Returns:

  • (String)

    The logging prefix for the given node.



112
113
114
# File 'lib/souffle/node.rb', line 112

def log_prefix
  "[#{tag}: #{name}]"
end

#providerSouffle::Provider::Base

Returns the current system provider.

Returns:



105
106
107
# File 'lib/souffle/node.rb', line 105

def provider
  system.provider
end

#tagString

The tag for the given node.

Returns:

  • (String)

    The tag for the given node.



119
120
121
# File 'lib/souffle/node.rb', line 119

def tag
  try_opt(:tag)
end

#to_hashHash

Returns the description of a node in hash format.

Returns:

  • (Hash)

    The description of a node in hash format.



140
141
142
143
144
145
146
147
148
# File 'lib/souffle/node.rb', line 140

def to_hash
  {
    :name => @name,
    :options => @options,
    :provisioner => @provisioner,
    :dependencies => @dependencies.to_hash,
    :run_list => @run_list.to_hash
  }
end

#try_opt(opt) ⇒ String

Tries to fetch an option parameter otherwise it grabs it from config.

Parameters:

  • opt (Symbol)

    The option to try and fetch.

Returns:

  • (String)

    The option return value.



92
93
94
95
96
97
98
99
100
# File 'lib/souffle/node.rb', line 92

def try_opt(opt)
  if system
    options.fetch(opt, system.try_opt(opt))
  else
    options.fetch(opt, Souffle::Config[opt])
  end
rescue
  nil
end

#weightFixnum

The dependency weight of a given node.

Returns:

  • (Fixnum)

    The relative weight of a node used for balancing.



83
84
85
# File 'lib/souffle/node.rb', line 83

def weight
  @parents.inject(1) { |res, p| res + p.weight * @parent_multiplier }
end