Friday, September 24, 2010

Connecting to Windows from Capistrano with WinRM

After developing the WinRM library for Ruby I thought about what uses it may have. One thing I had longed to put to use in the enterprise is a tool like Capistrano. It has long worked well in the UNIX world where every system really should be running SSH, but installing SSH on every MS Windows system enterprise-wide really isn't realistic in many cases. That's when I began to experiment in integrating WinRM into Capistrano. The goals were simple:

  1. Do not require the user to patch Capistrano.
  2. Make the usage of WinRM in the Capfile as seamless as possible.

To accomplish what I wanted I had to override some methods within Capistrano to inject WinRM functionality. The end result adds a winrm command that should work almost identical to Capistrano's own run command. It should also allow your current Capfile to run unmodified if you do not have any winrm tasks.

To define a WinRM task you can simply do this:

winrm also supports using blocks just like run so you can format your output a bit better like so:

There you have it. As long is WinRM is configured on the remote Windows systems you can now manage them with your Capistrano infrastructure. There is a gem up on Rubygems that adds in the functionality so just do a:
gem install -r capistrano_winrm

The source code for this extension is also up on Github: