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)


142
143
144
145
146
147
# File 'lib/rush/remote.rb', line 142

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

#bash(command, user, background) ⇒ Object



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

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

#configObject



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

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

#copy(src, dst) ⇒ Object



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

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

#create_dir(full_path) ⇒ Object



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

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

#destroy(full_path) ⇒ Object



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

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.



136
137
138
# File 'lib/rush/remote.rb', line 136

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

#file_contents(full_path) ⇒ Object



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

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

#index(base_path, glob) ⇒ Object



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

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

#kill_process(pid) ⇒ Object



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

def kill_process(pid)
	transmit(:action => 'kill_process', :pid => pid)
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.



128
129
130
131
132
133
# File 'lib/rush/remote.rb', line 128

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



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

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.



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rush/remote.rb', line 113

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



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

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

#purge(full_path) ⇒ Object



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

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

#read_archive(full_path) ⇒ Object



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

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

#rename(path, name, new_name) ⇒ Object



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

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

#set_access(full_path, access) ⇒ Object



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

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

#size(full_path) ⇒ Object



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

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

#stat(full_path) ⇒ Object



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

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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rush/remote.rb', line 88

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



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

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