-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwrite_file.rb
More file actions
50 lines (44 loc) · 1.83 KB
/
write_file.rb
File metadata and controls
50 lines (44 loc) · 1.83 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
# frozen_string_literal: true
require 'tempfile'
# Write contents to a file on the given set of targets.
#
# > **Note:** Not available in apply block
Puppet::Functions.create_function(:write_file) do
# @param content File content to write.
# @param destination An absolute path on the target(s).
# @param targets A pattern identifying zero or more targets. See {get_targets} for accepted patterns.
# @param options A hash of additional options.
# @option options [Boolean] _catch_errors Whether to catch raised errors.
# @option options [String] _run_as User to run as using privilege escalation.
# @return A list of results, one entry per target.
# @example Write a file to a target
# $content = 'Hello, world!'
# write_file($content, '/Users/me/hello.txt', $targets)
dispatch :write_file do
required_param 'String', :content
required_param 'String[1]', :destination
required_param 'Boltlib::TargetSpec', :targets
optional_param 'Hash[String[1], Any]', :options
return_type 'ResultSet'
end
def write_file(content, destination, target_spec, options = {})
unless Puppet[:tasks]
raise Puppet::ParseErrorWithIssue
.from_issue_and_stack(Bolt::PAL::Issues::PLAN_OPERATION_NOT_SUPPORTED_WHEN_COMPILING,
action: 'write_file')
end
executor = Puppet.lookup(:bolt_executor)
# Send Analytics Report
executor.report_function_call(self.class.name)
inventory = Puppet.lookup(:bolt_inventory)
targets = inventory.get_targets(target_spec)
executor.log_action("write file #{destination}", targets) do
executor.without_default_logging do
Tempfile.create do |tmp|
call_function('file::write', tmp.path, content)
call_function('upload_file', tmp.path, destination, targets, options)
end
end
end
end
end