class Rush::Box
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
Attributes
Public Class Methods
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')).
# File lib/rush/box.rb, line 20 def initialize(host='localhost', local_path = nil) @host = host @local_path = local_path end
Public Instance Methods
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.
# File lib/rush/box.rb, line 45 def [](key) filesystem[key] end
Returns true if the box is responding to commands.
# File lib/rush/box.rb, line 99 def alive? connection.alive? end
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
# File lib/rush/box.rb, line 78 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
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.
# File lib/rush/box.rb, line 107 def establish_connection(options={}) connection.ensure_tunnel(options) end
Access / on the box.
# File lib/rush/box.rb, line 34 def filesystem if host == 'localhost' Rush::Entry.factory('/', self) else connection.local_path end end
Guess if method missing then it's command for folder binded to that box.
# File lib/rush/box.rb, line 59 def method_missing(meth, *args, &block) filesystem.send(meth, *args, &block) end
Get the list of processes running on the box, not unlike “ps aux” in bash. Returns a Rush::ProcessSet
.
# File lib/rush/box.rb, line 51 def processes Rush::ProcessSet.new( connection.processes.map { |ps| Rush::Process.new(ps, self) } ) end
Print last backtrace
# File lib/rush/box.rb, line 126 def wtf t = $last_backtrace t.lines.count > 5 ? t.less : puts(t) end