setup_heroku 7.1 KB

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