Class: KuberKit::Shell::SshShell

Inherits:
LocalShell show all
Defined in:
lib/kuber_kit/shell/ssh_shell.rb

Constant Summary

Constants inherited from LocalShell

LocalShell::MAX_LINES_TO_PRINT

Constants inherited from AbstractShell

AbstractShell::DirNotFoundError, AbstractShell::ShellError

Instance Method Summary collapse

Methods inherited from LocalShell

#delete, #dir_exists?, #file_exists?, #list_dirs, #recursive_list_files, #wrap_command_with_pid

Methods inherited from AbstractShell

#recursive_list_files

Instance Method Details

#connect(host:, user:, port:) ⇒ Object



11
12
13
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 11

def connect(host:, user:, port:)
  @ssh_session = KuberKit::Shell::SshSession.new(host: host, user: user, port: port)
end

#connected?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 15

def connected?
  @ssh_session && @ssh_session.connected?
end

#disconnectObject



19
20
21
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 19

def disconnect
  @ssh_session.disconnect if @ssh_session
end

#exec!(command, log_command: true, merge_stderr: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 23

def exec!(command, log_command: true, merge_stderr: false)
  command_number = command_counter.get_number.to_s.rjust(2, "0")
  
  if log_command
    ui.print_debug("SshShell", "#{ssh_session.host.green} > Execute: [#{command_number}]: #{command.to_s.cyan}")
  end

  result = ssh_session.exec!(wrap_command_with_pid(command), merge_stderr: merge_stderr)

  if result && result != "" && log_command
    ui.print_debug("SshShell", "#{ssh_session.host.green} > Finished [#{command_number}] with result: \n#{result.grey}")
  end

  result
rescue KuberKit::Shell::SshSession::SshSessionError => e
  raise ShellError.new(e.message)
end

#interactive!(command, log_command: true) ⇒ Object



41
42
43
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 41

def interactive!(command, log_command: true)
  raise "Currently interactive run is not supported for ssh shell."
end

#read(file_path) ⇒ Object



58
59
60
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 58

def read(file_path)
  exec!("cat #{file_path}")
end

#replace!(shell_name: nil, env: []) ⇒ Object



45
46
47
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 45

def replace!(shell_name: nil, env: [])
  raise "Currently repliace run is not supported for ssh shell."
end

#sync(local_path, remote_path, exclude: nil, delete: true) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 49

def sync(local_path, remote_path, exclude: nil, delete: true)
  rsync_commands.rsync(
    local_shell, local_path, remote_path, 
    target_host: "#{ssh_session.user}@#{ssh_session.host}",
    exclude:     exclude,
    delete:      delete
  )
end

#write(file_path, content) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 62

def write(file_path, content)
  Tempfile.create do |file| 
    file << content
    file.flush
    sync(file.path, file_path)
  end

  ui.print_debug("SshShell", "Created file #{file_path.to_s.cyan}\r\n  ----\r\n#{content.grey}\r\n  ----")

  true
end