Class: Rush::Box

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

Overview

A rush box is a single unix machine - a server, workstation, or VPS instance.

Specify a box by hostname (default = ‘localhost’). If the box is remote, the first action performed will attempt to open an ssh tunnel. Use square brackets to access the filesystem, or processes to access the process list.

Example:

local = Rush::Box.new
local['/etc/hosts'].contents
local.processes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost') ⇒ Box

Instantiate a box. No action is taken to make a connection until you try to perform an action. If the box is remote, an ssh tunnel will be opened. Specify a username with the host if the remote ssh user is different from the local one (e.g. Rush::Box.new(‘user@host’)).



20
21
22
# File 'lib/rush/box.rb', line 20

def initialize(host='localhost')
	@host = host
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/rush/box.rb', line 14

def host
  @host
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



112
113
114
# File 'lib/rush/box.rb', line 112

def ==(other)          # :nodoc:
	host == other.host
end

#[](key) ⇒ Object

Look up an entry on the filesystem, e.g. box. Returns a subclass of Rush::Entry - either Rush::Dir if you specifiy trailing slash, or Rush::File otherwise.



40
41
42
# File 'lib/rush/box.rb', line 40

def [](key)
	filesystem[key]
end

#alive?Boolean

Returns true if the box is responding to commands.

Returns:

  • (Boolean)


92
93
94
# File 'lib/rush/box.rb', line 92

def alive?
	connection.alive?
end

#bash(command, options = {}) ⇒ Object

Execute a command in the standard unix shell. Returns the contents of stdout if successful, or raises Rush::BashFailed with the output of stderr if the shell returned a non-zero value. Options:

:user => unix username to become via sudo :env => hash of environment variables :background => run in the background (returns Rush::Process instead of stdout)

Examples:

box.bash '/etc/init.d/mysql restart', :user => 'root'
box.bash 'rake db:migrate', :user => 'www', :env => { :RAILS_ENV => 'production' }
box.bash 'mongrel_rails start', :background => true
box.bash 'rake db:migrate', :user => 'www', :env => { :RAILS_ENV => 'production' }, :reset_environment => true


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rush/box.rb', line 69

def bash(command, options={})
	cmd_with_env = command_with_environment(command, options[:env])
	options[:reset_environment] ||= false

	if options[:background]
		pid = connection.bash(cmd_with_env, options[:user], true, options[:reset_environment])
		processes.find_by_pid(pid)
	else
		connection.bash(cmd_with_env, options[:user], false, options[:reset_environment])
	end
end

#command_with_environment(command, env) ⇒ Object

:nodoc:



81
82
83
84
85
86
87
88
89
# File 'lib/rush/box.rb', line 81

def command_with_environment(command, env)   # :nodoc:
	return command unless env

	vars = env.map do |key, value|
		escaped = value.to_s.gsub('"', '\\"').gsub('`', '\\\`')
		"export #{key}=\"#{escaped}\""
	end
	vars.push(command).join("\n")
end

#connectionObject

:nodoc:



104
105
106
# File 'lib/rush/box.rb', line 104

def connection         # :nodoc:
	@connection ||= make_connection
end

#establish_connection(options = {}) ⇒ Object

This is called automatically the first time an action is invoked, but you may wish to call it manually ahead of time in order to have the tunnel already set up and running. You can also use this to pass a timeout option, either :timeout => (seconds) or :timeout => :infinite.



100
101
102
# File 'lib/rush/box.rb', line 100

def establish_connection(options={})
	connection.ensure_tunnel(options)
end

#filesystemObject

Access / on the box.



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

def filesystem
	Rush::Entry.factory('/', self)
end

#inspectObject

:nodoc:



28
29
30
# File 'lib/rush/box.rb', line 28

def inspect     # :nodoc:
	host
end

#make_connectionObject

:nodoc:



108
109
110
# File 'lib/rush/box.rb', line 108

def make_connection    # :nodoc:
	host == 'localhost' ? Rush::Connection::Local.new : Rush::Connection::Remote.new(host)
end

#processesObject

Get the list of processes running on the box, not unlike “ps aux” in bash. Returns a Rush::ProcessSet.



46
47
48
49
50
51
52
# File 'lib/rush/box.rb', line 46

def processes
	Rush::ProcessSet.new(
		connection.processes.map do |ps|
			Rush::Process.new(ps, self)
		end
	)
end

#to_sObject

:nodoc:



24
25
26
# File 'lib/rush/box.rb', line 24

def to_s        # :nodoc:
	host
end