-
Notifications
You must be signed in to change notification settings - Fork 10
/
configure.py
403 lines (352 loc) · 17 KB
/
configure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
'''
Hackpack Configure
A script to configure your TwiML apps and Twilio phone numbers to use your
hackpack's Heroku app.
Usage:
Auto-configure using your local_settings.py:
python configure.py
Deploy to new Twilio number and App Sid:
python configure.py --new
Deploy to specific App Sid:
python configure.py --app APxxxxxxxxxxxxxx
Deploy to specific Twilio number:
python configure.py --number +15556667777
Deploy to custom domain:
python configure.py --domain example.com
'''
from argparse import ArgumentParser
import sys
import subprocess
import logging
from twilio.rest import TwilioRestClient
from twilio.exceptions import TwilioException
from hackpack import local_settings
class Configure(object):
def __init__(self, account_sid=local_settings.TWILIO_ACCOUNT_SID,
auth_token=local_settings.TWILIO_AUTH_TOKEN,
app_sid=local_settings.TWILIO_APP_SID,
phone_number=local_settings.TWILIO_CALLER_ID,
voice_url='/voice',
sms_url='/sms',
host=None,
logger=None, **kwargs):
self.account_sid = account_sid
self.auth_token = auth_token
self.app_sid = app_sid
self.phone_number = phone_number
self.host = host
self.voice_url = voice_url
self.sms_url = sms_url
self.friendly_phone_number = None
self.logger = logger or logging.getLogger(__name__)
def start(self):
self.logger.info("Configuring your Twilio hackpack...")
self.logger.debug("Checking if credentials are set...")
if not self.account_sid:
raise ConfigurationError("ACCOUNT_SID is not set in "
"local_settings.")
if not self.auth_token:
raise ConfigurationError("AUTH_TOKEN is not set in "
"local_settings.")
self.logger.debug("Creating Twilio client...")
self.client = TwilioRestClient(self.account_sid, self.auth_token)
self.logger.debug("Checking if host is set.")
if not self.host:
self.logger.debug("Hostname is not set...")
self.host = self.getHerokuHostname()
# Check if urls are set.
self.logger.debug("Checking if all urls are set.")
if "://" not in self.voice_url:
self.voice_url = self.host + self.voice_url
self.logger.debug("Setting voice_url with host: "
"{0}".format(self.voice_url))
if "://" not in self.sms_url:
self.sms_url = self.host + self.sms_url
self.logger.debug("Setting sms_url with host: "
" {0}".format(self.sms_url))
if self.configureHackpack(self.voice_url, self.sms_url,
self.app_sid, self.phone_number):
# Configure Heroku environment variables.
configuration = {'TWILIO_ACCOUNT_SID': self.account_sid,
'TWILIO_AUTH_TOKEN': self.auth_token,
'TWILIO_APP_SID': self.app_sid,
'TWILIO_CALLER_ID': self.phone_number}
self.setHerokuEnvironmentVariables(**configuration)
# Ensure local environment variables are set.
self.printLocalEnvironmentVariableCommands(**configuration)
self.logger.info("\nHackpack is now configured. Call {0} to "
"test!".format(self.friendly_phone_number))
else:
logging.error("There was an error configuring your hackpack. "
"Weak sauce.")
def configureHackpack(self, voice_url, sms_url, app_sid,
phone_number, *args):
# Check if app sid is configured and available.
if not app_sid:
app = self.createNewTwiMLApp(voice_url, sms_url)
else:
app = self.setAppRequestUrls(app_sid, voice_url, sms_url)
# Check if phone_number is set.
if not phone_number:
number = self.purchasePhoneNumber()
else:
number = self.retrievePhoneNumber(phone_number)
# Configure phone number to use App Sid.
self.logger.info("Setting {0} to use application sid: "
"{0}".format(number.friendly_name, app.sid))
try:
self.client.phone_numbers.update(number.sid,
voice_application_sid=app.sid,
sms_application_sid=app.sid)
self.logger.debug("Number set.")
except TwilioException as e:
raise ConfigurationError("An error occurred setting the "
"application sid for "
"{0}: "
"{1}".format(number.friendly_name, e))
# We're done!
if number:
return number
else:
raise ConfigurationError("An unknown error occurred configuring "
"request urls for this hackpack.")
def createNewTwiMLApp(self, voice_url, sms_url):
self.logger.debug("Asking user to create new app sid...")
i = 0
while True:
i = i + 1
choice = raw_input("Your APP_SID is not configured in your "
"local_settings. Create a new one? "
"[y/n]").lower()
if choice == "y":
try:
self.logger.info("Creating new application...")
app = self.client.applications.create(voice_url=voice_url,
sms_url=sms_url,
friendly_name="Hack"
"pack"
" for "
"Hero"
"ku a"
"nd F"
"lask")
break
except TwilioException as e:
raise ConfigurationError("Your Twilio app couldn't "
"be created: {0}".format(e))
elif choice == "n" or i >= 3:
raise ConfigurationError("Your APP_SID setting must be "
"set in local_settings.")
else:
logging.error("Please choose yes or no with a 'y' or 'n'")
if app:
self.logger.info("Application created: {0}".format(app.sid))
self.app_sid = app.sid
return app
else:
raise ConfigurationError("There was an unknown error "
"creating your TwiML application.")
def setAppRequestUrls(self, app_sid, voice_url, sms_url):
self.logger.info("Setting request urls for application sid: "
"{0}".format(app_sid))
try:
app = self.client.applications.update(app_sid, voice_url=voice_url,
sms_url=sms_url,
friendly_name="Hackpack for "
"Heroku and "
"Flask")
except TwilioException as e:
if "HTTP ERROR 404" in str(e):
raise ConfigurationError("This application sid was not "
"found: {0}".format(app_sid))
else:
raise ConfigurationError("An error setting the request URLs "
"occured: {0}".format(e))
if app:
self.logger.debug("Updated application sid: {0}".format(app.sid))
return app
else:
raise ConfigurationError("An unknown error occuring "
"configuring request URLs for app sid.")
def retrievePhoneNumber(self, phone_number):
self.logger.debug("Retrieving phone number: {0}".format(phone_number))
try:
self.logger.debug("Getting sid for phone number: "
"{0}".format(phone_number))
number = self.client.phone_numbers.list(phone_number=phone_number)
except TwilioException as e:
raise ConfigurationError("An error setting the request URLs "
"occured: {0}".format(e))
if number:
self.logger.debug("Retrieved sid: {0}".format(number[0].sid))
self.friendly_phone_number = number[0].friendly_name
return number[0]
else:
raise ConfigurationError("An unknown error occurred retrieving "
"number: {0}".format(phone_number))
def purchasePhoneNumber(self):
self.logger.debug("Asking user to purchase phone number...")
i = 0
while True:
i = i + 1
# Find number to purchase
choice = raw_input("Your CALLER_ID is not configured in your "
"local_settings. Purchase a new one? "
"[y/n]").lower()
if choice == "y":
break
elif choice == "n" or i >= 3:
raise ConfigurationError("To configure this "
"hackpack CALLER_ID must set in "
"local_settings or a phone number "
"must be purchased.")
else:
logging.error("Please choose yes or no with a 'y' or 'n'")
self.logger.debug("Confirming purchase...")
i = 0
while True:
i = i + 1
# Confirm phone number purchase.
choice = raw_input("Are you sure you want to purchase? "
"Your Twilio account will be charged $1. "
"[y/n]").lower()
if choice == "y":
try:
self.logger.debug("Purchasing phone number...")
number = self.client.phone_numbers.purchase(area_code="6"
"46")
self.logger.debug("Phone number purchased: "
"{0}".format(number.friendly_name))
break
except TwilioException as e:
raise ConfigurationError("Your Twilio app couldn't "
"be created: {0}".format(e))
elif choice == "n" or i >= 3:
raise ConfigurationError("To configure this hackpack "
"CALLER_ID must set in "
"local_settings or a phone number "
"must be purchased.")
else:
logging.error("Please choose yes or no with a 'y' or 'n'")
# Return number or error out.
if number:
self.logger.debug("Returning phone number: "
"{0}".format(number.friendly_name))
self.phone_number = number.phone_number
self.friendly_phone_number = number.friendly_name
return number
else:
raise ConfigurationError("There was an unknown error purchasing "
"your phone number.")
def getHerokuHostname(self, git_config_path='./.git/config'):
self.logger.debug("Getting hostname from git configuration file: "
"{0}".format(git_config_path))
# Load git configuration
try:
self.logger.debug("Loading git config...")
git_config = open(git_config_path).readlines()
except IOError as e:
raise ConfigurationError("Could not find .git config. Does it "
"still exist? Failed path: {0}".format(e))
self.logger.debug("Finding Heroku remote in git configuration...")
subdomain = None
for line in git_config:
if "git.heroku.com" in line:
s = line.split("/")
subdomain = s[3].replace('.git', '')
self.logger.debug("Heroku remote found: {0}".format(subdomain))
# if "[email protected]" in line:
# s = line.split(":")
# subdomain = s[1].replace('.git', '')
# self.logger.debug("Heroku remote found: {0}".format(subdomain))
if subdomain:
host = "http://{0}.herokuapp.com".format(subdomain.strip())
self.logger.debug("Returning full host: {0}".format(host))
return host
else:
raise ConfigurationError("Could not find Heroku remote in "
"your .git config. Have you "
"created the Heroku app?")
def printLocalEnvironmentVariableCommands(self, **kwargs):
self.logger.info("Copy/paste these commands to set your local "
"environment to use this hackpack...\n")
for k, v in kwargs.items():
if v:
self.logger.info("export {0}={1}".format(k, v))
def setHerokuEnvironmentVariables(self, **kwargs):
self.logger.info("Setting Heroku environment variables...")
envvars = ["{0}={1}".format(k, v) for k, v in kwargs.items() if v]
envvars.insert(0, "heroku")
envvars.insert(1, "config:add")
return subprocess.call(envvars)
class ConfigurationError(Exception):
def __init__(self, message):
logger = logging.getLogger(__name__)
logger.error(message)
def parse_args(args):
""" Configures the command line interface.
Args:
Arguments from command (usually sys.argv)
Returns:
ArgumentParser object
"""
parser = ArgumentParser(description="Twilio Hackpack Configurator - an "
"easy way to configure your hackpack!",
epilog="Written by Rob Spectre.\n "
"http://www.brooklynhacker.com")
parser.add_argument("-S", "--account_sid", default=None,
help="Use a specific Twilio ACCOUNT_SID.")
parser.add_argument("-K", "--auth_token", default=None,
help="Use a specific Twilio AUTH_TOKEN.")
parser.add_argument("-n", "--new", default=False, action="store_true",
help="Purchase new Twilio phone number and configure "
"app to use your hackpack.")
parser.add_argument("-N", "--new_app", default=False, action="store_true",
help="Create a new TwiML application sid to use for "
" your hackpack.")
parser.add_argument("-a", "--app_sid", default=None,
help="Configure specific AppSid to use your hackpack.")
parser.add_argument("-#", "--phone-number", default=None,
help="Configure specific Twilio number to use your "
"hackpack.")
parser.add_argument("-v", "--voice_url", default=None,
help="Set the route for your Voice Request URL: "
"(e.g. '/voice').")
parser.add_argument("-s", "--sms_url", default=None,
help="Set the route for your SMS Request URL: "
"(e.g. '/sms').")
parser.add_argument("-d", "--domain", default=None,
help="Set a custom domain.")
parser.add_argument("-D", "--debug", default=False,
action="store_true", help="Turn on debug output.")
configure = Configure()
parser.parse_args(args, namespace=configure)
if configure.new:
configure.phone_number = None
if configure.new_app:
configure.app_sid = None
if configure.domain:
configure.host = configure.domain
# Configure logger
if configure.debug:
formatter = logging.Formatter("%(asctime)s - %(name)s - "
"%(levelname)s - - %(message)s")
level = logging.DEBUG
else:
formatter = logging.Formatter("%(message)s")
level = logging.INFO
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(level)
configure.logger = logger
return configure
if __name__ == "__main__":
if "configure.py" in sys.argv:
configure = parse_args(sys.argv[1:])
else:
configure = parse_args(sys.argv)
configure.logger.debug("Received these command line arguments: "
"{0}".format(sys.argv))
configure.start()