|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import subprocess as sub |
| 4 | +__author__ = 'Benjamin P. Trachtenberg' |
| 5 | +__copyright__ = "Copyright (c) 2017, Benjamin P. Trachtenberg" |
| 6 | +__credits__ = None |
| 7 | +__license__ = 'The MIT License (MIT)' |
| 8 | +__status__ = 'prod' |
| 9 | +__version_info__ = (1, 0, 0, __status__) |
| 10 | +__version__ = '.'.join(map(str, __version_info__)) |
| 11 | +__maintainer__ = 'Benjamin P. Trachtenberg' |
| 12 | +__email__ = 'e_ben_75-python@yahoo.com' |
| 13 | + |
| 14 | + |
| 15 | +def create_conf(folder_name): |
| 16 | + """ |
| 17 | + Function to create apache conf files for loaded sites |
| 18 | + Currently only non-SSL Sites |
| 19 | + :param folder_name: The folder name of the site |
| 20 | + :return: |
| 21 | + """ |
| 22 | + temp_list = list() |
| 23 | + temp_list.append('<VirtualHost *:80>') |
| 24 | + if os.environ.get('SITE_SERVER_NAME'): |
| 25 | + temp_list.append(' ServerName %s' % (os.environ.get('SITE_SERVER_NAME'),)) |
| 26 | + |
| 27 | + else: |
| 28 | + temp_list.append(' # ServerName www.example.com') |
| 29 | + |
| 30 | + if os.environ.get('SITE_SERVER_ADMIN'): |
| 31 | + temp_list.append(' ServerAdmin %s' % (os.environ.get('SITE_SERVER_ADMIN'),)) |
| 32 | + |
| 33 | + else: |
| 34 | + temp_list.append(' ServerAdmin webmaster@localhost') |
| 35 | + temp_list.append('') |
| 36 | + temp_list.append(' Alias /media/ /DjangoSites/%s/media/' % (folder_name,)) |
| 37 | + temp_list.append('') |
| 38 | + temp_list.append(' <Directory /DjangoSites/%s/media>' % (folder_name,)) |
| 39 | + temp_list.append(' Order deny,allow') |
| 40 | + temp_list.append(' Require all granted') |
| 41 | + temp_list.append(' </Directory>') |
| 42 | + temp_list.append('') |
| 43 | + temp_list.append(' Alias /static/ /DjangoSites/%s/static/site/' % (folder_name,)) |
| 44 | + temp_list.append('') |
| 45 | + temp_list.append(' <Directory /DjangoSites/%s/static/site>' % (folder_name,)) |
| 46 | + temp_list.append(' Order deny,allow') |
| 47 | + temp_list.append(' Require all granted') |
| 48 | + temp_list.append(' </Directory>') |
| 49 | + temp_list.append('') |
| 50 | + temp_list.append(' WSGIScriptAlias / /DjangoSites/%s/apache/django.wsgi process-group=%s' % (folder_name, |
| 51 | + folder_name)) |
| 52 | + temp_list.append(' WSGIDaemonProcess %s' % (folder_name,)) |
| 53 | + temp_list.append(' WSGIProcessGroup %s' % (folder_name,)) |
| 54 | + temp_list.append('') |
| 55 | + temp_list.append(' <Directory /DjangoSites/%s/apache>' % (folder_name,)) |
| 56 | + temp_list.append(' Order deny,allow') |
| 57 | + temp_list.append(' Require all granted') |
| 58 | + temp_list.append(' </Directory>') |
| 59 | + temp_list.append('') |
| 60 | + temp_list.append(' # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,') |
| 61 | + temp_list.append(' # error, crit, alert, emerg.') |
| 62 | + temp_list.append(' # It is also possible to configure the loglevel for particular') |
| 63 | + temp_list.append(' # modules, e.g.') |
| 64 | + temp_list.append(' LogLevel info') |
| 65 | + temp_list.append('') |
| 66 | + temp_list.append(' ErrorLog ${APACHE_LOG_DIR}/error.log') |
| 67 | + temp_list.append(' CustomLog ${APACHE_LOG_DIR}/access.log combined') |
| 68 | + temp_list.append('') |
| 69 | + temp_list.append('</VirtualHost>') |
| 70 | + temp_list.append('') |
| 71 | + temp_list.append('# vim: syntax=apache ts=4 sw=4 sts=4 sr noet') |
| 72 | + |
| 73 | + def output_file(orig_list, file_name): |
| 74 | + """ |
| 75 | + Function to output the file |
| 76 | + :param orig_list: |
| 77 | + :param file_name: |
| 78 | + :return: file_name |
| 79 | + """ |
| 80 | + |
| 81 | + file_name = '%s.conf' % ('-'.join(file_name.split()).lower(), ) |
| 82 | + |
| 83 | + def add_line_break(list_line): |
| 84 | + """ |
| 85 | + Create a line break at the end of a string |
| 86 | + Args: |
| 87 | + list_line: string |
| 88 | + Returns: A string with a line break |
| 89 | + """ |
| 90 | + list_line = ('%s\n' % (list_line,)) |
| 91 | + return list_line |
| 92 | + |
| 93 | + write_file = open('/etc/apache2/sites-available/%s' % (file_name, ), "a") |
| 94 | + for orig_list_line in orig_list: |
| 95 | + write_file.write(add_line_break(str(orig_list_line))) |
| 96 | + write_file.close() |
| 97 | + |
| 98 | + return file_name |
| 99 | + |
| 100 | + site_to_enable = output_file(temp_list, folder_name) |
| 101 | + |
| 102 | + sub.call('a2ensite %s' % (site_to_enable,), stderr=sub.STDOUT, shell=True) |
| 103 | + |
| 104 | + |
| 105 | +def main(): |
| 106 | + """ |
| 107 | + Main script function |
| 108 | + :return: None |
| 109 | +
|
| 110 | + """ |
| 111 | + for item_name in os.listdir(os.getcwd()): |
| 112 | + if os.path.isdir(item_name): |
| 113 | + create_conf(item_name) |
| 114 | + |
| 115 | + sub.call('a2dissite 000-default.conf', stderr=sub.STDOUT, shell=True) |
| 116 | + |
| 117 | + |
| 118 | +main() |
0 commit comments