Class: MotherBrain::CommandInvoker

Inherits:
Object
  • Object
show all
Includes:
Celluloid, MB::Mixin::Locks, MB::Mixin::Services, Logging
Defined in:
lib/mb/command_invoker.rb,
lib/mb/command_invoker/worker.rb

Defined Under Namespace

Classes: Worker

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

add_argument_header, dev, filename, #log_exception, logger, #logger, reset, set_logger, setup

Constructor Details

#initializeCommandInvoker

Returns a new instance of CommandInvoker.



21
22
23
# File 'lib/mb/command_invoker.rb', line 21

def initialize
  log.debug { "Command Invoker starting..." }
end

Class Method Details

.instanceCelluloid::Actor(CommandInvoker)

Returns:

Raises:

  • (Celluloid::DeadActorError)

    if command invoker has not been started



9
10
11
# File 'lib/mb/command_invoker.rb', line 9

def instance
  MB::Application[:command_invoker] or raise Celluloid::DeadActorError, "command invoker not running"
end

Instance Method Details

#async_invoke(command_name, options = {}) ⇒ MB::Job

Asynchronously invoke a command on a plugin or a component of a plugin.

Parameters:

  • command_name (String)

    Name of the command to invoke

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :plugin (String)
  • :component (String) — default: optional
  • :version (String) — default: optional
  • :environment (String)
  • :arguments (Array) — default: Array.new
  • :force (Boolean) — default: false

Returns:



38
39
40
41
42
# File 'lib/mb/command_invoker.rb', line 38

def async_invoke(command_name, options = {})
  job = Job.new(:command)
  async(:invoke, job, command_name, options)
  job.ticket
end

#find(command_name, plugin_name, options = {}) ⇒ MB::Command

Find a plugin or component level which has already been loaded by a plugin.

Parameters:

  • command_name (String)

    Name of the command to find

  • plugin_name (String)

    The name of the plugin that the command you are looking for belongs to

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :component (String) — default: optional

    Name of the component that this command belongs to. If no component name is specified then it is assumed that you are searching for a plugin level command and not a component level command.

  • :environment (String) — default: optional

    The environment the command will be executed on. The best version of the command to run on that environment will be returned. If no environment is specified the latest version of the command will be returned.

  • :version (String) — default: optional

    The specific version of the plugin you are looking for the command on

Returns:

Raises:



68
69
70
71
72
73
74
75
76
# File 'lib/mb/command_invoker.rb', line 68

def find(command_name, plugin_name, options = {})
  if options[:version]
    for_version(options[:version], command_name, plugin_name, options[:component])
  elsif options[:environment]
    for_environment(options[:environment], command_name, plugin_name, options[:component])
  else
    find_latest(command_name, plugin_name, options[:component])
  end
end

#invoke(job, command_name, options = {}) ⇒ Boolean

Invoke a command on a plugin or a component of a plugin

Parameters:

  • job (MB::Job)

    A job to update with progress

  • command_name (String)

    Name of the command to invoke

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :plugin (String)
  • :component (String) — default: optional
  • :environment (String)
  • :version (String) — default: optional
  • :arguments (Array) — default: Array.new
  • :force (Boolean) — default: false
  • :node_filter (Array) — default: nil

Returns:

  • (Boolean)

Raises:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mb/command_invoker.rb', line 96

def invoke(job, command_name, options = {})
  options = options.reverse_merge(arguments: Array.new, force: false)

  job.report_running

  if options[:plugin].nil?
    job.report_failure MB::ArgumentError.new("must specify a plugin that the command belongs to")
    return false
  end

  if options[:environment].nil?
    job.report_failure MB::ArgumentError.new("must specify an environment to run this command on")
    return false
  end

  job.set_status("Finding environment #{options[:environment]}")
  environment_manager.find(options[:environment])

  command = find(command_name, options[:plugin], options.slice(:component, :environment, :version))
  worker  = Worker.new(command, options[:environment], options[:node_filter])

  chef_synchronize(chef_environment: options[:environment], force: options[:force], job: job) do
    worker.run(job, options[:arguments])
  end

  job.report_success("successfully executed command")
  true
rescue => ex
  job.report_failure(ex)
  false
ensure
  job.terminate if job && job.alive?
  worker.terminate if worker && worker.alive?
end