|
2 | 2 |
|
3 | 3 | require 'bolt/error'
|
4 | 4 | require 'json'
|
| 5 | +require 'shellwords' |
5 | 6 |
|
6 | 7 | # Runs a command on the given set of targets and returns the result from each command execution.
|
7 | 8 | # This function does nothing if the list of targets is empty.
|
@@ -66,7 +67,7 @@ def run_command(command, targets, options = {})
|
66 | 67 | end
|
67 | 68 |
|
68 | 69 | def run_commands(commands, targets, options = {})
|
69 |
| - run_command_with_description(commands.join(' && '), targets, nil, options) |
| 70 | + run_command_with_description(commands, targets, nil, options) |
70 | 71 | end
|
71 | 72 |
|
72 | 73 | def run_command_with_description(command, targets, description = nil, options = {})
|
@@ -104,24 +105,60 @@ def run_command_with_description(command, targets, description = nil, options =
|
104 | 105 | # Ensure that given targets are all Target instances
|
105 | 106 | targets = inventory.get_targets(targets)
|
106 | 107 |
|
107 |
| - if targets.empty? |
108 |
| - call_function('debug', "Simulating run_command('#{command}') - no targets given - no action taken") |
109 |
| - Bolt::ResultSet.new([]) |
110 |
| - else |
111 |
| - file_line = Puppet::Pops::PuppetStack.top_of_stack |
112 |
| - r = if executor.in_parallel? |
113 |
| - executor.run_in_thread do |
114 |
| - executor.run_command(targets, command, options, file_line) |
115 |
| - end |
116 |
| - else |
117 |
| - executor.run_command(targets, command, options, file_line) |
| 108 | + # if command is an array, run each command in sequence |
| 109 | + if command.is_a?(Array) |
| 110 | + results = [] |
| 111 | + command.each do |cmd| |
| 112 | + # Split the command into an array of arguments |
| 113 | + split_cmd = Shellwords.split(cmd) |
| 114 | + |
| 115 | + # Escape each argument |
| 116 | + escaped_cmd = split_cmd.map { |arg| Shellwords.escape(arg) }.join(' ') |
| 117 | + |
| 118 | + if targets.empty? |
| 119 | + call_function('debug', "Simulating run_command('#{escaped_cmd}') - no targets given - no action taken") |
| 120 | + Bolt::ResultSet.new([]) |
| 121 | + else |
| 122 | + file_line = Puppet::Pops::PuppetStack.top_of_stack |
| 123 | + result = if executor.in_parallel? |
| 124 | + executor.run_in_thread do |
| 125 | + executor.run_command(targets, escaped_cmd, options, file_line) |
| 126 | + end |
| 127 | + else |
| 128 | + executor.run_command(targets, escaped_cmd, options, file_line) |
| 129 | + end |
| 130 | + |
| 131 | + if !result.ok && !options[:catch_errors] |
| 132 | + raise Bolt::RunFailure.new(result, 'run_command', escaped_cmd) |
118 | 133 | end
|
119 |
| - |
120 |
| - if !r.ok && !options[:catch_errors] |
121 |
| - raise Bolt::RunFailure.new(r, 'run_command', command) |
| 134 | + results.concat(result.results) |
| 135 | + end |
| 136 | + end |
| 137 | + Bolt::ResultSet.new(results) |
| 138 | + else |
| 139 | + # Split the command into an array of arguments |
| 140 | + split_cmd = Shellwords.split(command) |
| 141 | + |
| 142 | + escaped_cmd = split_cmd.map { |arg| Shellwords.escape(arg) }.join(' ') |
| 143 | + |
| 144 | + if targets.empty? |
| 145 | + call_function('debug', "Simulating run_command('#{escaped_cmd}') - no targets given - no action taken") |
| 146 | + Bolt::Result.new([]) |
| 147 | + else |
| 148 | + file_line = Puppet::Pops::PuppetStack.top_of_stack |
| 149 | + result = if executor.in_parallel? |
| 150 | + executor.run_in_thread do |
| 151 | + executor.run_command(targets, escaped_cmd, options, file_line) |
| 152 | + end |
| 153 | + else |
| 154 | + executor.run_command(targets, escaped_cmd, options, file_line) |
| 155 | + end |
| 156 | + |
| 157 | + if !result.ok && !options[:catch_errors] |
| 158 | + raise Bolt::RunFailure.new(result, 'run_command', escaped_cmd) |
| 159 | + end |
| 160 | + result |
122 | 161 | end
|
123 |
| - |
124 |
| - r |
125 | 162 | end
|
126 | 163 | end
|
127 | 164 | end
|
0 commit comments