Skip to content

Commit

Permalink
[COOK-1077] - error handling if data bag is missing
Browse files Browse the repository at this point in the history
* Use begin/rescue in case the data bag does not [yet] exist.
* Convert the item to a hash instead of a data_bag_item.
  • Loading branch information
jtimberman committed Mar 8, 2012
1 parent 130baa9 commit 2727bcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
10 changes: 1 addition & 9 deletions ssh_known_hosts/README.md
Expand Up @@ -22,7 +22,7 @@ Adding custom host keys
-----------------------

If you want to add custom host keys for hosts not in your Chef deployment (such
as github.com, for example), create a data bag called "ssh_known_hosts" and add
as github.com, for example), create a data bag called "`ssh_known_hosts`" and add
an item for each host to it that looks like this:

{
Expand All @@ -37,14 +37,6 @@ You can also specify the following optional values in the data bag:
* hostname : Short hostname form of the host without domain name
* dsa : If the host has a dsa host key, specify it as "dsa" instead of "rsa"

Changes
=======

## v0.4.0:

* COOK-493: include fqdn
* COOK-721: corrected permissions

License and Author
==================

Expand Down
31 changes: 19 additions & 12 deletions ssh_known_hosts/recipes/default.rb
Expand Up @@ -23,18 +23,25 @@
nodes = search(:node, "keys_ssh:* NOT name:#{node.name}")
nodes << node

other_hosts = data_bag('ssh_known_hosts')
require 'resolv'
r = Resolv.new
other_hosts.each do |h|
host = data_bag_item('ssh_known_hosts', h)
host['ipaddress'] ||= r.getaddress(host['fqdn'])
host['keys'] = {
'ssh' => {}
}
host['keys']['ssh']['host_rsa_public'] = host['rsa'] if host.has_key?('rsa')
host['keys']['ssh']['host_dsa_public'] = host['dsa'] if host.has_key?('dsa')
nodes << host
begin
other_hosts = data_bag('ssh_known_hosts')
rescue
Chef::Log.info("Could not load data bag 'ssh_known_hosts', this is optional, moving on...")
end

if other_hosts
require 'resolv'
r = Resolv.new
other_hosts.each do |h|
host = data_bag_item('ssh_known_hosts', h).to_hash
host['ipaddress'] ||= r.getaddress(host['fqdn'])
host['keys'] = {
'ssh' => {}
}
host['keys']['ssh']['host_rsa_public'] = host['rsa'] if host.has_key?('rsa')
host['keys']['ssh']['host_dsa_public'] = host['dsa'] if host.has_key?('dsa')
nodes << host
end
end

template "/etc/ssh/ssh_known_hosts" do
Expand Down

0 comments on commit 2727bcf

Please sign in to comment.