setup_heroku 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env ruby
  2. require 'open3'
  3. require 'io/console'
  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 capture(cmd, opts = {})
  11. o, s = Open3.capture2e(cmd, opts)
  12. o.strip
  13. end
  14. def ask(question, opts = {})
  15. print question + " "
  16. STDOUT.flush
  17. (opts[:noecho] ? STDIN.noecho(&:gets) : gets).strip
  18. end
  19. def nag(question, opts = {})
  20. answer = ''
  21. while answer.length == 0
  22. answer = ask(question, opts)
  23. end
  24. answer
  25. end
  26. def yes?(question)
  27. ask(question + " (y/n)") =~ /^y/i
  28. end
  29. def grab_heroku_config!
  30. config_data = capture("heroku config -s")
  31. $config = {}
  32. if config_data !~ /has no config vars/
  33. config_data.split("\n").map do |line|
  34. next if line =~ /^\s*(#|$)/ # skip comments and empty lines
  35. first_equal_sign = line.index('=')
  36. $config[line.slice(0, first_equal_sign)] = line.slice(first_equal_sign + 1, line.length)
  37. end
  38. end
  39. end
  40. def set_value(key, value, options = {})
  41. if $config[key].nil? || $config[key] == '' || ($config[key] != value && options[:force] != false)
  42. puts "Setting #{key} to #{value}" unless options[:silent]
  43. puts capture("heroku config:set #{key}=#{value}")
  44. $config[key] = value
  45. end
  46. end
  47. unless File.exists?(File.expand_path("~/.netrc")) && File.read(File.expand_path("~/.netrc")) =~ /heroku/
  48. puts "It looks like you need to log in to Heroku. Please run 'heroku auth:login' before continuing."
  49. exit 1
  50. end
  51. puts "Welcome #{`heroku auth:whoami`.strip}! It looks like you're logged into Heroku."
  52. puts
  53. info = capture("heroku info")
  54. if info =~ /No app specified/i
  55. puts "It looks like you don't have a Heroku app set up yet for this repo."
  56. puts "You can either exit now and run 'heroku create', or I can do it for you."
  57. if yes?("Would you like me to create a Heroku app for you now in this repo?")
  58. puts `heroku create`
  59. info = capture("heroku info")
  60. else
  61. puts "Okay, exiting so you can do it."
  62. exit 0
  63. end
  64. end
  65. app_name = info.scan(/http:\/\/([\w\d-]+)\.herokuapp\.com/).flatten.first
  66. unless yes?("Your Heroku app name is #{app_name}. Is this correct?")
  67. puts "Well, then I'm not sure what to do here, sorry."
  68. exit 1
  69. end
  70. grab_heroku_config!
  71. if $config.length > 0
  72. puts
  73. puts "Your current Heroku config:"
  74. $config.each do |key, value|
  75. puts ' ' + key + ' ' * (25 - [key.length, 25].min) + '= ' + value
  76. end
  77. end
  78. unless $config['APP_SECRET_TOKEN']
  79. puts "Setting up APP_SECRET_TOKEN..."
  80. puts capture("heroku config:set APP_SECRET_TOKEN=`rake secret`")
  81. end
  82. unless $config['DOMAIN']
  83. set_value 'DOMAIN', "#{app_name}.herokuapp.com", force: false
  84. first_time = true
  85. end
  86. set_value 'BUILDPACK_URL', "https://github.com/ddollar/heroku-buildpack-multi.git"
  87. set_value 'PROCFILE_PATH', "deployment/heroku/Procfile.heroku", force: false
  88. set_value 'ON_HEROKU', "true"
  89. set_value 'FORCE_SSL', "true"
  90. set_value 'USE_GRAPHVIZ_DOT', 'dot'
  91. unless $config['INVITATION_CODE']
  92. puts "You need to set an invitation code for your Huginn instance. If you plan to share this instance, you will"
  93. puts "tell this code to anyone who you'd like to invite. If you won't share it, then just set this to something"
  94. puts "that people will not guess."
  95. invitation_code = nag("What code would you like to use?")
  96. set_value 'INVITATION_CODE', invitation_code
  97. end
  98. unless $config['SMTP_DOMAIN'] && $config['SMTP_USER_NAME'] && $config['SMTP_PASSWORD'] && $config['SMTP_SERVER'] && $config['EMAIL_FROM_ADDRESS']
  99. puts "Okay, let's setup outgoing email settings. The simplest solution is to use the free sendgrid Heroku addon."
  100. puts "If you'd like to use your own server, or your Gmail account, please see .env.example and set"
  101. puts "SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set'."
  102. if yes?("Should I enable the free sendgrid addon?")
  103. puts capture("heroku addons:add sendgrid")
  104. set_value 'SMTP_SERVER', "smtp.sendgrid.net", silent: true
  105. set_value 'SMTP_DOMAIN', "heroku.com", silent: true
  106. grab_heroku_config!
  107. set_value 'SMTP_USER_NAME', $config['SENDGRID_USERNAME'], silent: true
  108. set_value 'SMTP_PASSWORD', $config['SENDGRID_PASSWORD'], silent: true
  109. else
  110. puts "Okay, you'll need to set SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set' manually."
  111. end
  112. unless $config['EMAIL_FROM_ADDRESS']
  113. email = nag("What email address would you like email to appear to be sent from?")
  114. set_value 'EMAIL_FROM_ADDRESS', email
  115. end
  116. end
  117. branch = capture("git rev-parse --abbrev-ref HEAD")
  118. if yes?("Should I push your current branch (#{branch}) to heroku?")
  119. puts "This may take a moment..."
  120. puts capture("git push heroku #{branch}:master -f")
  121. puts "Running database migrations..."
  122. puts capture("heroku run rake db:migrate")
  123. end
  124. if first_time
  125. puts "Restarting..."
  126. puts capture("heroku restart")
  127. puts "Done!"
  128. puts
  129. puts
  130. puts "I can make an admin user on your new Huginn instance and setup some example Agents."
  131. if yes?("Should I create a new admin user and some example Agents?")
  132. done = false
  133. while !done
  134. seed_email = nag "Okay, what is your email address?"
  135. seed_username = nag "And what username would you like to login as?"
  136. seed_password = nag "Finally, what password would you like to use?", noecho: true
  137. puts "\nJust a moment..."
  138. result = capture("heroku run rake db:seed SEED_EMAIL=#{seed_email} SEED_USERNAME=#{seed_username} SEED_PASSWORD=#{seed_password}")
  139. if result =~ /Validation failed/
  140. puts "ERROR:"
  141. puts
  142. puts result
  143. puts
  144. else
  145. done = true
  146. end
  147. end
  148. puts
  149. puts
  150. puts "Okay, you should be all set! Visit https://#{app_name}.herokuapp.com and login as '#{seed_username}' with your password."
  151. puts
  152. 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:"
  153. else
  154. puts
  155. puts "Visit https://#{app_name}.herokuapp.com/users/sign_up and use the invitation code shown below:"
  156. end
  157. puts
  158. puts "\t#{$config['INVITATION_CODE']}"
  159. end
  160. puts
  161. puts "Done!"