Skip to content

Instantly share code, notes, and snippets.

@stanchan
Created August 19, 2015 18:17
Show Gist options
  • Save stanchan/d7373cbbfa65546f298a to your computer and use it in GitHub Desktop.
Save stanchan/d7373cbbfa65546f298a to your computer and use it in GitHub Desktop.
Upload data bag
ruby_block "upload-pubkey" do
block do
cert_contents = File.read("/etc/chef/client.pem")
raw_data = { "server" => cert_contents, "loaded" => false }
begin
tls_keys_data_bag = Chef::DataBag.load("tls_keys")
rescue Net::HTTPServerException => e
if e.response.code == "404" then
Chef::Log.error("Creating a new tls_keys data bag")
tls_keys_data_bag = Chef::DataBag.new
tls_keys_data_bag.name("tls_keys")
tls_keys_data_bag.save
end
end
begin
tls_keys = Chef::DataBagItem.load("tls_keys", node["hostname"])
rescue Net::HTTPServerException => e
if e.response.code == "404" then
Chef::Log.error("Creating a new tls_keys data bag item for #{node["hostname"]}")
tls_keys = Chef::DataBagItem.new
tls_keys.data_bag("tls_keys")
tls_keys["id"] = node["hostname"]
end
end
tls_keys["certificate"] = raw_data
tls_keys.save
end
action :run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment