LWRP for deleting old builds

In your cookbook, in libraries folder, create the following:


class BuildCacheCleaner
  # Provider for build_cache_cleaner
  class BuildCacheCleanerProvider < Chef::Provider::LWRPBase
    provides :build_cache_cleaner
    action :delete do
      delete_count = 0
      if ::File.directory?(new_resource.cache_path)
        search_path = ::File.join(new_resource.cache_path.to_path, “#{new_resource.suffix}*\.#{new_resource.extension}”)
        cached_files = ::Dir.glob(search_path).sort_by { |f| ::File.ctime(f) }
        if cached_files.count > new_resource.keep_count
          delete_count = cached_files.count – new_resource.keep_count
        files_to_delete = cached_files.first(delete_count)
        unless files_to_delete.empty?
          Chef::Log.info(“#{delete_count} cached builds will be deleted !”)
          files_to_delete.each do |file_name|
            Chef::Log.info(“Deleting file #{::File.basename(file_name)} …”)
      new_resource.updated_by_last_action(true) if delete_count > 0


class BuildCacheCleaner
  # Resource for build_cache_cleaner
  class BuildCacheCleanerResource < Chef::Resource::LWRPBase
    resource_name :build_cache_cleaner
    provides :build_cache_cleaner


    attribute :keep_count, :kind_of => Integer, :required => false, :default => 5
    attribute :suffix, :kind_of => String, :required => false, :default => ‘Build’
    attribute :extension, :kind_of => String, :required => false, :default => ‘zip’
    attribute :cache_path, :kind_of => String, :required => false, :default => Chef::Config[:file_cache_path]


# String utility extension methods
class String
  def to_path(end_slash = false)
    “#{‘/’ if self[0] == ‘\\’}#{split(‘\\’).join(‘/’)}#{‘/’ if end_slash}”


# ChefSpec is a tool to unit test cookbooks in conjunction with rspec
# Learn more on the README or at https://github.com/sethvargo/chefspec.
if defined?(ChefSpec)
  def delete_build_cache_cleaner(resource_name)
    ChefSpec::Matchers::ResourceMatcher.new(:build_cache_cleaner, :delete, resource_name)

In your recipe, use the resource as follow:

build_cache_cleaner ‘delete cached builds’ do
  keep_count 7

In your ChefSpec test, add the following:

it ‘deletes unused cached builds’ do
  expect(chef_run).to delete_build_cache_cleaner(‘delete cached builds’)

Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.
Note 3: While I may have tested myself most of the bits of code posted here, please be aware that you’re trying the code/scripts at your own risk, and I take no responsibility for any damage that may occur on your system(s).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: