Class: Kdeploy::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/kdeploy/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(hosts, tasks, parallel: 5) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
# File 'lib/kdeploy/runner.rb', line 7

def initialize(hosts, tasks, parallel: 5)
  @hosts = hosts
  @tasks = tasks
  @parallel = parallel
  @pool = Concurrent::FixedThreadPool.new(@parallel)
  @results = Concurrent::Hash.new
end

Instance Method Details

#run(task_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kdeploy/runner.rb', line 15

def run(task_name)
  task = @tasks[task_name]
  raise "Task not found: #{task_name}" unless task

  futures = @hosts.map do |name, config|
    Concurrent::Future.execute(executor: @pool) do
      executor = Executor.new(config)
      result = { status: :success, output: [] }

      task[:block].call.each do |command|
        case command[:type]
        when :run
          output = executor.execute(command[:command])
          result[:output] << { command: command[:command], output: output }
        when :upload
          executor.upload(command[:source], command[:destination])
          result[:output] << { command: "upload: #{command[:source]} -> #{command[:destination]}" }
        when :upload_template
          executor.upload_template(command[:source], command[:destination], command[:variables])
          result[:output] << { command: "upload_template: #{command[:source]} -> #{command[:destination]}" }
        end
      end

      @results[name] = result
    rescue StandardError => e
      @results[name] = { status: :failed, error: e.message }
    end
  end

  futures.each(&:wait)
  @results
ensure
  @pool.shutdown
end