Search code examples
chef-infrachef-recipedevopschef-soloconfiguration-management

How do people manage leftover or stranded resources in Chef?


Is there a clean way to manage stranded resources through cookbook modifications? For instance, let's say I have a recipe resource that writes a file from a template like so:

template 'C:\dir\somefile.txt' do
    source 'somefile.erb'
end

And then I update the cookbook to write that file to a different place like so:

template 'C:\some_different_dir\new_file_location.txt' do
    source 'sample.erb'
end

The file from the original recipe doesn't get cleaned up automatically does it? So at that point C:\dir\somefile.txt is just stranded out there not being used.

The reason I care is because our app demands a lot of resources and I don't want a lot of disk drive space taken up over time with cookbook updates. I also don't like stranded files placed about the server because I don't want something to accidentally get picked up by one of our applications and get executed. The app I'm working on has a bunch of different services running all over the place and plus we have contracts with partners and other organizations concerning left over files and source code.

Also we can't easily move into a direction where we spin up fresh new servers when our app updates using something like Vagrant or Terraform. Once our servers are spun up, they are there for a long time. Some of them aren't even VMs.

Thanks for any help.


Solution

  • You would have to add a new resource to clean up the old file:

    file 'C:\\dir\\somefile.txt' do
      action :delete
    end
    

    Once you are sure every server has cleaned up the file you can remove that from the recipe, or just leave it. You can also check out the zap cookbook, which attempts to do automated cleanups but this is really hard to do in a stable way, so it comes with some "gotchas", mostly related to nested execution contexts and custom resources.