Class: Nvoi::External::Ssh
- Inherits:
-
Object
- Object
- Nvoi::External::Ssh
- Defined in:
- lib/nvoi/external/ssh.rb
Overview
Ssh handles command execution on remote servers
Instance Attribute Summary collapse
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#ssh_key ⇒ Object
readonly
Returns the value of attribute ssh_key.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #download(remote_path, local_path) ⇒ Object
- #execute(command, stream: false) ⇒ Object
- #execute_ignore_errors(command) ⇒ Object
-
#initialize(ip, ssh_key, user: "deploy") ⇒ Ssh
constructor
A new instance of Ssh.
- #open_shell ⇒ Object
- #upload(local_path, remote_path) ⇒ Object
Constructor Details
#initialize(ip, ssh_key, user: "deploy") ⇒ Ssh
Returns a new instance of Ssh.
9 10 11 12 13 14 |
# File 'lib/nvoi/external/ssh.rb', line 9 def initialize(ip, ssh_key, user: "deploy") @ip = ip @ssh_key = ssh_key @user = user @strict_mode = ENV["SSH_STRICT_HOST_KEY_CHECKING"] == "true" end |
Instance Attribute Details
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
7 8 9 |
# File 'lib/nvoi/external/ssh.rb', line 7 def ip @ip end |
#ssh_key ⇒ Object (readonly)
Returns the value of attribute ssh_key.
7 8 9 |
# File 'lib/nvoi/external/ssh.rb', line 7 def ssh_key @ssh_key end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
7 8 9 |
# File 'lib/nvoi/external/ssh.rb', line 7 def user @user end |
Instance Method Details
#download(remote_path, local_path) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/nvoi/external/ssh.rb', line 57 def download(remote_path, local_path) scp_args = build_scp_args scp_args += ["#{@user}@#{@ip}:#{remote_path}", local_path] output, status = Open3.capture2e("scp", *scp_args) raise Errors::SshCommandError, "SCP download failed: #{output}" unless status.success? end |
#execute(command, stream: false) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nvoi/external/ssh.rb', line 16 def execute(command, stream: false) ssh_args = build_ssh_args ssh_args += ["#{@user}@#{@ip}", command] if stream success = system("ssh", *ssh_args) raise Errors::SshCommandError, "SSH command failed" unless success "" else output, status = Open3.capture2e("ssh", *ssh_args) unless status.success? raise Errors::SshCommandError, "SSH command failed (exit code: #{status.exitstatus}): #{output}" end output.strip end end |
#execute_ignore_errors(command) ⇒ Object
36 37 38 39 40 |
# File 'lib/nvoi/external/ssh.rb', line 36 def execute_ignore_errors(command) execute(command) rescue StandardError nil end |
#open_shell ⇒ Object
42 43 44 45 46 47 |
# File 'lib/nvoi/external/ssh.rb', line 42 def open_shell ssh_args = build_ssh_args ssh_args += ["-t", "#{@user}@#{@ip}"] exec("ssh", *ssh_args) end |
#upload(local_path, remote_path) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/nvoi/external/ssh.rb', line 49 def upload(local_path, remote_path) scp_args = build_scp_args scp_args += [local_path, "#{@user}@#{@ip}:#{remote_path}"] output, status = Open3.capture2e("scp", *scp_args) raise Errors::SshCommandError, "SCP upload failed: #{output}" unless status.success? end |