Class: Rush::Connection::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/rush/remote.rb

Overview

This class it the mirror of Rush::Connection::Local. A Rush::Box which is not localhost has a remote connection, which it can use to convert method calls to text suitable for transmission across the wire.

This is an internal class that does not need to be accessed in normal use of the rush shell or library.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Remote

Returns a new instance of Remote.



12
13
14
15
# File 'lib/rush/remote.rb', line 12

def initialize(host)
	@host = host
	@tunnel = Rush::SshTunnel.new(host)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/rush/remote.rb', line 10

def host
  @host
end

#tunnelObject (readonly)

Returns the value of attribute tunnel.



10
11
12
# File 'lib/rush/remote.rb', line 10

def tunnel
  @tunnel
end

Instance Method Details

#alive?Boolean

Remote connections are alive when the box on the other end is responding to commands.

Returns:

  • (Boolean)


146
147
148
149
150
151
# File 'lib/rush/remote.rb', line 146

def alive?
	index('/', 'alive_check')
	true
rescue
	false
end

#append_to_file(full_path, contents) ⇒ Object



21
22
23
# File 'lib/rush/remote.rb', line 21

def append_to_file(full_path, contents)
	transmit(:action => 'append_to_file', :full_path => full_path, :payload => contents)
end

#bash(command, user, background, reset_environment) ⇒ Object



85
86
87
# File 'lib/rush/remote.rb', line 85

def bash(command, user, background, reset_environment)
	transmit(:action => 'bash', :payload => command, :user => user, :background => background, :reset_environment => reset_environment)
end

#configObject



153
154
155
# File 'lib/rush/remote.rb', line 153

def config
	@config ||= Rush::Config.new
end

#copy(src, dst) ⇒ Object



45
46
47
# File 'lib/rush/remote.rb', line 45

def copy(src, dst)
	transmit(:action => 'copy', :src => src, :dst => dst)
end

#create_dir(full_path) ⇒ Object



37
38
39
# File 'lib/rush/remote.rb', line 37

def create_dir(full_path)
	transmit(:action => 'create_dir', :full_path => full_path)
end

#destroy(full_path) ⇒ Object



29
30
31
# File 'lib/rush/remote.rb', line 29

def destroy(full_path)
	transmit(:action => 'destroy', :full_path => full_path)
end

#ensure_tunnel(options = {}) ⇒ Object

Set up the tunnel if it is not already running.



140
141
142
# File 'lib/rush/remote.rb', line 140

def ensure_tunnel(options={})
	tunnel.ensure_tunnel(options)
end

#file_contents(full_path) ⇒ Object



25
26
27
# File 'lib/rush/remote.rb', line 25

def file_contents(full_path)
	transmit(:action => 'file_contents', :full_path => full_path)
end

#index(base_path, glob) ⇒ Object



57
58
59
# File 'lib/rush/remote.rb', line 57

def index(base_path, glob)
	transmit(:action => 'index', :base_path => base_path, :glob => glob).split("\n")
end

#kill_process(pid, options = {}) ⇒ Object



81
82
83
# File 'lib/rush/remote.rb', line 81

def kill_process(pid, options={})
	transmit(:action => 'kill_process', :pid => pid, :payload => YAML.dump(options))
end

#parse_exception(body) ⇒ Object

Parse an exception returned from the server, with the class name on the first line and the message on the second.



132
133
134
135
136
137
# File 'lib/rush/remote.rb', line 132

def parse_exception(body)
	klass, message = body.split("\n", 2)
	raise "invalid exception class: #{klass}" unless klass.match(/^Rush::[A-Za-z]+$/)
	klass = Object.module_eval(klass)
	[ klass, message.strip ]
end

#process_alive(pid) ⇒ Object



77
78
79
# File 'lib/rush/remote.rb', line 77

def process_alive(pid)
	transmit(:action => 'process_alive', :pid => pid)
end

#process_result(code, body) ⇒ Object

Take the http result of a transmit and raise an error, or return the body of the result when valid.



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rush/remote.rb', line 117

def process_result(code, body)
	raise Rush::NotAuthorized if code == "401"

	if code == "400"	
		klass, message = parse_exception(body)
		raise klass, "#{host}:#{message}"
	end

	raise Rush::FailedTransmit if code != "200"

	body
end

#processesObject



73
74
75
# File 'lib/rush/remote.rb', line 73

def processes
	YAML.load(transmit(:action => 'processes'))
end

#purge(full_path) ⇒ Object



33
34
35
# File 'lib/rush/remote.rb', line 33

def purge(full_path)
	transmit(:action => 'purge', :full_path => full_path)
end

#read_archive(full_path) ⇒ Object



49
50
51
# File 'lib/rush/remote.rb', line 49

def read_archive(full_path)
	transmit(:action => 'read_archive', :full_path => full_path)
end

#rename(path, name, new_name) ⇒ Object



41
42
43
# File 'lib/rush/remote.rb', line 41

def rename(path, name, new_name)
	transmit(:action => 'rename', :path => path, :name => name, :new_name => 'new_name')
end

#set_access(full_path, access) ⇒ Object



65
66
67
# File 'lib/rush/remote.rb', line 65

def set_access(full_path, access)
	transmit access.to_hash.merge(:action => 'set_access', :full_path => full_path)
end

#size(full_path) ⇒ Object



69
70
71
# File 'lib/rush/remote.rb', line 69

def size(full_path)
	transmit(:action => 'size', :full_path => full_path).to_i
end

#stat(full_path) ⇒ Object



61
62
63
# File 'lib/rush/remote.rb', line 61

def stat(full_path)
	YAML.load(transmit(:action => 'stat', :full_path => full_path))
end

#transmit(params) ⇒ Object

Given a hash of parameters (converted by the method call on the connection object), send it across the wire to the RushServer listening on the other side. Uses http basic auth, with credentials fetched from the Rush::Config.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rush/remote.rb', line 92

def transmit(params)
	ensure_tunnel

	require 'net/http'

	payload = params.delete(:payload)

	uri = "/?"
	params.each do |key, value|
		uri += "#{key}=#{value}&"
	end

	req = Net::HTTP::Post.new(uri)
	req.basic_auth config.credentials_user, config.credentials_password

	Net::HTTP.start(tunnel.host, tunnel.port) do |http|
		res = http.request(req, payload)
		process_result(res.code, res.body)
	end
rescue EOFError
	raise Rush::RushdNotRunning
end

#write_archive(archive, dir) ⇒ Object



53
54
55
# File 'lib/rush/remote.rb', line 53

def write_archive(archive, dir)
	transmit(:action => 'write_archive', :dir => dir, :payload => archive)
end

#write_file(full_path, contents) ⇒ Object



17
18
19
# File 'lib/rush/remote.rb', line 17

def write_file(full_path, contents)
	transmit(:action => 'write_file', :full_path => full_path, :payload => contents)
end