1 | import os |
---|
2 | import pwd |
---|
3 | import logging |
---|
4 | import shutil |
---|
5 | import commands |
---|
6 | |
---|
7 | class ExportWebSites: |
---|
8 | |
---|
9 | logging.basicConfig(format = '%(asctime)s %(message)s',datefmt = '%m/%d/%Y %I:%M:%S %p',filename = '/var/log/export-web-sites.log',level=logging.DEBUG) |
---|
10 | |
---|
11 | DEBUG=False |
---|
12 | |
---|
13 | moodle="moodle" |
---|
14 | pmb="pmb" |
---|
15 | jclic="jclic" |
---|
16 | dir_export="/etc/apache2/lliurex-location" |
---|
17 | mod_extensions=[".conf",".load"] |
---|
18 | mod_files=["proxy","proxy_html","proxy_http","rewrite","xml2enc"] |
---|
19 | mod_available="/etc/apache2/mods-available/" |
---|
20 | mod_enabled="/etc/apache2/mods-enabled/" |
---|
21 | file_sites_configuration="/etc/apache2/sites-enabled/000-default.conf" |
---|
22 | |
---|
23 | #web_sites=[moodle,jclic,pmb] |
---|
24 | |
---|
25 | |
---|
26 | def create_dict(self,web_sites_vector): |
---|
27 | |
---|
28 | try: |
---|
29 | self.dprint("Creating dictionary for web sites....") |
---|
30 | resume={} |
---|
31 | for sites in web_sites_vector: |
---|
32 | resume[sites]={} |
---|
33 | suffix='.conf' |
---|
34 | site_path=os.path.join(self.dir_export, sites + suffix) |
---|
35 | resume[sites]["file"]=site_path |
---|
36 | resume[sites]["install"]=False |
---|
37 | self.dprint("%s : %s"%(sites,resume[sites])) |
---|
38 | self.dprint("Creating dictionary for web sites: %s"%resume) |
---|
39 | return [True,resume] |
---|
40 | |
---|
41 | except Exception as e: |
---|
42 | print ("[ExportWebSitesServer] %s"%e) |
---|
43 | self.dprint(e) |
---|
44 | return [False,e] |
---|
45 | |
---|
46 | #def create_dict |
---|
47 | |
---|
48 | def lprint(self,arg): |
---|
49 | |
---|
50 | logging.debug(arg) |
---|
51 | |
---|
52 | #def_lprint |
---|
53 | |
---|
54 | def dprint(self,arg): |
---|
55 | |
---|
56 | self.lprint("[ExportWebSitesServer] %s"%arg) |
---|
57 | if ExportWebSites.DEBUG: |
---|
58 | print("[ExportWebSitesServer] %s"%arg) |
---|
59 | |
---|
60 | #def dprint |
---|
61 | |
---|
62 | def export_moodle(self): |
---|
63 | |
---|
64 | try: |
---|
65 | self.dprint("Moodle is exporting....") |
---|
66 | |
---|
67 | # Exporting lliurex-location/moodle.conf file to external netcard |
---|
68 | |
---|
69 | if self.directory_exist(self.dir_export): |
---|
70 | suffix='.conf' |
---|
71 | site_path=os.path.join(self.dir_export, "moodle" + suffix) |
---|
72 | if not os.path.isfile(site_path): |
---|
73 | os.mknod(site_path) |
---|
74 | os.system('chmod 644 %s'%site_path) |
---|
75 | with open(site_path,'w') as file: |
---|
76 | |
---|
77 | file.write('RewriteEngine on\n') |
---|
78 | file.write('RewriteCond "%{REQUEST_URI}" "^/moodle$"\n') |
---|
79 | file.write('RewriteRule ".*" http://server/moodle/ [R=permament]\n') |
---|
80 | |
---|
81 | file.write('<Location /moodle>\n') |
---|
82 | |
---|
83 | file.write('RewriteCond "%{REQUEST_URI}" "^/moodle/moodle/(.+)"\n') |
---|
84 | file.write('RewriteRule ".*" "http://moodle/%1" [P,QSA,L]\n') |
---|
85 | file.write('RewriteCond "%{REQUEST_URI}" "^/moodle/moodle/$"\n') |
---|
86 | file.write('RewriteRule ".*" "http://moodle/" [P]\n') |
---|
87 | file.write('RewriteRule ".*" "http:/%{REQUEST_URI}" [P]\n') |
---|
88 | |
---|
89 | file.write('SetOutputFilter INFLATE;proxy-html;DEFLATE\n') |
---|
90 | file.write('ProxyHTMLExtended On\n') |
---|
91 | file.write('ProxyHTMLURLMap "http://moodle" "http://server"\n') |
---|
92 | file.write('ProxyHTMLURLMap "http://moodle/" "http://server/"\n') |
---|
93 | file.write('ProxyHTMLURLMap "http:\\/\\/moodle" "http:\\/\\/server" x\n') |
---|
94 | file.write('ProxyHTMLURLMap "http:\\/\\/moodle\\/" "http:\\/\\/server\\/" x\n') |
---|
95 | file.write('ProxyPassReverse http://moodle\n') |
---|
96 | |
---|
97 | file.write('</Location>\n') |
---|
98 | |
---|
99 | file.write('RewriteCond %{REQUEST_URI} ^/theme/.*$\n') |
---|
100 | file.write('RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f\n') |
---|
101 | file.write('RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-d\n') |
---|
102 | file.write('RewriteRule "^(.*)$" "http://server/moodle/moodle/%{REQUEST_URI}" [P,QSA]\n') |
---|
103 | |
---|
104 | self.dprint("/etc/apache2/lliurex-location/moodle.conf file is created to exported....") |
---|
105 | |
---|
106 | |
---|
107 | # Modify sites-avaiable/moodle.conf in internal netcard |
---|
108 | |
---|
109 | modify=True |
---|
110 | suffix='.conf' |
---|
111 | moodle_site_available=os.path.join(self.site_available, "moodle" + suffix) |
---|
112 | if os.path.isfile(moodle_site_available): |
---|
113 | with open(moodle_site_available, "r") as in_file: |
---|
114 | buf = in_file.readlines() |
---|
115 | with open(moodle_site_available, "w") as out_file: |
---|
116 | for line in buf: |
---|
117 | if "RewriteEngine" in line: |
---|
118 | modify=False |
---|
119 | else: |
---|
120 | if ( "Alias /moodle" in line ) & (modify): |
---|
121 | line = 'RewriteEngine on\nRewriteCond "%{REQUEST_URI}" "^/moodle/(.*)$"\nRewriteRule ".*" "http://moodle/%1" [P]\n' + line |
---|
122 | out_file.write(line) |
---|
123 | |
---|
124 | self.dprint("/etc/apache2/sites-available/moodle.conf file modified to integer moodle in export netcard....") |
---|
125 | |
---|
126 | modify=True |
---|
127 | moodle_config="/usr/share/moodle/config.php" |
---|
128 | if os.path.isfile(moodle_config): |
---|
129 | with open(moodle_config, "r") as in_file: |
---|
130 | buf = in_file.readlines() |
---|
131 | with open(moodle_config, "w") as out_file: |
---|
132 | for line in buf: |
---|
133 | if "reverseproxy" in line: |
---|
134 | modify=False |
---|
135 | else: |
---|
136 | if ( "$CFG->admin" in line ) & (modify): |
---|
137 | line = line + ' $CFG->reverseproxy = 1;\n' |
---|
138 | out_file.write(line) |
---|
139 | |
---|
140 | self.dprint("/usr/share/moodle/config.conf file modified to integer moodle in export netcard....") |
---|
141 | |
---|
142 | return [True] |
---|
143 | |
---|
144 | except Exception as e: |
---|
145 | print ("[ExportWebSitesServer] %s"%e) |
---|
146 | self.dprint(e) |
---|
147 | return [False,str(e)] |
---|
148 | |
---|
149 | #def export_moodle |
---|
150 | |
---|
151 | def export_jclic(self): |
---|
152 | |
---|
153 | try: |
---|
154 | self.dprint("JCLIC is exporting....") |
---|
155 | if self.directory_exist(self.dir_export): |
---|
156 | suffix='.conf' |
---|
157 | site_path=os.path.join(self.dir_export, "jclic" + suffix) |
---|
158 | if not os.path.isfile(site_path): |
---|
159 | #os.mknod(site_path) |
---|
160 | pass |
---|
161 | self.dprint("Jclic file is created to exported....") |
---|
162 | return [True] |
---|
163 | |
---|
164 | except Exception as e: |
---|
165 | print ("[ExportWebSitesServer] %s"%e) |
---|
166 | self.dprint(e) |
---|
167 | return [False,str(e)] |
---|
168 | |
---|
169 | #def export_jclic |
---|
170 | |
---|
171 | def export_pmb(self): |
---|
172 | |
---|
173 | try: |
---|
174 | self.dprint("PMB is exporting....") |
---|
175 | if self.directory_exist(self.dir_export): |
---|
176 | suffix='.conf' |
---|
177 | site_path=os.path.join(self.dir_export, "pmb" + suffix) |
---|
178 | if not os.path.isfile(site_path): |
---|
179 | os.mknod(site_path) |
---|
180 | os.system('chmod 644 %s'%site_path) |
---|
181 | with open(site_path,'w') as file: |
---|
182 | file.write('<Location /pmb/>\n') |
---|
183 | file.write(' ProxyPass http://pmb:800/\n') |
---|
184 | file.write(' ProxyPassReverse http://pmb:800/\n') |
---|
185 | file.write('</Location>\n') |
---|
186 | site_path=os.path.join(self.dir_export, "opac" + suffix) |
---|
187 | if not os.path.isfile(site_path): |
---|
188 | os.mknod(site_path) |
---|
189 | os.system('chmod 644 %s'%site_path) |
---|
190 | with open(site_path,'w') as file: |
---|
191 | file.write('<Location /opac/>\n') |
---|
192 | file.write(' ProxyPass http://opac:800/\n') |
---|
193 | file.write(' ProxyPassReverse http://opac:800/\n') |
---|
194 | file.write('</Location>\n') |
---|
195 | self.dprint("PMB file is created to exported....") |
---|
196 | return [True] |
---|
197 | |
---|
198 | except Exception as e: |
---|
199 | print ("[ExportWebSitesServer] %s"%e) |
---|
200 | self.dprint(e) |
---|
201 | return [False,str(e)] |
---|
202 | |
---|
203 | #def export_pmb |
---|
204 | |
---|
205 | def un_export_moodle(self): |
---|
206 | |
---|
207 | try: |
---|
208 | self.dprint("Moodle is cancelling the exportation....") |
---|
209 | if self.directory_exist(self.dir_export): |
---|
210 | suffix='.conf' |
---|
211 | site_path=os.path.join(self.dir_export, "moodle" + suffix) |
---|
212 | if os.path.isfile(site_path): |
---|
213 | os.remove(site_path) |
---|
214 | self.dprint("Moodle removed !!!") |
---|
215 | |
---|
216 | # Modify sites-avaiable/moodle.conf in internal netcard |
---|
217 | |
---|
218 | modify=True |
---|
219 | suffix='.conf' |
---|
220 | moodle_site_available=os.path.join(self.site_available, "moodle" + suffix) |
---|
221 | if os.path.isfile(moodle_site_available): |
---|
222 | with open(moodle_site_available, "r") as in_file: |
---|
223 | buf = in_file.readlines() |
---|
224 | with open(moodle_site_available, "w") as out_file: |
---|
225 | for line in buf: |
---|
226 | if not "Rewrite" in line: |
---|
227 | out_file.write(line) |
---|
228 | |
---|
229 | self.dprint("/etc/apache2/sites-available/moodle.conf file modified to cancel moodle in export netcard....") |
---|
230 | |
---|
231 | |
---|
232 | return [True] |
---|
233 | |
---|
234 | except Exception as e: |
---|
235 | print ("[ExportWebSitesServer] %s"%e) |
---|
236 | self.dprint(e) |
---|
237 | return [False,str(e)] |
---|
238 | |
---|
239 | #def un_export_moodle |
---|
240 | |
---|
241 | def un_export_jclic(self): |
---|
242 | |
---|
243 | try: |
---|
244 | self.dprint("JCLIC is cancelling the exportation....") |
---|
245 | if self.directory_exist(self.dir_export): |
---|
246 | suffix='.conf' |
---|
247 | site_path=os.path.join(self.dir_export, "jclic" + suffix) |
---|
248 | if os.path.isfile(site_path): |
---|
249 | os.remove(site_path) |
---|
250 | self.dprint("Jclic removed !!!") |
---|
251 | return [True] |
---|
252 | |
---|
253 | except Exception as e: |
---|
254 | print ("[ExportWebSitesServer] %s"%e) |
---|
255 | self.dprint(e) |
---|
256 | return [False,str(e)] |
---|
257 | |
---|
258 | #def un_export_jclic |
---|
259 | |
---|
260 | def un_export_pmb(self): |
---|
261 | |
---|
262 | try: |
---|
263 | self.dprint("PMB is cancelling the exportation....") |
---|
264 | if self.directory_exist(self.dir_export): |
---|
265 | suffix='.conf' |
---|
266 | site_path=os.path.join(self.dir_export, "pmb" + suffix) |
---|
267 | if os.path.isfile(site_path): |
---|
268 | os.remove(site_path) |
---|
269 | site_path=os.path.join(self.dir_export, "opac" + suffix) |
---|
270 | if os.path.isfile(site_path): |
---|
271 | os.remove(site_path) |
---|
272 | self.dprint("Pmb removed !!!") |
---|
273 | return [True] |
---|
274 | |
---|
275 | except Exception as e: |
---|
276 | print ("[ExportWebSitesServer] %s"%e) |
---|
277 | self.dprint(e) |
---|
278 | return [False,str(e)] |
---|
279 | |
---|
280 | #def un_export_pmb |
---|
281 | |
---|
282 | def read_export_sites(self,web_sites): |
---|
283 | |
---|
284 | try: |
---|
285 | self.dprint("READING export sites: %s"%web_sites) |
---|
286 | resume=self.create_dict(web_sites) |
---|
287 | if resume[0]: |
---|
288 | resume=resume[1] |
---|
289 | for service in resume: |
---|
290 | if os.path.exists(resume[service]["file"]): |
---|
291 | resume[service]["install"]=True |
---|
292 | self.dprint(resume) |
---|
293 | return [True,resume] |
---|
294 | else: |
---|
295 | return [False] |
---|
296 | |
---|
297 | except Exception as e: |
---|
298 | print ("[ExportWebSitesServer] %s"%e) |
---|
299 | self.dprint(e) |
---|
300 | return [False,str(e)] |
---|
301 | |
---|
302 | #def read_export_sites |
---|
303 | |
---|
304 | |
---|
305 | def directory_exist(self,directory): |
---|
306 | try: |
---|
307 | self.dprint("Testing directory: %s"%directory) |
---|
308 | if not os.path.exists(directory): |
---|
309 | os.makedirs(directory) |
---|
310 | return [True] |
---|
311 | |
---|
312 | except Exception as e: |
---|
313 | print ("[ExportWebSitesServer] %s"%e) |
---|
314 | self.dprint(e) |
---|
315 | return [False,str(e)] |
---|
316 | |
---|
317 | #def directory_exist |
---|
318 | |
---|
319 | def export_active(self): |
---|
320 | try: |
---|
321 | self.dprint("Activating Exportation.....") |
---|
322 | for filename in self.mod_files: |
---|
323 | for extension in self.mod_extensions: |
---|
324 | if ( filename in ("proxy_http","rewrite","xml2enc") ): |
---|
325 | extension=".load" |
---|
326 | self.dprint("Creating path file, Path: %s Filename: %s Extension: %s"%(self.mod_enabled,filename, extension)) |
---|
327 | path_mod_enabled=os.path.join(self.mod_enabled, filename + extension) |
---|
328 | path_mod_available=os.path.join(self.mod_available, filename + extension) |
---|
329 | if not os.path.exists(path_mod_enabled): |
---|
330 | self.dprint("Creating link...") |
---|
331 | os.symlink(path_mod_available,path_mod_enabled) |
---|
332 | else: |
---|
333 | self.dprint("Link exists, do nothing...") |
---|
334 | return [True] |
---|
335 | |
---|
336 | except Exception as e: |
---|
337 | print ("[ExportWebSitesServer] %s"%e) |
---|
338 | self.dprint(e) |
---|
339 | return [False,str(e)] |
---|
340 | #def export_active |
---|
341 | |
---|
342 | def apache2_restart(self): |
---|
343 | try: |
---|
344 | status, output = commands.getstatusoutput("systemctl restart apache2") |
---|
345 | self.dprint("*** APACHE2 Service ***") |
---|
346 | self.dprint("Status: %s"%status) |
---|
347 | self.dprint("Output: %s"%output) |
---|
348 | return [True,status,output] |
---|
349 | |
---|
350 | except Exception as e: |
---|
351 | print ("[ExportWebSitesServer] %s"%e) |
---|
352 | self.dprint(e) |
---|
353 | return [False,str(e)] |
---|
354 | # def apache2_restart |
---|
355 | |
---|
356 | def sites_configuration(self): |
---|
357 | try: |
---|
358 | modify=True |
---|
359 | path, dirs, files = next(os.walk(self.dir_export)) |
---|
360 | number_files=len(files) |
---|
361 | self.dprint("*** Configuration File ***") |
---|
362 | if ( number_files > 0 ): |
---|
363 | self.dprint("Adding configuration line.....") |
---|
364 | if os.path.isfile(self.file_sites_configuration): |
---|
365 | with open(self.file_sites_configuration, "r") as in_file: |
---|
366 | buf = in_file.readlines() |
---|
367 | with open(self.file_sites_configuration, "w") as out_file: |
---|
368 | for line in buf: |
---|
369 | if "lliurex-location" in line: |
---|
370 | modify=False |
---|
371 | else: |
---|
372 | if ( "<Directory /var/www/admin-center>" in line ) & (modify): |
---|
373 | line = "include /etc/apache2/lliurex-location/*.conf\n" + line |
---|
374 | out_file.write(line) |
---|
375 | else: |
---|
376 | if os.path.isfile(self.file_sites_configuration): |
---|
377 | self.dprint("Deleting configuration line.....") |
---|
378 | self.dprint("Compare this lines:") |
---|
379 | with open(self.file_sites_configuration, "r") as in_file: |
---|
380 | buf = in_file.readlines() |
---|
381 | with open(self.file_sites_configuration, "w") as out_file: |
---|
382 | for line in buf: |
---|
383 | self.dprint(line) |
---|
384 | if "lliurex-location" in line: |
---|
385 | self.dprint("Deleting.......!!!!!!!!!!!!") |
---|
386 | line = "\n" |
---|
387 | out_file.write(line) |
---|
388 | |
---|
389 | return [True] |
---|
390 | |
---|
391 | except Exception as e: |
---|
392 | print ("[ExportWebSitesServer] %s"%e) |
---|
393 | self.dprint(e) |
---|
394 | return [False,str(e)] |
---|
395 | #def sites_configuration |
---|