Class: Rush::Box
- Inherits:
-
Object
- Object
- Rush::Box
- 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
-
#host ⇒ Object
readonly
Returns the value of attribute host.
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
-
#[](key) ⇒ Object
Look up an entry on the filesystem, e.g.
-
#alive? ⇒ Boolean
Returns true if the box is responding to commands.
-
#bash(command, options = {}) ⇒ Object
Execute a command in the standard unix shell.
-
#command_with_environment(command, env) ⇒ Object
:nodoc:.
-
#connection ⇒ Object
:nodoc:.
-
#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.
-
#filesystem ⇒ Object
Access / on the box.
-
#initialize(host = 'localhost') ⇒ Box
constructor
Instantiate a box.
-
#inspect ⇒ Object
:nodoc:.
-
#make_connection ⇒ Object
:nodoc:.
-
#processes ⇒ Object
Get the list of processes running on the box, not unlike “ps aux” in bash.
-
#to_s ⇒ Object
:nodoc:.
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
#host ⇒ Object (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:
111 112 113 |
# File 'lib/rush/box.rb', line 111 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.
91 92 93 |
# File 'lib/rush/box.rb', line 91 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, ={}) cmd_with_env = command_with_environment(command, [:env]) [:reset_environment] ||= false if [:background] pid = connection.bash(cmd_with_env, [:user], true, [:reset_environment]) processes.find_by_pid(pid) else connection.bash(cmd_with_env, [:user], false, [:reset_environment]) end end |
#command_with_environment(command, env) ⇒ Object
:nodoc:
81 82 83 84 85 86 87 88 |
# File 'lib/rush/box.rb', line 81 def command_with_environment(command, env) # :nodoc: return command unless env vars = env.map do |key, value| "export #{key}=\"#{value.to_s.gsub('"', '\\"')}\"" end vars.push(command).join("\n") end |
#connection ⇒ Object
:nodoc:
103 104 105 |
# File 'lib/rush/box.rb', line 103 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.
99 100 101 |
# File 'lib/rush/box.rb', line 99 def establish_connection(={}) connection.ensure_tunnel() end |
#filesystem ⇒ Object
Access / on the box.
33 34 35 |
# File 'lib/rush/box.rb', line 33 def filesystem Rush::Entry.factory('/', self) end |
#inspect ⇒ Object
:nodoc:
28 29 30 |
# File 'lib/rush/box.rb', line 28 def inspect # :nodoc: host end |
#make_connection ⇒ Object
:nodoc:
107 108 109 |
# File 'lib/rush/box.rb', line 107 def make_connection # :nodoc: host == 'localhost' ? Rush::Connection::Local.new : Rush::Connection::Remote.new(host) end |
#processes ⇒ Object
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_s ⇒ Object
:nodoc:
24 25 26 |
# File 'lib/rush/box.rb', line 24 def to_s # :nodoc: host end |