Net::SSH::Multi + Rake == Tasty Potential
Last night I released the first preview of Net::SSH::Multi (gem install --source http://gems.jamisbuck.org net-ssh-multi
). Today, let me show you a tasty hint of what you can do with it.
Consider the following Rakefile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
def remote @remote ||= begin require 'net/ssh/multi' session = Net::SSH::Multi.start session.via 'gateway.host', session.default_user session.group :web => session.use('web1', 'web2') session.group :app => session.use(*(1..8).map { |n| "app%02d" % n }) session.group :db => session.use('db1', :properties => { :primary => true }) session end end namespace :remote do task :hostnames do remote.exec("hostname").wait end task :app_hostnames do remote.with(:app).exec("hostname").wait end task :web_hostnames do remote.with(:web).exec("hostname").wait end end |
You can now do things like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ rake remote:hostnames (in /home/jamis) [web1] web1.host [app02] app02.host [app05] app05.host [app03] app03.host [app06] app06.host [app08] app08.host [db1] db1.host [app01] app01.host [web2] web2.host [app04] app04.host [app07] app07.host |
The Net::SSH::Multi library is still experimental, but it is stable and full-featured enough that I seriously considered implementing the next release of Capistrano on top of it. (I’ll probably put that off until cap3, though, due to the magnitude of the change.) If you do something cool with it, let me know!
Reader Comments
Hey Jamis, that’s cool stuff. I like the idea of using Rake tasks for doing stuff remotely. For the sake of my curiosity, could you explain the benefits/differences between this and the way that Vlad does it? Is there some benefit that Net::SSH::Multi provides that just wrapping the “ssh” command doesn’t? Thanks!
11 Apr 2008
@Scott, I’ve never done more than glance at vlad, so I’m probably the wrong person to ask. Doesn’t shelling out to ssh mean you have to reconnect for every command you want to execute? Net::SSH::Multi opens the connection and caches it, so if you are executing lots of commands in a row you might see better performance. Also, I don’t think vlad works on windows yet…? (As I said, I’m way out of the loop regarding vlad, so that might not be true any more). Net::SSH::Multi is platform independent, as long as OpenSSL and the OpenSSL ruby bindings are installed. At any rate, this isn’t out to replace vlad, or cap. It’s another tool for people to use, is all.
11 Apr 2008
I’m not terribly familiar with Vlad’s internals either, so I don’t know about the connection-caching question. Anyhow, thanks again for the great work on this stuff.
11 Apr 2008
Seems like a perfect match for glTail as well. Guess I should start preparing for the next release.
11 Apr 2008
Nice post, I will try this solution.
12 Apr 2008