Class: Kdeploy::SSHConnection
- Inherits:
-
Object
- Object
- Kdeploy::SSHConnection
- Defined in:
- lib/kdeploy/ssh_connection.rb
Overview
SSHConnection class for managing SSH connections to remote hosts
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
-
#cleanup ⇒ Object
Clean up connection.
-
#connect ⇒ Boolean
Establish SSH connection.
-
#connected? ⇒ Boolean
Check if connection is active.
-
#disconnect ⇒ Object
Close SSH connection.
-
#download(remote_path, local_path) ⇒ Boolean
Download file from remote host.
-
#execute(command, timeout: nil) ⇒ Hash
Execute command on remote host.
-
#initialize(host) ⇒ SSHConnection
constructor
A new instance of SSHConnection.
-
#upload(local_path, remote_path) ⇒ Boolean
Upload file to remote host.
Constructor Details
#initialize(host) ⇒ SSHConnection
Returns a new instance of SSHConnection.
8 9 10 11 12 |
# File 'lib/kdeploy/ssh_connection.rb', line 8 def initialize(host) @host = host @session = nil @connected = false end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
6 7 8 |
# File 'lib/kdeploy/ssh_connection.rb', line 6 def host @host end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
6 7 8 |
# File 'lib/kdeploy/ssh_connection.rb', line 6 def session @session end |
Instance Method Details
#cleanup ⇒ Object
Clean up connection
94 95 96 |
# File 'lib/kdeploy/ssh_connection.rb', line 94 def cleanup disconnect end |
#connect ⇒ Boolean
Establish SSH connection
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/kdeploy/ssh_connection.rb', line 17 def connect return true if connected? KdeployLogger.debug("Connecting to #{@host}") establish_connection KdeployLogger.debug("Connected to #{@host}") true rescue Net::SSH::Exception => e handle_connection_error(e) end |
#connected? ⇒ Boolean
Check if connection is active
30 31 32 |
# File 'lib/kdeploy/ssh_connection.rb', line 30 def connected? @connected && @session && !@session.closed? end |
#disconnect ⇒ Object
Close SSH connection
84 85 86 87 88 89 90 91 |
# File 'lib/kdeploy/ssh_connection.rb', line 84 def disconnect return unless @session @session.close unless @session.closed? @session = nil @connected = false KdeployLogger.debug("Disconnected from #{@host}") end |
#download(remote_path, local_path) ⇒ Boolean
Download file from remote host
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/kdeploy/ssh_connection.rb', line 72 def download(remote_path, local_path) ensure_connected KdeployLogger.debug("Downloading #{@host}:#{remote_path} to #{local_path}") perform_download(remote_path, local_path) KdeployLogger.debug("Download completed: #{@host}:#{remote_path} -> #{local_path}") true rescue Net::SCP::Error => e handle_download_error(e, remote_path, local_path) end |
#execute(command, timeout: nil) ⇒ Hash
Execute command on remote host
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/kdeploy/ssh_connection.rb', line 38 def execute(command, timeout: nil) ensure_connected result = initialize_result timeout ||= Kdeploy.configuration&.command_timeout || 300 KdeployLogger.debug("Executing on #{@host}: #{command}") execute_command(command, result) KdeployLogger.debug("Command completed on #{@host}: exit_code=#{result[:exit_code]}") result rescue Net::SSH::Exception => e handle_execution_error(e) end |
#upload(local_path, remote_path) ⇒ Boolean
Upload file to remote host
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/kdeploy/ssh_connection.rb', line 57 def upload(local_path, remote_path) ensure_connected KdeployLogger.debug("Uploading #{local_path} to #{@host}:#{remote_path}") perform_upload(local_path, remote_path) KdeployLogger.debug("Upload completed: #{local_path} -> #{@host}:#{remote_path}") true rescue Net::SCP::Error => e handle_upload_error(e, local_path, remote_path) end |