Class: Trooper::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/trooper/strategy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, config = {}, &block) ⇒ Strategy

Public: Initialize a new Strategy object.

name - A Symbol of the strategy name description - A String of what this strategy will do. config - A Hash of config options expects a Trooper::Configuration object block - A block to be eval with the strategy object

Examples

Strategy.new :my_strategy, 'Does something cool' do
  actions :my_action
end

Returns a Host object.



22
23
24
25
# File 'lib/trooper/strategy.rb', line 22

def initialize(name, description, config = {}, &block)
  @name, @description, @config = name, description, config
  @run_list, @prereq_run_list, @block = [], [], block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

:nodoc:



142
143
144
# File 'lib/trooper/strategy.rb', line 142

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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



6
7
8
# File 'lib/trooper/strategy.rb', line 6

def block
  @block
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/trooper/strategy.rb', line 6

def config
  @config
end

#descriptionObject (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/trooper/strategy.rb', line 6

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/trooper/strategy.rb', line 6

def name
  @name
end

#prereq_run_listObject (readonly)

Returns the value of attribute prereq_run_list.



6
7
8
# File 'lib/trooper/strategy.rb', line 6

def prereq_run_list
  @prereq_run_list
end

#run_listObject (readonly)

Returns the value of attribute run_list.



6
7
8
# File 'lib/trooper/strategy.rb', line 6

def run_list
  @run_list
end

Instance Method Details

#action(name, description = "No Description", options = {}, &block) ⇒ Object

Public: Add an Action to the Task list but scoped to this Strategy (Action Not available outside this object).

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

@strategy.action(:my_action, 'Does great things') { run 'touch file' }

Returns an Action object.



118
119
120
121
122
123
124
125
126
# File 'lib/trooper/strategy.rb', line 118

def action(name, description = "No Description", options = {}, &block)
  action_name = "#{self.name}_#{name}".to_sym

  action = Trooper::Action.new action_name, description, options, &block
  Arsenal.actions.add action
  actions action_name
  
  action
end

#actions(*action_names) ⇒ Object

Public: Add actions to the run list.

action_names - A list of Action names.

Examples

@strategy.actions(:my_action) # => nil

Returns nil.



94
95
96
97
98
99
100
101
# File 'lib/trooper/strategy.rb', line 94

def actions(*action_names)
  [*action_names].each do |name| 
    if Arsenal.actions[name]
      # strategy_name, type, name
      @run_list << [self.name, Arsenal.actions[name].type, name]
    end
  end
end

#call(*strategy_names) ⇒ Object

Public: Add other strategies actions to the run list.

strategy_names - A list of other Strategy names.

Examples

@strategy.call(:my_other_strategy) # => nil

Returns nil.



47
48
49
50
51
52
53
54
55
56
# File 'lib/trooper/strategy.rb', line 47

def call(*strategy_names)
  [*strategy_names].each do |strategy_name|
    if Arsenal.strategies[strategy_name]
      Arsenal.strategies[strategy_name].list(config).each do |action|
        # strategy_name, type, name
        @run_list << action
      end
    end
  end
end

#list(configuration = {}) ⇒ Object

Public: The Task List.

configuration - A optional Trooper::Configuration object.

Examples

@strategy.list() # => [[:my_strategy, :action, :my_strategy_my_new_action]]

Returns and Array of Arrays.



137
138
139
140
# File 'lib/trooper/strategy.rb', line 137

def list(configuration = {})
  build_list(configuration) if run_list == []
  prereq_run_list + run_list
end

#ok?Boolean

Public: Validates the strategy object. (NOT WORKING)

Examples

@strategy.ok? # => true

Returns true.

Returns:

  • (Boolean)


34
35
36
# File 'lib/trooper/strategy.rb', line 34

def ok?
  true
end

#prerequisites(*strategy_names) ⇒ Object

Public: Add other strategies actions to the prerequisite run list.

strategy_names - A list of other Strategy names.

Examples

@strategy.prerequisites(:my_other_strategy) # => nil

Returns nil.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/trooper/strategy.rb', line 67

def prerequisites(*strategy_names)
  if @prereq_run_list == []
    @prereq_run_list = [[@name, :action, :prepare_prerequisite]]
  end

  [*strategy_names].each do |strategy_name|
    if Arsenal.strategies[strategy_name]
      Arsenal.strategies[strategy_name].list(config).each do |action|
        # strategy_name, type, name
        if action[1] == :local_action
          raise InvalidActionError, 'Cant use prerequisite strategies that have local actions'
        end
        @prereq_run_list << [action[0], :prerequisite, action[2]]
      end
    end  
  end
end