Class: SwitchTower::Actor::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/switchtower/actor.rb

Overview

Represents the definition of a single task.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, actor, options) ⇒ Task

Returns a new instance of Task.



74
75
76
77
# File 'lib/switchtower/actor.rb', line 74

def initialize(name, actor, options)
  @name, @actor, @options = name, actor, options
  @servers = nil
end

Instance Attribute Details

#actorObject (readonly)

:nodoc:



72
73
74
# File 'lib/switchtower/actor.rb', line 72

def actor
  @actor
end

#nameObject (readonly)

:nodoc:



72
73
74
# File 'lib/switchtower/actor.rb', line 72

def name
  @name
end

#optionsObject (readonly)

:nodoc:



72
73
74
# File 'lib/switchtower/actor.rb', line 72

def options
  @options
end

Instance Method Details

#serversObject

Returns the list of servers (not connections to servers) that are the target of this task.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/switchtower/actor.rb', line 81

def servers
  unless @servers
    roles = [*(@options[:roles] || actor.configuration.roles.keys)].
      map { |name|
        actor.configuration.roles[name] or
          raise ArgumentError, "task #{self.name.inspect} references non-existant role #{name.inspect}"
      }.flatten
    only  = @options[:only] || {}

    unless only.empty?
      roles = roles.delete_if do |role|
        catch(:done) do
          only.keys.each do |key|
            throw(:done, true) if role.options[key] != only[key]
          end
          false
        end
      end
    end

    @servers = roles.map { |role| role.host }.uniq
  end

  @servers
end