Class: MotherBrain::Group
- Inherits:
-
Object
- Object
- MotherBrain::Group
- Includes:
- VariaModel
- Defined in:
- lib/mb/group.rb
Defined Under Namespace
Classes: CleanRoom
Instance Attribute Summary collapse
- #chef_attributes ⇒ Hashie::Mash readonly
- #recipes ⇒ Set readonly
- #roles ⇒ Set readonly
Instance Method Summary collapse
- #add_chef_attribute(key, value) ⇒ Object
- #add_recipe(name) ⇒ Object
- #add_role(name) ⇒ Object
- #chef_attribute(name) ⇒ Object
- #id ⇒ Symbol
-
#includes_recipe?(recipe) ⇒ TrueClass, FalseClass
Indicates whether the run list contains the recipe.
-
#initialize(name, &block) ⇒ Group
constructor
A new instance of Group.
-
#nodes(environment) ⇒ Array<Ridley::Node>
Returns an Array Ridley::Node objects from Chef that match this Group‘s signature.
-
#run_list ⇒ Array<String>
Combines the recipes and roles of this group into a run_list that can be passed to Chef or Ridley.
-
#search_query(environment) ⇒ String
Returns an escape search query for Solr from the roles, rescipes, and chef_attributes assigned to this Group.
Constructor Details
#initialize(name, &block) ⇒ Group
Returns a new instance of Group.
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_attributes ⇒ Hashie::Mash (readonly)
14 15 16 |
# File 'lib/mb/group.rb', line 14 def chef_attributes @chef_attributes end |
#recipes ⇒ Set (readonly)
12 13 14 |
# File 'lib/mb/group.rb', line 12 def recipes @recipes end |
#roles ⇒ Set (readonly)
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
83 84 85 |
# File 'lib/mb/group.rb', line 83 def chef_attribute(name) self.chef_attributes.fetch(name.to_sym, nil) end |
#id ⇒ 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
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.
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" ]) end |
#run_list ⇒ Array<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]" ]
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.
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| "roles:#{solr_escape(role)}" } items += recipes.collect { |recipe| "recipes:#{solr_escape(recipe)}" } items.join(' AND ') end |