def templateLoader(template): file = "{}/{}/{}".format(os.path.abspath("{}/../../..".format(__file__)), configs.TAMPLATES_FOLDER, template) try: with open(file, 'r') as f: return f.read() except Exception as e: error('templateLoader from helper file', e)
def _setLinkTo(thisFile): import os try: link = thisFile.replace(configs.VIRTUAL_HOSTS_AVAILABLE_PATH, configs.VIRTUAL_HOSTS_ENABLED_PATH) os.link(thisFile, link) info('Linked file "{}" to "{}"'.format(thisFile, link)) except Exception as e: error('_setLinkTo from helper file', e)
def _checkHTTP2Enabled(): try: enable_modules = os.listdir(configs.APACHE2_MODULES_PATH) for emodule in enable_modules: if emodule.__contains__("http2"): return True error('_checkHTTP2Enabled from helper file', "'http2' Module of Apache not found or Disable") except Exception as e: error('_checkHTTP2Enabled from helper file', e)
def __validEmail(email): """ Validate email :param email: get email to Validation :return: Valid Email , if not show ERROR and exit ! """ try: if not emailValidator(email): error('__validEmail from helper file', "Correct Email: [email protected]") return email except Exception as e: error('__validEmail from helper file', e)
def __backup(File): """ Backup From Your File :param File: Your File """ from time import strftime, gmtime try: with open(File, 'r') as file: data = file.read() with open("{}.localadmin_{}.bkp".format(File, strftime("%Y-%m-%d_%H:%M:%S", gmtime())), 'w') as file: file.write(data) except Exception as e: error('__backup from helper file', (e))
def __validUrl(url): """ Validate domain :param url: Get Url For Validate , Without "http", "https" and "www" :return: Valid URL , if Not , Return None """ try: if not domainValidator(url): error( '__validEmail from helper file', "Correct Domain: example.com\n without 'http', 'https' , 'www' =)" ) return url.replace("www.", '') if url.startswith("www.") else url except Exception as e: error('__validUrl from helper file', e)
def _createVirtualHost(domain, content): """ Create VirtualHost File :param content: Your Content That you want Write to File :return: True if File Created , False if Not Created """ try: PATH = os.path.join(configs.VIRTUAL_HOSTS_AVAILABLE_PATH, domain) VirtualHostFileAddress = "{}{}".format(PATH, configs.EXTENSION) with open(VirtualHostFileAddress, 'w') as file: file.write(content) info("VirtualHost Created On '{}'".format(VirtualHostFileAddress)) _setLinkTo(VirtualHostFileAddress) return True except Exception as e: error('_createVirtualHost from helper file', (e))
def _addToHosts(domain): """ Add Your Domain in configs.HOSTS :param domain: Your Domain """ try: __backup(configs.HOSTS) data = mapping(templateLoader(configs.HOST_TEMPLATE_NAME), hostTemplateMaps( domain=domain )) with open(configs.HOSTS, 'a') as file: file.write("{}\n".format(data)) info("Added Domain On '{}'".format(configs.HOSTS)) return True except Exception as e: error('_addToHosts from helper file', (e))
def wsgi( domain, documentRoot, wsgiScript, email, virtualenv, StaticFolderName, enable_static, http2, ): """ Initialize WSGI Template """ try: # Check Enable HTTP2 or NOT if http2: _checkHTTP2Enabled() # Check Enable WSGI or NOT _checkWSGIEnabled() # validation DOMAIN = __validUrl(domain) email = __validEmail(email if email else "admin@{}".format(DOMAIN)) __wsgiAddressValidation( documentRoot, wsgiScript, virtualenv, StaticFolderName, enable_static ) # Load and Mapping result = mapping(templateLoader(WSGI_TEMPLATE_NAME),wsgiTemplateMaps( domain, documentRoot, wsgiScript, email, StaticFolderName, virtualenv, enable_static, http2, )) # Try to Create VirtualHost File if not _createVirtualHost(DOMAIN,result) : error('wsgi from cli file',"Cant Create VirtualHost File") # # Try add Domain to HOSTS file if not _addToHosts(DOMAIN): error('wsgi from cli file', "Cant Add Domain to '{}' File".format(HOSTS)) info('Now Reload Your Apache2 Service: `sudo systemctl reload apache2.service`') except Exception as e: error('wsgi from cli file', e)
def php(domain, documentRoot, email, http2): """ Initialize PHP Template """ try: # Check Enable HTTP2 or NOT if http2: _checkHTTP2Enabled() # validation DOMAIN = __validUrl(domain) email = __validEmail(email if email else "admin@{}".format(DOMAIN)) __phpAddressValidation(documentRoot) # get Result result = mapping( templateLoader(PHP_TEMPLATE_NAME), phpTemplateMaps(server_admin=email, document_root=documentRoot, server_name=DOMAIN, http2=http2)) # Try to Create VirtualHost File if not _createVirtualHost(DOMAIN, result): error('php from cli file', "Cant Create VirtualHost File") # # Try add Domain to HOSTS file if not _addToHosts(DOMAIN): error('php from cli file', "Cant Add Domain to '{}' File".format(HOSTS)) info( 'Now Reload Your Apache2 Service: `sudo systemctl reload apache2.service`' ) except Exception as e: error('php from cli file', e)