Class: DeploYML::RemoteShell

Inherits:
Shell
  • Object
show all
Defined in:
lib/deployml/remote_shell.rb

Overview

Represents a shell running on a remote server.

Instance Attribute Summary collapse

Attributes inherited from Shell

#uri

Instance Method Summary collapse

Methods inherited from Shell

#rake, #rake_task, #ruby, #shellescape, #status

Constructor Details

#initialize(uri, environment = nil) {|session| ... } ⇒ RemoteShell

Initializes a remote shell session.

Parameters:

  • uri (Addressable::URI, String)

    The URI of the host to connect to.

  • environment (Environment) (defaults to: nil)

    The environment the shell is connected to.

Yields:

  • (session)

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

Yield Parameters:

  • session (ShellSession)

    The remote shell session.



30
31
32
33
34
35
36
# File 'lib/deployml/remote_shell.rb', line 30

def initialize(uri,environment=nil,&block)
  @history = []

  super(uri,environment,&block)

  replay if block
end

Instance Attribute Details

#historyObject (readonly)

The history of the Remote Shell



13
14
15
# File 'lib/deployml/remote_shell.rb', line 13

def history
  @history
end

Instance Method Details

#cd(path) { ... } ⇒ Object

Enqueues a directory change for the session.

Parameters:

  • path (String)

    The path of the new current working directory to use.

Yields:

  • [] If a block is given, then the directory will be changed back after the block has returned.



83
84
85
86
87
88
89
90
# File 'lib/deployml/remote_shell.rb', line 83

def cd(path)
  @history << ['cd', path]

  if block_given?
    yield
    @history << ['cd', '-']
  end
end

#echo(message) ⇒ Object

Enqueues an echo command to be ran in the session.

Parameters:

  • message (String)

    The message to echo.



69
70
71
# File 'lib/deployml/remote_shell.rb', line 69

def echo(message)
  run 'echo', message
end

#exec(command) ⇒ Object

Adds a command to be executed.

Parameters:

  • command (String)

    The command string.

Since:

  • 0.5.2



59
60
61
# File 'lib/deployml/remote_shell.rb', line 59

def exec(command)
  @history << [command]
end

#joinString

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

Returns:

  • (String)

    A single command string.



99
100
101
102
103
104
105
106
107
108
109
110
# 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

#replayObject

Replays the command history on the remote server.



158
159
160
# File 'lib/deployml/remote_shell.rb', line 158

def replay
  ssh(self.join) unless @history.empty?
end

#run(program, *arguments) ⇒ Object

Enqueues a program to be ran in the session.

Parameters:

  • program (String)

    The name or path of the program to run.

  • arguments (Array<String>)

    Additional arguments for the program.



47
48
49
# File 'lib/deployml/remote_shell.rb', line 47

def run(program,*arguments)
  @history << [program, *arguments]
end

#ssh(*arguments) ⇒ Object

Starts a SSH session with the destination server.

Parameters:

  • arguments (Array)

    Additional arguments to pass to SSH.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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_uriString

Converts the URI to one compatible with SSH.

Returns:

  • (String)

    The SSH compatible URI.

Raises:

  • (InvalidConfig)

    The URI of the shell does not have a host component.



121
122
123
124
125
126
127
128
129
130
# 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