Class: MotherBrain::Group

Inherits:
Object
  • Object
show all
Includes:
VariaModel
Defined in:
lib/mb/group.rb

Defined Under Namespace

Classes: CleanRoom

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Group

Returns a new instance of Group.

Parameters:

  • name (#to_s)


17
18
19
20
21
22
23
24
25
26
# File 'lib/mb/group.rb', line 17

def initialize(name, &block)
  set_attribute(:name, name.to_s)
  @recipes         = Set.new
  @roles           = Set.new
  @chef_attributes = Hashie::Mash.new

  if block_given?
    dsl_eval(&block)
  end
end

Instance Attribute Details

#chef_attributesHashie::Mash (readonly)

Returns:

  • (Hashie::Mash)


14
15
16
# File 'lib/mb/group.rb', line 14

def chef_attributes
  @chef_attributes
end

#recipesSet (readonly)

Returns:

  • (Set)


12
13
14
# File 'lib/mb/group.rb', line 12

def recipes
  @recipes
end

#rolesSet (readonly)

Returns:

  • (Set)


10
11
12
# File 'lib/mb/group.rb', line 10

def roles
  @roles
end

Instance Method Details

#add_chef_attribute(key, value) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/mb/group.rb', line 74

def add_chef_attribute(key, value)
  if chef_attribute(key).present?
    raise DuplicateChefAttribute, "An attribute '#{key}' has already been defined on group '#{_attributes_[:name]}'"
  end

  self.chef_attributes[key] = value
end

#add_recipe(name) ⇒ Object



70
71
72
# File 'lib/mb/group.rb', line 70

def add_recipe(name)
  self.recipes.add(name)
end

#add_role(name) ⇒ Object



66
67
68
# File 'lib/mb/group.rb', line 66

def add_role(name)
  self.roles.add(name)
end

#chef_attribute(name) ⇒ Object

Parameters:

  • name (#to_sym)


83
84
85
# File 'lib/mb/group.rb', line 83

def chef_attribute(name)
  self.chef_attributes.fetch(name.to_sym, nil)
end

#idSymbol

Returns:

  • (Symbol)


29
30
31
# File 'lib/mb/group.rb', line 29

def id
  self.name.to_sym
end

#includes_recipe?(recipe) ⇒ TrueClass, FalseClass

Indicates whether the run list contains the recipe

Returns:

  • (TrueClass, FalseClass)


101
102
103
104
# File 'lib/mb/group.rb', line 101

def includes_recipe?(recipe)
  # todo expand roles?
  self.run_list.include?("#{recipe}")
end

#nodes(environment) ⇒ Array<Ridley::Node>

Returns an Array Ridley::Node objects from Chef that match this MotherBrain::Group‘s signature.

A signature is any combination of a recipe(s) or role(s) in a node’s run_list or an attribute(s) on a node.

Parameters:

  • environment (#to_s)

Returns:

  • (Array<Ridley::Node>)


42
43
44
# File 'lib/mb/group.rb', line 42

def nodes(environment)
  Application.ridley.partial_search(:node, search_query(environment), [ "public_ipv4", "public_hostname", "os" ])
end

#run_listArray<String>

Combines the recipes and roles of this group into a run_list that can be passed to Chef or Ridley

[ "role[web_server]", "recipe[nginx::default]" ]

Returns:

  • (Array<String>)


93
94
95
96
# File 'lib/mb/group.rb', line 93

def run_list
  self.roles.collect { |role| "role[#{role}]" } +
    self.recipes.collect { |recipe| "recipe[#{recipe}]" }
end

#search_query(environment) ⇒ String

Returns an escape search query for Solr from the roles, rescipes, and chef_attributes assigned to this Group.

Parameters:

  • environment (#to_s)

Returns:

  • (String)


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mb/group.rb', line 52

def search_query(environment)
  items = ["chef_environment:#{environment}"]

  items += chef_attributes.collect do |key, value|
    key = key.gsub(/\./, "_")
    "#{attribute_escape(key)}:#{value}"
  end

  items += roles.collect { |role| "run_list:#{solr_escape('role['+role+']')}" }
  items += recipes.collect { |recipe| "run_list:#{solr_escape('recipe['+recipe+']')}" }

  items.join(' AND ')
end