-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_db.py
executable file
·211 lines (193 loc) · 6.56 KB
/
local_db.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
#!/usr/local/bin/python3
"""
This is a utility script to help set up some accounts for testing and
development. It create a registered, manager and root account for every country
currently under active development. NOTE: the passwords for every account is
just 'password'.
Run:
`local_db.py --clear` (To get rid of any existing db)
`local_db.py --setup` (To setup the db tables)
`local_db.py --populate` (To populate the tables with accounts & roles)
`local_db.py --list` (To list the acounts in the db)
If no flag is provided (i.e. you just run `local_db.py`) it will perform all
steps in the above order.
You can run these commands inside the docker container if there are database
issues.
"""
from meerkat_hermes import app
from meerkat_hermes import util
import boto3
import os
import ast
import argparse
# PARSE ARGUMENTS
parser = argparse.ArgumentParser()
parser.add_argument(
'--setup',
help='Setup the local dynamodb development database.', action='store_true'
)
parser.add_argument(
'--list',
help='List data from the local dynamodb development database.',
action='store_true'
)
parser.add_argument(
'--clear',
help='Clear the local dynamodb development database.',
action='store_true'
)
parser.add_argument(
'--populate',
help='Populate the local dynamodb development database.',
action='store_true'
)
args = parser.parse_args()
args_dict = vars(args)
# If no arguments are specified assume that we want to do everything.
if all(arg is False for arg in args_dict.values()):
print("Re-starting the dev database.")
for arg in args_dict:
args_dict[arg] = True
# Clear the database
if args.clear:
db = boto3.resource(
'dynamodb',
endpoint_url='http://dynamodb:8000',
region_name='eu-west-1'
)
try:
print('Cleaning the dev db.')
response = db.Table(app.config['SUBSCRIBERS']).delete()
response = db.Table(app.config['LOG']).delete()
print('Cleaned the db.')
except Exception as e:
print(e)
print('There has been error, probably because no tables currently '
'exist. Skipping the clean process.')
# Create the db tables required and perform any other db setup.
if args.setup:
print('Creating dev db')
# Create the client for the local database
db = boto3.client(
'dynamodb',
endpoint_url='http://dynamodb:8000',
region_name='eu-west-1'
)
# Create the required tables in the database
response = db.create_table(
TableName=app.config['SUBSCRIBERS'],
AttributeDefinitions=[
{'AttributeName': 'id', 'AttributeType': 'S'},
{'AttributeName': 'email', 'AttributeType': 'S'}
],
KeySchema=[{'AttributeName': 'id', 'KeyType': 'HASH'}],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
},
GlobalSecondaryIndexes=[{
'IndexName': 'email-index',
'KeySchema': [{
'AttributeName': 'email',
'KeyType': 'HASH'
}],
'Projection': {'ProjectionType': 'ALL'},
'ProvisionedThroughput': {
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
}],
)
print("Table {} status: {}".format(
app.config['SUBSCRIBERS'],
response['TableDescription'].get('TableStatus')
))
response = db.create_table(
TableName=app.config['LOG'],
AttributeDefinitions=[
{'AttributeName': 'id', 'AttributeType': 'S'},
{'AttributeName': 'message', 'AttributeType': 'S'}
],
KeySchema=[{'AttributeName': 'id', 'KeyType': 'HASH'}],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
},
GlobalSecondaryIndexes=[{
'IndexName': 'message-index',
'KeySchema': [{
'AttributeName': 'message',
'KeyType': 'HASH'
}],
'Projection': {'ProjectionType': 'ALL'},
'ProvisionedThroughput': {
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
}],
)
print("Table {} status: {}".format(
app.config['LOG'],
response['TableDescription'].get('TableStatus')
))
# Put initial fake data into the database.
if args.populate:
print('Populating the hermes dev db.')
try:
# Get developer accounts to be inserted into local database.
path = (os.path.dirname(os.path.realpath(__file__)) +
'/../.settings/accounts.cfg')
users_file = open(path, 'r+').read()
users = ast.literal_eval(users_file) if users_file else {}
# Create the subscriptions for the developer's accounts.
for username, user in users.items():
util.subscribe(
user['first_name'],
user['last_name'],
user['email'],
'All',
['test-emails', 'error-reporting', 'notify-dev'],
sms=user.get('sms', ''),
verified=True,
slack=user.get('slack', '')
)
print('Added subscriber: {} {}'.format(
user['first_name'], user['last_name']
))
except FileNotFoundError:
print('No developer account details available.')
print('Populated dev db.')
# Finally list all items in the database, so we know what it is populated with.
if args.list:
print('Listing data in the database.')
db = boto3.resource(
'dynamodb',
endpoint_url='http://dynamodb:8000',
region_name='eu-west-1'
)
try:
# List subscribers.
subscribers = db.Table(app.config['SUBSCRIBERS'])
subscribers = subscribers.scan().get("Items", [])
if subscribers:
print("Subscribers created:")
for subscriber in subscribers:
print("{} {} ({}) - {} {}".format(
subscriber['first_name'],
subscriber['last_name'],
subscriber['country'],
subscriber['email'],
subscriber.get('sms', '(no sms)')
))
else:
print("No subscribers exist.")
# List log.
log = db.Table(app.config['LOG'])
log = log.scan().get("Items", [])
if log:
print("Log created:")
print(log)
else:
print("No log exist.")
except Exception as e:
print("Listing failed. Has database been setup?")