/
subdir_select_app_settings.live.py
341 lines (288 loc) · 22.6 KB
/
subdir_select_app_settings.live.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
#!/usr/bin/python
import os
import yaml
import cgi
import cgitb
import sys
import psutil
import platform
from commoninclude import close_cpanel_liveapisock, print_nontoast_error, print_disabled, bcrumb, return_sys_tip, return_prepend, return_label, print_header, print_footer, print_modals, print_loader, cardheader, cardfooter
__author__ = "Anoop P Alias"
__copyright__ = "Copyright Anoop P Alias"
__license__ = "GPL"
__email__ = "anoopalias01@gmail.com"
installation_path = "/opt/nDeploy" # Absolute Installation Path
app_template_file = installation_path+"/conf/apptemplates_subdir.yaml"
cpaneluser = os.environ["USER"]
user_app_template_file = installation_path+"/conf/"+cpaneluser+"_apptemplates_subdir.yaml"
backend_config_file = installation_path+"/conf/backends.yaml"
cgitb.enable()
close_cpanel_liveapisock()
form = cgi.FieldStorage()
print_header('Subdirectory Upstream Configuration')
bcrumb('Subdirectory Upstream Configuration', 'fas fa-cogs')
if form.getvalue('domain') and form.getvalue('backend') and form.getvalue('thesubdir'):
# Get the domain name from form data
mydomain = form.getvalue('domain')
mybackend = form.getvalue('backend')
thesubdir = form.getvalue('thesubdir')
profileyaml = installation_path + "/domain-data/" + mydomain
# Get data about the backends available
if os.path.isfile(backend_config_file):
with open(backend_config_file, 'r') as backend_data_yaml:
backend_data_yaml_parsed = yaml.safe_load(backend_data_yaml)
if os.path.isfile(app_template_file):
with open(app_template_file, 'r') as apptemplate_data_yaml:
apptemplate_data_yaml_parsed = yaml.safe_load(apptemplate_data_yaml)
if os.path.isfile(user_app_template_file):
with open(user_app_template_file, 'r') as user_apptemplate_data_yaml:
user_apptemplate_data_yaml_parsed = yaml.safe_load(user_apptemplate_data_yaml)
else:
commoninclude.print_error_wrapper('Error: app template data file error')
sys.exit(0)
if os.path.isfile(profileyaml):
# Get all config settings from the domains domain-data config file
with open(profileyaml, 'r') as profileyaml_data_stream:
yaml_parsed_profileyaml = yaml.safe_load(profileyaml_data_stream)
subdir_apps_dict = yaml_parsed_profileyaml.get('subdir_apps')
# If there are no entries in subdir_apps_dict or there is no specific config for the subdirectory
# We do a fresh config
if subdir_apps_dict:
if not subdir_apps_dict.get(thesubdir):
# Ok we are done with getting the settings,now lets present it to the user
print(' <!-- cPanel Starter Row -->')
print(' <div class="row justify-content-lg-center">')
print('')
print(' <!-- Column Start -->')
print(' <div class="col-lg-6">')
cardheader('New Upstream Configuration', 'fas fa-sliders-h')
print(' <div class="card-body text-center"> <!-- Card Body Start -->')
print(' <div class="alert alert-info text-center">')
print(' <p class="m-0 pb-1">You selected <span class="badge badge-warning">'+mybackend+'</span> as the new upstream type for:</p>')
print(' <kbd class="m-1">'+mydomain+'/'+thesubdir+'</kbd>')
print(' <p class="m-0 pt-1">Select the version and template for this upstream below.')
print(' </div>')
print(' <form class="form" method="post" id="toastForm8" onsubmit="return false;">')
backends_dict = backend_data_yaml_parsed.get(mybackend)
new_apptemplate_dict = apptemplate_data_yaml_parsed.get(mybackend)
if os.path.isfile(user_app_template_file):
user_new_apptemplate_dict = user_apptemplate_data_yaml_parsed.get(mybackend)
else:
user_new_apptemplate_dict = {}
print(' <div class="input-group">')
print(' <div class="input-group-prepend input-group-prepend-min">')
print(' <label class="input-group-text">Upstream</label>')
print(' </div>')
print(' <select name="backendversion" class="custom-select">')
for mybackend_version in backends_dict.keys():
print(' <option value="'+mybackend_version+'">'+mybackend_version+'</option>')
print(' </select>')
print(' </div>')
print(' <div class="input-group">')
print(' <div class="input-group-prepend input-group-prepend-min">')
print(' <label class="input-group-text">Template</label>')
print(' </div>')
print(' <select name="apptemplate" class="custom-select">')
for myapptemplate in sorted(new_apptemplate_dict.keys()):
print(' <option value="'+myapptemplate+'">'+new_apptemplate_dict.get(myapptemplate)+'</option>')
if user_new_apptemplate_dict:
for user_myapptemplate in sorted(user_new_apptemplate_dict.keys()):
print(' <option value="'+user_myapptemplate+'">'+user_new_apptemplate_dict.get(user_myapptemplate)+'</option>')
print(' </select>')
print(' </div>')
# Pass on the domain name to the next stage
print(' <input hidden name="domain" value="'+mydomain+'">')
print(' <input hidden name="backend" value="'+mybackend+'">')
print(' <input hidden name="thesubdir" value="'+thesubdir+'">')
print(' <button class="btn btn-outline-primary btn-block" type="submit">Apply Upstream Configuration</button>')
print(' </form>')
print(' </div> <!-- Card Body End -->')
cardfooter('')
else:
# We get the current app settings for the subdirectory
the_subdir_dict = subdir_apps_dict.get(thesubdir)
backend_category = the_subdir_dict.get('backend_category')
backend_version = the_subdir_dict.get('backend_version')
backend_path = the_subdir_dict.get('backend_path')
apptemplate_code = the_subdir_dict.get('apptemplate_code')
# Get the human friendly name of the app template
apptemplate_dict = apptemplate_data_yaml_parsed.get(backend_category)
apptemplate_description = apptemplate_dict.get(apptemplate_code)
if apptemplate_code in apptemplate_dict.keys():
apptemplate_description = apptemplate_dict.get(apptemplate_code)
else:
user_apptemplate_dict = user_apptemplate_data_yaml_parsed.get(backend_category)
if apptemplate_code in user_apptemplate_dict.keys():
apptemplate_description = user_apptemplate_dict.get(apptemplate_code)
# Ok we are done with getting the settings,now lets present it to the user
print(' <!-- cPanel Starter Row -->')
print(' <div class="row justify-content-lg-center">')
print('')
print(' <!-- Column Start -->')
print(' <div class="col-lg-8">')
cardheader('Upstream Configuration Settings')
print(' <div class="card-body p-0"> <!-- Card Body Start -->')
print(' <div class="row no-gutters"> <!-- Row Start -->')
print(' <div class="d-flex w-50 alert alert-light align-items-center"><i class="fas fa-play"></i> Nginx</div>')
print(' <div class="d-flex w-50 alert alert-success align-items-center justify-content-center"><i class="fas fa-check"></i> Active</div>')
# Backend
print(' <div class="d-flex w-50 alert alert-light align-items-center"><i class="fas fa-server"></i> Current Upstream</div>')
print(' <div class="d-flex w-50 alert alert-success align-items-center justify-content-center">'+backend_version+'</div>')
# Description
print(' <div class="d-flex w-50 alert alert-light align-items-center"><i class="fas fa-cog"></i> Current Template</div>')
print(' <div class="d-flex w-50 alert alert-success align-items-center justify-content-center text-center">'+apptemplate_description+'</div>')
# .htaccess
if backend_category == 'PROXY' and backend_version == 'httpd':
print(' <div class="d-flex w-50 alert alert-light align-items-center"><i class="fas fa-file-code"></i> Current .htaccess Status</div>')
print(' <div class="d-flex w-50 alert alert-success align-items-center justify-content-center"><i class="fas fa-check"></i> </div>')
else:
print(' <div class="d-flex w-50 alert alert-light align-items-center"><i class="fas fa-file-code"></i> Current .htaccess Status</div>')
print(' <div class="d-flex w-50 alert alert-danger align-items-center justify-content-center"><i class="fas fa-times"></i> Ignored</div>')
# New Upstream
print(' <div class="d-flex w-50 alert alert-light align-items-center"><i class="fas fa-server"></i> New Upstream Type</div>')
print(' <div class="d-flex w-50 alert alert-warning align-items-right justify-content-center">'+mybackend+'</div>')
print(' </div> <!-- Row End -->')
print(' </div> <!-- Card Body End -->')
print(' <div class="card-body text-center"> <!-- Card Body Start -->')
print(' <div class="alert alert-info text-center">')
print(' <p class="m-0 pb-1">You selected <span class="badge badge-warning">'+mybackend+'</span> as the new upstream type for:</p>')
print(' <kbd class="m-1">'+mydomain+'/'+thesubdir+'</kbd>')
print(' <p class="m-0 pt-1">Select the version and template for this upstream below.')
print(' </div>')
print(' <form class="form" method="post" id="toastForm8" onsubmit="return false;">')
backends_dict = backend_data_yaml_parsed.get(mybackend)
new_apptemplate_dict = apptemplate_data_yaml_parsed.get(mybackend)
if os.path.isfile(user_app_template_file):
user_new_apptemplate_dict = user_apptemplate_data_yaml_parsed.get(mybackend)
else:
user_new_apptemplate_dict = {}
if mybackend == backend_category:
print(' <div class="input-group">')
print(' <div class="input-group-prepend input-group-prepend-min">')
print(' <label class="input-group-text">Upstream</label>')
print(' </div>')
print(' <select name="backendversion" class="custom-select">')
for mybackend_version in backends_dict.keys():
if mybackend_version == backend_version:
print(' <option selected value="'+mybackend_version+'">'+mybackend_version+'</option>')
else:
print(' <option value="'+mybackend_version+'">'+mybackend_version+'</option>')
print(' </select>')
print(' </div>')
print(' <div class="input-group">')
print(' <div class="input-group-prepend input-group-prepend-min">')
print(' <label class="input-group-text">Template</label>')
print(' </div>')
print(' <select name="apptemplate" class="custom-select">')
for myapptemplate in sorted(new_apptemplate_dict.keys()):
if myapptemplate == apptemplate_code:
print(' <option selected value="'+myapptemplate+'">'+new_apptemplate_dict.get(myapptemplate)+'</option>')
else:
print(' <option value="'+myapptemplate+'">'+new_apptemplate_dict.get(myapptemplate)+'</option>')
if user_new_apptemplate_dict:
for user_myapptemplate in sorted(user_new_apptemplate_dict.keys()):
if user_myapptemplate == apptemplate_code:
print(' <option selected value="'+user_myapptemplate+'">'+user_new_apptemplate_dict.get(user_myapptemplate)+'</option>')
else:
print(' <option value="'+user_myapptemplate+'">'+user_new_apptemplate_dict.get(user_myapptemplate)+'</option>')
print(' </select>')
print(' </div>')
else:
print(' <div class="input-group">')
print(' <div class="input-group-prepend input-group-prepend-min">')
print(' <label class="input-group-text">Upstream</label>')
print(' </div>')
print(' <select name="backendversion" class="custom-select">')
for mybackend_version in backends_dict.keys():
print(' <option selected value="'+mybackend_version+'">'+mybackend_version+'</option>')
print(' </select>')
print(' </div>')
print(' <div class="input-group">')
print(' <div class="input-group-prepend input-group-prepend-min">')
print(' <label class="input-group-text">Template</label>')
print(' </div>')
print(' <select name="apptemplate" class="custom-select">')
for myapptemplate in sorted(new_apptemplate_dict.keys()):
print(' <option value="'+myapptemplate+'">'+new_apptemplate_dict.get(myapptemplate)+'</option>')
if user_new_apptemplate_dict:
for user_myapptemplate in sorted(user_new_apptemplate_dict.keys()):
print(' <option value="'+user_myapptemplate+'">'+user_new_apptemplate_dict.get(user_myapptemplate)+'</option>')
print(' </select>')
print(' </div>')
# Pass on the domain name to the next stage
print(' <input hidden name="domain" value="'+mydomain+'">')
print(' <input hidden name="backend" value="'+mybackend+'">')
print(' <input hidden name="thesubdir" value="'+thesubdir+'">')
print(' <button class="btn btn-outline-primary btn-block" type="submit">Apply Upstream Configuration</button>')
print(' </form>')
print(' </div> <!-- Card Body End -->')
cardfooter('')
else:
# Ok we are done with getting the settings,now lets present it to the user
print(' <!-- cPanel Starter Row -->')
print(' <div class="row justify-content-lg-center">')
print('')
print(' <!-- Column Start -->')
print(' <div class="col-lg-6">')
cardheader('Initial Upstream Configuration', 'fas fa-user-cog')
print(' <div class="card-body text-center"> <!-- Card Body Start -->')
print(' <div class="alert alert-info text-center">')
print(' <p class="m-0 pb-1">You selected <span class="badge badge-warning">'+mybackend+'</span> as the new upstream type for:</p>')
print(' <kbd class="m-1">'+mydomain+'/'+thesubdir+'</kbd>')
print(' <p class="m-0 pt-1">Select the version and template for this upstream below.')
print(' </div>')
print(' <form class="form" id="toastForm8" onsubmit="return false;">')
backends_dict = backend_data_yaml_parsed.get(mybackend)
new_apptemplate_dict = apptemplate_data_yaml_parsed.get(mybackend)
if os.path.isfile(user_app_template_file):
user_new_apptemplate_dict = user_apptemplate_data_yaml_parsed.get(mybackend)
else:
user_new_apptemplate_dict = {}
print(' <div class="input-group">')
print(' <div class="input-group-prepend input-group-prepend-min">')
print(' <label class="input-group-text">Upstream</label>')
print(' </div>')
print(' <select name="backendversion" class="custom-select">')
for mybackend_version in backends_dict.keys():
print(' <option value="'+mybackend_version+'">'+mybackend_version+'</option>')
print(' </select>')
print(' </div>')
print(' <div class="input-group">')
print(' <div class="input-group-prepend input-group-prepend-min">')
print(' <label class="input-group-text">Template</label>')
print(' </div>')
print(' <select name="apptemplate" class="custom-select">')
for myapptemplate in sorted(new_apptemplate_dict.keys()):
print(' <option value="'+myapptemplate+'">'+new_apptemplate_dict.get(myapptemplate)+'</option>')
for user_myapptemplate in sorted(user_new_apptemplate_dict.keys()):
print(' <option value="'+user_myapptemplate+'">'+user_new_apptemplate_dict.get(user_myapptemplate)+'</option>')
print(' </select>')
print(' </div>')
# Pass on the domain name to the next stage
print(' <input hidden name="domain" value="'+mydomain+'">')
print(' <input hidden name="backend" value="'+mybackend+'">')
print(' <input hidden name="thesubdir" value="'+thesubdir+'">')
print(' <button class="btn btn-outline-primary btn-block" type="submit">Apply Upstream Configuration</button>')
print(' </form>')
print(' </div> <!-- Card Body End -->')
cardfooter('')
else:
print_nontoast_error('Error!', 'Domain Data File IO Error!')
sys.exit(0)
else:
print_nontoast_error('Forbidden!', 'Domain Data Missing!')
sys.exit(0)
# Column End
print(' <!-- Column End -->')
print(' </div>')
print('')
print(' <!-- cPanel End Row -->')
print(' </div>')
print_footer()
print(' </div> <!-- Main Container End -->')
print('')
print_modals()
print_loader()
print(' <!-- Body End -->')
print(' </body>')
print('</html>')