[chef] A command within the context of an ssh-agent session


Chronological Thread 
  • From: Graeme Mathieson <mathie@woss.name>
  • To: chef@lists.opscode.com
  • Subject: [chef] A command within the context of an ssh-agent session
  • Date: Wed, 29 Jul 2009 14:17:33 +0100

Hi,

I've just thrown together a customised version of the execute resource, called execute_with_key, and, while it works, I'd like to know if I'm tackling it the right way, or how else I should be doing it?  Basic premise: it's exactly the same as "execute", but it takes an extra argument, called "key", which points to the ssh key the command would like to have available when it runs.  It will then run the command inside an ssh-agent, add that key, then run the command.  Here's my attempt:

class Chef
  class Provider
    class ExecuteWithKey < Execute
      def action_run
        if @new_resource.key
          @new_resource.command = "ssh-agent bash -c \'ssh-add #{@new_resource.key}; #{@new_resource.command}\'"
        end

        super
      end
    end
  end

  class Resource
    class ExecuteWithKey < Execute
      def initialize(name, collection = nil, node = nil)
        super
        @key = nil
      end

      def key(arg = nil)
        set_or_return(:key, arg, :kind_of => [ String ])
      end
    end
  end
end

Couple of things:

* I ought to be escaping single quotes in the command that's passed in to avoid trouble; I'll get to that shortly.

* Have I tackled it in the right way? I'm still a little hazy on the Provider/Resource split, so I'm wondering if I've put things in the wrong place?

* Have I managed to change the semantics of command in this subclass? That would be suboptimal because it's meant to behave identically other than the ssh-agent support. :-)

Thoughts?

Cheers,

Graeme.
--
Graeme Mathieson
Managing Director
Rubaidh Ltd: Scottish for Ruby on Rails

Telephone: +44 (0)131 273 5271
Mobile: +44 (0)7949 0777 44

Rubaidh Ltd is a limited company registered in Scotland with registration number SC297029 and VAT number GB 916 0341 53. The registered address is: Stuart House, Eskmills, Musselburgh, East Lothian, EH21 7PB, United Kingdom.




Archive powered by MHonArc 2.6.16.

§