Class: MotherBrain::Component
- Inherits:
-
Object
- Object
- MotherBrain::Component
- Includes:
- VariaModel
- Defined in:
- lib/mb/component.rb
Defined Under Namespace
Classes: CleanRoom
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
- #plugin ⇒ MB::Plugin readonly
Instance Method Summary collapse
- #add_command(command) ⇒ Object
-
#add_gear(gear) ⇒ Object
Adds a gear to this component.
- #add_group(group) ⇒ Object
- #command(name) ⇒ Object
-
#command!(name) ⇒ MB::Command
Return a command from the component’s list of commands.
- #description ⇒ String
-
#gears(klass) ⇒ Array<MB::Gear>
Returns the gears of class klass defined on this component.
-
#get_gear(klass, *args) ⇒ MB::Gear
Finds a gear of class klass identified by *args.
- #get_service(name) ⇒ Object
- #group(name) ⇒ Object
- #group!(group_name) ⇒ MB::Group
- #has_group?(name) ⇒ Boolean
- #id ⇒ Symbol
-
#initialize(name, plugin, &block) ⇒ Component
constructor
A new instance of Component.
-
#invoke(job, environment, name, *args) ⇒ Object
Run a command of the given name on the component.
-
#nodes(environment) ⇒ Hash
Finds the nodes for the given environment for each Group and groups them by Group#name into a Hash where the keys are Group#name and values are a Hash representation of a node from Chef.
- #to_s ⇒ Object
Constructor Details
#initialize(name, plugin, &block) ⇒ Component
Returns a new instance of Component.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mb/component.rb', line 22 def initialize(name, plugin, &block) set_attribute(:name, name.to_s) @plugin = plugin @groups = Set.new @commands = Set.new @gears = Hash.new if block_given? dsl_eval(&block) end end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
18 19 20 |
# File 'lib/mb/component.rb', line 18 def commands @commands end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
17 18 19 |
# File 'lib/mb/component.rb', line 17 def groups @groups end |
#plugin ⇒ MB::Plugin (readonly)
16 17 18 |
# File 'lib/mb/component.rb', line 16 def plugin @plugin end |
Instance Method Details
#add_command(command) ⇒ Object
152 153 154 |
# File 'lib/mb/component.rb', line 152 def add_command(command) self.commands.add(command) end |
#add_gear(gear) ⇒ Object
Adds a gear to this component.
168 169 170 171 172 173 174 175 176 |
# File 'lib/mb/component.rb', line 168 def add_gear(gear) klass = gear.class unless get_gear(klass, gear.name).nil? raise DuplicateGear, "#{klass.keyword.capitalize} '#{gear.name}' already defined" end gears(klass).add(gear) end |
#add_group(group) ⇒ Object
147 148 149 |
# File 'lib/mb/component.rb', line 147 def add_group(group) self.groups.add(group) end |
#command(name) ⇒ Object
72 73 74 |
# File 'lib/mb/component.rb', line 72 def command(name) self.commands.find { |command| command.name == name } end |
#command!(name) ⇒ MB::Command
Return a command from the component’s list of commands. If a command is not found an exception will be rasied.
84 85 86 87 88 89 90 91 92 |
# File 'lib/mb/component.rb', line 84 def command!(name) found = command(name) if found.nil? raise CommandNotFound.new(name, self) end found end |
#description ⇒ String
35 36 37 |
# File 'lib/mb/component.rb', line 35 def description _attributes_.description || "#{name} component commands" end |
#gears(klass) ⇒ Array<MB::Gear>
Returns the gears of class klass defined on this component.
161 162 163 |
# File 'lib/mb/component.rb', line 161 def gears(klass) @gears[klass.keyword] ||= Set.new end |
#get_gear(klass, *args) ⇒ MB::Gear
Finds a gear of class klass identified by *args.
192 193 194 195 196 197 198 |
# File 'lib/mb/component.rb', line 192 def get_gear(klass, *args) if klass.respond_to? :find klass.find(gears(klass), *args) else klass.new(*args) end end |
#get_service(name) ⇒ Object
206 207 208 |
# File 'lib/mb/component.rb', line 206 def get_service(name) get_gear(MB::Gear::Service, name) end |
#group(name) ⇒ Object
45 46 47 |
# File 'lib/mb/component.rb', line 45 def group(name) self.groups.find { |group| group.name == name } end |
#group!(group_name) ⇒ MB::Group
54 55 56 57 58 59 60 61 62 |
# File 'lib/mb/component.rb', line 54 def group!(group_name) group = group(group_name) if group.nil? raise GroupNotFound, "Group #{group_name} does not exist on #{name}!" end group end |
#has_group?(name) ⇒ Boolean
67 68 69 |
# File 'lib/mb/component.rb', line 67 def has_group?(name) group(name.to_s).present? end |
#id ⇒ Symbol
40 41 42 |
# File 'lib/mb/component.rb', line 40 def id self.name.to_sym end |
#invoke(job, environment, name, *args) ⇒ Object
Run a command of the given name on the component.
100 101 102 |
# File 'lib/mb/component.rb', line 100 def invoke(job, environment, name, *args) self.command(name).invoke(job, environment, args) end |
#nodes(environment) ⇒ Hash
Finds the nodes for the given environment for each Group and groups them by Group#name into a Hash where the keys are Group#name and values are a Hash representation of a node from Chef.
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mb/component.rb', line 135 def nodes(environment) unless Application.ridley.environment.find(environment) raise EnvironmentNotFound.new(environment) end {}.tap do |nodes| self.groups.each do |group| nodes[group.name] = group.nodes(environment) end end end |
#to_s ⇒ Object
210 211 212 |
# File 'lib/mb/component.rb', line 210 def to_s "#{name}: #{description}" end |