class AvoDeploy::Task::TaskManager

Attributes

chains[R]
dependencies[R]

Public Class Methods

new() click to toggle source

Initializes the task manager

# File lib/avodeploy/task/task_manager.rb, line 27
def initialize
  @chains = []
  @remote_env = nil
  @local_env = nil
end

Public Instance Methods

add_task(name, options, &block) click to toggle source

Adds a task to the task manager

@param name [Symbol] task name @param options [Hash] task options @param block [Block] code of the task

# 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) click to toggle source

Executes a task for all defined targets

@param task [Task] the task to start @param env [RemoteTaskExecutionEnvironment] the environment

# 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) click to toggle source

Finds the chain containing a specifc task

@param name [Symbol] task name @param [Integer] chain index

# 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 = {}) click to toggle source

Invokes a task

@param task [Task] the task @param options [Hash] a hash contining additional options

# 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 = {}) click to toggle source

Invokes the task chain, that contains the requested task

@param task_name [Symbol] the task name @param options [Hash] a hash contining additional options

# 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 = {}) click to toggle source

Invokes a task without dependencies

@param task_name [Symbol] the task name @param options [Hash] a hash contining additional options

# 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) click to toggle source

Finds a task by its name

@param name [Symbol] name of the task @return [Task] the task if found

# 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