setup_heroku 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env ruby
  2. require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'setup_tools'))
  3. include SetupTools
  4. unless `which heroku` =~ /heroku/
  5. puts "It looks like the heroku command line tool hasn't been installed yet. Please install"
  6. puts "the Heroku Toolbelt from https://toolbelt.heroku.com, run 'heroku auth:login', and then"
  7. puts "run this script again."
  8. exit 1
  9. end
  10. def grab_heroku_config!
  11. grab_config_with_cmd!("heroku config -s", no_stderr: true)
  12. end
  13. def set_env(key, value)
  14. capture("heroku config:set #{key}=#{value}")
  15. end
  16. def check_login!
  17. unless File.exists?(File.expand_path("~/.netrc")) && File.read(File.expand_path("~/.netrc")) =~ /heroku/
  18. puts "It looks like you need to log in to Heroku. Please run 'heroku auth:login' before continuing."
  19. exit 1
  20. end
  21. puts "Welcome #{`heroku auth:whoami`.strip}! It looks like you're logged into Heroku."
  22. puts
  23. end
  24. check_login!
  25. info = capture("heroku info")
  26. if info =~ /Incomplete credentials detected/i
  27. puts info
  28. puts
  29. puts "When this is resolved, please run 'bin/setup_heroku' again."
  30. exit
  31. elsif info =~ /No app specified/i
  32. puts "It looks like you don't have a Heroku app set up yet for this repo."
  33. puts "You can either exit now and run 'heroku create', or I can do it for you."
  34. if yes?("Would you like me to create a Heroku app for you now in this repo?")
  35. puts `heroku create`
  36. info = capture("heroku info")
  37. else
  38. puts "Okay, exiting so you can do it."
  39. exit 0
  40. end
  41. end
  42. if (root_id = `git rev-list --max-parents=0 HEAD`.chomp) != '620acffa5a302c6a27165d3214cf3da6be6c1d0d'
  43. if (`git remote`.split - %w[heroku]).empty?
  44. puts "You don't seem to have cantino/huginn set up as upstream repository."
  45. if yes?("Would you like me to set this working tree up for you?", default: :yes)
  46. if system('git remote add origin https://github.com/cantino/huginn.git') &&
  47. system('git remote update origin')
  48. rebase_command = "git rebase #{root_id} --onto origin/master"
  49. if system(rebase_command)
  50. puts "Done!"
  51. else
  52. system('git rebase --abort')
  53. puts "Rebasing your working tree onto the upstream master failed."
  54. puts "Please run the following command and merge your local changes by yourself."
  55. puts "\t#{rebase_command}"
  56. exit 1
  57. end
  58. else
  59. exit 1
  60. end
  61. end
  62. end
  63. end
  64. app_name = info.scan(/https?:\/\/([\w\d-]+)\.herokuapp\.com/).flatten.first
  65. confirm_app_name app_name
  66. grab_heroku_config!
  67. print_config
  68. set_defaults!
  69. unless $config['DOMAIN']
  70. set_value 'DOMAIN', "#{app_name}.herokuapp.com", force: false
  71. first_time = true
  72. end
  73. set_value 'BUILDPACK_URL', "https://github.com/ddollar/heroku-buildpack-multi.git"
  74. set_value 'PROCFILE_PATH', "deployment/heroku/Procfile.heroku", force: false
  75. set_value 'ON_HEROKU', "true"
  76. unless $config['SMTP_DOMAIN'] && $config['SMTP_USER_NAME'] && $config['SMTP_PASSWORD'] && $config['SMTP_SERVER'] && $config['EMAIL_FROM_ADDRESS']
  77. puts "Okay, let's setup outgoing email settings. The simplest solution is to use the free sendgrid Heroku addon."
  78. puts "If you'd like to use your own server, or your Gmail account, please see .env.example and set"
  79. puts "SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set'."
  80. if yes?("Should I enable the free sendgrid addon?")
  81. puts capture("heroku addons:add sendgrid")
  82. set_value 'SMTP_SERVER', "smtp.sendgrid.net", silent: true
  83. set_value 'SMTP_DOMAIN', "heroku.com", silent: true
  84. grab_heroku_config!
  85. set_value 'SMTP_USER_NAME', $config['SENDGRID_USERNAME'], silent: true
  86. set_value 'SMTP_PASSWORD', $config['SENDGRID_PASSWORD'], silent: true
  87. else
  88. puts "Okay, you'll need to set SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set' manually."
  89. end
  90. unless $config['EMAIL_FROM_ADDRESS']
  91. email = nag("What email address would you like email to appear to be sent from?")
  92. set_value 'EMAIL_FROM_ADDRESS', email
  93. end
  94. end
  95. branch = capture("git rev-parse --abbrev-ref HEAD")
  96. if yes?("Should I push your current branch (#{branch}) to heroku?", default: :yes)
  97. puts "This may take a moment..."
  98. puts capture("git push heroku #{branch}:master -f")
  99. puts "Running database migrations..."
  100. puts capture("heroku run rake db:migrate")
  101. end
  102. if first_time
  103. puts "Restarting..."
  104. puts capture("heroku restart")
  105. puts "Done!"
  106. puts
  107. puts
  108. puts "I can make an admin user on your new Huginn instance and setup some example Agents."
  109. if yes?("Should I create a new admin user and some example Agents?", default: :yes)
  110. done = false
  111. while !done
  112. seed_email = nag "Okay, what is your email address?"
  113. seed_username = nag "And what username would you like to login as?"
  114. seed_password = nag "Finally, what password would you like to use?", noecho: true
  115. puts "\nJust a moment..."
  116. result = capture("heroku run rake db:seed SEED_EMAIL=#{Shellwords.escape Shellwords.escape(seed_email)} SEED_USERNAME=#{Shellwords.escape Shellwords.escape(seed_username)} SEED_PASSWORD=#{Shellwords.escape Shellwords.escape(seed_password)}")
  117. if result =~ /Validation failed/
  118. puts "ERROR:"
  119. puts
  120. puts result
  121. puts
  122. else
  123. done = true
  124. end
  125. end
  126. puts
  127. puts
  128. puts "Okay, you should be all set! Visit https://#{app_name}.herokuapp.com and login as '#{seed_username}' with your password."
  129. puts
  130. puts "If you'd like to make more users, you can visit https://#{app_name}.herokuapp.com/users/sign_up and use the invitation code:"
  131. else
  132. puts
  133. puts "Visit https://#{app_name}.herokuapp.com/users/sign_up and use the invitation code shown below:"
  134. end
  135. puts
  136. puts "\t#{$config['INVITATION_CODE']}"
  137. puts
  138. puts "We recommend that you read https://github.com/cantino/huginn/wiki/Run-Huginn-for-free-on-Heroku and setup Pingdom to keep your app awake!"
  139. end
  140. puts
  141. puts "Done!"