Class: DeploYML::RemoteShell
- Defined in:
- lib/deployml/remote_shell.rb
Overview
Represents a shell running on a remote server.
Instance Attribute Summary collapse
-
#history ⇒ Object
readonly
The history of the Remote Shell.
Attributes inherited from Shell
Instance Method Summary collapse
-
#cd(path) { ... } ⇒ Object
Enqueues a directory change for the session.
-
#echo(message) ⇒ Object
Enqueues an
echo
command to be ran in the session. -
#exec(command) ⇒ Object
Adds a command to be executed.
-
#initialize(uri, environment = nil) {|session| ... } ⇒ RemoteShell
constructor
Initializes a remote shell session.
-
#join ⇒ String
Joins the command history together with
&&
, to form a single command. -
#replay ⇒ Object
Replays the command history on the remote server.
-
#run(program, *arguments) ⇒ Object
Enqueues a program to be ran in the session.
-
#ssh(*arguments) ⇒ Object
Starts a SSH session with the destination server.
-
#ssh_uri ⇒ String
Converts the URI to one compatible with SSH.
Methods inherited from Shell
#rake, #rake_task, #ruby, #shellescape, #status
Constructor Details
#initialize(uri, environment = nil) {|session| ... } ⇒ RemoteShell
Initializes a 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
#history ⇒ Object (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.
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.
69 70 71 |
# File 'lib/deployml/remote_shell.rb', line 69 def echo() run 'echo', end |
#exec(command) ⇒ Object
Adds a command to be executed.
59 60 61 |
# File 'lib/deployml/remote_shell.rb', line 59 def exec(command) @history << [command] end |
#join ⇒ String
Joins the command history together with &&
, to form a
single command.
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 |
#replay ⇒ Object
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.
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.
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) = [] # Add the -p option if an alternate destination port is given if @uri.port += ['-p', @uri.port.to_s] end # append the SSH URI << ssh_uri # append the additional arguments arguments.each { |arg| << arg.to_s } return system('ssh',*) end |
#ssh_uri ⇒ String
Converts the URI to one compatible with SSH.
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 |