Skip to content

Instantly share code, notes, and snippets.

@ctgswallow
Created January 24, 2013 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctgswallow/4629288 to your computer and use it in GitHub Desktop.
Save ctgswallow/4629288 to your computer and use it in GitHub Desktop.
actions :create
attribute :user, :kind_of => String, :name_attribute => true, :required => true
attribute :type, :kind_of => String, :default => "rsa", :equal_to => ["rsa", "dsa"]
attribute :length, :kind_of => String, :default => "2048", :equal_to => [ "1024", "2048", "4096" ]
action :create do
gem_package "sshkey" do
action :install
notifies :create, "ruby_block[clear gems]", :immediately
end
ruby_block "clear gems" do
block do
Gem.clear_paths
end
end
require 'sshkey'
require 'etc'
homedir = Etc::getpwuid( Etc::getpwnam(new_resource.user).uid ).dir
desc = Etc::getpwnam(new_resource.user).gecos
ruby_block "create ssh key" do
block do
k = SSHkey.generate(:type => new_resource.type, :bits => new_resource.length, :comment => desc)
node.set[:openssh][:pubkeys]["#{new_resource.user}"] = k.public_key
# Much of the chef DSL goes away in a ruby block; here's how
# to create a template from scratch.
rc = Chef::RunContext.new(node, node.cookbook_collection)
t = Chef::Resource::Template.new "#{homedir}/.ssh/id_#{new_resource.type}"
t.source("key.erb")
t.owner(new_resource.user)
t.group(new_resource.user)
t.cookbook("openssh")
t.mode("0600")
t.variables(
:k => k.private_key
)
t.action(:create_if_missing)
t.run_context=(rc)
t.run_action("create_if_missing")
new_resource.updated_by_last_action(true)
end
not_if { File.exists?("#{homedir}/.ssh/id_#{new_resource.type}") }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment