This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
post_installation_script.py
126 lines (93 loc) · 3.2 KB
/
post_installation_script.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
#!/usr/bin/env python3
"""
This script helps you to automate the creation of helper files and configurations to integrate
the front-end and the backend.
"""
import json
from multiprocessing.sharedctypes import Value
import boto3
import os
# Configuration files
CONFIGFILE = 'outputs.json'
DB_SCHEMA = 'ivs_moderation/schemas/db_default.json'
WEB_CONFIG_FILE = '../web-ui/.env'
def create_file(contents):
''' Function to create file '''
configs = ''
for key, val in contents.items():
configs = configs + key + '=' + val + '\n'
try:
file = open(WEB_CONFIG_FILE, 'w+')
try:
file.write(configs)
except Exception as error:
print(error)
finally:
file.close()
except Exception as error:
print(error)
def read_file(filename):
''' Function to read the files '''
try:
file = open(filename, 'r')
try:
content = file.read()
except Exception as error:
print(error)
finally:
file.close()
except Exception as error:
print(error)
return json.loads(content)
def deploy_db_defaults(dbtable, content):
''' Function to deploy default parameters '''
db = boto3.resource('dynamodb')
table = db.Table(dbtable)
for item in ('action', 'alert'):
try:
response = table.put_item(
Item=content[item]
)
except Exception as error:
print(error)
raise
return response
def create_web_config_file(contents):
''' Function to create the environment file for the UI '''
for key in contents['ivs-moderation'].keys():
if str(key).startswith('ivsmoderationapiEndpoint'):
api_gw_url = key
web_config = {
'REACT_APP_IDENTITY_POOL_ID': contents['ivs-moderation']['identitypoolid'],
'REACT_APP_REGION': contents['ivs-moderation']['region'],
'REACT_APP_USER_POOL_ID': contents['ivs-moderation']['userpoolsid'],
'REACT_APP_USER_POOL_WEB_CLIENT_ID': contents['ivs-moderation']['userpoolswebclientid'],
'REACT_APP_API_GATEWAY': contents['ivs-moderation'][api_gw_url],
'REACT_APP_AWS_APPSYNC_GRAPHQLENDPOINT': contents['ivs-moderation']['awsappsyncgraphqlEndpoint'],
'REACT_APP_AWS_APPSYNC_AUTHENTICATIONTYPE': 'AMAZON_COGNITO_USER_POOLS'
}
create_file(web_config)
def reset_config_files():
''' Resetting the config files '''
if os.path.isfile(WEB_CONFIG_FILE):
try:
os.remove(WEB_CONFIG_FILE)
except Exception as error:
print(error)
def main():
''' Main Function '''
config = read_file(CONFIGFILE)
db_content = read_file(DB_SCHEMA)
# Finding the settings dbtable from the configuration
settings_table = config['ivs-moderation']['settingsdbtable']
# deploying the default settings
print('Updating the db with default settings....')
deploy_db_defaults(settings_table, db_content)
# Creating the web config
print('Creating the web-ui config.....')
reset_config_files()
create_web_config_file(config)
print('Completed the updates.....')
# Main function
if __name__ == '__main__':
main()