BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Rush - OOP shell in Ruby

Rush - OOP shell in Ruby

This item in japanese

Rush - the Ruby shell - is an object oriented shell using the Ruby syntax. Object oriented means that you don't just manipulate and pass around strings but real Ruby objects with their own methods and attributes. So instead of ls -l /var, one can call the ls method of a directory object:

rush> root['/var/'].ls
 //var/
 db+
 lib+
 log+
 run+
 [...]

 Rush is supposed to be a replacement for the most commonly used built-in commands and core-utilities of common Unix environments, with all the benefits of an object oriented programming language. But there's even more:

But rush is more than just an interactive shell and a library: it can also control any number of remote machines from a single location. Copy files or directories between servers as seamlessly as if it was all local.

We talked to Adam Wiggins, the developer of rush. Adam started working on it for his own needs: 

I've wanted a Ruby-syntax replacement for the unix shell from almost the moment I began using Ruby. Whenever I can, I write shell scripts as Ruby scripts with lots of backticks.

Object oriented shells aren't really a new idea, for instance Microsoft already ships its new .NET based Windows PowerShell with Windows Server 2008, which also served as an inspiration to create rush.

Ruby's powerful meta-programming abilities make extending rush quite easy. For example, lets extend Box (the representation of a single machine) with a method that returns the IP address. Customization happens in the .rush/env.rb file, so we can simply add our new method there:

class Rush::Box
 def ip_address
 bash("ifconfig | grep inet | grep -v 127.0.0.1").match(/addr:([\d.]+)/)[1]
 end
end

Back in rush, the following should now print your local IP address:

rush> Rush::Box.new.ip_address
 192.168.1.104

Wiggins already has some ideas on how to make other administration tasks like starting and stopping of services, database creation or iptables rules manipulation easier:

[...] netstat -lptn is awesome for figuring out what process is hogging a particular port, but it's difficult to combine it with other operations. In rush, you should be able to do something like: box.processes.each { |p| p.kill if p.listen_port == 3000 }

And about rush's long-term goals:

Reimplementing the thousands of commands that live in /usr/bin (plus all the bash builtins) might seem like a daunting task. But I suspect that the vast majority of these are rarely used. A small subset, implemented well, could cover 80% of what I and other unixheads want to do - particularly in the realm of cluster management, which is what rush was really created for. rush's libraries are currently about 1500 lines of code, and already it covers most of the major file and process operations. I'm thinking it should be possible to cover almost everything I ever want to do in managing a unix system in around 10k lines of code.

For more on rush, check rush's official website or join the rush Google Group. Note that the ruSH project on RubyForge is a different project and doesn't seem to be alive anymore.

Rate this Article

Adoption
Style

BT