diff options
author | Christopher Baines <mail@cbaines.net> | 2018-06-24 11:11:49 +0100 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2018-06-24 11:11:49 +0100 |
commit | ac45ed064b096f715805d21638ee9286804d12ef (patch) | |
tree | bd0390124a0229d438668c4ae290f2a03ce88047 /lib/shell_utils.rb | |
parent | 3e3e975df56e6048594b1eaaed5ddbeab80918fb (diff) | |
download | govuk-mini-environment-admin-ac45ed064b096f715805d21638ee9286804d12ef.tar govuk-mini-environment-admin-ac45ed064b096f715805d21638ee9286804d12ef.tar.gz |
Neaten up SSH handling
Explicitly use the specified private key where possible. Also, use a
struct for the user, address and private key.
Diffstat (limited to 'lib/shell_utils.rb')
-rw-r--r-- | lib/shell_utils.rb | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/lib/shell_utils.rb b/lib/shell_utils.rb index 0cd8947..75ee3ca 100644 --- a/lib/shell_utils.rb +++ b/lib/shell_utils.rb @@ -20,6 +20,7 @@ require 'open3' require 'shellwords' +require 'tempfile' module ShellUtils def run_command(*command, run_remotely_on_host: nil) @@ -28,11 +29,19 @@ module ShellUtils Shellwords.escape(arg) end + identity_file = Tempfile.new( + 'private-identity-file', + Rails.root.join('tmp') + ) + identity_file.write(run_remotely_on_host.private_key) + identity_file.close + command = [ 'ssh', # Use a automatically trust on first use model '-o', 'StrictHostKeyChecking=no', - run_remotely_on_host, + '-i', identity_file.path, + run_remotely_on_host.user_at_address, *command ] end @@ -52,9 +61,12 @@ module ShellUtils unless exit_status == 0 logger.error(self.class) { "failed, exit status #{exit_status}" } + identity_file.unlink if identity_file raise "Running #{command.join(' ')} failed:\n\n#{output.join}\n" end + identity_file.unlink if identity_file + output end end |