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', 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

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#local_pathObject (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.

Returns:

  • (Boolean)


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, 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:



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

#connectionObject

: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(options={})
  connection.ensure_tunnel(options)
end

#filesystemObject

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

#inspectObject

:nodoc:



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

def inspect     # :nodoc:
  host
end

#make_connectionObject

: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

#processesObject

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_sObject

:nodoc:



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

def to_s        # :nodoc:
  host
end

#wtfObject

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