setup_heroku 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 huginn/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/huginn/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/heroku/heroku-buildpack-multi.git"
  74. set_value 'PROCFILE_PATH', "deployment/heroku/Procfile.heroku", force: false
  75. set_value 'ON_HEROKU', "true"
  76. unless $config['DATABASE_URL']
  77. puts "Setting up the postgres addon"
  78. puts capture("heroku addons:add heroku-postgresql")
  79. puts
  80. end
  81. unless $config['SMTP_DOMAIN'] && $config['SMTP_USER_NAME'] && $config['SMTP_PASSWORD'] && $config['SMTP_SERVER'] && $config['EMAIL_FROM_ADDRESS']
  82. puts "Okay, let's setup outgoing email settings. The simplest solution is to use the free sendgrid Heroku addon."
  83. puts "If you'd like to use your own server, or your Gmail account, please see .env.example and set"
  84. puts "SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set'."
  85. if yes?("Should I enable the free sendgrid addon?")
  86. puts capture("heroku addons:add sendgrid")
  87. set_value 'SMTP_SERVER', "smtp.sendgrid.net", silent: true
  88. set_value 'SMTP_DOMAIN', "heroku.com", silent: true
  89. grab_heroku_config!
  90. set_value 'SMTP_USER_NAME', $config['SENDGRID_USERNAME'], silent: true
  91. set_value 'SMTP_PASSWORD', $config['SENDGRID_PASSWORD'], silent: true
  92. else
  93. puts "Okay, you'll need to set SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set' manually."
  94. end
  95. unless $config['EMAIL_FROM_ADDRESS']
  96. email = nag("What email address would you like email to appear to be sent from?")
  97. set_value 'EMAIL_FROM_ADDRESS', email
  98. end
  99. end
  100. branch = capture("git rev-parse --abbrev-ref HEAD")
  101. if yes?("Should I push your current branch (#{branch}) to heroku?", default: :yes)
  102. puts "This may take a moment..."
  103. puts capture("git push heroku #{branch}:master -f")
  104. puts "Running database migrations..."
  105. puts capture("heroku run rake db:migrate")
  106. end
  107. if first_time
  108. puts "Restarting..."
  109. puts capture("heroku restart")
  110. puts "Done!"
  111. puts
  112. puts
  113. puts "I can make an admin user on your new Huginn instance and setup some example Agents."
  114. if yes?("Should I create a new admin user and some example Agents?", default: :yes)
  115. done = false
  116. while !done
  117. seed_email = nag "Okay, what is your email address?"
  118. seed_username = nag "And what username would you like to login as?"
  119. seed_password = nag "Finally, what password would you like to use?", noecho: true
  120. puts "\nJust a moment..."
  121. 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)}")
  122. if result =~ /Validation failed/
  123. puts "ERROR:"
  124. puts
  125. puts result
  126. puts
  127. else
  128. done = true
  129. end
  130. end
  131. puts
  132. puts
  133. puts "Okay, you should be all set! Visit https://#{app_name}.herokuapp.com and login as '#{seed_username}' with your password."
  134. puts
  135. 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:"
  136. else
  137. puts
  138. puts "Visit https://#{app_name}.herokuapp.com/users/sign_up and use the invitation code shown below:"
  139. end
  140. puts
  141. puts "\t#{$config['INVITATION_CODE']}"
  142. puts
  143. puts "We strongly recommend that you read https://github.com/huginn/huginn/blob/master/doc/heroku/install.md thoroughly!"
  144. end
  145. puts
  146. puts "Done!"