-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwait_until_available.rb
More file actions
60 lines (51 loc) · 2.5 KB
/
wait_until_available.rb
File metadata and controls
60 lines (51 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
# frozen_string_literal: true
require 'bolt/util'
# Wait until all targets accept connections. This function allows a plan execution to wait for a customizable
# amount of time via the `wait_time` option until a target connection can be reestablished. The plan proceeds
# to the next step if the connection fails to reconnect in the time specified (default: 120 seconds). A typical
# use case for this function is if your plan reboots a remote host and the plan needs to wait for the host to reconnect
# before it continues to the next step.
#
# > **Note:** Not available in apply block
Puppet::Functions.create_function(:wait_until_available) do
# Wait until targets are available.
# @param targets A pattern identifying zero or more targets. See {get_targets} for accepted patterns.
# @param options A hash of additional options.
# @option options [String] description A description for logging. (default: 'wait until available')
# @option options [Numeric] wait_time The time to wait, in seconds. (default: 120)
# @option options [Numeric] retry_interval The interval to wait before retrying, in seconds. (default: 1)
# @option options [Boolean] _catch_errors Whether to catch raised errors.
# @return A list of results, one entry per target. Successful results have no value.
# @example Wait for targets
# wait_until_available($targets, wait_time => 300)
dispatch :wait_until_available do
param 'Boltlib::TargetSpec', :targets
optional_param 'Hash[String[1], Any]', :options
return_type 'ResultSet'
end
def wait_until_available(targets, options = nil)
unless Puppet[:tasks]
raise Puppet::ParseErrorWithIssue
.from_issue_and_stack(Bolt::PAL::Issues::PLAN_OPERATION_NOT_SUPPORTED_WHEN_COMPILING,
action: 'wait_until_available')
end
options ||= {}
executor = Puppet.lookup(:bolt_executor)
inventory = Puppet.lookup(:bolt_inventory)
# Send Analytics Report
executor.report_function_call(self.class.name)
# Ensure that given targets are all Target instances
targets = inventory.get_targets(targets)
if targets.empty?
call_function('debug', "Simulating wait_until_available - no targets given")
r = Bolt::ResultSet.new([])
else
opts = Bolt::Util.symbolize_top_level_keys(options.reject { |k| k == '_catch_errors' })
r = executor.wait_until_available(targets, **opts)
end
if !r.ok && !options['_catch_errors']
raise Bolt::RunFailure.new(r, 'wait_until_available')
end
r
end
end