Skip to content

Commit

Permalink
allow for array input for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
alex501212 committed Jul 26, 2024
1 parent 035859f commit bd83374
Showing 1 changed file with 54 additions and 17 deletions.
71 changes: 54 additions & 17 deletions bolt-modules/boltlib/lib/puppet/functions/run_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'bolt/error'
require 'json'
require 'shellwords'

# Runs a command on the given set of targets and returns the result from each command execution.
# This function does nothing if the list of targets is empty.
Expand Down Expand Up @@ -66,7 +67,7 @@ def run_command(command, targets, options = {})
end

def run_commands(commands, targets, options = {})
run_command_with_description(commands.join(' && '), targets, nil, options)
run_command_with_description(commands, targets, nil, options)
end

def run_command_with_description(command, targets, description = nil, options = {})
Expand Down Expand Up @@ -104,24 +105,60 @@ def run_command_with_description(command, targets, description = nil, options =
# Ensure that given targets are all Target instances
targets = inventory.get_targets(targets)

if targets.empty?
call_function('debug', "Simulating run_command('#{command}') - no targets given - no action taken")
Bolt::ResultSet.new([])
else
file_line = Puppet::Pops::PuppetStack.top_of_stack
r = if executor.in_parallel?
executor.run_in_thread do
executor.run_command(targets, command, options, file_line)
end
else
executor.run_command(targets, command, options, file_line)
# if command is an array, run each command in sequence
if command.is_a?(Array)
results = []
command.each do |cmd|
# Split the command into an array of arguments
split_cmd = Shellwords.split(cmd)

# Escape each argument
escaped_cmd = split_cmd.map { |arg| Shellwords.escape(arg) }.join(' ')

if targets.empty?
call_function('debug', "Simulating run_command('#{escaped_cmd}') - no targets given - no action taken")
Bolt::ResultSet.new([])
else
file_line = Puppet::Pops::PuppetStack.top_of_stack
result = if executor.in_parallel?
executor.run_in_thread do
executor.run_command(targets, escaped_cmd, options, file_line)
end
else
executor.run_command(targets, escaped_cmd, options, file_line)
end

if !result.ok && !options[:catch_errors]
raise Bolt::RunFailure.new(result, 'run_command', escaped_cmd)
end

if !r.ok && !options[:catch_errors]
raise Bolt::RunFailure.new(r, 'run_command', command)
results.concat(result.results)
end
end
Bolt::ResultSet.new(results)
else
# Split the command into an array of arguments
split_cmd = Shellwords.split(command)

escaped_cmd = split_cmd.map { |arg| Shellwords.escape(arg) }.join(' ')

if targets.empty?
call_function('debug', "Simulating run_command('#{escaped_cmd}') - no targets given - no action taken")
Bolt::Result.new([])
else
file_line = Puppet::Pops::PuppetStack.top_of_stack
result = if executor.in_parallel?
executor.run_in_thread do
executor.run_command(targets, escaped_cmd, options, file_line)
end
else
executor.run_command(targets, escaped_cmd, options, file_line)
end

if !result.ok && !options[:catch_errors]
raise Bolt::RunFailure.new(result, 'run_command', escaped_cmd)
end
result
end

r
end
end
end

0 comments on commit bd83374

Please sign in to comment.