Class: Kdeploy::Runner
- Inherits:
-
Object
- Object
- Kdeploy::Runner
- Defined in:
- lib/kdeploy/runner.rb
Instance Method Summary collapse
-
#initialize(hosts, tasks, parallel: 5) ⇒ Runner
constructor
A new instance of Runner.
- #run(task_name) ⇒ Object
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# 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.group_by do |cmd| cmd[:type].to_s + (if cmd[:type] == :upload cmd[:source].to_s else cmd[:type] == :run ? cmd[:command].to_s.lines.first.strip : '' end) end.each_value do |commands| # 生成 TASK 横幅 pastel = Pastel.new first_cmd = commands.first task_desc = case first_cmd[:type] when :upload "upload #{first_cmd[:source]}" when :upload_template "template #{first_cmd[:source]}" when :run first_cmd[:command].to_s.lines.first.strip else first_cmd[:type].to_s end puts pastel.cyan("\nTASK [#{task_desc}] " + ('*' * 64)) commands.each do |command| case command[:type] when :run pastel = Pastel.new puts pastel.bright_white("\n#{name.ljust(24)} : ") puts pastel.cyan(" [run] #{command[:command].lines.first.strip}") command[:command].lines[1..].each { |line| puts " > #{line.strip}" unless line.strip.empty? } t1 = Time.now output = executor.execute(command[:command]) t2 = Time.now duration = t2 - t1 # 统一输出命令结果 if output[:stdout] && !output[:stdout].empty? output[:stdout].each_line { |line| puts " #{line.rstrip}" unless line.strip.empty? } end if output[:stderr] && !output[:stderr].empty? output[:stderr].each_line { |line| puts pastel.green(" #{line.rstrip}") unless line.strip.empty? } end result[:output] << { command: command[:command], output: output, duration: duration, type: :run } when :upload pastel = Pastel.new puts pastel.bright_white("\n#{name.ljust(24)} : ") puts pastel.green(" [upload] #{command[:source]} -> #{command[:destination]}") t1 = Time.now executor.upload(command[:source], command[:destination]) t2 = Time.now duration = t2 - t1 result[:output] << { command: "upload: #{command[:source]} -> #{command[:destination]}", duration: duration, type: :upload } when :upload_template pastel = Pastel.new puts pastel.bright_white("\n#{name.ljust(24)} : ") puts pastel.yellow(" [template] #{command[:source]} -> #{command[:destination]}") t1 = Time.now executor.upload_template(command[:source], command[:destination], command[:vars]) t2 = Time.now duration = t2 - t1 result[:output] << { command: "upload_template: #{command[:source]} -> #{command[:destination]}", duration: duration, type: :upload_template } end end end @results[name] = result rescue StandardError => e @results[name] = { status: :failed, error: e. } end end futures.each(&:wait) @results ensure @pool.shutdown end |