recipes/alpha_omega/deploy.rb in alpha_omega-0.0.11 vs recipes/alpha_omega/deploy.rb in alpha_omega-0.0.16

- old
+ new

@@ -83,10 +83,15 @@ # # with persistent releases, the latest release is always the current release _cset(:latest_release) { current_release } # ========================================================================= + # deploy:lock defaults + # ========================================================================= + _cset(:lock_timeout) { 300 } + + # ========================================================================= # These are helper methods that will be available to your recipes. # ========================================================================= # Auxiliary helper method for the `deploy:check' task. Lets you set up your # own dependencies. @@ -234,11 +239,11 @@ logger.important "no previous release to rollback to, rollback of symlink skipped" end end if releases.length == 1 - sudo "ln -snf #{latest_release} #{current_path}" + run "[[ $(readlink #{current_path} 2>&-) = #{latest_release} ]] || sudo ln -snf #{latest_release} #{current_path}" else run "ln -snf #{latest_release} #{current_path}" end system "figlet -w 200 #{release_name} activated" @@ -412,25 +417,60 @@ from = source.next_revision(current_revision) system(source.local.log(from)) end end - end + task :lock do + epoch = Time.now.to_i + locker = '' + run "cat #{deploy_to}/log/.#{application}_deploy_lock 2>&- || true" do |ch, stream, data| + locker << data + end + + if !locker.empty? + lock_epoch = locker.split[0].to_i + lock_user = locker.split[1] + + lock_elasped = epoch-lock_epoch + + if lock_elasped < lock_timeout + puts "deploy in progress by #{lock_user} #{epoch-lock_epoch} seconds ago" + abort + elsif + puts "Found a chef lock by #{lock_user} #{epoch-lock_epoch} seconds ago: too old, deleting and ignoring" + end + end + + run_script = <<-SCRIPT + echo #{epoch} #{ENV['USER']} > #{deploy_to}/log/.#{application}_deploy_lock; + SCRIPT + + at_exit { self.unlock; } + + run run_script.gsub(/[\n\r]+[ \t]+/, " ") + end + + task :unlock do + run "rm -f #{deploy_to}/log/.#{application}_deploy_lock" + end + + end # :deploy + on :exit do put full_log, "#{deploy_to}/log/#{application}_last_deploy.log-#{Time.now.strftime('%Y%m%d-%H%M')}" end -end # Capistrano::Configuration - -namespace :ruby do - task :bundle do - run_script = <<-SCRIPT - set -e; cd #{release_path}; - [[ -f #{ruby_env} ]] && . #{ruby_env}; - [[ -f #{ruby_rvm} ]] && { set +e; source #{ruby_rvm}; set -e; }; - bundle check || bundle install --deployment --quiet --local --without development test || bundle check; - SCRIPT - run run_script + namespace :ruby do + task :bundle do + run_script = <<-SCRIPT + set -e; cd #{release_path}; + [[ -f #{ruby_env} ]] && . #{ruby_env}; + [[ -f #{ruby_rvm} ]] && { set +e; source #{ruby_rvm}; set -e; }; + bundle check || bundle install --deployment --quiet --local --without development test || bundle check; + SCRIPT + run run_script.gsub(/[\n\r]+[ \t]+/, " ") + end end -end + +end # Capistrano::Configuration