Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. namespace :deploy do
  2. DEVELOPMENT_APPS = ['example-development']
  3. STAGING_APPS = ['example-staging']
  4. PRODUCTION_APPS = ['example', 'example2', 'example-demo']
  5. REMOTE = ENV['REMOTE_HOST'] || 'git@heroku.com'
  6.  
  7. def shell_cmd(cmd)
  8. begin
  9. sh "#{cmd}"
  10. rescue
  11. puts 'Command failed'.yellow
  12. exit 0
  13. end
  14. end
  15.  
  16. def heroku_cmd(cmd)
  17. begin
  18. Bundler.with_clean_env do
  19. sh "heroku #{cmd}"
  20. end
  21. rescue
  22. puts 'Command failed'.yellow
  23. exit 1
  24. end
  25. end
  26.  
  27. def choose_app(apps)
  28. return apps.first if apps.size == 1
  29. puts 'There are many apps for this environment.'.cyan
  30. puts 'Please, choose one:'.cyan
  31. puts apps.map { |a| " - #{a}".green }
  32. print 'Selected app: '.cyan
  33. app = STDIN.gets.strip
  34. unless apps.include?(app)
  35. puts 'Invalid app selected.'.red
  36. exit 0
  37. end
  38. app
  39. end
  40.  
  41. desc 'Deploy to heroku development app'
  42. task :development => [:set_development_app, :check, :off, :push, :migrate, :restart, :on]
  43.  
  44. desc 'Deploy to heroku staging app'
  45. task :staging => [:set_staging_app, :check, :off, :promote, :migrate, :restart, :on]
  46.  
  47. desc 'Deploy to heroku production app'
  48. task :production => [:set_production_app, :check, :off, :promote, :migrate, :restart, :on]
  49.  
  50. task :set_development_app do
  51. APP = choose_app(DEVELOPMENT_APPS)
  52. DEPLOY = [APP]
  53. print 'Deploy to development will be done through git push to the following app: '.yellow
  54. puts APP.cyan
  55. end
  56.  
  57. task :set_staging_app do
  58. puts 'Deploy to staging is done with heroku pipelines:promote from development app.'.cyan
  59. PROMOTE = choose_app(DEVELOPMENT_APPS)
  60. DEPLOY = STAGING_APPS
  61.  
  62. print 'The app '.yellow
  63. print PROMOTE.green
  64. print ' will be promoted to the following apps: '.yellow
  65. puts DEPLOY.to_sentence.green
  66. end
  67.  
  68. task :set_production_app do
  69. puts 'Deploy to production is done with heroku pipelines:promote from staging app.'.cyan
  70. PROMOTE = choose_app(STAGING_APPS)
  71. DEPLOY = PRODUCTION_APPS
  72.  
  73. print 'The app '.yellow
  74. print PROMOTE.green
  75. print ' will be promoted to the following apps: '.yellow
  76. puts DEPLOY.to_sentence.green
  77. end
  78.  
  79. task :check do
  80. print 'Are you sure you want to proceed (y/n)? '.yellow
  81. input = STDIN.gets.strip
  82. if input == 'y'
  83. puts 'Lets go'.green
  84. else
  85. puts [
  86. 'Sorry for the confusion.',
  87. 'More luck next time.',
  88. 'That was close.'
  89. ].sample.green
  90. exit 0
  91. end
  92. end
  93.  
  94. task :promote do
  95. puts 'Promoting app to its downstream app(s)'
  96. heroku_cmd "pipelines:promote --app #{PROMOTE}"
  97. end
  98.  
  99. task :push do
  100. puts 'Pushing to heroku git remote'
  101. shell_cmd "git push #{REMOTE}:#{APP}.git master"
  102. end
  103.  
  104. task :migrate do
  105. puts 'Running database migrations...'
  106. DEPLOY.each do |app|
  107. heroku_cmd "run rake db:migrate --app #{app}"
  108. end
  109. end
  110.  
  111. task :off do
  112. puts 'Putting apps in maintenance mode...'
  113. DEPLOY.each do |app|
  114. heroku_cmd "maintenance:on --app #{app}"
  115. end
  116. end
  117.  
  118. task :on do
  119. puts 'Taking apps out of maintenance mode...'
  120. DEPLOY.each do |app|
  121. heroku_cmd "maintenance:off --app #{app}"
  122. end
  123. end
  124.  
  125. task :restart do
  126. puts 'Restarting app servers ...'
  127. DEPLOY.each do |app|
  128. heroku_cmd "restart --app #{app}"
  129. end
  130. end
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement