heroku setup script

Andrew Cantino 10 years ago
parent
commit
f0697b6db2
2 changed files with 130 additions and 2 deletions
  1. 127 0
      bin/setup_heroku
  2. 3 2
      deployment/heroku/unicorn.rb

+ 127 - 0
bin/setup_heroku

@@ -0,0 +1,127 @@
1
+#!/usr/bin/env ruby
2
+require 'open3'
3
+
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 Toolback from https://toolbelt.heroku.com, run 'heroku auth:login', and then"
7
+  puts "run this script again."
8
+  exit 1
9
+end
10
+
11
+def capture(cmd, opts = {})
12
+  o, s = Open3.capture2e(cmd, opts)
13
+  o
14
+end
15
+
16
+def ask(question)
17
+  print question + " "
18
+  STDOUT.flush
19
+  gets.strip
20
+end
21
+
22
+def nag(question)
23
+  answer = ''
24
+  while answer.length == 0
25
+    answer = ask(question)
26
+  end
27
+  answer
28
+end
29
+
30
+def yes?(question)
31
+  ask(question) =~ /^y/i
32
+end
33
+
34
+def grab_heroku_config
35
+  config_data = capture("heroku config -s")
36
+  config = {}
37
+  if config_data !~ /has no config vars/
38
+    config_data.split("\n").map do |line|
39
+      next if line =~ /^\s*(#|$)/ # skip comments and empty lines
40
+      first_equal_sign = line.index('=')
41
+      config[line.slice(0, first_equal_sign)] = line.slice(first_equal_sign + 1, line.length)
42
+    end
43
+  end
44
+  config
45
+end
46
+
47
+puts "Welcome #{`heroku auth:whoami`.strip}!  It looks like you're logged into Heroku."
48
+puts
49
+
50
+info = capture("heroku info")
51
+if info =~ /No app specified/i
52
+  puts "It looks like you don't have a Heroku app set up yet for this repo."
53
+  puts "You can either exit now and run 'heroku create', or I can do it for you."
54
+  if yes?("Would you like me to create a heroku app for you now in this repo? (y/n)")
55
+    puts `heroku create`
56
+    info = capture("heroku info")
57
+  else
58
+    puts "Okay, exiting so you can do it."
59
+    exit 0
60
+  end
61
+end
62
+
63
+app_name = info.scan(/http:\/\/([\w\d-]+)\.herokuapp\.com/).flatten.first
64
+
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
+
69
+config = grab_heroku_config
70
+
71
+if config.length > 0
72
+  puts
73
+  puts "Your current Heroku config:"
74
+  config.each do |key, value|
75
+    puts '  ' + key + ' ' * (20 - key.length) + '= ' + value
76
+  end
77
+end
78
+
79
+unless config['APP_SECRET_TOKEN']
80
+  puts "Setting up APP_SECRET_TOKEN..."
81
+  puts capture("heroku config:set APP_SECRET_TOKEN=`rake secret`")
82
+end
83
+
84
+unless config['FORCE_SSL']
85
+  puts "Setting FORCE_SSL to true..."
86
+  puts capture("heroku config:set FORCE_SSL=true")
87
+end
88
+
89
+unless config['DOMAIN']
90
+  puts "Setting DOMAIN to #{app_name}.herokuapp.com..."
91
+  puts capture("heroku config:set DOMAIN=#{app_name}.herokuapp.com")
92
+end
93
+
94
+unless config['INVITATION_CODE']
95
+  puts "You need to set an invitation code for your Huginn instance.  If you plan to share this instance, you will"
96
+  puts "tell this code to anyone who you'd like to invite.  If you won't share it, then just set this to something"
97
+  puts "that people will not guess."
98
+
99
+  invitation_code = nag("What code would you like to use?")
100
+  puts "Setting INVITATION_CODE to #{invitation_code}..."
101
+  puts capture("heroku config:set INVITATION_CODE=#{invitation_code}")
102
+end
103
+
104
+unless config['SMTP_DOMAIN'] && config['SMTP_USER_NAME'] && config['SMTP_PASSWORD'] && config['SMTP_SERVER'] && config['EMAIL_FROM_ADDRESS']
105
+  puts "Okay, let's setup outgoing email settings.  The simplest solution is to use the free sendgrid Heroku addon."
106
+  puts "If you'd like to use your own server, or your Gmail account, please see .env.example and set"
107
+  puts "SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set'."
108
+  if yes?("Should I enable the free sendgrid addon? (y/n)")
109
+    puts capture("heroku addons:add sendgrid")
110
+    puts capture("heroku config:set SMTP_SERVER=smtp.sendgrid.net")
111
+    puts capture("heroku config:set SMTP_DOMAIN=heroku.com")
112
+
113
+    config = grab_heroku_config
114
+    puts capture("heroku config:set SMTP_USER_NAME=#{config['SENDGRID_USERNAME']}")
115
+    puts capture("heroku config:set SMTP_PASSWORD=#{config['SENDGRID_PASSWORD']}")
116
+  else
117
+    puts "Okay, you'll need to set SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set' manually."
118
+  end
119
+
120
+  unless config['EMAIL_FROM_ADDRESS']
121
+    email = nag("What email address would you like email to appear to be sent from?")
122
+    puts "Setting EMAIL_FROM_ADDRESS to #{email}..."
123
+    puts capture("heroku config:set EMAIL_FROM_ADDRESS=#{email}")
124
+  end
125
+end
126
+
127
+puts "Done!"

+ 3 - 2
deployment/heroku/unicorn.rb

@@ -16,8 +16,9 @@ Thread.new do
16 16
 
17 17
     sleep 30
18 18
 
19
-    if ENV['PING_URL']
20
-      Net::HTTP.get_response(URI(ENV['PING_URL']))
19
+    if ENV['DOMAIN']
20
+      force_ssl = ENV['FORCE_SSL'].present? && ENV['FORCE_SSL'] == 'true'
21
+      Net::HTTP.get_response(URI((force_ssl ? "https://" : "http://") + ENV['DOMAIN']))
21 22
     end
22 23
 
23 24
     begin