Class: Trooper::Action

Inherits:
Object
  • Object
show all
Includes:
DSL::Bundler, DSL::Folders, DSL::Rake
Defined in:
lib/trooper/action.rb

Direct Known Subclasses

Trooper::Actions::DefaultAction

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL::Bundler

#bundle_exec, #bundle_install, #rake

Methods included from DSL::Rake

#rake

Methods included from DSL::Folders

#cd, #create_folder, #create_folders, #delete_folder

Constructor Details

#initialize(name, description, options = {}, &block) ⇒ Action

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.



28
29
30
31
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object

:nodoc:



130
131
132
# File 'lib/trooper/action.rb', line 130

def method_missing(method_sym, *arguments, &block) # :nodoc:
  config[method_sym] || super
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



11
12
13
# File 'lib/trooper/action.rb', line 11

def block
  @block
end

#commandsObject

Returns the value of attribute commands.



12
13
14
# File 'lib/trooper/action.rb', line 12

def commands
  @commands
end

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/trooper/action.rb', line 12

def config
  @config
end

#descriptionObject (readonly)

Returns the value of attribute description.



11
12
13
# File 'lib/trooper/action.rb', line 11

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/trooper/action.rb', line 11

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/trooper/action.rb', line 11

def options
  @options
end

Instance Method Details

#call(configuration) ⇒ Object Also known as: execute

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).



42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

#ok?Boolean Also known as: everything_ok?

Public: Validates the action object.

Examples

@action.ok? # => true

Returns true or raise an InvalidActionError exception.

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
# 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

#prerequisite_call(configuration) ⇒ Object

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.



66
67
68
69
70
71
# 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

#reset_commands!Object

Public: Resets Action commands to blank.

Examples

@action.reset_commands! # => []

Returns a blank array.



126
127
128
# File 'lib/trooper/action.rb', line 126

def reset_commands!
  self.commands = []
end

#run(command) ⇒ Object

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.



115
116
117
# File 'lib/trooper/action.rb', line 115

def run(command)
  commands << command if command != ''
end

#typeObject

Public: What type of action this is.

Examples

@action.type # => :action
@action.type # => :local_action

Returns a Symbol.



99
100
101
# File 'lib/trooper/action.rb', line 99

def type
  options && options[:local] ? :local_action : :action
end