Capistrano recipe for automatic deployment deployment: clean only when needed

We do more than 20 deployments per day using capistrano (actually webistrano ), and we have a problem when the disk space on our servers is full of old deployment folders.

From time to time, I run the deploy:cleanup task to clean up all deployments (it stores the last one :keep_releases , currently set to 30). I would like to automate cleaning.

One solution would be to add to the recipe to automatically start cleaning after each deployment:

 after "deploy", "deploy:cleanup" 

But , I do not want to do this after each deployment, I would like to limit it only when the number of previous deployments falls into threashold, for example. 70. Does anyone know how I can do this?


Thoughts:

  • Does Capistrano provide a variable that contains the number of previous deployments?
    • If not, does anyone know how to calculate it. those. set :num_releases, <what-can-I-put-here-to-count-previous-deployments>
  • Is there a way to deploy:cleanup pimping, so it uses a minimum threshold, i.e. exits if < :max_releases previous deployments (where :max_releases is different from :keep_releases ).
  • Can the except keyword be used? that is, something like :except => { :num_releases < 70} .
+4
source share
2 answers

Does Capistrano provide a variable that contains the number of previous deployments?

Yes, releases.length

Is there a way for a pimp to deploy: cleanup, so it uses a minimum threshold?

Yes, here is a special task with names that will run the usual cleaning task ONLY if a certain number of folders for release have been created:

 namespace :mystuff do task :mycleanup, :except => { :no_release => true } do thresh = fetch(:cleanup_threshold, 70).to_i if releases.length > thresh logger.info "Threshold of #{thresh} releases reached, runing deploy:cleanup." deploy.cleanup end end end 

To run this run automatically after deployment, put this at the top of the recipe:

 after "deploy", "mystuff:mycleanup" 

It’s good that the before and after directives installed on deploy:cleanup are executed as usual. For example, we require the following:

 before 'deploy:cleanup', 'mystuff:prepare_cleanup_permissions' after 'deploy:cleanup', 'mystuff:restore_cleanup_permissions' 
+3
source

A quick and dirty approach using the current capistrano code:

Change the cleanup task in https://github.com/capistrano/capistrano/blob/master/lib/capistrano/recipes/deploy.rb#L405 :

  task :cleanup, :except => { :no_release => true } do thresh = fetch(:cleanup_threshold, 70).to_i count = fetch(:keep_releases, 5).to_i if thresh >= releases.length logger.important "no old releases to clean up" else logger.info "threshold of #{thresh} releases reached, keeping #{count} of #{releases.length} deployed releases" directories = (releases - releases.last(count)).map { |release| File.join(releases_path, release) }.join(" ") try_sudo "rm -rf #{directories}" end end 

and then you can add

 set :cleanup_threshold, 70 

to your deployment recipe.

0
source

All Articles