Class: Moku::Shell::SecureRemote

Inherits:
Object
  • Object
show all
Defined in:
lib/moku/shell/secure_remote.rb

Overview

A shell that uses ssh

Constant Summary collapse

SSH_OPTIONS =
[
  "-o BatchMode=yes",
  "-o ConnectTimeout=3",
  "-o ChallengeResponseAuthentication=no",
  "-o PasswordAuthentication=no",
  "-o LogLevel=ERROR",
  "-a",
  "-i #{ENV["HOME"]}/.ssh/id_rsa-moku"
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(system_shell) ⇒ SecureRemote

Returns a new instance of SecureRemote.


19
20
21
# File 'lib/moku/shell/secure_remote.rb', line 19

def initialize(system_shell)
  @system_shell = system_shell
end

Instance Method Details

#run(host:, command:, user: Moku.user) ⇒ Object


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

def run(host:, command:, user: Moku.user)
  # This is a little gnarly because we need the local bash to pass any
  # variable expressions through. That can either be done by escaping
  # individual special characters or single quoting the whole command.
  # Quoting the whole remote command is preferable, to avoid missing or
  # mangling anything, but because it used as a local argument, it must
  # be single quoted again. Using the heredoc allows us to use both types
  # of quotes in the Ruby string without a bunch of \'s.
  #
  # If the ultimate remote command should be: echo $PATH
  # The local command should be: ssh ... bash -l -c "'"'echo $PATH"'"'
  # So the command passed to ssh is: bash -l -c 'echo $PATH'
  system_shell.run <<~CMD
    ssh #{SSH_OPTIONS.join(" ")} #{user}@#{host} bash -l -c "'"'#{command}'"'"
  CMD
end