class MotherBrain::Group

Attributes

chef_attributes[R]

@return [Hashie::Mash]

recipes[R]

@return [Set]

roles[R]

@return [Set]

Public Class Methods

new(name, &block) click to toggle source

@param [#to_s] name

# 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

Public Instance Methods

add_chef_attribute(key, value) click to toggle source
# 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) click to toggle source
# File lib/mb/group.rb, line 70
def add_recipe(name)
  self.recipes.add(name)
end
add_role(name) click to toggle source
# File lib/mb/group.rb, line 66
def add_role(name)
  self.roles.add(name)
end
chef_attribute(name) click to toggle source

@param [#to_sym] name

# File lib/mb/group.rb, line 83
def chef_attribute(name)
  self.chef_attributes.fetch(name.to_sym, nil)
end
id() click to toggle source

@return [Symbol]

# File lib/mb/group.rb, line 29
def id
  self.name.to_sym
end
includes_recipe?(recipe) click to toggle source

Indicates whether the run list contains the recipe

@return [TrueClass, FalseClass]

# File lib/mb/group.rb, line 101
def includes_recipe?(recipe)
  # todo expand roles?
  self.run_list.include?("#{recipe}")
end
nodes(environment) click to toggle source

Returns an Array Ridley::Node objects from Chef that match this {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.

@param [#to_s] environment

@return [Array<Ridley::Node>]

# 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_list() click to toggle source

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]" ]

@return [Array<String>]

# 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) click to toggle source

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

@param [#to_s] environment

@return [String]

# 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

Private Instance Methods

attribute_escape(value) click to toggle source
# File lib/mb/group.rb, line 112
def attribute_escape(value)
  value.gsub(/\./, "_")
end
dsl_eval(&block) click to toggle source
# File lib/mb/group.rb, line 108
def dsl_eval(&block)
  CleanRoom.new(self).instance_eval(&block)
end
solr_escape(value) click to toggle source
# File lib/mb/group.rb, line 116
def solr_escape(value)
  value.gsub(/[\:\[\]\+\!\^\(\)\{\}]/) { |x| "\\#{x}" }
end