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 |
# 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 # 美化输出 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? } output = executor.execute(command[:command]) result[:output] << { command: command[:command], output: output } when :upload pastel = Pastel.new puts pastel.bright_white("\n#{name.ljust(24)} : ") puts pastel.green(" [upload] #{command[:source]} -> #{command[:destination]}") executor.upload(command[:source], command[:destination]) result[:output] << { command: "upload: #{command[:source]} -> #{command[:destination]}" } when :upload_template pastel = Pastel.new puts pastel.bright_white("\n#{name.ljust(24)} : ") puts pastel.yellow(" [template] #{command[:source]} -> #{command[:destination]}") 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. } end end futures.each(&:wait) @results ensure @pool.shutdown end |