[chef] Re: 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] Re: A command within the context of an ssh-agent session
  • Date: Wed, 29 Jul 2009 15:13:45 +0100

Hi,

OK, the observant among you will have noticed that this couldn't possibly work because I haven't even registered the provider, never mind told it which provider to use by default (through @resource_name). ;-)  Here's another attempt and this one really does appear to be working.  Feedback would be much appreciated!

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
        @resource_name = :execute_with_key
        @key = nil
      end

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

Chef::Platform.set(
  :resource => :execute_with_key,
  :provider => Chef::Provider::ExecuteWithKey
)

Cheers,

Graeme.

On 29 Jul 2009, at 14:17, Graeme Mathieson wrote:

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.


--
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.

§