setup_heroku 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. unless $config[key] == value
  42. puts "Setting #{key} to #{value}" unless options[:silent]
  43. puts capture("heroku config:set #{key}=#{value}")
  44. end
  45. end
  46. unless File.exists?(File.expand_path("~/.netrc")) && File.read(File.expand_path("~/.netrc")) =~ /heroku/
  47. puts "It looks like you need to log in to Heroku. Please run 'heroku auth:login' before continuing."
  48. exit 1
  49. end
  50. puts "Welcome #{`heroku auth:whoami`.strip}! It looks like you're logged into Heroku."
  51. puts
  52. info = capture("heroku info")
  53. if info =~ /No app specified/i
  54. puts "It looks like you don't have a Heroku app set up yet for this repo."
  55. puts "You can either exit now and run 'heroku create', or I can do it for you."
  56. if yes?("Would you like me to create a Heroku app for you now in this repo?")
  57. puts `heroku create`
  58. info = capture("heroku info")
  59. else
  60. puts "Okay, exiting so you can do it."
  61. exit 0
  62. end
  63. end
  64. app_name = info.scan(/http:\/\/([\w\d-]+)\.herokuapp\.com/).flatten.first
  65. unless yes?("Your Heroku app name is #{app_name}. Is this correct?")
  66. puts "Well, then I'm not sure what to do here, sorry."
  67. end
  68. grab_heroku_config!
  69. if $config.length > 0
  70. puts
  71. puts "Your current Heroku config:"
  72. $config.each do |key, value|
  73. puts ' ' + key + ' ' * (25 - [key.length, 25].min) + '= ' + value
  74. end
  75. end
  76. unless $config['APP_SECRET_TOKEN']
  77. puts "Setting up APP_SECRET_TOKEN..."
  78. puts capture("heroku config:set APP_SECRET_TOKEN=`rake secret`")
  79. end
  80. set_value 'BUILDPACK_URL', "https://github.com/ddollar/heroku-buildpack-multi.git"
  81. set_value 'PROCFILE_PATH', "deployment/heroku/Procfile.heroku"
  82. set_value 'ON_HEROKU', "true"
  83. set_value 'FORCE_SSL', "true"
  84. set_value 'DOMAIN', "#{app_name}.herokuapp.com"
  85. unless $config['INVITATION_CODE']
  86. puts "You need to set an invitation code for your Huginn instance. If you plan to share this instance, you will"
  87. puts "tell this code to anyone who you'd like to invite. If you won't share it, then just set this to something"
  88. puts "that people will not guess."
  89. invitation_code = nag("What code would you like to use?")
  90. set_value 'INVITATION_CODE', invitation_code
  91. end
  92. unless $config['SMTP_DOMAIN'] && $config['SMTP_USER_NAME'] && $config['SMTP_PASSWORD'] && $config['SMTP_SERVER'] && $config['EMAIL_FROM_ADDRESS']
  93. puts "Okay, let's setup outgoing email settings. The simplest solution is to use the free sendgrid Heroku addon."
  94. puts "If you'd like to use your own server, or your Gmail account, please see .env.example and set"
  95. puts "SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set'."
  96. if yes?("Should I enable the free sendgrid addon?")
  97. puts capture("heroku addons:add sendgrid")
  98. set_value 'SMTP_SERVER', "smtp.sendgrid.net", silent: true
  99. set_value 'SMTP_DOMAIN', "heroku.com", silent: true
  100. grab_heroku_config!
  101. set_value 'SMTP_USER_NAME', $config['SENDGRID_USERNAME'], silent: true
  102. set_value 'SMTP_PASSWORD', $config['SENDGRID_PASSWORD'], silent: true
  103. else
  104. puts "Okay, you'll need to set SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set' manually."
  105. end
  106. unless $config['EMAIL_FROM_ADDRESS']
  107. email = nag("What email address would you like email to appear to be sent from?")
  108. set_value 'EMAIL_FROM_ADDRESS', email
  109. end
  110. end
  111. branch = capture("git rev-parse --abbrev-ref HEAD")
  112. if yes?("Should I push your current branch (#{branch}) to heroku?")
  113. puts "This may take a moment..."
  114. puts capture("git push heroku #{branch}:master -f")
  115. puts "Running database migrations..."
  116. puts capture("heroku run rake db:migrate")
  117. puts
  118. puts
  119. puts "I can make an admin user on your new Huginn instance and setup some example Agents."
  120. if yes?("Should I create a new admin user and some example Agents?")
  121. seed_email = nag "Okay, what is your email address?"
  122. seed_username = nag "And what username would you like to login as?"
  123. seed_password = nag "Finally, what password would you like to use?", noecho: true
  124. puts "\nJust a moment..."
  125. capture("heroku run rake db:seed SEED_EMAIL=#{seed_email} SEED_USERNAME=#{seed_username} SEED_PASSWORD=#{seed_password}")
  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. end
  130. end
  131. puts
  132. puts "Done!"