forked from farrukh90/stormpath-flask-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.py
96 lines (70 loc) · 3.08 KB
/
bootstrap.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
"""
bootstrap.py
~~~~~~~~~~~~
This simple command line script will 'bootstrap' this simple Flask
application by performing a few simple tasks:
- It will collect your Stormpath credentials from you (via the command
line).
- It will get or create a new Stormpath Application in your account named
(so that we can run this project).
- It will create a `.env` file in this directory which holds 3 environment
variables needed for this sample application to run successfully.
- Lastly, it will tell you how to run this website!
"""
from stormpath.client import Client
def get_app(client):
"""Return an existing Stormpath Application if a default one exists."""
total_apps = len(client.applications)
if total_apps == 2:
for app in client.applications:
if app.name != 'Stormpath':
return app
apps = client.applications.search({'name': 'flask-stormpath-sample'})
return apps[0] if len(apps) == 1 else None
def create_app(client):
"""Create a new Stormpath Application for this project."""
try:
app = client.applications.create({
'name': 'flask-stormpath-sample',
'description': 'A sample application required to run the flask-stormpath-sample application. Feel free to delete this!',
}, create_directory=True)
print("""\n
I've just created a new Stormpath application in your account named:
flask-stormpath-sample, when you're done using this sample application, feel
free to delete it!""")
return app
except Exception as err:
print('ERROR! Failed to create Stormpath Application: {}'.format(err))
print("""
Hi, and welcome to the flask-stormpath-sample bootstrap app!
I'll help get you up and running in no time! If you don't already have a
Stormpath account, please create one: https://api.stormpath.com/register
Once you've made an account, be sure to create an API keypair in your
dashboard, and download your credentials. You'll need these to continue.
""")
id = None
while not id:
id = raw_input('To get started, please enter your Stormpath API Key ID: ')
secret = None
while not secret:
secret = raw_input('Please enter your Stormpath API Key Secret: ')
client = Client(id=id, secret=secret)
app = get_app(client)
if not app:
app = create_app(client)
env_file = open('.env', 'wb')
env_file.write('export STORMPATH_API_KEY_ID={}\n'.format(id))
env_file.write('export STORMPATH_API_KEY_SECRET={}\n'.format(secret))
env_file.write('export STORMPATH_APPLICATION={}\n'.format(app.name))
env_file.close()
print("""I've just created a new file in this directory named .env
This file contains all of the necessary environment variables to make this
sample application run!
Now that we've completed all bootstrapping stuff, all you need to do to get
your sample application running is to execute the following commands:
$ source .env
$ python app.py
You should then be able to visit 'http://localhost:5000' in your browser to
play around with the sample application!
Have questions? Email us! support@stormpath.com
""")