/
select_app_settings.live.py
226 lines (183 loc) · 12.7 KB
/
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
#!/usr/bin/python
import commoninclude
import os
import yaml
import cgi
import cgitb
import sys
import psutil
import platform
from commoninclude import close_cpanel_liveapisock, bcrumb, print_nontoast_error, return_prepend, 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.yaml"
cpaneluser = os.environ["USER"]
user_app_template_file = installation_path+"/conf/"+cpaneluser+"_apptemplates.yaml"
backend_config_file = installation_path+"/conf/backends.yaml"
cgitb.enable()
close_cpanel_liveapisock()
form = cgi.FieldStorage()
print_header('Upstream Configuration')
bcrumb('Upstream Configuration', 'fas fa-cogs')
if form.getvalue('domain') and form.getvalue('backend'):
# Get the domain name from form data
mydomain = form.getvalue('domain')
mybackend = form.getvalue('backend')
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(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)
# App settings
backend_category = yaml_parsed_profileyaml.get('backend_category')
backend_version = yaml_parsed_profileyaml.get('backend_version')
backend_path = yaml_parsed_profileyaml.get('backend_path')
apptemplate_code = yaml_parsed_profileyaml.get('apptemplate_code')
# Get the human friendly name of the app template
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)
apptemplate_dict = apptemplate_data_yaml_parsed.get(backend_category)
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)
user_apptemplate_dict = user_apptemplate_data_yaml_parsed.get(backend_category)
else:
user_apptemplate_dict = {}
if apptemplate_code in apptemplate_dict.keys():
apptemplate_description = apptemplate_dict.get(apptemplate_code)
else:
if apptemplate_code in user_apptemplate_dict.keys():
apptemplate_description = user_apptemplate_dict.get(apptemplate_code)
else:
print_nontoast_error('Error!', 'Application Template Data File Error!')
sys.exit(0)
# 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')
print(' <div class="card-body p-0"> <!-- Card Body Start -->')
print(' <div class="row no-gutters"> <!-- Row Start -->') #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"> <!-- 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+'</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="toastForm2" 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 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(' <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>')