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.
-
#local_path ⇒ Object
readonly
Returns the value of attribute local_path.
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', local_path = nil) ⇒ Box
constructor
Instantiate a box.
-
#inspect ⇒ Object
:nodoc:.
-
#make_connection ⇒ Object
:nodoc:.
-
#method_missing(meth, *args, &block) ⇒ Object
Guess if method missing then it’s command for folder binded to that box.
-
#processes ⇒ Object
Get the list of processes running on the box, not unlike “ps aux” in bash.
-
#to_s ⇒ Object
:nodoc:.
-
#wtf ⇒ Object
Print last backtrace.
Constructor Details
#initialize(host = 'localhost', local_path = nil) ⇒ 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 23 |
# File 'lib/rush/box.rb', line 20 def initialize(host='localhost', local_path = nil) @host = host @local_path = local_path end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Guess if method missing then it’s command for folder binded to that box.
59 60 61 |
# File 'lib/rush/box.rb', line 59 def method_missing(meth, *args, &block) filesystem.send(meth, *args, &block) 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 |
#local_path ⇒ Object (readonly)
Returns the value of attribute local_path.
14 15 16 |
# File 'lib/rush/box.rb', line 14 def local_path @local_path end |
Instance Method Details
#==(other) ⇒ Object
:nodoc:
121 122 123 |
# File 'lib/rush/box.rb', line 121 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.
45 46 47 |
# File 'lib/rush/box.rb', line 45 def [](key) filesystem[key] end |
#alive? ⇒ Boolean
Returns true if the box is responding to commands.
99 100 101 |
# File 'lib/rush/box.rb', line 99 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
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rush/box.rb', line 78 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:
90 91 92 93 94 95 96 |
# File 'lib/rush/box.rb', line 90 def command_with_environment(command, env) # :nodoc: return command unless env env.map do |key, value| escaped = value.to_s.gsub('"', '\\"').gsub('`', '\\\`') "export #{key}=\"#{escaped}\"" end.push(command).join("\n") end |
#connection ⇒ Object
:nodoc:
111 112 113 |
# File 'lib/rush/box.rb', line 111 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.
107 108 109 |
# File 'lib/rush/box.rb', line 107 def establish_connection(={}) connection.ensure_tunnel() end |
#filesystem ⇒ Object
Access / on the box.
34 35 36 37 38 39 40 |
# File 'lib/rush/box.rb', line 34 def filesystem if host == 'localhost' Rush::Entry.factory('/', self) else connection.local_path end end |
#inspect ⇒ Object
:nodoc:
29 30 31 |
# File 'lib/rush/box.rb', line 29 def inspect # :nodoc: host end |
#make_connection ⇒ Object
:nodoc:
115 116 117 118 119 |
# File 'lib/rush/box.rb', line 115 def make_connection # :nodoc: host == 'localhost' ? Rush::Connection::Local.new : Rush::Connection::Remote.new(host, local_path) end |
#processes ⇒ Object
Get the list of processes running on the box, not unlike “ps aux” in bash. Returns a Rush::ProcessSet.
51 52 53 54 55 |
# File 'lib/rush/box.rb', line 51 def processes Rush::ProcessSet.new( connection.processes.map { |ps| Rush::Process.new(ps, self) } ) end |
#to_s ⇒ Object
:nodoc:
25 26 27 |
# File 'lib/rush/box.rb', line 25 def to_s # :nodoc: host end |
#wtf ⇒ Object
Print last backtrace
126 127 128 129 |
# File 'lib/rush/box.rb', line 126 def wtf t = $last_backtrace t.lines.count > 5 ? t.less : puts(t) end |