Select Page
NOTE: This is a static archive of an old blog, no interactions like search or categories are current.

I’ve had a pipe dream of creating something like IRB for ruby but tailored for mcollective, something with DDL assisted completion and all sorts of crazy kewl things.

Having looked a few times into this I concluded IRB is a black box of undocumented voodoo and always gave up. I had another google this weekend and came across what set me off in the right direction.

Christopher Burnett has a great little Posterous post up showing how to build custom IRB shells. With a little further digging I came across Bond from Gabriel Horner. These two combined into something that will definitely be one of my favorite toys.

The result is a mcollective IRB shell that you can grab in the ext directory of the mcollective tarball – it brings in some native gem dependencies that I really don’t want in the base deploy.

It’s best to see it in action before looking at the code so you know what the behavior is, see the screen cast below.

Getting the basic mc-irb going was pretty much exactly as Christopher’s posterous shows so I won’t go into the detail of that, what I do want to show is the DDL based command completion with Bond.

        require 'bond'
        Bond.start
 
        Bond.complete(:method => "rpc") do |e|
            begin
                if e.argument == 1
                    if e.arguments.last == "?"
                        puts "\n\nActions for #{@agent_name}:\n"
 
                        @agent.ddl.actions.each do |action|
                           puts "%20s - %s" % [ ":#{action}", @agent.ddl.action_interface(action)[:description] ] 
                        end
 
                        print "\n" + e.line
                    end
 
                    @agent.ddl.actions
 
                elsif e.argument > 1
                    action = eval(e.arguments[0]).to_s
                    ddl = @agent.ddl.action_interface(action)
 
                    if e.arguments.last == "?"
                        puts "\n\nArguments for #{action}:\n"
                        ddl[:input].keys.each do |input|
                            puts "%20s - %s" % [ ":#{input}", ddl[:input][input][:description] ]
                        end
 
                        print "\n" + e.line
                    end
 
                    ddl[:input].keys
                end
            rescue Exception 
                []
            end
        end

This code checks the argument count, handles the first and subsequent arguments differently and supports the ? special case by loading the DDL and displaying the right stuff.

I’ve not found much by way of complex examples on the Bond site or its own bundled completions, hopefully this helps someone.