0
votes

I am running a Chef 12 Opsworks stack and I created a custom cookbook to take some backups of a few folders on my webserver.

Maybe my approach to this is wrong, but I basically have two (or more) recipes for each website I want backed up and then I'm updating some attributes (site name, backup folder, etc) in each recipe.

So I start with the following in my default.rb file in the attributes folder:

default['backup']['site'] = "SITE1"
default['backup']['root'] = "/var/www/SITE1"

Then in each backup recipe I have the following at the top of the recipe followed by my back up code:

site1.rb

node.override['backup']['site'] = "SITE1"
node.override['backup']['root'] = "/var/www/SITE1"

site2.rb

node.override['backup']['site'] = "SITE2"
node.override['backup']['root'] = "/var/www/SITE2"

Now in my Setup step on the Opsworks Layer I add all the backup recipes, but the problem arises when I start an instance (or run the Setup step from the Deployments) because the attributes seem to be set to whatever the last recipe sets them in alphabetical order.

So SITE1 backup script for example will end up being built with the /var/www/SITE2 root folder in its config and thus not backing up the right site.

Is there are way to prevent this from happening? From what I gather (from my example and reading Chef docs) the attributes are all compiled together at the beginning and then the recipes are being run - which is why the last set of attributes gets set as the final version and then all recipes using those attributes will get those values.

The only way I can deploy them at the moment is by running each recipe independently thus using the correct attribute values, but the moment the instance gets rebooted or the Setup step is ran manually all backup scripts will go back to backing up just one site.

Is my approach to this wrong? Should I be creating separate named attributes for each recipe?

1

1 Answers

0
votes

I ended up fixing this by slightly modifying my approach to using attributes upon reading more about the 'chef run' process.

I am still using attributes for the actual 'common' attributes between all recipes, but then in each recipe instead of overriding the recipe specific attributes I replaced them with local ruby variables which then get passed to my 'template' via the 'variables' property.

So my site1.rb recipe for example looks the following way:

site = "SITE1"
root = "/var/www/SITE1"
...
config = {
   :site => site,
   :root => root,
   ...
}
...
template "/path/to/config" do
   source "config.erb"
   variables(
     :config => config
   )
   ...
end

This way I get to keep the same variable names inside my config template yet have each backup recipe use its own custom variables without interfering with other recipes.