Friday, September 12, 2014

Managing several API keys of CopperEgg in Chef cookbook

I've got an idea about how to manage API key while installing CopperEgg agent via chef.

The official instruction to install CopperEgg agent via Chef says the User API key is to be input in attribute files. But I think it would be more general and easier to manage several API keys in a cookbook by inputting the key in a node object file, for example when we own several CopperEgg account, several API keys, and want to manage the API keys not to create several cookbooks.

This is the official instruction and it says to replace USER_API_KEY in attribute files with your user API key.


The API key is coded in the attribute file after replaced.

$ cat cookbooks/copperegg/attributes/default.rb > /tmp/default.rb.$$ ; cat /tmp/default.rb.$$ | sed -r -e 's/YOUR_USER_API_KEY/<user_api_key>/' > cookbooks/copperegg/attributes/default.rb

$ grep '\[:copperegg\]\[:apikey\]' cookbooks/copperegg/attributes/default.rb
default[:copperegg][:apikey] = '<user_api_key>'

But we will be able to manage several API keys by inputting the API key in a node object files.

$ cat nodes/<node>.json 
{
  "copperegg": {
    "apikey": "<user_api_key>"
  },
  "run_list" : [
     "recipe[copperegg]"
  ]
}

Please let me know if you have any better idea.