Add WOTemplate

* Add new function to handle rendering template
* Make a copy of php configuration and use this copy to create new config files
This commit is contained in:
VirtuBox
2019-08-19 17:40:21 +02:00
parent d6807da095
commit 8664dd9d21
2 changed files with 115 additions and 116 deletions

View File

@@ -15,6 +15,7 @@ from wo.core.cron import WOCron
from wo.core.extract import WOExtract from wo.core.extract import WOExtract
from wo.core.fileutils import WOFileUtils from wo.core.fileutils import WOFileUtils
from wo.core.git import WOGit from wo.core.git import WOGit
from wo.core.template import WOTemplate
from wo.core.logging import Log from wo.core.logging import Log
from wo.core.mysql import WOMysql from wo.core.mysql import WOMysql
from wo.core.services import WOService from wo.core.services import WOService
@@ -146,6 +147,9 @@ def post_pref(self, apt_packages, packages):
# Nginx configuration # Nginx configuration
if set(WOVariables.wo_nginx).issubset(set(apt_packages)): if set(WOVariables.wo_nginx).issubset(set(apt_packages)):
# Nginx main configuration # Nginx main configuration
ngxcnf = '/etc/nginx/conf.d'
ngxcom = '/etc/nginx/common'
ngxroot = '/var/www'
if os.path.isfile('/etc/nginx/nginx.conf'): if os.path.isfile('/etc/nginx/nginx.conf'):
if (WOVariables.wo_distro == 'ubuntu' or if (WOVariables.wo_distro == 'ubuntu' or
WOVariables.wo_platform_codename == 'buster'): WOVariables.wo_platform_codename == 'buster'):
@@ -160,32 +164,18 @@ def post_pref(self, apt_packages, packages):
(data), 'nginx-core.mustache', out=wo_nginx) (data), 'nginx-core.mustache', out=wo_nginx)
wo_nginx.close() 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() data = dict()
Log.debug(self, 'Writting the nginx configuration to ' WOTemplate.tmpl_render(self, '{0}/gzip.conf',
'file /etc/nginx/conf.d/gzip.conf') 'gzip.mustache')
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()
if not os.path.isfile('/etc/nginx/conf.d/brotli.conf'): if not os.path.isfile('{0}/brotli.conf'):
Log.debug(self, 'Writting the nginx configuration to ' WOTemplate.tmpl_render(self,
'file /etc/nginx/conf.d/brotli.conf.disabled') '{0}/brotli.conf.disabled',
wo_nginx = open('/etc/nginx/conf.d/brotli.conf.disabled', 'brotli.mustache')
encoding='utf-8', mode='w')
self.app.render(
(data), 'brotli.mustache', out=wo_nginx)
wo_nginx.close()
Log.debug(self, 'Writting the nginx configuration to ' WOTemplate.tmpl_render(self, '{0}/tweaks.conf',
'file /etc/nginx/conf.d/tweaks.conf') 'tweaks.mustache')
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()
# Fix for white screen death with NGINX PLUS # Fix for white screen death with NGINX PLUS
if not WOFileUtils.grep(self, '/etc/nginx/fastcgi_params', 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'): if os.path.isfile('/etc/nginx/nginx.conf'):
data = dict(php="9000", debug="9001", data = dict(php="9000", debug="9001",
php7="9070", debug7="9170") php7="9070", debug7="9170")
Log.debug(self, 'Writting the nginx configuration to ' WOTemplate.tmpl_render(
'file /etc/nginx/conf.d/upstream.conf') self, '{0}/upstream.conf'.format(ngxcnf),
wo_nginx = open('/etc/nginx/conf.d/upstream.conf', 'upstream.mustache', data, overwrite=True)
wo_nginx = open('{0}/upstream.conf'.format(ngxcnf),
encoding='utf-8', mode='w') encoding='utf-8', mode='w')
self.app.render( self.app.render(
(data), 'upstream.mustache', out=wo_nginx) (data), 'upstream.mustache', out=wo_nginx)
@@ -209,43 +200,34 @@ def post_pref(self, apt_packages, packages):
data = dict(phpconf=True if data = dict(phpconf=True if
WOAptGet.is_installed(self, 'php7.2-fpm') WOAptGet.is_installed(self, 'php7.2-fpm')
else False) else False)
Log.debug(self, 'Writting the nginx configuration to ' wo_nginx = open('{0}/stub_status.conf'.format(ngxcnf),
'file /etc/nginx/conf.d/stub_status.conf')
wo_nginx = open('/etc/nginx/conf.d/stub_status.conf',
encoding='utf-8', mode='w') encoding='utf-8', mode='w')
self.app.render( self.app.render(
(data), 'stub_status.mustache', out=wo_nginx) (data), 'stub_status.mustache', out=wo_nginx)
wo_nginx.close() wo_nginx.close()
data = dict() data = dict()
Log.debug(self, 'Writting the nginx configuration to ' wo_nginx = open('{0}/webp.conf'.format(ngxcnf),
'file /etc/nginx/conf.d/webp.conf')
wo_nginx = open('/etc/nginx/conf.d/webp.conf',
encoding='utf-8', mode='w') encoding='utf-8', mode='w')
self.app.render((data), 'webp.mustache', self.app.render((data), 'webp.mustache',
out=wo_nginx) out=wo_nginx)
wo_nginx.close() wo_nginx.close()
Log.debug(self, 'Writting the nginx configuration to ' wo_nginx = open('{0}/cloudflare.conf'.format(ngxcnf),
'file /etc/nginx/conf.d/cloudflare.conf')
wo_nginx = open('/etc/nginx/conf.d/cloudflare.conf',
encoding='utf-8', mode='w') encoding='utf-8', mode='w')
self.app.render((data), 'cloudflare.mustache', self.app.render((data), 'cloudflare.mustache',
out=wo_nginx) out=wo_nginx)
wo_nginx.close() wo_nginx.close()
Log.debug(self, 'Writting the nginx configuration to ' wo_nginx = open('{0}/'
'file /etc/nginx/conf.d/' 'map-wp-fastcgi-cache.conf'.format(ngxcnf),
'map-wp-fastcgi-cache.conf')
wo_nginx = open('/etc/nginx/conf.d/'
'map-wp-fastcgi-cache.conf',
encoding='utf-8', mode='w') encoding='utf-8', mode='w')
self.app.render((data), 'map-wp.mustache', self.app.render((data), 'map-wp.mustache',
out=wo_nginx) out=wo_nginx)
wo_nginx.close() wo_nginx.close()
# Setup Nginx common directory # 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' Log.debug(self, 'Creating directory'
'/etc/nginx/common') '/etc/nginx/common')
os.makedirs('/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 # Following files should not be overwrited
if not os.path.isfile('/etc/nginx/common/acl.conf'): 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 ' Log.debug(self, 'Writting the nginx configuration to '
'file /etc/nginx/common/acl.conf') 'file /etc/nginx/common/acl.conf')
wo_nginx = open('/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', self.app.render((data), 'acl.mustache',
out=wo_nginx) out=wo_nginx)
wo_nginx.close() 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 ' Log.debug(self, 'Writting the nginx configuration to '
'file /etc/nginx/conf.d/blockips.conf') 'file {0}/blockips.conf'.format(ngxcnf))
wo_nginx = open('/etc/nginx/conf.d/blockips.conf', wo_nginx = open('{0}/blockips.conf'.format(ngxcnf),
encoding='utf-8', mode='w') encoding='utf-8', mode='w')
self.app.render( self.app.render(
(data), 'blockips.mustache', out=wo_nginx) (data), 'blockips.mustache', out=wo_nginx)
wo_nginx.close() 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 ' Log.debug(self, 'Writting the nginx configuration to '
'file /etc/nginx/conf.d/fastcgi.conf') 'file {0}/fastcgi.conf'.format(ngxcnf))
wo_nginx = open('/etc/nginx/conf.d/fastcgi.conf', wo_nginx = open('{0}/fastcgi.conf'.format(ngxcnf),
encoding='utf-8', mode='w') encoding='utf-8', mode='w')
self.app.render( self.app.render(
(data), 'fastcgi.mustache', out=wo_nginx) (data), 'fastcgi.mustache', out=wo_nginx)
@@ -486,72 +468,74 @@ def post_pref(self, apt_packages, packages):
'22222']) '22222'])
# Create log and cert folder and softlinks # Create log and cert folder and softlinks
if not os.path.exists('{0}22222/logs' if not os.path.exists('{0}22222/logs'
.format(WOVariables.wo_webroot)): .format(ngxroot)):
Log.debug(self, "Creating directory " Log.debug(self, "Creating directory "
"{0}22222/logs " "{0}22222/logs "
.format(WOVariables.wo_webroot)) .format(ngxroot))
os.makedirs('{0}22222/logs' os.makedirs('{0}22222/logs'
.format(WOVariables.wo_webroot)) .format(ngxroot))
if not os.path.exists('{0}22222/cert' if not os.path.exists('{0}22222/cert'
.format(WOVariables.wo_webroot)): .format(ngxroot)):
Log.debug(self, "Creating directory " Log.debug(self, "Creating directory "
"{0}22222/cert" "{0}22222/cert"
.format(WOVariables.wo_webroot)) .format(ngxroot))
os.makedirs('{0}22222/cert' os.makedirs('{0}22222/cert'
.format(WOVariables.wo_webroot)) .format(ngxroot))
if not os.path.exists('{0}22222/conf/nginx' if not os.path.exists('{0}22222/conf/nginx'
.format(WOVariables.wo_webroot)): .format(ngxroot)):
Log.debug(self, "Creating directory " Log.debug(self, "Creating directory "
"{0}22222/conf/nginx" "{0}22222/conf/nginx"
.format(WOVariables.wo_webroot)) .format(ngxroot))
os.makedirs('{0}22222/conf/nginx' os.makedirs('{0}22222/conf/nginx'
.format(WOVariables.wo_webroot)) .format(ngxroot))
WOFileUtils.create_symlink(self, ['/var/log/nginx/' WOFileUtils.create_symlink(self,
'22222.access.log', ['/var/log/nginx/'
'{0}22222/' '22222.access.log',
'logs/access.log' '{0}22222/'
.format(WOVariables.wo_webroot)] 'logs/access.log'
.format(ngxroot)]
) )
WOFileUtils.create_symlink(self, ['/var/log/nginx/' WOFileUtils.create_symlink(self,
'22222.error.log', ['/var/log/nginx/'
'{0}22222/' '22222.error.log',
'logs/error.log' '{0}22222/'
.format(WOVariables.wo_webroot)] 'logs/error.log'
.format(ngxroot)]
) )
try: try:
WOShellExec.cmd_exec(self, "openssl genrsa -out " WOShellExec.cmd_exec(self, "openssl genrsa -out "
"{0}22222/cert/22222.key 2048" "{0}22222/cert/22222.key 2048"
.format(WOVariables.wo_webroot)) .format(ngxroot))
WOShellExec.cmd_exec(self, "openssl req -new -batch " WOShellExec.cmd_exec(self, "openssl req -new -batch "
"-subj /commonName=localhost/ " "-subj /commonName=localhost/ "
"-key {0}22222/cert/22222.key " "-key {0}22222/cert/22222.key "
"-out {0}22222/cert/" "-out {0}22222/cert/"
"22222.csr" "22222.csr"
.format(WOVariables.wo_webroot)) .format(ngxroot))
WOFileUtils.mvfile(self, "{0}22222/cert/22222.key" WOFileUtils.mvfile(self, "{0}22222/cert/22222.key"
.format(WOVariables.wo_webroot), .format(ngxroot),
"{0}22222/cert/" "{0}22222/cert/"
"22222.key.org" "22222.key.org"
.format(WOVariables.wo_webroot)) .format(ngxroot))
WOShellExec.cmd_exec(self, "openssl rsa -in " WOShellExec.cmd_exec(self, "openssl rsa -in "
"{0}22222/cert/" "{0}22222/cert/"
"22222.key.org -out " "22222.key.org -out "
"{0}22222/cert/22222.key" "{0}22222/cert/22222.key"
.format(WOVariables.wo_webroot)) .format(ngxroot))
WOShellExec.cmd_exec(self, "openssl x509 -req -days " WOShellExec.cmd_exec(self, "openssl x509 -req -days "
"3652 -in {0}22222/cert/" "3652 -in {0}22222/cert/"
"22222.csr -signkey {0}" "22222.csr -signkey {0}"
"22222/cert/22222.key -out " "22222/cert/22222.key -out "
"{0}22222/cert/22222.crt" "{0}22222/cert/22222.crt"
.format(WOVariables.wo_webroot)) .format(ngxroot))
except CommandExecutionError as e: except CommandExecutionError as e:
Log.debug(self, "{0}".format(e)) Log.debug(self, "{0}".format(e))
@@ -560,7 +544,7 @@ def post_pref(self, apt_packages, packages):
"certificate for 22222") "certificate for 22222")
if not os.path.isfile('{0}22222/conf/nginx/ssl.conf' if not os.path.isfile('{0}22222/conf/nginx/ssl.conf'
.format(WOVariables.wo_webroot)): .format(ngxroot)):
with open("/var/www/22222/conf/nginx/" with open("/var/www/22222/conf/nginx/"
"ssl.conf", "a") as php_file: "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/') Log.debug(self, 'Creating directory /var/log/php/7.2/')
os.makedirs('/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() config = configparser.ConfigParser()
Log.debug(self, "configuring php file " Log.debug(self, "configuring php file "
"/etc/php/7.2/fpm/php.ini") "/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']['expose_php'] = 'Off'
config['PHP']['post_max_size'] = '100M' config['PHP']['post_max_size'] = '100M'
config['PHP']['upload_max_filesize'] = '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) self.app.render((data), 'php-fpm.mustache', out=wo_php_fpm)
wo_php_fpm.close() 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 # Parse /etc/php/7.2/fpm/pool.d/www.conf
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read_file(codecs.open('/etc/php/7.2/fpm/' config.read_file(codecs.open('/etc/php/7.2/fpm/'
'pool.d/www.conf', 'pool.d/www.conf.orig',
"r", "utf8")) "r", "utf8"))
config['www']['ping.path'] = '/ping' config['www']['ping.path'] = '/ping'
config['www']['pm.status_path'] = '/status' config['www']['pm.status_path'] = '/status'
@@ -773,35 +764,35 @@ def post_pref(self, apt_packages, packages):
# PHP and Debug pull configuration # PHP and Debug pull configuration
if not os.path.exists('{0}22222/htdocs/fpm/status/' if not os.path.exists('{0}22222/htdocs/fpm/status/'
.format(WOVariables.wo_webroot)): .format(ngxroot)):
Log.debug(self, 'Creating directory ' Log.debug(self, 'Creating directory '
'{0}22222/htdocs/fpm/status/ ' '{0}22222/htdocs/fpm/status/ '
.format(WOVariables.wo_webroot)) .format(ngxroot))
os.makedirs('{0}22222/htdocs/fpm/status/' os.makedirs('{0}22222/htdocs/fpm/status/'
.format(WOVariables.wo_webroot)) .format(ngxroot))
open('{0}22222/htdocs/fpm/status/debug72' open('{0}22222/htdocs/fpm/status/debug72'
.format(WOVariables.wo_webroot), .format(ngxroot),
encoding='utf-8', mode='a').close() encoding='utf-8', mode='a').close()
open('{0}22222/htdocs/fpm/status/php72' open('{0}22222/htdocs/fpm/status/php72'
.format(WOVariables.wo_webroot), .format(ngxroot),
encoding='utf-8', mode='a').close() encoding='utf-8', mode='a').close()
# Write info.php # Write info.php
if not os.path.exists('{0}22222/htdocs/php/' if not os.path.exists('{0}22222/htdocs/php/'
.format(WOVariables.wo_webroot)): .format(ngxroot)):
Log.debug(self, 'Creating directory ' Log.debug(self, 'Creating directory '
'{0}22222/htdocs/php/ ' '{0}22222/htdocs/php/ '
.format(WOVariables.wo_webroot)) .format(ngxroot))
os.makedirs('{0}22222/htdocs/php' os.makedirs('{0}22222/htdocs/php'
.format(WOVariables.wo_webroot)) .format(ngxroot))
with open("{0}22222/htdocs/php/info.php" with open("{0}22222/htdocs/php/info.php"
.format(WOVariables.wo_webroot), .format(ngxroot),
encoding='utf-8', mode='w') as myfile: encoding='utf-8', mode='w') as myfile:
myfile.write("<?php\nphpinfo();\n?>") myfile.write("<?php\nphpinfo();\n?>")
WOFileUtils.chown(self, "{0}22222/htdocs" WOFileUtils.chown(self, "{0}22222/htdocs"
.format(WOVariables.wo_webroot), .format(ngxroot),
WOVariables.wo_php_user, WOVariables.wo_php_user,
WOVariables.wo_php_user, recursive=True) 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/') Log.debug(self, 'Creating directory /var/log/php/7.3/')
os.makedirs('/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 # Parse etc/php/7.3/fpm/php.ini
config = configparser.ConfigParser() config = configparser.ConfigParser()
Log.debug(self, "configuring php file /etc/php/7.3/" Log.debug(self, "configuring php file /etc/php/7.3/"
"fpm/php.ini") "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']['expose_php'] = 'Off'
config['PHP']['post_max_size'] = '100M' config['PHP']['post_max_size'] = '100M'
config['PHP']['upload_max_filesize'] = '100M' config['PHP']['upload_max_filesize'] = '100M'
@@ -853,9 +848,12 @@ def post_pref(self, apt_packages, packages):
wo_php_fpm.close() wo_php_fpm.close()
# Parse /etc/php/7.3/fpm/pool.d/www.conf # 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 = configparser.ConfigParser()
config.read_file(codecs.open('/etc/php/7.3/fpm/' config.read_file(codecs.open('/etc/php/7.3/fpm/'
'pool.d/www.conf', 'pool.d/www.conf.orig',
"r", "utf8")) "r", "utf8"))
config['www']['ping.path'] = '/ping' config['www']['ping.path'] = '/ping'
config['www']['pm.status_path'] = '/status' config['www']['pm.status_path'] = '/status'

View File

@@ -1,27 +1,28 @@
from wo.core.logging import Log from wo.core.logging import Log
import os import os
""" """
Render Templates Render Templates
""" """
class WOTemplate(): class WOTemplate():
def tmpl_render(self, fileconf, template, data, overwrite=False): def tmpl_render(self, fileconf, template, data, overwrite=False):
if overwrite: if overwrite:
Log.debug(self, 'Writting the configuration to ' data = dict(data)
'file {0}'.format(fileconf)) Log.debug(self, 'Writting the configuration to '
wo_template = open('{0}'.format(fileconf), 'file {0}'.format(fileconf))
encoding='utf-8', mode='w') wo_template = open('{0}'.format(fileconf),
self.app.render((data), '{0}'.format(template), encoding='utf-8', mode='w')
out=wo_template) self.app.render((data), '{0}'.format(template),
wo_template.close() out=wo_template)
else: wo_template.close()
if not os.path.isfile('{0}'.format(fileconf)): else:
Log.debug(self, 'Writting the configuration to ' if not os.path.isfile('{0}'.format(fileconf)):
'file {0}'.format(fileconf)) Log.debug(self, 'Writting the configuration to '
wo_template = open('{0}'.format(fileconf), 'file {0}'.format(fileconf))
encoding='utf-8', mode='w') wo_template = open('{0}'.format(fileconf),
self.app.render((data), '{0}'.format(template), encoding='utf-8', mode='w')
out=wo_template) self.app.render((data), '{0}'.format(template),
wo_template.close() out=wo_template)
wo_template.close()