【问题标题】:Capistrano recipe to automatically run deploy:cleanup only when neededCapistrano 配方仅在需要时自动运行 deploy:cleanup
【发布时间】:2011-08-10 09:17:47
【问题描述】:

我们每天使用 capistrano(实际上是 webistrano)进行 20 多次部署,但我们的服务器上的磁盘空间充满了旧的部署文件夹。

我不时地运行deploy:cleanup 任务来清除所有部署(它保留最后一个:keep_releases,当前设置为30)。我想自动化清理。

一种解决方案是将以下内容添加到配方中,以便在每次部署后自动运行清理:

after "deploy", "deploy:cleanup"

但是,我不想在每次部署之后都这样做,我想将其限制为仅在先前部署的数量达到阈值时,例如70. 有人知道我该怎么做吗?


想法:

  • Capistrano 是否提供一个变量来保存先前部署的数量?
    • 如果没有,有没有人知道计算它的方法。即set :num_releases, <what-can-I-put-here-to-count-previous-deployments>
  • 有没有办法拉皮条 deploy:cleanup 所以它使用最小阈值,即如果 < :max_releases 以前的部署(其中 :max_releases:keep_releases 不同)则退出。
  • 可以使用except 关键字吗?即类似:except => { :num_releases < 70}

【问题讨论】:

    标签: ruby deployment capistrano webistrano


    【解决方案1】:

    Capistrano 是否提供了一个变量来保存先前部署的数量?

    是的,releases.length

    有没有办法 pimp deploy:cleanup 使用最小阈值?

    是的,这是一个私有命名空间的任务,只有在建立了一定数量的发布文件夹时才会触发正常的清理任务:

    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
    

    要在部署后自动运行,请将其放在配方的顶部:

    after "deploy", "mystuff:mycleanup"
    

    这样做的好处是,在deploy:cleanup 上设置的beforeafter 指令可以正常执行。例如,我们需要以下内容:

    before 'deploy:cleanup', 'mystuff:prepare_cleanup_permissions'
    after 'deploy:cleanup', 'mystuff:restore_cleanup_permissions'
    

    【讨论】:

      【解决方案2】:

      使用当前 capistrano 代码的快速而肮脏的方法:

      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
      

      然后你就可以添加了

      set :cleanup_threshold, 70
      

      到您的部署配方。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-29
        • 2023-03-19
        • 2017-04-13
        • 1970-01-01
        • 2018-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多