Class: Kdeploy::SSHConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/kdeploy/ssh_connection.rb

Overview

SSHConnection class for managing SSH connections to remote hosts

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/kdeploy/ssh_connection.rb', line 6

def host
  @host
end

#sessionObject (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

#cleanupObject

Clean up connection



94
95
96
# File 'lib/kdeploy/ssh_connection.rb', line 94

def cleanup
  disconnect
end

#connectBoolean

Establish SSH connection

Returns:

  • (Boolean)

    True if connection successful

Raises:



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

Returns:

  • (Boolean)

    True if connected



30
31
32
# File 'lib/kdeploy/ssh_connection.rb', line 30

def connected?
  @connected && @session && !@session.closed?
end

#disconnectObject

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

Parameters:

  • remote_path (String)

    Remote file path

  • local_path (String)

    Local file path

Returns:

  • (Boolean)

    True if download successful



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

Parameters:

  • command (String)

    Command to execute

  • timeout (Integer) (defaults to: nil)

    Command timeout in seconds

Returns:

  • (Hash)

    Result with stdout, stderr, exit_code, and success



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

Parameters:

  • local_path (String)

    Local file path

  • remote_path (String)

    Remote file path

Returns:

  • (Boolean)

    True if upload successful



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