Skip to content

Instantly share code, notes, and snippets.

@woods
Created December 5, 2012 19:01
Show Gist options
  • Save woods/4218510 to your computer and use it in GitHub Desktop.
Save woods/4218510 to your computer and use it in GitHub Desktop.
The script that we use to clean up old capistrano releases
#!/opt/ruby/bin/ruby
# This script will clean out old capistrano releases from a set of
# subdirectories, but will keep a minimum number. It's designed to be run as
# a cron job.
#
# (c) Copyright 2008 West Arete Computing, Inc.
require 'thread'
require 'fileutils'
require 'rubygems'
gem 'activesupport', '2.3.5'
require 'active_support'
# Return the same format for the given time object that capistrano uses to
# name its release directories.
def release_name(time)
time.strftime("%Y%m%d%H%M%S")
end
# Returns true if the given filename looks like a release.
def is_release?(filename)
filename =~ /^\d{14}$/
end
# Returns true if the given release directory looks too old to keep.
def is_old?(filename)
filename < release_name(1.month.ago)
end
# Clean out all the old releases, keeping a handful of recent ones.
def clean_up_old_releases
Dir.glob("/var/www/domains/*/releases").each do |release_dir|
# Get all the entries that look like a release
releases = Dir.entries(release_dir).select { |f| is_release?(f) }
# Always keep a few of the most recent releases
releases -= releases.sort.last(5)
# Remove any that are getting old
releases.select { |release| is_old?(release) }.each do |release|
full_path = File.join(release_dir, release)
puts "Removing #{full_path}"
FileUtils.remove_dir(full_path, true)
end
end
end
# Actually perform the cleanup if this script is being run directly.
clean_up_old_releases if File.basename($0) == File.basename(__FILE__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment