class Trooper::Action
Attributes
Public Class Methods
Public: Define a new action.
name - The name of the action. description - A description of action to be used in the cli output. options - The Hash options used to refine the selection (default: {}):
:local - A boolean of whether this action should be run locally (optional). :on - A symbol(:first_host, :last_host) to determine if to run on the first or last host (optional).
block - A block containing the tasks to run in this action.
Examples
Action.new(:my_action, 'Does great things') { run 'touch file' }
Returns a new action object.
# File lib/trooper/action.rb, line 28 def initialize(name, description, options = {}, &block) @name, @description, @options, @config = name, description, options, {} @call_count, @commands, @block = 0, [], block end
Public Instance Methods
Public: Eval's the block passed on initialize.
configuration - The configuration object to be used for block eval.
Examples
@action.call(config_object) # => ['touch file']
Returns an array of commands(strings).
# File lib/trooper/action.rb, line 42 def call(configuration) @config = configuration @call_count += 1 if everything_ok? && continue_call? reset_commands! build_commands commands else reset_commands! end end
Public: Validates the action object.
Examples
@action.ok? # => true
Returns true or raise an InvalidActionError
exception.
# File lib/trooper/action.rb, line 80 def ok? begin build_commands reset_commands! true rescue Exception => e raise InvalidActionError, "Action missing config variables - #{e.message}" end end
Public: Modifies the commands list to include the prerequisite list checker .
configuration - The configuration object to be used for block eval.
Examples
@action.call(config_object) # => "..."
Returns a command String.
# File lib/trooper/action.rb, line 66 def prerequisite_call(configuration) original_commands = call configuration original_commands << "echo '#{self.name}' >> #{prerequisite_list}" original_commands << "echo '#{self.description}'" "touch #{prerequisite_list}; if grep -vz #{self.name} #{prerequisite_list}; then #{original_commands.join(' && ')}; else echo 'Already Done'; fi" end
Public: Resets Action
commands to blank.
Examples
@action.reset_commands! # => []
Returns a blank array.
# File lib/trooper/action.rb, line 126 def reset_commands! self.commands = [] end
Public: Appends command(string) to commands(array).
command - A String to be added to the commands array.
Examples
@action.run 'touch file' # => 'touch file' @action.run '' # => nil
Returns the command or nil.
# File lib/trooper/action.rb, line 115 def run(command) commands << command if command != '' end
Public: What type of action this is.
Examples
@action.type # => :action @action.type # => :local_action
Returns a Symbol.
# File lib/trooper/action.rb, line 99 def type options && options[:local] ? :local_action : :action end
Private Instance Methods
builds commands array from block passed on init
# File lib/trooper/action.rb, line 137 def build_commands eval_block(&block) end
determines whether to continue calling, to build the commands array
# File lib/trooper/action.rb, line 142 def continue_call? if options && options[:on] case options[:on] when :first_host @call_count == 1 when :last_host config[:hosts] && @call_count == config[:hosts].count else true end else true end end