vigetlabs / viget_deployment

Capistrano recipes and strategies

This URL has Read+Write access

viget_deployment / recipes / cleanup.rb
100644 26 lines (25 sloc) 1.256 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require 'rubygems'
require 'active_support'
 
namespace :deploy do
  desc <<-DESC
Clean up releases from before a given time. By default, this time is midnight \
on the current day, but can be changed with the purge_before_time variable. \
Note that because releases are timestamped in UTC, this task calculates the \
threshold in UTC as well. This task will abort without removing anything if \
all of the releases on the server fall below the threshold.
DESC
  task :cleanup_before_time, :except => { :no_release => true } do
    threshold = fetch(:purge_before_time, Time.now.midnight).utc
    logger.info "considering removing releases from before #{threshold}"
    old_releases = releases.select{|r| r.to_i < threshold.strftime("%Y%m%d%H%M%S").to_i}
    if old_releases.empty?
      logger.important "no old releases to clean up"
    elsif old_releases.length == releases.length
      logger.important "refusing to proceed, as doing so would remove all releases"
    else
      logger.info "keeping #{releases.length - old_releases.length} of #{releases.length} deployed releases"
      directories = old_releases.map {|release| File.join(releases_path, release)}.join(" ")
      try_sudo "rm -rf #{directories}"
    end
  end
end