Class: MotherBrain::Gear::MySQL::Action Private

Inherits:
Object
  • Object
show all
Includes:
MB::Mixin::Services
Defined in:
lib/mb/gears/mysql/action.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql, options) ⇒ Action

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Action.

Parameters:

  • sql (String)

    the sql to run

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :data_bag (Hash)

    specify the data bag, item, and location inside the item to find the MySQL credentials



41
42
43
44
45
# File 'lib/mb/gears/mysql/action.rb', line 41

def initialize(sql, options)
  self.class.validate_options(options)
  @sql     = sql
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash)


18
19
20
# File 'lib/mb/gears/mysql/action.rb', line 18

def options
  @options
end

#sqlString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


16
17
18
# File 'lib/mb/gears/mysql/action.rb', line 16

def sql
  @sql
end

Class Method Details

.validate_options(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • options (Hash)

    the options to validate

Raises:



25
26
27
28
29
30
31
32
33
# File 'lib/mb/gears/mysql/action.rb', line 25

def validate_options(options)
  unless options.key?(:data_bag)
    raise ArgumentError, "You are missing a :data_bag key in your MySQL gear options!"
  end

  unless options[:data_bag].key?(:name)
    raise ArgumentError, "You are missing a :name key in your MySQL gear data bag options!"
  end
end

Instance Method Details

#connection_info(environment, node) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The MySQL connection information/credentials for the specified node.

Parameters:

  • environment (String)

    name of the environment to retrieve credentials from

  • node (Ridley::Node)

    the node to to find connection information for

Returns:

  • (Hash)

    MySQL connection information for the node

Raises:

  • (MB::GearError)

    if the MySQL credentials cannot be found or are malformed



77
78
79
80
81
# File 'lib/mb/gears/mysql/action.rb', line 77

def connection_info(environment, node)
  credentials(environment).merge(host: node.public_hostname)
rescue MB::DataBagNotFound, MB::DataBagItemNotFound => ex
  raise MB::GearError.new(ex)
end

#data_bag_keysHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The keys used to look up MySQL connection information in a data bag item.

Returns:

  • (Hash)

    The keys used to look up MySQL connection information in a data bag item.



84
85
86
87
88
89
90
91
92
# File 'lib/mb/gears/mysql/action.rb', line 84

def data_bag_keys
  hash = data_bag_spec[:location][:hash]

  if hash
    Hash[data_bag_spec[:location][:keys].map { |k, v| [k, "#{hash}.#{v}"] }]
  else
    data_bag_spec[:location][:keys]
  end
end

#run(job, environment, nodes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run this action on the specified nodes

Parameters:

  • job (MB::Job)

    a job to update with status

  • environment (String)

    the environment this command is being run on

  • nodes (Array<Ridley::Node>)

    the nodes to run this action on



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mb/gears/mysql/action.rb', line 55

def run(job, environment, nodes)
  threads = []

  nodes.each do |node|
    threads << Thread.new(node) do |node|
      query(environment, sql, node)
    end
  end

  threads.each(&:join)
end