Skip to content

Instantly share code, notes, and snippets.

@adamhjk
Created March 23, 2010 22:03
Show Gist options
  • Save adamhjk/341736 to your computer and use it in GitHub Desktop.
Save adamhjk/341736 to your computer and use it in GitHub Desktop.
#
# cookbooks/ssh_authorized_key/resources/default.rb
#
def initialize(name, collection=nil, node=nil)
super(name, collection, node)
@action = :create
end
actions :create, :delete
attribute :key_type, :equal_to => [ :rsa, "rsa", :dsa, "dsa" ], :default => :rsa
attribute :key, :kind_of => String, :required => true
attribute :user, :kind_of => String, :required => true
attribute :target, :kind_of => String, :default => nil
# cookbooks/ssh_authorized_key/provider/default.rb
def find_authorized_keys_file
new_resource.target ? new_resource.target : "/tmp/#{new_resource.user}.ssh_authorized_keys"
end
action :create do
execute "create_authorized_key-#{new_resource.name]}" do
command "/bin/echo '#{new_resource.key_type]} #{new_resource.key}' >> '#{find_authorized_keys_file}'"
not_if "/bin/grep '#{params[:key]}' '#{find_authorized_keys_file}'"
end
end
action :delete do
execute "delete_authorized_key-#{new_resource.name}" do
command "/bin/sed -i '/#{new_resource.key}/d' '#{find_authorized_keys_file}'"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment