-
Notifications
You must be signed in to change notification settings - Fork 1
/
configure.py
272 lines (210 loc) · 7.74 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
#!/usr/bin/env python3
from getpass import getpass
import json
import os
import re
import shutil
import sys
class ConfigFile:
def __init__(self, *args, **kwargs):
filename = kwargs.get('filename')
with open(filename, 'r') as f:
self.contents = f.read()
def configure_file(self, variables):
pass
class DotEnv(ConfigFile):
def __init__(self):
super().__init__(filename='./config/templates/.env')
self.title = ''
self.blurb = ''
self.backend_url = ''
def __str__(self):
return self.contents.format(self.title, self.blurb, self.backend_url)
def configure_file(self, variables):
self.title = variables.get('title')
self.blurb = variables.get('blurb')
self.backend_url = variables.get('backend_url')
return str(self)
class DatabaseXML(ConfigFile):
def __init__(self):
super().__init__(filename='./config/templates/database.xml')
self.db_password = ''
def __str__(self):
return self.contents.format(self.db_password)
def configure_file(self, variables):
self.db_password = variables.get('db_password')
return str(self)
class JKSProperties(ConfigFile):
def __init__(self):
super().__init__(filename='./config/templates/jks.properties')
self.store_pass = ''
self.key_pass = ''
def __str__(self):
return self.contents.format(self.store_pass, self.key_pass)
def configure_file(self, variables):
self.store_pass = variables.get('store_pass')
self.key_pass = variables.get('key_pass')
return str(self)
class PomXML(ConfigFile):
def __init__(self):
super().__init__(filename='./config/templates/pom.xml')
self.store_pass = ''
self.key_pass = ''
def __str__(self):
output_contents = re.sub(r"STOREPASSHERE", self.store_pass, self.contents)
output_contents = re.sub(r"KEYPASSHERE", self.key_pass, output_contents)
return output_contents
def configure_file(self, variables):
self.store_pass = variables.get('store_pass')
self.key_pass = variables.get('key_pass')
return str(self)
class WebProperties(ConfigFile):
def __init__(self):
super().__init__(filename='./config/templates/web.properties')
self.app_root = ''
self.frontend_origin = ''
def __str__(self):
return self.contents.format(self.app_root, self.frontend_origin)
def configure_file(self, variables):
self.app_root = variables.get('app_root')
self.frontend_origin = variables.get('frontend_origin')
return str(self)
class PackageJSON(ConfigFile):
def __init__(self):
super().__init__(filename='./package.json')
self.json = json.loads(self.contents)
def __str__(self):
return json.dumps(self.json, indent=2)
def configure_file(self, variables):
homepage = variables.get('homepage')
self.json['homepage'] = homepage
return str(self)
class DatabaseUserSQL(ConfigFile):
def __init__(self):
super().__init__(filename='./config/templates/database_users.sql')
self.db_password = ''
def __str__(self):
return self.contents.format(self.db_password)
def configure_file(self, variables):
self.db_password = variables.get('db_password')
return str(self)
def is_branch_master():
head_contents = None
with open('./.git/HEAD', 'r') as f:
head_contents = f.read()
if re.search(r"master$", head_contents) is None:
return False
return True
def successful_exit():
print("""Apply the ./config/database_users.sql file to your MySQL server.
TO CREATE THE DATABASE SCHEMA:
Run the backend server in migration mode via: --mode migrate
TO CREATE BLOG USERS:
Create initial blog users with: --mode shell
""")
return sys.exit(0)
if __name__ == '__main__':
if is_branch_master():
print("Please checkout to a new branch before running configure.")
sys.exit(1)
# .ENV VARIABLES
blog_title = input('Please enter the name of the blog: ')
blog_blurb = input('Please enter the blog\'s blurb: ')
blog_backend_url = input('Please enter where the backend is located (either full URL or relative path): ')
# DATABASE.XML, POM.XML, & JKS.PROPERTIES VARIABLES
database_pw = getpass('Enter database user\'s password: ')
keystore_pw = getpass('Enter keystore password: ')
key_pw = getpass('Enter the key itself\'s password: ')
# WEB.PROPERTIES VARIABLES
backend_root = input('Please enter the backend\'s root (leave blank if \'/\'): ')
backend_frontend_origin = input('Please enter the CORS origin for the frontend: ')
# PACKAGE.JSON VARIABLES
frontend_homepage = input('Please enter the subdirectory the frontend will be deployed in (leave blank if none): ')
if not backend_root:
backend_root = '/'
env_file = DotEnv()
database_xml = DatabaseXML()
db_user_sql = DatabaseUserSQL()
jks_props = JKSProperties()
pom_xml = PomXML()
web_props = WebProperties()
package_json = PackageJSON() if frontend_homepage else None
# WRITE GENERATED FILES
with open('./config/.env', 'w') as f:
f.write(env_file.configure_file({
'title': blog_title,
'blurb': blog_blurb,
'backend_url': blog_backend_url,
}))
with open('./config/database.xml', 'w') as f:
f.write(database_xml.configure_file({
'db_password': database_pw,
}))
with open('./config/database_users.sql', 'w') as f:
f.write(db_user_sql.configure_file({
'db_password': database_pw,
}))
with open('./config/jks.properties', 'w') as f:
f.write(jks_props.configure_file({
'store_pass': keystore_pw,
'key_pass': key_pw,
}))
with open('./config/pom.xml', 'w') as f:
f.write(pom_xml.configure_file({
'store_pass': keystore_pw,
'key_pass': key_pw,
}))
with open('./config/web.properties', 'w') as f:
f.write(web_props.configure_file({
'app_root': backend_root,
'frontend_origin': backend_frontend_origin,
}))
if package_json is not None:
with open('./config/package.json', 'w') as f:
f.write(package_json.configure_file({
'homepage': frontend_homepage,
}))
print('\n\nConfiguration has been applied to configurable files in ./config/')
# CONFIRM WHETHER TO PROCEED WITH COPY
do_copy = None
while True:
do_copy = input('Do you wish to automatically copy these files now? [Y/N]: ')
if 'Y' in do_copy.upper():
break
elif 'N' in do_copy.upper():
successful_exit()
# BACKUP NON-TRACKED FILES
if os.path.exists('./.env'):
shutil.move('./.env', './.env_backup')
if os.path.exists('./server/src/main/resources/keystore.jks'):
shutil.move(
'./server/src/main/resources/keystore.jks',
'./server/src/main/resources/keystore.jks.bak'
)
# MOVE CONFIGURED FILES TO WHERE THEY GO
shutil.move(
'./config/database.xml',
'./server/src/main/resources/database.xml'
)
shutil.move(
'./config/jks.properties',
'./server/src/main/resources/jks.properties'
)
shutil.move(
'./config/web.properties',
'./server/src/main/resources/web.properties'
)
shutil.move(
'./config/pom.xml',
'./server/pom.xml'
)
shutil.move(
'./config/.env',
'./.env'
)
if os.path.exists('./config/package.json'):
shutil.move(
'./config/package.json',
'./package.json'
)
successful_exit()