setup_heroku 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 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. exit 1
  68. end
  69. grab_heroku_config!
  70. if $config.length > 0
  71. puts
  72. puts "Your current Heroku config:"
  73. $config.each do |key, value|
  74. puts ' ' + key + ' ' * (25 - [key.length, 25].min) + '= ' + value
  75. end
  76. end
  77. unless $config['APP_SECRET_TOKEN']
  78. puts "Setting up APP_SECRET_TOKEN..."
  79. puts capture("heroku config:set APP_SECRET_TOKEN=`rake secret`")
  80. end
  81. set_value 'BUILDPACK_URL', "https://github.com/ddollar/heroku-buildpack-multi.git"
  82. set_value 'PROCFILE_PATH', "deployment/heroku/Procfile.heroku", force: false
  83. set_value 'ON_HEROKU', "true"
  84. set_value 'FORCE_SSL', "true"
  85. set_value 'DOMAIN', "#{app_name}.herokuapp.com", force: false
  86. set_value 'USE_GRAPHVIZ_DOT', 'dot'
  87. unless $config['INVITATION_CODE']
  88. puts "You need to set an invitation code for your Huginn instance. If you plan to share this instance, you will"
  89. puts "tell this code to anyone who you'd like to invite. If you won't share it, then just set this to something"
  90. puts "that people will not guess."
  91. invitation_code = nag("What code would you like to use?")
  92. set_value 'INVITATION_CODE', invitation_code
  93. end
  94. unless $config['SMTP_DOMAIN'] && $config['SMTP_USER_NAME'] && $config['SMTP_PASSWORD'] && $config['SMTP_SERVER'] && $config['EMAIL_FROM_ADDRESS']
  95. puts "Okay, let's setup outgoing email settings. The simplest solution is to use the free sendgrid Heroku addon."
  96. puts "If you'd like to use your own server, or your Gmail account, please see .env.example and set"
  97. puts "SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set'."
  98. if yes?("Should I enable the free sendgrid addon?")
  99. puts capture("heroku addons:add sendgrid")
  100. set_value 'SMTP_SERVER', "smtp.sendgrid.net", silent: true
  101. set_value 'SMTP_DOMAIN', "heroku.com", silent: true
  102. grab_heroku_config!
  103. set_value 'SMTP_USER_NAME', $config['SENDGRID_USERNAME'], silent: true
  104. set_value 'SMTP_PASSWORD', $config['SENDGRID_PASSWORD'], silent: true
  105. else
  106. puts "Okay, you'll need to set SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set' manually."
  107. end
  108. unless $config['EMAIL_FROM_ADDRESS']
  109. email = nag("What email address would you like email to appear to be sent from?")
  110. set_value 'EMAIL_FROM_ADDRESS', email
  111. end
  112. end
  113. branch = capture("git rev-parse --abbrev-ref HEAD")
  114. if yes?("Should I push your current branch (#{branch}) to heroku?")
  115. puts "This may take a moment..."
  116. puts capture("git push heroku #{branch}:master -f")
  117. puts "Running database migrations..."
  118. puts capture("heroku run rake db:migrate")
  119. puts
  120. puts
  121. puts "I can make an admin user on your new Huginn instance and setup some example Agents."
  122. if yes?("Should I create a new admin user and some example Agents?")
  123. seed_email = nag "Okay, what is your email address?"
  124. seed_username = nag "And what username would you like to login as?"
  125. seed_password = nag "Finally, what password would you like to use?", noecho: true
  126. puts "\nJust a moment..."
  127. capture("heroku run rake db:seed SEED_EMAIL=#{seed_email} SEED_USERNAME=#{seed_username} SEED_PASSWORD=#{seed_password}")
  128. puts
  129. puts
  130. puts "Okay, you should be all set! Visit https://#{app_name}.herokuapp.com and login as '#{seed_username}' with your password."
  131. end
  132. end
  133. puts
  134. puts "Done!"