class KuberKit::Shell::SshShell

Public Instance Methods

connect(host:, user:, port:) click to toggle source
# 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?() click to toggle source
# File lib/kuber_kit/shell/ssh_shell.rb, line 15
def connected?
  @ssh_session && @ssh_session.connected?
end
disconnect() click to toggle source
# File lib/kuber_kit/shell/ssh_shell.rb, line 19
def disconnect
  @ssh_session.disconnect if @ssh_session
end
exec!(command, log_command: true) click to toggle source
# File lib/kuber_kit/shell/ssh_shell.rb, line 23
def exec!(command, log_command: true)
  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))

  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) click to toggle source
# 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) click to toggle source
# File lib/kuber_kit/shell/ssh_shell.rb, line 54
def read(file_path)
  exec!("cat #{file_path}")
end
sync(local_path, remote_path, exclude: nil, delete: true) click to toggle source
# File lib/kuber_kit/shell/ssh_shell.rb, line 45
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) click to toggle source
# File lib/kuber_kit/shell/ssh_shell.rb, line 58
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

Private Instance Methods

ensure_directory_exists(file_path) click to toggle source
# File lib/kuber_kit/shell/ssh_shell.rb, line 79
def ensure_directory_exists(file_path)
  exec!("mkdir -p #{file_path}")
end
ssh_session() click to toggle source
# File lib/kuber_kit/shell/ssh_shell.rb, line 71
def ssh_session
  unless connected?
    raise ArgumentError, "ssh session is not created, please call #connect"
  end

  @ssh_session
end