Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #simple rake task to restart passenger
- # paste into lib/tasks/server.rb
- namespace :server do
- desc 'Deplying to server'
- task deploy: :environment do
- asset_precompilation_triggers = %w(app/assets vendor/assets Gemfile.lock config)
- if (deployed_file_exists?("db/schema.rb") || deployed_file_exists?("db/structure.sql")) && trigger_update?("db/")
- puts "post_deploy: migrating..."
- system "bundle exec rake db:migrate 2>&1"
- end
- if asset_precompilation_triggers.detect {|path| trigger_update?(path)}
- puts "post_deploy: assets:precompile..."
- system "bundle exec rake assets:precompile 2>&1"
- end
- system "bundle exec rake server:restart 2>&1"
- save_commit_count
- end
- desc 'Restarting server'
- task restart: :environment do
- puts 'Restarting...'
- unless File.directory?('tmp')
- system 'mkdir tmp'
- end
- system "touch tmp/restart.txt 2>&1"
- end
- def deploy_to
- "."
- end
- # Capture the result of a git command run within the `deploy_to` directory
- def capture_git(command)
- `cd #{deploy_to} && git #{command}`
- end
- def trigger_update?(path)
- # puts "updated? #{path} in #{changed_files.inspect}"
- changed_files.detect {|p| p[0, path.length] == path}
- end
- def changed_files
- # puts "latest tag: #{latest_tag}"
- cnt = latest_merge_count
- # puts "commit since last push: #{cnt}"
- @changed_files = capture_git('diff --name-only HEAD~'+cnt.to_s+' | cat').split
- # @changed_files ||= if latest_tag
- # capture_git("diff --name-only #{latest_tag} origin/#{branch} | cat").split
- # else
- # capture_git("ls-files | cat").split
- # end
- end
- def deployed_file_exists?(path)
- exit_code("cd #{deploy_to} && [ -f #{path} ]") == "0"
- end
- # Does the given directory exist within the deployment directory?
- def deployed_dir_exists?(path)
- exit_code("cd #{deploy_to} && [ -d #{path} ]") == "0"
- end
- # Has the given path been created or changed since the previous deployment? During the first
- # successful deployment this will always return true if the file exists.
- def deployed_file_changed?(path)
- return deployed_file_exists?(path) unless latest_tag
- exit_code("cd #{deploy_to} && git diff --exit-code #{latest_tag} origin/#{branch} #{path}") == "1"
- end
- def exit_code(command)
- # puts "cmd: #{command} > /dev/null 2>&1; echo $?".strip
- # Ammeter.OutputCapturer.capture("#{command} > /dev/null 2>&1; echo $?").strip
- `#{command} > /dev/null 2>&1; echo $?`.strip
- end
- def latest_merge_count
- if File.exists?("../shared/git_count")
- res = `cat ../shared/git_count`.strip.to_i
- new_count = capture_git('rev-list --all --count').to_i
- return new_count - res
- else
- return 1
- end
- end
- def save_commit_count
- new_count = capture_git('rev-list --all --count').to_i
- File.open('../shared/git_count', 'w') { |file| file.write(new_count.to_s) }
- end
- def latest_tag
- latest_tag_from_repository
- end
- def release_matcher
- /\A[0-9]{14}\Z/
- end
- def latest_tag_from_repository
- tags = capture_git("tag").strip.split
- tags.grep(release_matcher).last
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement