Advertisement
c00lways

task restart passenger

Jan 4th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.25 KB | None | 0 0
  1. #simple rake task to restart passenger
  2. # paste into lib/tasks/server.rb
  3. namespace :server do
  4.  
  5.   desc 'Deplying to server'
  6.   task deploy: :environment do
  7.    
  8.     asset_precompilation_triggers = %w(app/assets vendor/assets Gemfile.lock config)
  9.    
  10.     if (deployed_file_exists?("db/schema.rb") || deployed_file_exists?("db/structure.sql")) && trigger_update?("db/")
  11.       puts "post_deploy: migrating..."
  12.       system "bundle exec rake db:migrate 2>&1"
  13.     end  
  14.    
  15.     if asset_precompilation_triggers.detect {|path| trigger_update?(path)}
  16.       puts "post_deploy: assets:precompile..."
  17.       system "bundle exec rake assets:precompile 2>&1"
  18.     end
  19.    
  20.     system "bundle exec rake server:restart 2>&1"
  21.    
  22.     save_commit_count
  23.   end
  24.  
  25.   desc 'Restarting server'
  26.   task restart: :environment do
  27.     puts 'Restarting...'
  28.     unless File.directory?('tmp')
  29.       system 'mkdir tmp'
  30.     end
  31.     system "touch tmp/restart.txt 2>&1"
  32.   end
  33.  
  34.  
  35.   def deploy_to
  36.     "."
  37.   end
  38.  
  39.  
  40.   # Capture the result of a git command run within the `deploy_to` directory
  41.   def capture_git(command)
  42.     `cd #{deploy_to} && git #{command}`
  43.   end
  44.  
  45.   def trigger_update?(path)
  46.     # puts "updated? #{path} in #{changed_files.inspect}"
  47.     changed_files.detect {|p| p[0, path.length] == path}
  48.   end
  49.  
  50.   def changed_files
  51.     # puts "latest tag: #{latest_tag}"
  52.     cnt = latest_merge_count
  53.     # puts "commit since last push: #{cnt}"
  54.     @changed_files = capture_git('diff --name-only HEAD~'+cnt.to_s+' | cat').split
  55.     # @changed_files ||= if latest_tag
  56.     #   capture_git("diff --name-only #{latest_tag} origin/#{branch} | cat").split
  57.     # else
  58.     #   capture_git("ls-files | cat").split
  59.     # end
  60.   end
  61.  
  62.   def deployed_file_exists?(path)
  63.     exit_code("cd #{deploy_to} && [ -f #{path} ]") == "0"
  64.   end
  65.  
  66.   # Does the given directory exist within the deployment directory?
  67.   def deployed_dir_exists?(path)
  68.     exit_code("cd #{deploy_to} && [ -d #{path} ]") == "0"
  69.   end
  70.  
  71.   # Has the given path been created or changed since the previous deployment?  During the first
  72.   # successful deployment this will always return true if the file exists.
  73.   def deployed_file_changed?(path)
  74.     return deployed_file_exists?(path) unless latest_tag
  75.     exit_code("cd #{deploy_to} && git diff --exit-code #{latest_tag} origin/#{branch} #{path}") == "1"
  76.   end
  77.  
  78.   def exit_code(command)
  79.     # puts "cmd: #{command} > /dev/null 2>&1; echo $?".strip
  80.     # Ammeter.OutputCapturer.capture("#{command} > /dev/null 2>&1; echo $?").strip
  81.     `#{command} > /dev/null 2>&1; echo $?`.strip
  82.   end
  83.  
  84.   def latest_merge_count
  85.     if File.exists?("../shared/git_count")
  86.       res = `cat ../shared/git_count`.strip.to_i
  87.       new_count = capture_git('rev-list --all --count').to_i
  88.       return new_count - res
  89.     else
  90.       return 1
  91.     end
  92.   end
  93.  
  94.   def save_commit_count
  95.     new_count = capture_git('rev-list --all --count').to_i
  96.     File.open('../shared/git_count', 'w') { |file| file.write(new_count.to_s) }
  97.   end
  98.  
  99.   def latest_tag
  100.     latest_tag_from_repository
  101.   end
  102.  
  103.   def release_matcher
  104.     /\A[0-9]{14}\Z/
  105.   end
  106.  
  107.   def latest_tag_from_repository
  108.     tags = capture_git("tag").strip.split
  109.     tags.grep(release_matcher).last
  110.   end
  111.  
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement