From 8664dd9d21aaccd1b3fb5ab2b84612d70d28d265 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Mon, 19 Aug 2019 17:40:21 +0200 Subject: [PATCH] Add WOTemplate * Add new function to handle rendering template * Make a copy of php configuration and use this copy to create new config files --- wo/cli/plugins/stack_pref.py | 176 +++++++++++++++++------------------ wo/core/template.py | 55 +++++------ 2 files changed, 115 insertions(+), 116 deletions(-) diff --git a/wo/cli/plugins/stack_pref.py b/wo/cli/plugins/stack_pref.py index 559a6bc..45e57a9 100644 --- a/wo/cli/plugins/stack_pref.py +++ b/wo/cli/plugins/stack_pref.py @@ -15,6 +15,7 @@ from wo.core.cron import WOCron from wo.core.extract import WOExtract from wo.core.fileutils import WOFileUtils from wo.core.git import WOGit +from wo.core.template import WOTemplate from wo.core.logging import Log from wo.core.mysql import WOMysql from wo.core.services import WOService @@ -146,6 +147,9 @@ def post_pref(self, apt_packages, packages): # Nginx configuration if set(WOVariables.wo_nginx).issubset(set(apt_packages)): # Nginx main configuration + ngxcnf = '/etc/nginx/conf.d' + ngxcom = '/etc/nginx/common' + ngxroot = '/var/www' if os.path.isfile('/etc/nginx/nginx.conf'): if (WOVariables.wo_distro == 'ubuntu' or WOVariables.wo_platform_codename == 'buster'): @@ -160,32 +164,18 @@ def post_pref(self, apt_packages, packages): (data), 'nginx-core.mustache', out=wo_nginx) wo_nginx.close() - if not os.path.isfile('/etc/nginx/conf.d/gzip.conf.disabled'): + if not os.path.isfile('{0}/gzip.conf.disabled'.forat(ngxcnf)): data = dict() - Log.debug(self, 'Writting the nginx configuration to ' - 'file /etc/nginx/conf.d/gzip.conf') - wo_nginx = open('/etc/nginx/conf.d/gzip.conf', - encoding='utf-8', mode='w') - self.app.render( - (data), 'gzip.mustache', out=wo_nginx) - wo_nginx.close() + WOTemplate.tmpl_render(self, '{0}/gzip.conf', + 'gzip.mustache') - if not os.path.isfile('/etc/nginx/conf.d/brotli.conf'): - Log.debug(self, 'Writting the nginx configuration to ' - 'file /etc/nginx/conf.d/brotli.conf.disabled') - wo_nginx = open('/etc/nginx/conf.d/brotli.conf.disabled', - encoding='utf-8', mode='w') - self.app.render( - (data), 'brotli.mustache', out=wo_nginx) - wo_nginx.close() + if not os.path.isfile('{0}/brotli.conf'): + WOTemplate.tmpl_render(self, + '{0}/brotli.conf.disabled', + 'brotli.mustache') - Log.debug(self, 'Writting the nginx configuration to ' - 'file /etc/nginx/conf.d/tweaks.conf') - wo_nginx = open('/etc/nginx/conf.d/tweaks.conf', - encoding='utf-8', mode='w') - self.app.render( - (data), 'tweaks.mustache', out=wo_nginx) - wo_nginx.close() + WOTemplate.tmpl_render(self, '{0}/tweaks.conf', + 'tweaks.mustache') # Fix for white screen death with NGINX PLUS if not WOFileUtils.grep(self, '/etc/nginx/fastcgi_params', @@ -198,9 +188,10 @@ def post_pref(self, apt_packages, packages): if os.path.isfile('/etc/nginx/nginx.conf'): data = dict(php="9000", debug="9001", php7="9070", debug7="9170") - Log.debug(self, 'Writting the nginx configuration to ' - 'file /etc/nginx/conf.d/upstream.conf') - wo_nginx = open('/etc/nginx/conf.d/upstream.conf', + WOTemplate.tmpl_render( + self, '{0}/upstream.conf'.format(ngxcnf), + 'upstream.mustache', data, overwrite=True) + wo_nginx = open('{0}/upstream.conf'.format(ngxcnf), encoding='utf-8', mode='w') self.app.render( (data), 'upstream.mustache', out=wo_nginx) @@ -209,43 +200,34 @@ def post_pref(self, apt_packages, packages): data = dict(phpconf=True if WOAptGet.is_installed(self, 'php7.2-fpm') else False) - Log.debug(self, 'Writting the nginx configuration to ' - 'file /etc/nginx/conf.d/stub_status.conf') - wo_nginx = open('/etc/nginx/conf.d/stub_status.conf', + wo_nginx = open('{0}/stub_status.conf'.format(ngxcnf), encoding='utf-8', mode='w') self.app.render( (data), 'stub_status.mustache', out=wo_nginx) wo_nginx.close() data = dict() - Log.debug(self, 'Writting the nginx configuration to ' - 'file /etc/nginx/conf.d/webp.conf') - wo_nginx = open('/etc/nginx/conf.d/webp.conf', + wo_nginx = open('{0}/webp.conf'.format(ngxcnf), encoding='utf-8', mode='w') self.app.render((data), 'webp.mustache', out=wo_nginx) wo_nginx.close() - Log.debug(self, 'Writting the nginx configuration to ' - 'file /etc/nginx/conf.d/cloudflare.conf') - wo_nginx = open('/etc/nginx/conf.d/cloudflare.conf', + wo_nginx = open('{0}/cloudflare.conf'.format(ngxcnf), encoding='utf-8', mode='w') self.app.render((data), 'cloudflare.mustache', out=wo_nginx) wo_nginx.close() - Log.debug(self, 'Writting the nginx configuration to ' - 'file /etc/nginx/conf.d/' - 'map-wp-fastcgi-cache.conf') - wo_nginx = open('/etc/nginx/conf.d/' - 'map-wp-fastcgi-cache.conf', + wo_nginx = open('{0}/' + 'map-wp-fastcgi-cache.conf'.format(ngxcnf), encoding='utf-8', mode='w') self.app.render((data), 'map-wp.mustache', out=wo_nginx) wo_nginx.close() # Setup Nginx common directory - if not os.path.exists('/etc/nginx/common'): + if not os.path.exists('{0}'.format(ngxcom)): Log.debug(self, 'Creating directory' '/etc/nginx/common') os.makedirs('/etc/nginx/common') @@ -396,7 +378,7 @@ def post_pref(self, apt_packages, packages): # Following files should not be overwrited if not os.path.isfile('/etc/nginx/common/acl.conf'): - data = dict(webroot=WOVariables.wo_webroot) + data = dict(webroot=ngxroot) Log.debug(self, 'Writting the nginx configuration to ' 'file /etc/nginx/common/acl.conf') wo_nginx = open('/etc/nginx/common/acl.conf', @@ -404,19 +386,19 @@ def post_pref(self, apt_packages, packages): self.app.render((data), 'acl.mustache', out=wo_nginx) wo_nginx.close() - if not os.path.isfile('/etc/nginx/conf.d/blockips.conf'): + if not os.path.isfile('{0}/blockips.conf'.format(ngxcnf)): Log.debug(self, 'Writting the nginx configuration to ' - 'file /etc/nginx/conf.d/blockips.conf') - wo_nginx = open('/etc/nginx/conf.d/blockips.conf', + 'file {0}/blockips.conf'.format(ngxcnf)) + wo_nginx = open('{0}/blockips.conf'.format(ngxcnf), encoding='utf-8', mode='w') self.app.render( (data), 'blockips.mustache', out=wo_nginx) wo_nginx.close() - if not os.path.isfile('/etc/nginx/conf.d/fastcgi.conf'): + if not os.path.isfile('{0}/fastcgi.conf'.format(ngxcnf)): Log.debug(self, 'Writting the nginx configuration to ' - 'file /etc/nginx/conf.d/fastcgi.conf') - wo_nginx = open('/etc/nginx/conf.d/fastcgi.conf', + 'file {0}/fastcgi.conf'.format(ngxcnf)) + wo_nginx = open('{0}/fastcgi.conf'.format(ngxcnf), encoding='utf-8', mode='w') self.app.render( (data), 'fastcgi.mustache', out=wo_nginx) @@ -486,72 +468,74 @@ def post_pref(self, apt_packages, packages): '22222']) # Create log and cert folder and softlinks if not os.path.exists('{0}22222/logs' - .format(WOVariables.wo_webroot)): + .format(ngxroot)): Log.debug(self, "Creating directory " "{0}22222/logs " - .format(WOVariables.wo_webroot)) + .format(ngxroot)) os.makedirs('{0}22222/logs' - .format(WOVariables.wo_webroot)) + .format(ngxroot)) if not os.path.exists('{0}22222/cert' - .format(WOVariables.wo_webroot)): + .format(ngxroot)): Log.debug(self, "Creating directory " "{0}22222/cert" - .format(WOVariables.wo_webroot)) + .format(ngxroot)) os.makedirs('{0}22222/cert' - .format(WOVariables.wo_webroot)) + .format(ngxroot)) if not os.path.exists('{0}22222/conf/nginx' - .format(WOVariables.wo_webroot)): + .format(ngxroot)): Log.debug(self, "Creating directory " "{0}22222/conf/nginx" - .format(WOVariables.wo_webroot)) + .format(ngxroot)) os.makedirs('{0}22222/conf/nginx' - .format(WOVariables.wo_webroot)) + .format(ngxroot)) - WOFileUtils.create_symlink(self, ['/var/log/nginx/' - '22222.access.log', - '{0}22222/' - 'logs/access.log' - .format(WOVariables.wo_webroot)] + WOFileUtils.create_symlink(self, + ['/var/log/nginx/' + '22222.access.log', + '{0}22222/' + 'logs/access.log' + .format(ngxroot)] ) - WOFileUtils.create_symlink(self, ['/var/log/nginx/' - '22222.error.log', - '{0}22222/' - 'logs/error.log' - .format(WOVariables.wo_webroot)] + WOFileUtils.create_symlink(self, + ['/var/log/nginx/' + '22222.error.log', + '{0}22222/' + 'logs/error.log' + .format(ngxroot)] ) try: WOShellExec.cmd_exec(self, "openssl genrsa -out " "{0}22222/cert/22222.key 2048" - .format(WOVariables.wo_webroot)) + .format(ngxroot)) WOShellExec.cmd_exec(self, "openssl req -new -batch " "-subj /commonName=localhost/ " "-key {0}22222/cert/22222.key " "-out {0}22222/cert/" "22222.csr" - .format(WOVariables.wo_webroot)) + .format(ngxroot)) WOFileUtils.mvfile(self, "{0}22222/cert/22222.key" - .format(WOVariables.wo_webroot), + .format(ngxroot), "{0}22222/cert/" "22222.key.org" - .format(WOVariables.wo_webroot)) + .format(ngxroot)) WOShellExec.cmd_exec(self, "openssl rsa -in " "{0}22222/cert/" "22222.key.org -out " "{0}22222/cert/22222.key" - .format(WOVariables.wo_webroot)) + .format(ngxroot)) WOShellExec.cmd_exec(self, "openssl x509 -req -days " "3652 -in {0}22222/cert/" "22222.csr -signkey {0}" "22222/cert/22222.key -out " "{0}22222/cert/22222.crt" - .format(WOVariables.wo_webroot)) + .format(ngxroot)) except CommandExecutionError as e: Log.debug(self, "{0}".format(e)) @@ -560,7 +544,7 @@ def post_pref(self, apt_packages, packages): "certificate for 22222") if not os.path.isfile('{0}22222/conf/nginx/ssl.conf' - .format(WOVariables.wo_webroot)): + .format(ngxroot)): with open("/var/www/22222/conf/nginx/" "ssl.conf", "a") as php_file: @@ -651,11 +635,15 @@ def post_pref(self, apt_packages, packages): Log.debug(self, 'Creating directory /var/log/php/7.2/') os.makedirs('/var/log/php/7.2/') - # Parse etc/php/7.2/fpm/php.ini + if not os.path.isfile('/etc/php/7.2/fpm/php.ini.orig'): + WOFileUtils.copyfile(self, '/etc/php/7.2/fpm/php.ini', + '/etc/php/7.2/fpm/php.ini.orig') + + # Parse etc/php/7.2/fpm/php.ini config = configparser.ConfigParser() Log.debug(self, "configuring php file " "/etc/php/7.2/fpm/php.ini") - config.read('/etc/php/7.2/fpm/php.ini') + config.read('/etc/php/7.2/fpm/php.ini.orig') config['PHP']['expose_php'] = 'Off' config['PHP']['post_max_size'] = '100M' config['PHP']['upload_max_filesize'] = '100M' @@ -688,10 +676,13 @@ def post_pref(self, apt_packages, packages): self.app.render((data), 'php-fpm.mustache', out=wo_php_fpm) wo_php_fpm.close() + if not os.path.isfile('/etc/php/7.2/fpm/pool.d/www.conf'): + WOFileUtils.copyfile(self, '/etc/php/7.2/fpm/pool.d/www.conf', + '/etc/php/7.2/fpm/pool.d/www.conf.orig') # Parse /etc/php/7.2/fpm/pool.d/www.conf config = configparser.ConfigParser() config.read_file(codecs.open('/etc/php/7.2/fpm/' - 'pool.d/www.conf', + 'pool.d/www.conf.orig', "r", "utf8")) config['www']['ping.path'] = '/ping' config['www']['pm.status_path'] = '/status' @@ -773,35 +764,35 @@ def post_pref(self, apt_packages, packages): # PHP and Debug pull configuration if not os.path.exists('{0}22222/htdocs/fpm/status/' - .format(WOVariables.wo_webroot)): + .format(ngxroot)): Log.debug(self, 'Creating directory ' '{0}22222/htdocs/fpm/status/ ' - .format(WOVariables.wo_webroot)) + .format(ngxroot)) os.makedirs('{0}22222/htdocs/fpm/status/' - .format(WOVariables.wo_webroot)) + .format(ngxroot)) open('{0}22222/htdocs/fpm/status/debug72' - .format(WOVariables.wo_webroot), + .format(ngxroot), encoding='utf-8', mode='a').close() open('{0}22222/htdocs/fpm/status/php72' - .format(WOVariables.wo_webroot), + .format(ngxroot), encoding='utf-8', mode='a').close() # Write info.php if not os.path.exists('{0}22222/htdocs/php/' - .format(WOVariables.wo_webroot)): + .format(ngxroot)): Log.debug(self, 'Creating directory ' '{0}22222/htdocs/php/ ' - .format(WOVariables.wo_webroot)) + .format(ngxroot)) os.makedirs('{0}22222/htdocs/php' - .format(WOVariables.wo_webroot)) + .format(ngxroot)) with open("{0}22222/htdocs/php/info.php" - .format(WOVariables.wo_webroot), + .format(ngxroot), encoding='utf-8', mode='w') as myfile: myfile.write("") WOFileUtils.chown(self, "{0}22222/htdocs" - .format(WOVariables.wo_webroot), + .format(ngxroot), WOVariables.wo_php_user, WOVariables.wo_php_user, recursive=True) @@ -815,11 +806,15 @@ def post_pref(self, apt_packages, packages): Log.debug(self, 'Creating directory /var/log/php/7.3/') os.makedirs('/var/log/php/7.3/') + if not os.path.isfile('/etc/php/7.3/fpm/php.ini.orig'): + WOFileUtils.copyfile(self, '/etc/php/7.3/fpm/php.ini', + '/etc/php/7.3/fpm/php.ini.orig') + # Parse etc/php/7.3/fpm/php.ini config = configparser.ConfigParser() Log.debug(self, "configuring php file /etc/php/7.3/" "fpm/php.ini") - config.read('/etc/php/7.3/fpm/php.ini') + config.read('/etc/php/7.3/fpm/php.ini.orig') config['PHP']['expose_php'] = 'Off' config['PHP']['post_max_size'] = '100M' config['PHP']['upload_max_filesize'] = '100M' @@ -853,9 +848,12 @@ def post_pref(self, apt_packages, packages): wo_php_fpm.close() # Parse /etc/php/7.3/fpm/pool.d/www.conf + if not os.path.isfile('/etc/php/7.3/fpm/pool.d/www.conf'): + WOFileUtils.copyfile(self, '/etc/php/7.3/fpm/pool.d/www.conf', + '/etc/php/7.3/fpm/pool.d/www.conf.orig') config = configparser.ConfigParser() config.read_file(codecs.open('/etc/php/7.3/fpm/' - 'pool.d/www.conf', + 'pool.d/www.conf.orig', "r", "utf8")) config['www']['ping.path'] = '/ping' config['www']['pm.status_path'] = '/status' diff --git a/wo/core/template.py b/wo/core/template.py index cacad75..0611eba 100644 --- a/wo/core/template.py +++ b/wo/core/template.py @@ -1,27 +1,28 @@ -from wo.core.logging import Log -import os - -""" -Render Templates -""" - - -class WOTemplate(): - def tmpl_render(self, fileconf, template, data, overwrite=False): - if overwrite: - Log.debug(self, 'Writting the configuration to ' - 'file {0}'.format(fileconf)) - wo_template = open('{0}'.format(fileconf), - encoding='utf-8', mode='w') - self.app.render((data), '{0}'.format(template), - out=wo_template) - wo_template.close() - else: - if not os.path.isfile('{0}'.format(fileconf)): - Log.debug(self, 'Writting the configuration to ' - 'file {0}'.format(fileconf)) - wo_template = open('{0}'.format(fileconf), - encoding='utf-8', mode='w') - self.app.render((data), '{0}'.format(template), - out=wo_template) - wo_template.close() +from wo.core.logging import Log +import os + +""" +Render Templates +""" + + +class WOTemplate(): + def tmpl_render(self, fileconf, template, data, overwrite=False): + if overwrite: + data = dict(data) + Log.debug(self, 'Writting the configuration to ' + 'file {0}'.format(fileconf)) + wo_template = open('{0}'.format(fileconf), + encoding='utf-8', mode='w') + self.app.render((data), '{0}'.format(template), + out=wo_template) + wo_template.close() + else: + if not os.path.isfile('{0}'.format(fileconf)): + Log.debug(self, 'Writting the configuration to ' + 'file {0}'.format(fileconf)) + wo_template = open('{0}'.format(fileconf), + encoding='utf-8', mode='w') + self.app.render((data), '{0}'.format(template), + out=wo_template) + wo_template.close()