aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/shell_utils.rb22
1 files changed, 20 insertions, 2 deletions
diff --git a/lib/shell_utils.rb b/lib/shell_utils.rb
index b22e791..22bf05b 100644
--- a/lib/shell_utils.rb
+++ b/lib/shell_utils.rb
@@ -23,7 +23,11 @@ require 'shellwords'
require 'tempfile'
module ShellUtils
- def run_command(*command, run_remotely_on_host: nil)
+ def run_command(
+ *command,
+ run_remotely_on_host: nil,
+ environment_variables: {}
+ )
if run_remotely_on_host
command = command.map do |arg|
Shellwords.escape(arg)
@@ -42,13 +46,21 @@ module ShellUtils
'-o', 'StrictHostKeyChecking=no',
'-i', identity_file.path,
run_remotely_on_host.user_at_address,
+ *environment_variables_to_shell_arguments(environment_variables),
*command
]
+
+ local_environment_variables = {}
+ else
+ local_environment_variables = environment_variables
end
logger.info("#{self.class}: Running command #{command.join(' ')}")
- Open3.popen2e(*command) do |_stdin, stdout_and_stderr, wait_thr|
+ Open3.popen2e(
+ local_environment_variables,
+ *command
+ ) do |_stdin, stdout_and_stderr, wait_thr|
logger.info("#{self.class}: commmand running, pid #{wait_thr.pid}")
output = []
@@ -119,4 +131,10 @@ module ShellUtils
end
end
end
+
+ def environment_variables_to_shell_arguments(environment_variables)
+ environment_variables.map do |key, value|
+ "#{key}=\"#{value}\""
+ end
+ end
end