Class: MotherBrain::Component

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

Defined Under Namespace

Classes: CleanRoom

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, plugin, &block) ⇒ Component

Returns a new instance of Component.

Parameters:



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

#commandsObject (readonly)

Returns the value of attribute commands.



18
19
20
# File 'lib/mb/component.rb', line 18

def commands
  @commands
end

#groupsObject (readonly)

Returns the value of attribute groups.



17
18
19
# File 'lib/mb/component.rb', line 17

def groups
  @groups
end

#pluginMB::Plugin (readonly)

Returns:



16
17
18
# File 'lib/mb/component.rb', line 16

def plugin
  @plugin
end

Instance Method Details

#add_command(command) ⇒ Object

Parameters:



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.

Parameters:



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

Parameters:

  • name (#to_sym)


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.

Parameters:

  • name (#to_s)

    name of the command to find and return

Returns:

Raises:

  • (CommandNotFound)

    if a command matching the given name is not found on this component



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

#descriptionString

Returns:

  • (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.

Parameters:

  • klass (MB::Gear)

    the class of the gears to find

Returns:



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.

Examples:

searching for a service gear


get_gear(MB::Gear::Service, "service_name")

searching for a jmx gear


get_gear(MB::Gear::Jmx)

Parameters:

  • klass (MB::Gear)

    the class of the gear to search for

  • args (Array)

    the identifiers for the gear to find

Returns:



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

Parameters:

  • name (#to_sym)


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

Parameters:

  • group_name (#to_sym)

Returns:

Raises:



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

Parameters:

Returns:

  • (Boolean)


67
68
69
# File 'lib/mb/component.rb', line 67

def has_group?(name)
  group(name.to_s).present?
end

#idSymbol

Returns:

  • (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.

Parameters:

  • job (Job)
  • environment (String)
  • name (String, Symbol)
  • args (Array)


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.

Examples:


{
  "database_masters" => [
    {
      "name" => "db-master1",
      ...
    }
  ],
  "database_slaves" => [
    {
      "name" => "db-slave1",
      ...
    },
    {
      "name" => "db-slave2"
      ...
    }
  ]
}

Parameters:

  • environment (#to_s)

Returns:

  • (Hash)

Raises:



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_sObject



210
211
212
# File 'lib/mb/component.rb', line 210

def to_s
  "#{name}: #{description}"
end