Class: AvoDeploy::Task::TaskManager

Inherits:
Object
  • Object
show all
Defined in:
lib/avodeploy/task/task_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTaskManager

Initializes the task manager



27
28
29
30
31
# File 'lib/avodeploy/task/task_manager.rb', line 27

def initialize
  @chains = []
  @remote_env = nil
  @local_env = nil
end

Instance Attribute Details

#chainsObject (readonly)

Returns the value of attribute chains.



24
25
26
# File 'lib/avodeploy/task/task_manager.rb', line 24

def chains
  @chains
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



23
24
25
# File 'lib/avodeploy/task/task_manager.rb', line 23

def dependencies
  @dependencies
end

Instance Method Details

#add_task(name, options, &block) ⇒ Object

Adds a task to the task manager

Parameters:

  • name (Symbol)

    task name

  • options (Hash)

    task options

  • block (Block)

    code of the task



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/avodeploy/task/task_manager.rb', line 38

def add_task(name, options, &block)
  position = :after
  standalone = true

  if options.has_key?(:before)
    position = :before
  end

  key = name

  if options.has_key?(:before)
    key = options[:before]
    standalone = false
  elsif options.has_key?(:after)
    key = options[:after]
    standalone = false
  end

  if standalone == false
    idx = find_chain_index_containing(key)

    @chains[idx].delete(name)
    @chains[idx].insert_at(position, key, [name, Task.from_task_block(name, options, &block)])

  else
    chain = {}
    chain[name] = Task.from_task_block(name, options, &block)
    @chains << chain
  end
end

#execute_for_each_target(task, env) ⇒ Object

Executes a task for all defined targets

Parameters:

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/avodeploy/task/task_manager.rb', line 132

def execute_for_each_target(task, env)
  raise ArgumentError, 'task must be a task' unless task.kind_of?(Task)
  raise ArgumentError, 'env must be a RemoteTaskExecutionEnvironment' unless env.kind_of?(RemoteTaskExecutionEnvironment)

  avo = AvoDeploy::Deployment.instance

  avo.config.targets.each_pair do |key, target|
    # 'only' check
    next if task.remote_only.nil? == false && ((task.remote_only.is_a?(Array) && task.remote_only.include?(target.name) == false) || (task.remote_only.is_a?(Symbol) && task.remote_only != target.name))

    # 'except' check
    next if task.remote_except.nil? == false && ((task.remote_except.is_a?(Array) && task.remote_except.include?(target.name)) || (task.remote_except.is_a?(Symbol) && task.remote_except == target.name))

    avo.log.debug "invoking task #{task.name} for target #{target.name}..."

    env.config.merge!(target.config)
    env.establish_connection

    task.invoke(env)
  end
end

#find_chain_index_containing(name) ⇒ Object

Finds the chain containing a specifc task

Parameters:

  • name (Symbol)

    task name

  • chain (Integer)

    index

Raises:

  • (RuntimeError)


84
85
86
87
88
89
90
91
92
# File 'lib/avodeploy/task/task_manager.rb', line 84

def find_chain_index_containing(name)
  @chains.each_with_index do |chain, idx|
    if chain.has_key?(name)
      return idx
    end
  end

  raise RuntimeError, "could not find a chain containing task #{name}"
end

#invoke_task(task, options = {}) ⇒ Object

Invokes a task

Parameters:

  • task (Task)

    the task

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

    a hash contining additional options

Raises:

  • (ArgumentError)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/avodeploy/task/task_manager.rb', line 158

def invoke_task(task, options = {})
  raise ArgumentError, 'task must be a task' unless task.kind_of?(Task)

  avo = AvoDeploy::Deployment.instance
  env = nil

  if task.scope == :remote
    if @remote_env.nil?
      @remote_env = RemoteTaskExecutionEnvironment.new(avo.config.config)
    end

    env = @remote_env
  elsif task.scope == :local
    if @local_env.nil?
      @local_env = LocalTaskExecutionEnvironment.new(avo.config.config)
    end

    env = @local_env
  else
    raise RuntimeError, 'scope must either be remote or local'
  end

  # @todo this does not belong here
  env.scm = nil

  if avo.config.get(:scm) == :git
    env.scm = AvoDeploy::ScmProvider::GitScmProvider.new(env)
  elsif  avo.config.get(:scm) == :bzr
    env.scm = AvoDeploy::ScmProvider::BzrScmProvider.new(env)
  end

  if env.scm.nil?
    raise RuntimeError, 'No ScmProvider was instantiated'
  end

  if options.empty? == false
    env.options = options
  end

  # if remote task -> execute for each target
  if task.scope == :remote
    execute_for_each_target(task, env)
  else
    task.invoke(env)
  end
end

#invoke_task_chain_containing(task_name, options = {}) ⇒ Object

Invokes the task chain, that contains the requested task

Parameters:

  • task_name (Symbol)

    the task name

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

    a hash contining additional options



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/avodeploy/task/task_manager.rb', line 114

def invoke_task_chain_containing(task_name, options = {})
  task_name = task_name.to_sym if task_name.is_a?(String)

  cidx = find_chain_index_containing(task_name)

  begin
    @chains[cidx].each_pair do |name, task|
      invoke_task(task, options)
    end
  rescue Exception => e
    AvoDeploy::Deployment.instance.handle_abort(e)
  end
end

#invoke_task_oneshot(task_name, options = {}) ⇒ Object

Invokes a task without dependencies

Parameters:

  • task_name (Symbol)

    the task name

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

    a hash contining additional options



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/avodeploy/task/task_manager.rb', line 98

def invoke_task_oneshot(task_name, options = {})
  task_name = task_name.to_sym if task_name.is_a?(String)

  cidx = find_chain_index_containing(task_name)

  begin
    invoke_task(@chains[cidx][task_name], options)
  rescue Exception => e
    AvoDeploy::Deployment.instance.handle_abort(e)
  end
end

#task_by_name(name) ⇒ Task

Finds a task by its name

Parameters:

  • name (Symbol)

    name of the task

Returns:

  • (Task)

    the task if found



73
74
75
76
77
78
# File 'lib/avodeploy/task/task_manager.rb', line 73

def task_by_name(name)
  name = name.to_sym if name.is_a?(String)

  cidx = find_chain_index_containing(name)
  @chains[cidx][name]
end