A command within the context of an ssh-agent session

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

Thoughts?

Cheers,

Graeme.

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

Follow us on Twitter: http://twitter.com/rubaidh and <http://twitter.com/mathie

Web Site: http://rubaidh.com/
Blog: http://woss.name/
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.

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). :wink: 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. :slight_smile:

Thoughts?

Cheers,

Graeme.

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

Follow us on Twitter: http://twitter.com/rubaidh and <http://twitter.com/mathie

Web Site: http://rubaidh.com/
Blog: http://woss.name/
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

Follow us on Twitter: http://twitter.com/rubaidh and <http://twitter.com/mathie

Web Site: http://rubaidh.com/
Blog: http://woss.name/
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.

On Wed, Jul 29, 2009 at 7:13 AM, Graeme Mathiesonmathie@woss.name wrote:

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). :wink: 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
)

Sorry it took so long for me to follow this up - yeah, this looks
reasonable. Another way to accomplish this would be with definitions

  • you could basically just pass all the arguments to execute directly,
    and if you have the 'key' param, munge the command in advance.

Adam

--
Opscode, Inc.
Adam Jacob, CTO
T: (206) 508-7449 E: adam@opscode.com

Hi,

On 4 Aug 2009, at 18:32, Adam Jacob wrote:

Another way to accomplish this would be with definitions

  • you could basically just pass all the arguments to execute directly,
    and if you have the 'key' param, munge the command in advance.

I did try that at first. For the life of me, I can't remember why it
didn't work now. I'll have a wee look and see if git log reminds
me. :slight_smile:

G

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

Follow us on Twitter: http://twitter.com/rubaidh and <http://twitter.com/mathie

Web Site: http://rubaidh.com/
Blog: http://woss.name/
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.