Guest User

Untitled

a guest
Apr 26th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. require 'aws/s3'
  2. require 'erb'
  3.  
  4. namespace :heroku do
  5.  
  6. def latest_bundle_name(app)
  7. %x{ heroku bundles #{app_option(app)}| cut -f 1 -d ' ' }.chomp
  8. end
  9.  
  10. def app_option(app)
  11. '--app ' + app if app
  12. end
  13.  
  14. def app_name(app)
  15. return app if app
  16.  
  17. %x{heroku info}.split("\n")[0].split(' ')[1]
  18. end
  19.  
  20. desc "Capture a new bundle and back it up to S3."
  21. task :backup, :app do |t, args|
  22. app = args[:app]
  23.  
  24. # Warn that we're about to blow out the latest bundle.
  25.  
  26. print 'WARNING: This will destroy the most recent bundle. Do you wish to proceed? (y/n) '
  27. answer = STDIN.gets.chomp
  28. exit unless answer == 'y'
  29.  
  30. puts
  31. puts '===== Deleting most recent bundle from Heroku...'
  32.  
  33. %x{ heroku bundles:destroy #{latest_bundle_name(app)} #{app_option(app)} }
  34.  
  35. puts '===== Capturing a new bundle...'
  36.  
  37. %x{ heroku bundles:capture #{app_option(app)} }
  38.  
  39. while %x{ heroku bundles #{app_option(app)} | grep complete }.empty?
  40. sleep 10
  41. end
  42.  
  43. puts '===== Downloading new bundle...'
  44.  
  45. %x{ heroku bundles:download #{app_option(app)} }
  46.  
  47. puts '===== Pushing the bundle up to S3...'
  48.  
  49. # Establish a connection to S3.
  50. aws_creds = YAML::load(ERB.new(File.read(File.join(Dir.getwd, 'config', 'amazon_s3.yml'))).result)["default"]
  51.  
  52. AWS::S3::Base.establish_connection!(
  53. :access_key_id => aws_creds["access_key_id"],
  54. :secret_access_key => aws_creds["secret_access_key"]
  55. )
  56.  
  57. current_app_name = app_name(app)
  58. bundle_file_name = current_app_name + '.tar.gz'
  59.  
  60. AWS::S3::S3Object.store(latest_bundle_name(app) + '.tar.gz', open(bundle_file_name), current_app_name + '-backups')
  61.  
  62. puts '===== Deleting the temporary bundle file...'
  63.  
  64. FileUtils.rm(bundle_file_name)
  65. end
  66. end
Add Comment
Please, Sign In to add comment