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:
github.com/zenchild/capistrano_winrm

3 comments:

  1. hi..
    looks very nice.
    been working with cap for a while on rails,
    plus for java web apps. my system at the
    moment consists of java web (solr), mysql
    + asp.net mvc.

    on the windows + .net side.

    What do you do for uploads onto the server?
    The workflow i'd like is:

    upload a zip + powershell script
    run powershell script (on remote server).
    powershell would then unzip, install app etc.

    i've used scp with Cygwin but am not doing that
    anymore, it drove me insane.

    regards.

    ReplyDelete
  2. Hey Mick,

    I do not have a very good solution for transferring files today. There are a couple blog posts that talk about various ways of doing it via WinRM here:

    http://stage.vambenepe.com/archives/844
    http://stillpearling.blogspot.com/2009/02/more-powershell-remoting.html

    They're probably not exactly what you're looking for though. The best solution in my mind would be to get the powershell remoting protocol working over WinRM so one could use something like wget to fetch files to the remote computer. Something like this (http://poshcode.org/417). I'm still in the very early stages of figuring out powershell remoting though so that solution is a bit further down the road.

    Sorry I don't have a very good solution for you.

    Cheers,

    Dan

    ReplyDelete
  3. yeah, i've looked at various PS based ones. i always seem to get out-of-mem probs.
    i'll sure i'll find a solution. was just checking to make sure u didnt know anything obvious..
    cheers

    ReplyDelete