class MotherBrain::Component

Attributes

commands[R]
groups[R]
plugin[R]

@return [MB::Plugin]

Public Class Methods

new(name, plugin, &block) click to toggle source

@param [#to_s] name @param [MB::Plugin] plugin

# 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

Public Instance Methods

add_command(command) click to toggle source

@param [MB::Command] command

# File lib/mb/component.rb, line 152
def add_command(command)
  self.commands.add(command)
end
add_gear(gear) click to toggle source

Adds a gear to this component.

@param [MB::Gear] gear the gear

# 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) click to toggle source
# File lib/mb/component.rb, line 147
def add_group(group)
  self.groups.add(group)
end
command(name) click to toggle source

@param [#to_sym] name

# File lib/mb/component.rb, line 72
def command(name)
  self.commands.find { |command| command.name == name }
end
command!(name) click to toggle source

Return a command from the component’s list of commands. If a command is not found an exception will be rasied.

@param [#to_s] name

name of the command to find and return

@raise [CommandNotFound] if a command matching the given name is not found on this component

@return [MB::Command]

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

@return [String]

# File lib/mb/component.rb, line 35
def description
  _attributes_.description || "#{name} component commands"
end
gears(klass) click to toggle source

Returns the gears of class klass defined on this component.

@param [MB::Gear] klass the class of the gears to find

@return [Array<MB::Gear>]

# File lib/mb/component.rb, line 161
def gears(klass)
  @gears[klass.keyword] ||= Set.new
end
get_gear(klass, *args) click to toggle source

Finds a gear of class klass identified by *args.

@param [MB::Gear] klass the class of the gear to search for @param [Array] args the identifiers for the gear to find

@example searching for a service gear

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

@example searching for a jmx gear

get_gear(MB::Gear::Jmx)

@return [MB::Gear]

# 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) click to toggle source
# File lib/mb/component.rb, line 206
def get_service(name)
  get_gear(MB::Gear::Service, name)
end
group(name) click to toggle source

@param [#to_sym] name

# File lib/mb/component.rb, line 45
def group(name)
  self.groups.find { |group| group.name == name }
end
group!(group_name) click to toggle source

@param [#to_sym] group_name

@raise [GroupNotFound] if the group is not found

@return [MB::Group]

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

@param [#to_s] name

@return [Boolean]

# File lib/mb/component.rb, line 67
def has_group?(name)
  group(name.to_s).present?
end
id() click to toggle source

@return [Symbol]

# File lib/mb/component.rb, line 40
def id
  self.name.to_sym
end
invoke(job, environment, name, *args) click to toggle source

Run a command of the given name on the component.

@param [Job] job @param [String] environment @param [String, Symbol] name @param [Array] args

# File lib/mb/component.rb, line 100
def invoke(job, environment, name, *args)
  self.command(name).invoke(job, environment, args)
end
nodes(environment) click to toggle source

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.

@param [#to_s] environment

@raise [MB::EnvironmentNotFound] if the target environment does not exist @raise [MB::ChefConnectionError] if there was an error communicating to the Chef Server

@example

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

@return [Hash]

# 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() click to toggle source
# File lib/mb/component.rb, line 210
def to_s
  "#{name}: #{description}"
end

Private Instance Methods

dsl_eval(&block) click to toggle source
# File lib/mb/component.rb, line 216
def dsl_eval(&block)
  CleanRoom.new(self).instance_eval(&block)
end