Class: Kdeploy::CommandExecutor

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

Overview

Executes a single command and records execution time

Instance Method Summary collapse

Constructor Details

#initialize(executor, output, debug: false, retries: 0, retry_delay: 1) ⇒ CommandExecutor

Returns a new instance of CommandExecutor.



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

def initialize(executor, output, debug: false, retries: 0, retry_delay: 1)
  @executor = executor
  @output = output
  @debug = debug
  @retries = retries.to_i
  @retry_delay = retry_delay.to_f
end

Instance Method Details

#execute_run(command, host_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kdeploy/command_executor.rb', line 14

def execute_run(command, host_name)
  cmd = command[:command]
  use_sudo = command[:sudo]
  show_command_header(host_name, :run, cmd)

  # Show progress indicator for long-running commands
  pastel = @output.respond_to?(:pastel) ? @output.pastel : Pastel.new

  result, duration = measure_time do
    with_retries { @executor.execute(cmd, use_sudo: use_sudo) }
  end

  # Show execution time if command took more than 1 second
  @output.write_line(pastel.dim("    [completed in #{format('%.2f', duration)}s]")) if duration > 1.0

  { command: cmd, output: result, duration: duration, type: :run }
end

#execute_sync(command, host_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kdeploy/command_executor.rb', line 58

def execute_sync(command, host_name)
  source = command[:source]
  destination = command[:destination]
  description = build_sync_description(source, destination, command[:delete])
  show_command_header(host_name, :sync, description)

  result, duration = measure_time do
    with_retries do
      @executor.sync_directory(
        source,
        destination,
        ignore: command[:ignore] || [],
        exclude: command[:exclude] || [],
        delete: command[:delete] || false
      )
    end
  end

  build_sync_result(source, destination, result, duration)
end

#execute_upload(command, host_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kdeploy/command_executor.rb', line 32

def execute_upload(command, host_name)
  show_command_header(host_name, :upload, "#{command[:source]} -> #{command[:destination]}")
  _result, duration = measure_time do
    with_retries { @executor.upload(command[:source], command[:destination]) }
  end
  {
    command: "upload: #{command[:source]} -> #{command[:destination]}",
    duration: duration,
    type: :upload
  }
end

#execute_upload_template(command, host_name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kdeploy/command_executor.rb', line 44

def execute_upload_template(command, host_name)
  show_command_header(host_name, :upload_template, "#{command[:source]} -> #{command[:destination]}")
  _result, duration = measure_time do
    with_retries do
      @executor.upload_template(command[:source], command[:destination], command[:variables])
    end
  end
  {
    command: "upload_template: #{command[:source]} -> #{command[:destination]}",
    duration: duration,
    type: :upload_template
  }
end