class DeploYML::RemoteShell

Represents a shell running on a remote server.

Attributes

history[R]

The history of the Remote Shell

Public Class Methods

new(uri,environment=nil,&block) click to toggle source

Initializes a remote shell session.

@param [Addressable::URI, String] uri

The URI of the host to connect to.

@param [Environment] environment

The environment the shell is connected to.

@yield [session]

If a block is given, it will be passed the new remote shell session.

@yieldparam [ShellSession] session

The remote shell session.
Calls superclass method
# File lib/deployml/remote_shell.rb, line 30
def initialize(uri,environment=nil,&block)
  @history = []

  super(uri,environment,&block)

  replay if block
end

Public Instance Methods

cd(path) { || ... } click to toggle source

Enqueues a directory change for the session.

@param [String] path

The path of the new current working directory to use.

@yield []

If a block is given, then the directory will be changed back after
the block has returned.
# File lib/deployml/remote_shell.rb, line 83
def cd(path)
  @history << ['cd', path]

  if block_given?
    yield
    @history << ['cd', '-']
  end
end
echo(message) click to toggle source

Enqueues an `echo` command to be ran in the session.

@param [String] message

The message to echo.
# File lib/deployml/remote_shell.rb, line 69
def echo(message)
  run 'echo', message
end
exec(command) click to toggle source

Adds a command to be executed.

@param [String] command

The command string.

@since 0.5.2

# File lib/deployml/remote_shell.rb, line 59
def exec(command)
  @history << [command]
end
join() click to toggle source

Joins the command history together with ` && `, to form a single command.

@return [String]

A single command string.
# File lib/deployml/remote_shell.rb, line 99
def join
  commands = []

  @history.each do |command|
    program = command[0]
    arguments = command[1..-1].map { |word| shellescape(word.to_s) }

    commands << [program, *arguments].join(' ')
  end

  return commands.join(' && ')
end
replay() click to toggle source

Replays the command history on the remote server.

# File lib/deployml/remote_shell.rb, line 158
def replay
  ssh(self.join) unless @history.empty?
end
run(program,*arguments) click to toggle source

Enqueues a program to be ran in the session.

@param [String] program

The name or path of the program to run.

@param [Array<String>] arguments

Additional arguments for the program.
# File lib/deployml/remote_shell.rb, line 47
def run(program,*arguments)
  @history << [program, *arguments]
end
ssh(*arguments) click to toggle source

Starts a SSH session with the destination server.

@param [Array] arguments

Additional arguments to pass to SSH.
# File lib/deployml/remote_shell.rb, line 138
def ssh(*arguments)
  options = []

  # Add the -p option if an alternate destination port is given
  if @uri.port
    options += ['-p', @uri.port.to_s]
  end

  # append the SSH URI
  options << ssh_uri

  # append the additional arguments
  arguments.each { |arg| options << arg.to_s }

  return system('ssh',*options)
end
ssh_uri() click to toggle source

Converts the URI to one compatible with SSH.

@return [String]

The SSH compatible URI.

@raise [InvalidConfig]

The URI of the shell does not have a host component.
# File lib/deployml/remote_shell.rb, line 121
def ssh_uri
  unless @uri.host
    raise(InvalidConfig,"URI does not have a host: #{@uri}",caller)
  end

  new_uri = @uri.host
  new_uri = "#{@uri.user}@#{new_uri}" if @uri.user

  return new_uri
end