Class: PlanExecutor::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/plan_executor/executor.rb

Defined Under Namespace

Classes: TimeoutError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_id, http_client, noop = nil) ⇒ Executor

Returns a new instance of Executor.



19
20
21
22
23
24
25
# File 'lib/plan_executor/executor.rb', line 19

def initialize(job_id, http_client, noop = nil)
  @logger = Logging.logger[self]
  @plan_logging = false
  @noop = noop
  @logger.debug { "Started" }
  @orch_client = PlanExecutor::OrchClient.new(job_id, http_client, @logger)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



16
17
18
# File 'lib/plan_executor/executor.rb', line 16

def logger
  @logger
end

#noopObject (readonly)

Returns the value of attribute noop.



16
17
18
# File 'lib/plan_executor/executor.rb', line 16

def noop
  @noop
end

#orch_clientObject

Returns the value of attribute orch_client.



17
18
19
# File 'lib/plan_executor/executor.rb', line 17

def orch_client
  @orch_client
end

Instance Method Details

#as_resultset(targets) ⇒ Object

This handles running the job, catching errors, and turning the result into a result set



29
30
31
32
33
34
35
36
37
38
# File 'lib/plan_executor/executor.rb', line 29

def as_resultset(targets)
  result_array = begin
                   yield
                 rescue StandardError => e
                   @logger.warn(e)
                   # CODEREVIEW how should we fail if there's an error?
                   Array(Bolt::Result.from_exception(targets[0], e))
                 end
  Bolt::ResultSet.new(result_array)
end

#finish_plan(plan_result) ⇒ Object



151
152
153
# File 'lib/plan_executor/executor.rb', line 151

def finish_plan(plan_result)
  @orch_client.finish_plan(plan_result)
end

#log_action(description, targets) ⇒ Object

BOLT-1098



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/plan_executor/executor.rb', line 41

def log_action(description, targets)
  # When running a plan, info messages like starting a task are promoted to notice.
  log_method = @plan_logging ? :notice : :info
  target_str = if targets.length > 5
                 "#{targets.count} targets"
               else
                 targets.map(&:uri).join(', ')
               end

  @logger.send(log_method, "Starting: #{description} on #{target_str}")

  start_time = Time.now
  results = yield
  duration = Time.now - start_time

  failures = results.error_set.length
  plural = failures == 1 ? '' : 's'

  @logger.send(log_method, "Finished: #{description} with #{failures} failure#{plural} in #{duration.round(2)} sec")

  results
end

#log_plan(plan_name) ⇒ Object

BOLT-1098



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/plan_executor/executor.rb', line 65

def log_plan(plan_name)
  log_method = @plan_logging ? :notice : :info
  @logger.send(log_method, "Starting: plan #{plan_name}")
  start_time = Time.now

  results = nil
  begin
    results = yield
  ensure
    duration = Time.now - start_time
    @logger.send(log_method, "Finished: plan #{plan_name} in #{duration.round(2)} sec")
  end

  results
end

#report_bundled_content(mode, name) ⇒ Object



163
# File 'lib/plan_executor/executor.rb', line 163

def report_bundled_content(mode, name); end

#report_function_call(function) ⇒ Object



165
# File 'lib/plan_executor/executor.rb', line 165

def report_function_call(function); end

#run_command(targets, command, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/plan_executor/executor.rb', line 81

def run_command(targets, command, options = {})
  description = options.fetch('_description', "command '#{command}'")
  log_action(description, targets) do
    results = as_resultset(targets) do
      @orch_client.run_command(targets, command, options)
    end

    results
  end
end

#run_script(targets, script, arguments, options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/plan_executor/executor.rb', line 92

def run_script(targets, script, arguments, options = {})
  description = options.fetch('_description', "script #{script}")
  log_action(description, targets) do
    results = as_resultset(targets) do
      @orch_client.run_script(targets, script, arguments, options)
    end

    results
  end
end

#run_task(targets, task, arguments, options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/plan_executor/executor.rb', line 103

def run_task(targets, task, arguments, options = {})
  description = options.fetch('_description', "task #{task.name}")
  log_action(description, targets) do
    arguments['_task'] = task.name

    results = as_resultset(targets) do
      @orch_client.run_task(targets, task, arguments, options)
    end

    results
  end
end

#upload_file(targets, source, destination, options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/plan_executor/executor.rb', line 116

def upload_file(targets, source, destination, options = {})
  description = options.fetch('_description', "file upload from #{source} to #{destination}")
  log_action(description, targets) do
    results = as_resultset(targets) do
      @orch_client.file_upload(targets, source, destination, options)
    end

    results
  end
end

#wait_until(timeout, retry_interval) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/plan_executor/executor.rb', line 143

def wait_until(timeout, retry_interval)
  start = wait_now
  until yield
    raise(TimeoutError, 'Timed out waiting for target') if (wait_now - start).to_i >= timeout
    sleep(retry_interval)
  end
end

#wait_until_available(targets, description: 'wait until available', wait_time: 120, retry_interval: 1) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/plan_executor/executor.rb', line 129

def wait_until_available(targets,
                         description: 'wait until available',
                         wait_time: 120,
                         retry_interval: 1)
  log_action(description, targets) do
    begin
      wait_until(wait_time, retry_interval) { @orch_client.connected?(targets) }
      targets.map { |target| Bolt::Result.new(target) }
    rescue TimeoutError => e
      targets.map { |target| Bolt::Result.from_exception(target, e) }
    end
  end
end

#without_default_loggingObject



155
156
157
158
159
160
161
# File 'lib/plan_executor/executor.rb', line 155

def without_default_logging
  old_log = @plan_logging
  @plan_logging = false
  yield
ensure
  @plan_logging = old_log
end