Class: Kdeploy::CommandExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/kdeploy/executor/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, retry_on_nonzero: false, step_timeout: nil, retry_policy: nil) ⇒ CommandExecutor

Returns a new instance of CommandExecutor.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kdeploy/executor/command_executor.rb', line 8

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

Instance Method Details

#execute_run(command, _host_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kdeploy/executor/command_executor.rb', line 20

def execute_run(command, _host_name)
  cmd = command[:command]
  use_sudo = command[:sudo]

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

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

#execute_sync(command, _host_name) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kdeploy/executor/command_executor.rb', line 61

def execute_sync(command, _host_name)
  source = command[:source]
  destination = command[:destination]
  fast = command.key?(:fast) ? command[:fast] : Configuration.default_sync_fast
  parallel = command.key?(:parallel) ? command[:parallel] : Configuration.default_sync_parallel

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

  build_sync_result(source, destination, result, duration)
end

#execute_upload(command, _host_name) ⇒ Object



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

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

#execute_upload_template(command, _host_name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kdeploy/executor/command_executor.rb', line 46

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