Files
WPIQ/wo/cli/plugins/site_functions.py

1603 lines
67 KiB
Python
Raw Normal View History

2019-07-29 02:32:53 +02:00
import getpass
import glob
2019-09-04 20:36:15 +02:00
import json
2019-07-29 02:32:53 +02:00
import os
import random
import re
2019-09-30 03:10:30 +02:00
import shutil
2019-07-29 02:32:53 +02:00
import string
import subprocess
from subprocess import CalledProcessError
2019-08-17 13:40:28 +02:00
from wo.cli.plugins.sitedb import getSiteInfo
2019-07-29 02:32:53 +02:00
from wo.cli.plugins.stack import WOStackController
2019-09-04 20:36:15 +02:00
from wo.cli.plugins.stack_pref import post_pref
2019-09-23 18:32:38 +02:00
from wo.core.acme import WOAcme
2019-07-14 22:50:34 +02:00
from wo.core.aptget import WOAptGet
2019-07-29 02:32:53 +02:00
from wo.core.fileutils import WOFileUtils
2019-07-14 22:50:34 +02:00
from wo.core.git import WOGit
from wo.core.logging import Log
2019-09-30 12:38:28 +02:00
from wo.core.mysql import (MySQLConnectionError, StatementExcecutionError,
WOMysql)
2019-07-14 22:50:34 +02:00
from wo.core.services import WOService
2019-07-29 02:32:53 +02:00
from wo.core.shellexec import CommandExecutionError, WOShellExec
from wo.core.sslutils import SSL
2019-10-02 13:13:32 +02:00
from wo.core.variables import WOVar
2019-07-14 22:50:34 +02:00
class SiteError(Exception):
"""Custom Exception Occured when setting up site"""
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
def pre_run_checks(self):
# Check nginx configuration
Log.wait(self, "Running pre-run checks")
2019-07-14 22:50:34 +02:00
try:
Log.debug(self, "checking NGINX configuration ...")
2019-09-23 18:32:38 +02:00
fnull = open('/dev/null', 'w')
subprocess.check_call(["/usr/sbin/nginx", "-t"], stdout=fnull,
2019-08-05 00:20:09 +02:00
stderr=subprocess.STDOUT)
2019-07-14 22:50:34 +02:00
except CalledProcessError as e:
2019-09-05 12:49:04 +02:00
Log.failed(self, "Running pre-update checks")
2019-07-14 22:50:34 +02:00
Log.debug(self, "{0}".format(str(e)))
raise SiteError("nginx configuration check failed.")
2019-09-05 12:47:12 +02:00
else:
Log.valide(self, "Running pre-update checks")
2019-07-14 22:50:34 +02:00
def check_domain_exists(self, domain):
if getSiteInfo(self, domain):
return True
return False
def setupdomain(self, data):
# for debug purpose
# for key, value in data.items() :
# print (key, value)
wo_domain_name = data['site_name']
wo_site_webroot = data['webroot']
# Check if nginx configuration already exists
# if os.path.isfile('/etc/nginx/sites-available/{0}'
# .format(wo_domain_name)):
# raise SiteError("nginx configuration already exists for site")
Log.info(self, "Setting up NGINX configuration \t", end='')
# write nginx config for file
try:
wo_site_nginx_conf = open('/etc/nginx/sites-available/{0}'
.format(wo_domain_name), encoding='utf-8',
mode='w')
self.app.render((data), 'virtualconf.mustache',
out=wo_site_nginx_conf)
2019-07-14 22:50:34 +02:00
wo_site_nginx_conf.close()
except IOError as e:
2019-09-02 18:56:34 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
raise SiteError("create nginx configuration failed for site")
except Exception as e:
2019-09-02 18:56:34 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
raise SiteError("create nginx configuration failed for site")
finally:
# Check nginx -t and return status over it
try:
Log.debug(self, "Checking generated nginx conf, please wait...")
2019-10-03 12:58:44 +02:00
fnull = open('/dev/null', 'w')
subprocess.check_call(["/usr/sbin/nginx", "-t"], stdout=fnull,
2019-08-05 00:20:09 +02:00
stderr=subprocess.STDOUT)
2019-07-14 22:50:34 +02:00
Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
except CalledProcessError as e:
Log.debug(self, "{0}".format(str(e)))
Log.info(self, "[" + Log.ENDC + Log.FAIL + "Fail" +
Log.OKBLUE + "]")
raise SiteError("created nginx configuration failed for site."
" check with `nginx -t`")
# create symbolic link for
WOFileUtils.create_symlink(self, ['/etc/nginx/sites-available/{0}'
.format(wo_domain_name),
'/etc/nginx/sites-enabled/{0}'
.format(wo_domain_name)])
# Creating htdocs & logs directory
Log.info(self, "Setting up webroot \t\t", end='')
try:
if not os.path.exists('{0}/htdocs'.format(wo_site_webroot)):
os.makedirs('{0}/htdocs'.format(wo_site_webroot))
if not os.path.exists('{0}/logs'.format(wo_site_webroot)):
os.makedirs('{0}/logs'.format(wo_site_webroot))
if not os.path.exists('{0}/conf/nginx'.format(wo_site_webroot)):
os.makedirs('{0}/conf/nginx'.format(wo_site_webroot))
WOFileUtils.create_symlink(self, ['/var/log/nginx/{0}.access.log'
.format(wo_domain_name),
'{0}/logs/access.log'
.format(wo_site_webroot)])
WOFileUtils.create_symlink(self, ['/var/log/nginx/{0}.error.log'
.format(wo_domain_name),
'{0}/logs/error.log'
.format(wo_site_webroot)])
except Exception as e:
2019-09-02 18:56:34 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
raise SiteError("setup webroot failed for site")
finally:
# TODO Check if directories are setup
if (os.path.exists('{0}/htdocs'.format(wo_site_webroot)) and
os.path.exists('{0}/logs'.format(wo_site_webroot))):
Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
else:
Log.info(self, "[" + Log.ENDC + "Fail" + Log.OKBLUE + "]")
raise SiteError("setup webroot failed for site")
def setupdatabase(self, data):
wo_domain_name = data['site_name']
wo_random_pass = (''.join(random.sample(string.ascii_uppercase +
string.ascii_lowercase +
string.digits, 24)))
wo_replace_dash = wo_domain_name.replace('-', '_')
wo_replace_dot = wo_replace_dash.replace('.', '_')
wo_replace_underscore = wo_replace_dot.replace('_', '')
if self.app.config.has_section('mysql'):
prompt_dbname = self.app.config.get('mysql', 'db-name')
prompt_dbuser = self.app.config.get('mysql', 'db-user')
wo_mysql_grant_host = self.app.config.get('mysql', 'grant-host')
else:
prompt_dbname = False
prompt_dbuser = False
wo_mysql_grant_host = 'localhost'
2019-07-14 22:50:34 +02:00
wo_db_name = ''
wo_db_username = ''
wo_db_password = ''
if prompt_dbname == 'True' or prompt_dbname == 'true':
try:
wo_db_name = input('Enter the MySQL database name [{0}]: '
.format(wo_replace_dot))
except EOFError:
2019-07-14 22:50:34 +02:00
raise SiteError("Unable to input database name")
if not wo_db_name:
wo_db_name = (wo_replace_dot[0:32] + '_' + generate_8_random())
2019-07-14 22:50:34 +02:00
if prompt_dbuser == 'True' or prompt_dbuser == 'true':
try:
wo_db_username = input('Enter the MySQL database user name [{0}]: '
.format(wo_replace_dot))
wo_db_password = getpass.getpass(prompt='Enter the MySQL database'
' password [{0}]: '
.format(wo_random_pass))
except EOFError:
2019-07-14 22:50:34 +02:00
raise SiteError("Unable to input database credentials")
if not wo_db_username:
wo_db_username = (wo_replace_underscore[0:12] + generate_random())
2019-07-14 22:50:34 +02:00
if not wo_db_password:
wo_db_password = wo_random_pass
2019-07-14 22:50:34 +02:00
# create MySQL database
Log.info(self, "Setting up database\t\t", end='')
Log.debug(self, "Creating database {0}".format(wo_db_name))
try:
if WOMysql.check_db_exists(self, wo_db_name):
Log.debug(self, "Database already exists, Updating DB_NAME .. ")
wo_db_name = (wo_db_name[0:32] + '_' + generate_8_random())
wo_db_username = (wo_db_name[0:12] + generate_random())
except MySQLConnectionError:
2019-07-14 22:50:34 +02:00
raise SiteError("MySQL Connectivity problem occured")
try:
WOMysql.execute(self, "create database `{0}`"
.format(wo_db_name))
except StatementExcecutionError:
2019-07-14 22:50:34 +02:00
Log.info(self, "[" + Log.ENDC + Log.FAIL + "Failed" + Log.OKBLUE + "]")
raise SiteError("create database execution failed")
# Create MySQL User
Log.debug(self, "Creating user {0}".format(wo_db_username))
Log.debug(self, "create user `{0}`@`{1}` identified by ''"
.format(wo_db_username, wo_mysql_grant_host))
try:
WOMysql.execute(self,
"create user `{0}`@`{1}` identified by '{2}'"
.format(wo_db_username, wo_mysql_grant_host,
wo_db_password), log=False)
except StatementExcecutionError:
2019-07-14 22:50:34 +02:00
Log.info(self, "[" + Log.ENDC + Log.FAIL + "Failed" + Log.OKBLUE + "]")
raise SiteError("creating user failed for database")
# Grant permission
Log.debug(self, "Setting up user privileges")
try:
WOMysql.execute(self,
2020-10-12 10:20:03 +02:00
"grant select, insert, update, delete, create, drop, "
"references, index, alter, create temporary tables, "
"lock tables, execute, create view, show view, "
"create routine, alter routine, event, "
"trigger on `{0}`.* to `{1}`@`{2}`"
2019-07-14 22:50:34 +02:00
.format(wo_db_name,
wo_db_username, wo_mysql_grant_host))
except StatementExcecutionError:
2019-07-14 22:50:34 +02:00
Log.info(self, "[" + Log.ENDC + Log.FAIL + "Failed" + Log.OKBLUE + "]")
SiteError("grant privileges to user failed for database ")
Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
data['wo_db_name'] = wo_db_name
data['wo_db_user'] = wo_db_username
data['wo_db_pass'] = wo_db_password
2019-10-02 13:13:32 +02:00
data['wo_db_host'] = WOVar.wo_mysql_host
2019-07-14 22:50:34 +02:00
data['wo_mysql_grant_host'] = wo_mysql_grant_host
return(data)
def setupwordpress(self, data, vhostonly=False):
2019-07-14 22:50:34 +02:00
wo_domain_name = data['site_name']
wo_site_webroot = data['webroot']
if self.app.config.has_section('wordpress'):
prompt_wpprefix = self.app.config.get('wordpress', 'prefix')
wo_wp_user = self.app.config.get('wordpress', 'user')
wo_wp_pass = self.app.config.get('wordpress', 'password')
wo_wp_email = self.app.config.get('wordpress', 'email')
else:
prompt_wpprefix = False
wo_wp_user = ''
wo_wp_pass = ''
wo_wp_email = ''
2019-07-14 22:50:34 +02:00
# Random characters
wo_random_pass = (''.join(random.sample(string.ascii_uppercase +
string.ascii_lowercase +
string.digits, 24)))
2019-07-14 22:50:34 +02:00
wo_wp_prefix = ''
# wo_wp_user = ''
# wo_wp_pass = ''
if 'wp-user' in data.keys() and data['wp-user']:
wo_wp_user = data['wp-user']
if 'wp-email' in data.keys() and data['wp-email']:
wo_wp_email = data['wp-email']
if 'wp-pass' in data.keys() and data['wp-pass']:
wo_wp_pass = data['wp-pass']
Log.info(self, "Downloading WordPress \t\t", end='')
WOFileUtils.chdir(self, '{0}/htdocs/'.format(wo_site_webroot))
try:
if WOShellExec.cmd_exec(self, "wp --allow-root core"
" download"):
pass
else:
Log.info(self, "[" + Log.ENDC + Log.FAIL +
"Fail" + Log.OKBLUE + "]")
raise SiteError("download WordPress core failed")
except CommandExecutionError:
2019-07-14 22:50:34 +02:00
Log.info(self, "[" + Log.ENDC + Log.FAIL + "Fail" + Log.OKBLUE + "]")
2019-09-02 18:56:34 +02:00
raise SiteError("download WordPress core failed")
2019-07-14 22:50:34 +02:00
Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
if not (data['wo_db_name'] and data['wo_db_user'] and data['wo_db_pass']):
data = setupdatabase(self, data)
if prompt_wpprefix == 'True' or prompt_wpprefix == 'true':
try:
wo_wp_prefix = input('Enter the WordPress table prefix [wp_]: ')
while not re.match('^[A-Za-z0-9_]*$', wo_wp_prefix):
Log.warn(self, "table prefix can only "
"contain numbers, letters, and underscores")
wo_wp_prefix = input('Enter the WordPress table prefix [wp_]: '
)
except EOFError:
2019-07-14 22:50:34 +02:00
raise SiteError("input table prefix failed")
if not wo_wp_prefix:
wo_wp_prefix = 'wp_'
# Modify wp-config.php & move outside the webroot
WOFileUtils.chdir(self, '{0}/htdocs/'.format(wo_site_webroot))
Log.debug(self, "Setting up wp-config file")
if not data['multisite']:
Log.debug(self, "Generating wp-config for WordPress Single site")
2019-08-06 12:10:50 +02:00
Log.debug(self, "/bin/bash -c \"{0} --allow-root "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"config create " +
"--dbname=\'{0}\' --dbprefix=\'{1}\' --dbuser=\'{2}\' "
"--dbhost=\'{3}\' "
.format(data['wo_db_name'], wo_wp_prefix,
data['wo_db_user'], data['wo_db_host']) +
2019-10-12 11:46:11 +02:00
"--dbpass= "
"--extra-php<<PHP \n {0}\nPHP\""
.format("\n\ndefine(\'WP_DEBUG\', false);"))
2019-07-14 22:50:34 +02:00
try:
2019-08-06 12:10:50 +02:00
if WOShellExec.cmd_exec(self, "/bin/bash -c \"{0} --allow-root"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
" config create " +
"--dbname=\'{0}\' --dbprefix=\'{1}\' "
"--dbuser=\'{2}\' --dbhost=\'{3}\' "
.format(data['wo_db_name'], wo_wp_prefix,
data['wo_db_user'],
data['wo_db_host']
) +
"--dbpass=\'{0}\' "
"--extra-php<<PHP \n"
"\n{1}\nPHP\""
.format(data['wo_db_pass'],
"\ndefine(\'WP_DEBUG\', false);"),
log=False
):
pass
else:
raise SiteError("generate wp-config failed for wp single site")
except CommandExecutionError:
2019-07-14 22:50:34 +02:00
raise SiteError("generate wp-config failed for wp single site")
else:
Log.debug(self, "Generating wp-config for WordPress multisite")
2019-08-06 12:10:50 +02:00
Log.debug(self, "/bin/bash -c \"{0} --allow-root "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"config create " +
"--dbname=\'{0}\' --dbprefix=\'{1}\' --dbhost=\'{2}\' "
2019-07-15 10:43:40 +02:00
.format(data['wo_db_name'],
wo_wp_prefix, data['wo_db_host']) +
2019-10-12 11:46:11 +02:00
"--dbuser=\'{0}\' --dbpass= "
"--extra-php<<PHP \n {1} {2} {3} \nPHP\""
.format(data['wo_db_user'],
2019-07-14 22:50:34 +02:00
"\ndefine(\'WPMU_ACCEL_REDIRECT\',"
" true);",
"\ndefine(\'CONCATENATE_SCRIPTS\',"
" false);",
"\n\ndefine(\'WP_DEBUG\', false);"))
try:
2019-08-06 12:10:50 +02:00
if WOShellExec.cmd_exec(self, "/bin/bash -c \"{0} --allow-root"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
" config create " +
"--dbname=\'{0}\' --dbprefix=\'{1}\' "
"--dbhost=\'{2}\' "
.format(data['wo_db_name'], wo_wp_prefix,
data['wo_db_host']) +
"--dbuser=\'{0}\' --dbpass=\'{1}\' "
"--extra-php<<PHP \n "
"\n{2} {3}\nPHP\""
.format(data['wo_db_user'],
data['wo_db_pass'],
"\ndefine(\'WPMU_ACCEL_REDIRECT\',"
" true);",
"\ndefine(\'WP_DEBUG\', false);"),
log=False
):
pass
else:
raise SiteError("generate wp-config failed for wp multi site")
2019-07-29 16:50:32 +02:00
except CommandExecutionError:
2019-07-14 22:50:34 +02:00
raise SiteError("generate wp-config failed for wp multi site")
2019-10-29 19:26:51 +01:00
# set all wp-config.php variables
wp_conf_variables = [
['WP_REDIS_PREFIX', '{0}:'.format(wo_domain_name)],
2019-10-29 19:26:51 +01:00
['WP_MEMORY_LIMIT', '128M'],
['WP_MAX_MEMORY_LIMIT', '256M'],
['CONCATENATE_SCRIPTS', 'false'],
['WP_POST_REVISIONS', '10'],
['MEDIA_TRASH', 'true'],
['EMPTY_TRASH_DAYS', '15'],
['WP_AUTO_UPDATE_CORE', 'minor'],
['WP_REDIS_DISABLE_BANNERS', 'true']]
Log.wait(self, "Configuring WordPress")
2019-10-29 19:26:51 +01:00
for wp_conf in wp_conf_variables:
wp_var = wp_conf[0]
wp_val = wp_conf[1]
var_raw = (bool(wp_val == 'true' or wp_val == 'false'))
2019-10-29 19:26:51 +01:00
try:
WOShellExec.cmd_exec(
self, "/bin/bash -c \"{0} --allow-root "
.format(WOVar.wo_wpcli_path) +
"config set {0} "
"\'{1}\' {wp_raw}\""
.format(wp_var, wp_val,
2019-10-29 19:32:26 +01:00
wp_raw='--raw'
if var_raw is True else ''))
2019-10-29 19:26:51 +01:00
except CommandExecutionError as e:
Log.failed(self, "Configuring WordPress")
2019-10-29 19:26:51 +01:00
Log.debug(self, str(e))
Log.error(self, 'Unable to define wp-config.php variables')
Log.valide(self, "Configuring WordPress")
2019-07-14 22:50:34 +02:00
# WOFileUtils.mvfile(self, os.getcwd()+'/wp-config.php',
# os.path.abspath(os.path.join(os.getcwd(), os.pardir)))
try:
Log.debug(self, "Moving file from {0} to {1}".format(os.getcwd(
2019-10-30 04:30:14 +01:00
) + '/wp-config.php', os.path.abspath(os.path.join(os.getcwd(),
os.pardir))))
shutil.move(os.getcwd() + '/wp-config.php',
2019-07-14 22:50:34 +02:00
os.path.abspath(os.path.join(os.getcwd(), os.pardir)))
except Exception as e:
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
Log.error(self, 'Unable to move file from {0} to {1}'
2019-10-30 04:30:14 +01:00
.format(os.getcwd() + '/wp-config.php',
2019-07-14 22:50:34 +02:00
os.path.abspath(os.path.join(os.getcwd(),
os.pardir))), False)
raise SiteError("Unable to move wp-config.php")
if not wo_wp_user:
2019-10-02 13:13:32 +02:00
wo_wp_user = WOVar.wo_user
2019-07-14 22:50:34 +02:00
while not wo_wp_user:
Log.warn(self, "Username can have only alphanumeric"
"characters, spaces, underscores, hyphens,"
"periods and the @ symbol.")
try:
wo_wp_user = input('Enter WordPress username: ')
except EOFError:
2019-07-14 22:50:34 +02:00
raise SiteError("input WordPress username failed")
if not wo_wp_pass:
wo_wp_pass = wo_random_pass
2019-07-14 22:50:34 +02:00
if not wo_wp_email:
2019-10-02 13:13:32 +02:00
wo_wp_email = WOVar.wo_email
2019-07-14 22:50:34 +02:00
while not wo_wp_email:
try:
wo_wp_email = input('Enter WordPress email: ')
except EOFError:
2019-07-14 22:50:34 +02:00
raise SiteError("input WordPress username failed")
try:
while not re.match(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$",
wo_wp_email):
Log.info(self, "EMail not Valid in config, "
"Please provide valid email id")
wo_wp_email = input("Enter your email: ")
except EOFError:
2019-07-14 22:50:34 +02:00
raise SiteError("input WordPress user email failed")
Log.debug(self, "Setting up WordPress tables")
Log.wait(self, "Installing WordPress")
2019-07-14 22:50:34 +02:00
if not data['multisite']:
Log.debug(self, "Creating tables for WordPress Single site")
2019-10-29 19:26:51 +01:00
Log.debug(
self, "{0} --allow-root core install "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"--url=\'{0}\' --title=\'{0}\' --admin_name=\'{1}\' "
2019-10-12 11:09:35 +02:00
.format(data['site_name'], wo_wp_user) +
2019-10-12 11:46:11 +02:00
"--admin_password= --admin_email=\'{0}\'"
2019-10-12 11:09:35 +02:00
.format(wo_wp_email))
2019-07-14 22:50:34 +02:00
try:
2019-10-12 11:09:35 +02:00
if WOShellExec.cmd_exec(
self, "{0} --allow-root core "
.format(WOVar.wo_wpcli_path) +
"install --url=\'{0}\' --title=\'{0}\' "
"--admin_name=\'{1}\' "
.format(data['site_name'], wo_wp_user) +
"--admin_password=\'{0}\' "
"--admin_email=\'{1}\'"
.format(wo_wp_pass, wo_wp_email),
log=False):
2019-07-14 22:50:34 +02:00
pass
else:
Log.failed(self, "Installing WordPress")
2019-07-14 22:50:34 +02:00
raise SiteError(
"setup WordPress tables failed for single site")
except CommandExecutionError:
2019-07-14 22:50:34 +02:00
raise SiteError("setup WordPress tables failed for single site")
else:
Log.debug(self, "Creating tables for WordPress multisite")
2019-08-06 12:10:50 +02:00
Log.debug(self, "{0} --allow-root "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"core multisite-install "
"--url=\'{0}\' --title=\'{0}\' --admin_name=\'{1}\' "
2019-10-12 11:09:35 +02:00
.format(data['site_name'], wo_wp_user) +
2019-10-12 11:46:11 +02:00
"--admin_password= --admin_email=\'{0}\' "
2019-07-14 22:50:34 +02:00
"{subdomains}"
2019-10-12 11:09:35 +02:00
.format(wo_wp_email,
2019-07-14 22:50:34 +02:00
subdomains='--subdomains'
if not data['wpsubdir'] else ''))
try:
2019-10-12 11:09:35 +02:00
if WOShellExec.cmd_exec(
self, "{0} --allow-root "
.format(WOVar.wo_wpcli_path) +
"core multisite-install "
"--url=\'{0}\' --title=\'{0}\' "
"--admin_name=\'{1}\' "
.format(data['site_name'], wo_wp_user) +
"--admin_password=\'{0}\' "
"--admin_email=\'{1}\' "
"{subdomains}"
.format(wo_wp_pass, wo_wp_email,
subdomains='--subdomains'
if not data['wpsubdir'] else ''),
log=False):
2019-07-14 22:50:34 +02:00
pass
else:
Log.failed(self, "Installing WordPress")
2019-07-14 22:50:34 +02:00
raise SiteError(
"setup WordPress tables failed for wp multi site")
except CommandExecutionError:
2019-07-14 22:50:34 +02:00
raise SiteError("setup WordPress tables failed for wp multi site")
Log.valide(self, "Installing WordPress")
2019-07-14 22:50:34 +02:00
Log.debug(self, "Updating WordPress permalink")
try:
2019-08-06 12:10:50 +02:00
WOShellExec.cmd_exec(self, " {0} --allow-root "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"rewrite structure "
2019-08-31 02:42:46 +02:00
"/%postname%/")
2019-07-14 22:50:34 +02:00
except CommandExecutionError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
raise SiteError("Update wordpress permalinks failed")
"""Install nginx-helper plugin """
installwp_plugin(self, 'nginx-helper', data)
if data['wpfc']:
2019-07-29 16:08:49 +02:00
plugin_data_object = {"log_level": "INFO",
"log_filesize": 5,
"enable_purge": 1,
"enable_map": "0",
"enable_log": 0,
"enable_stamp": 1,
2019-07-29 16:08:49 +02:00
"purge_homepage_on_new": 1,
"purge_homepage_on_edit": 1,
"purge_homepage_on_del": 1,
"purge_archive_on_new": 1,
"purge_archive_on_edit": 1,
"purge_archive_on_del": 1,
2019-07-29 16:08:49 +02:00
"purge_archive_on_new_comment": 0,
"purge_archive_on_deleted_comment": 0,
"purge_page_on_mod": 1,
"purge_page_on_new_comment": 1,
"purge_page_on_deleted_comment": 1,
"cache_method": "enable_fastcgi",
"purge_method": "get_request",
"redis_hostname": "127.0.0.1",
"redis_port": "6379",
"redis_prefix": "nginx-cache:"}
plugin_data = json.dumps(plugin_data_object)
2019-07-29 15:38:16 +02:00
setupwp_plugin(self, "nginx-helper",
"rt_wp_nginx_helper_options", plugin_data, data)
2019-07-14 22:50:34 +02:00
elif data['wpredis']:
2019-07-29 16:08:49 +02:00
plugin_data_object = {"log_level": "INFO",
"log_filesize": 5,
"enable_purge": 1,
"enable_map": "0",
"enable_log": 0,
"enable_stamp": 1,
2019-07-29 16:08:49 +02:00
"purge_homepage_on_new": 1,
"purge_homepage_on_edit": 1,
"purge_homepage_on_del": 1,
"purge_archive_on_new": 1,
"purge_archive_on_edit": 1,
"purge_archive_on_del": 1,
2019-07-29 16:08:49 +02:00
"purge_archive_on_new_comment": 0,
"purge_archive_on_deleted_comment": 0,
"purge_page_on_mod": 1,
"purge_page_on_new_comment": 1,
"purge_page_on_deleted_comment": 1,
"cache_method": "enable_redis",
"purge_method": "get_request",
"redis_hostname": "127.0.0.1",
"redis_port": "6379",
"redis_prefix": "nginx-cache:"}
plugin_data = json.dumps(plugin_data_object)
2019-07-14 22:50:34 +02:00
setupwp_plugin(self, 'nginx-helper',
'rt_wp_nginx_helper_options', plugin_data, data)
"""Install Wp Super Cache"""
if data['wpsc']:
installwp_plugin(self, 'wp-super-cache', data)
"""Install Redis Cache"""
if data['wpredis']:
installwp_plugin(self, 'redis-cache', data)
2019-08-15 19:59:23 +02:00
"""Install Cache-Enabler"""
if data['wpce']:
installwp_plugin(self, 'cache-enabler', data)
2019-08-19 11:23:32 +02:00
plugin_data_object = {"expires": 24,
"new_post": 1,
"new_comment": 0,
"webp": 0,
"clear_on_upgrade": 1,
"compress": 0,
"excl_ids": "",
"excl_regexp": "",
"excl_cookies": "",
"incl_attributes": "",
"minify_html": 1}
plugin_data = json.dumps(plugin_data_object)
setupwp_plugin(self, 'cache-enabler', 'cache-enabler',
plugin_data, data)
2019-10-29 19:26:51 +01:00
WOShellExec.cmd_exec(
self, "/bin/bash -c \"{0} --allow-root "
.format(WOVar.wo_wpcli_path) +
"config set WP_CACHE "
"true --raw\"")
2019-08-15 19:59:23 +02:00
if vhostonly:
try:
WOShellExec.cmd_exec(self, "/bin/bash -c \"{0} --allow-root "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
"db clean --yes\"")
WOFileUtils.chdir(self, '{0}'.format(wo_site_webroot))
WOFileUtils.rm(self, "{0}/htdocs".format(wo_site_webroot))
WOFileUtils.mkdir(self, "{0}/htdocs".format(wo_site_webroot))
WOFileUtils.chown(self, "{0}/htdocs".format(wo_site_webroot),
'www-data', 'www-data')
2019-08-30 03:54:25 +02:00
except CommandExecutionError:
raise SiteError("Cleaning WordPress install failed")
2019-07-14 22:50:34 +02:00
wp_creds = dict(wp_user=wo_wp_user, wp_pass=wo_wp_pass,
wp_email=wo_wp_email)
return(wp_creds)
def setupwordpressnetwork(self, data):
wo_site_webroot = data['webroot']
WOFileUtils.chdir(self, '{0}/htdocs/'.format(wo_site_webroot))
Log.info(self, "Setting up WordPress Network \t", end='')
try:
if WOShellExec.cmd_exec(self, 'wp --allow-root core multisite-convert'
' --title=\'{0}\' {subdomains}'
.format(data['www_domain'],
subdomains='--subdomains'
if not data['wpsubdir'] else '')):
pass
else:
Log.info(self, "[" + Log.ENDC + Log.FAIL +
"Fail" + Log.OKBLUE + "]")
raise SiteError("setup WordPress network failed")
except CommandExecutionError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
Log.info(self, "[" + Log.ENDC + Log.FAIL + "Fail" + Log.OKBLUE + "]")
raise SiteError("setup WordPress network failed")
Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
def installwp_plugin(self, plugin_name, data):
wo_site_webroot = data['webroot']
2019-09-05 12:38:02 +02:00
Log.wait(self, "Installing plugin {0}"
2019-07-14 22:50:34 +02:00
.format(plugin_name))
WOFileUtils.chdir(self, '{0}/htdocs/'.format(wo_site_webroot))
try:
2019-08-06 12:10:50 +02:00
WOShellExec.cmd_exec(self, "{0} plugin "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"--allow-root install "
"{0}".format(plugin_name))
except CommandExecutionError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
raise SiteError("plugin installation failed")
try:
2019-08-06 12:10:50 +02:00
WOShellExec.cmd_exec(self, "{0} plugin "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"--allow-root activate "
"{0} {na}"
.format(plugin_name,
na='--network' if data['multisite']
else ''
))
except CommandExecutionError as e:
2019-09-05 12:38:02 +02:00
Log.failed(self, "Installing plugin {0}"
.format(plugin_name))
2019-09-05 12:49:04 +02:00
Log.debug(self, "{0}".format(e))
raise SiteError("plugin activation failed")
2019-09-05 12:38:02 +02:00
else:
Log.valide(self, "Installing plugin {0}"
.format(plugin_name))
2019-07-14 22:50:34 +02:00
return 1
def uninstallwp_plugin(self, plugin_name, data):
wo_site_webroot = data['webroot']
Log.debug(self, "Uninstalling plugin {0}, please wait..."
.format(plugin_name))
WOFileUtils.chdir(self, '{0}/htdocs/'.format(wo_site_webroot))
2019-09-05 12:38:02 +02:00
Log.wait(self, "Uninstalling plugin {0}"
2019-07-14 22:50:34 +02:00
.format(plugin_name))
try:
2019-08-19 11:23:32 +02:00
WOShellExec.cmd_exec(self, "{0} plugin "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"--allow-root deactivate "
"{0}".format(plugin_name))
2019-08-19 11:23:32 +02:00
WOShellExec.cmd_exec(self, "{0} plugin "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"--allow-root uninstall "
"{0}".format(plugin_name))
except CommandExecutionError as e:
2019-09-05 12:38:02 +02:00
Log.failed(self, "Uninstalling plugin {0}"
2019-09-05 12:44:33 +02:00
.format(plugin_name))
2019-09-05 12:49:04 +02:00
Log.debug(self, "{0}".format(e))
raise SiteError("plugin uninstall failed")
2019-09-05 12:38:02 +02:00
else:
Log.valide(self, "Uninstalling plugin {0}"
2019-09-05 12:44:33 +02:00
.format(plugin_name))
2019-07-14 22:50:34 +02:00
def setupwp_plugin(self, plugin_name, plugin_option, plugin_data, data):
wo_site_webroot = data['webroot']
2019-09-05 12:44:33 +02:00
Log.wait(self, "Setting plugin {0}"
2019-07-14 22:50:34 +02:00
.format(plugin_name))
WOFileUtils.chdir(self, '{0}/htdocs/'.format(wo_site_webroot))
if not data['multisite']:
try:
2019-08-19 11:23:32 +02:00
WOShellExec.cmd_exec(self, "{0} "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"--allow-root option update "
"{0} \'{1}\' --format=json"
.format(plugin_option, plugin_data))
except CommandExecutionError as e:
2019-09-05 12:45:27 +02:00
Log.failed(self, "Setting plugin {0}"
.format(plugin_name))
2019-09-05 12:49:04 +02:00
Log.debug(self, "{0}".format(e))
raise SiteError("plugin setup failed")
2019-09-05 12:45:27 +02:00
else:
Log.valide(self, "Setting plugin {0}"
.format(plugin_name))
2019-07-14 22:50:34 +02:00
else:
try:
2019-08-19 11:23:32 +02:00
WOShellExec.cmd_exec(self, "{0} "
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wpcli_path) +
2019-07-14 22:50:34 +02:00
"--allow-root network meta update 1 "
"{0} \'{1}\' --format=json"
.format(plugin_option, plugin_data
))
except CommandExecutionError as e:
2019-09-05 12:49:04 +02:00
Log.failed(self, "Setting plugin {0}"
.format(plugin_name))
Log.debug(self, "{0}".format(e))
2019-07-14 22:50:34 +02:00
raise SiteError("plugin setup failed")
2019-09-05 12:44:33 +02:00
else:
Log.valide(self, "Setting plugin {0}"
.format(plugin_name))
2019-07-14 22:50:34 +02:00
def setwebrootpermissions(self, webroot):
Log.debug(self, "Setting up permissions")
try:
2019-09-02 18:56:34 +02:00
WOFileUtils.findBrokenSymlink(self, '/var/www/')
2019-10-02 13:13:32 +02:00
WOFileUtils.chown(self, webroot, WOVar.wo_php_user,
WOVar.wo_php_user, recursive=True)
2019-07-14 22:50:34 +02:00
except Exception as e:
Log.debug(self, str(e))
raise SiteError("problem occured while setting up webroot permissions")
def sitebackup(self, data):
wo_site_webroot = data['webroot']
2019-10-02 13:13:32 +02:00
backup_path = wo_site_webroot + '/backup/{0}'.format(WOVar.wo_date)
2019-07-14 22:50:34 +02:00
if not WOFileUtils.isexist(self, backup_path):
WOFileUtils.mkdir(self, backup_path)
Log.info(self, "Backup location : {0}".format(backup_path))
WOFileUtils.copyfile(self, '/etc/nginx/sites-available/{0}'
.format(data['site_name']), backup_path)
if data['currsitetype'] in ['html', 'php', 'php72', 'php74',
2022-01-23 15:14:24 -03:00
'php73', 'php80', 'php81', 'proxy', 'mysql']:
if not data['wp']:
2019-07-14 22:50:34 +02:00
Log.info(self, "Backing up Webroot \t\t", end='')
WOFileUtils.copyfiles(self, wo_site_webroot +
'/htdocs', backup_path + '/htdocs')
Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
else:
Log.info(self, "Backing up Webroot \t\t", end='')
WOFileUtils.mvfile(self, wo_site_webroot + '/htdocs', backup_path)
Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
configfiles = glob.glob(wo_site_webroot + '/*-config.php')
if not configfiles:
# search for wp-config.php inside htdocs/
Log.debug(self, "Config files not found in {0}/ "
.format(wo_site_webroot))
if data['currsitetype'] in ['mysql']:
pass
else:
Log.debug(self, "Searching wp-config.php in {0}/htdocs/ "
.format(wo_site_webroot))
configfiles = glob.glob(wo_site_webroot + '/htdocs/wp-config.php')
# if configfiles and WOFileUtils.isexist(self, configfiles[0]):
# wo_db_name = (WOFileUtils.grep(self, configfiles[0],
# 'DB_NAME').split(',')[1]
# .split(')')[0].strip().replace('\'', ''))
if data['wo_db_name']:
Log.info(self, 'Backing up database \t\t', end='')
try:
if not WOShellExec.cmd_exec(
self, "mysqldump --single-transaction --hex-blob "
2021-01-08 14:49:09 +01:00
"{0} | zstd -c > {1}/{0}.zst"
.format(data['wo_db_name'],
backup_path)):
2019-07-14 22:50:34 +02:00
Log.info(self,
"[" + Log.ENDC + Log.FAIL + "Fail" + Log.OKBLUE + "]")
raise SiteError("mysqldump failed to backup database")
except CommandExecutionError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
Log.info(self, "[" + Log.ENDC + "Fail" + Log.OKBLUE + "]")
raise SiteError("mysqldump failed to backup database")
Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
# move wp-config.php/wo-config.php to backup
if data['currsitetype'] in ['mysql', 'proxy']:
if data['php73'] is True and not data['wp']:
WOFileUtils.copyfile(self, configfiles[0], backup_path)
else:
WOFileUtils.mvfile(self, configfiles[0], backup_path)
else:
WOFileUtils.copyfile(self, configfiles[0], backup_path)
def site_package_check(self, stype):
apt_packages = []
packages = []
stack = WOStackController()
stack.app = self.app
pargs = self.app.pargs
if stype in ['html', 'proxy', 'php', 'php72', 'mysql', 'wp', 'wpsubdir',
2022-01-23 15:14:24 -03:00
'wpsubdomain', 'php73', 'php74', 'php80', 'php81']:
2019-07-14 22:50:34 +02:00
Log.debug(self, "Setting apt_packages variable for Nginx")
# Check if server has nginx-custom package
if not (WOAptGet.is_installed(self, 'nginx-custom') or
WOAptGet.is_installed(self, 'nginx-mainline')):
# check if Server has nginx-plus installed
if WOAptGet.is_installed(self, 'nginx-plus'):
# do something
# do post nginx installation configuration
Log.info(self, "NGINX PLUS Detected ...")
2019-10-02 13:13:32 +02:00
apt = ["nginx-plus"] + WOVar.wo_nginx
# apt_packages = apt_packages + WOVar.wo_nginx
post_pref(self, apt, packages)
2019-07-14 22:50:34 +02:00
elif WOAptGet.is_installed(self, 'nginx'):
Log.info(self, "WordOps detected a previously"
"installed Nginx package. "
"It may or may not have required modules. "
"\nIf you need help, please create an issue at "
"https://github.com/WordOps/WordOps/issues/ \n")
2019-10-02 13:13:32 +02:00
apt = ["nginx"] + WOVar.wo_nginx
# apt_packages = apt_packages + WOVar.wo_nginx
2019-08-15 19:59:23 +02:00
post_pref(self, apt, packages)
2019-10-01 04:08:54 +02:00
elif os.path.isfile('/usr/sbin/nginx'):
2019-10-02 13:13:32 +02:00
post_pref(self, WOVar.wo_nginx, [])
2019-07-14 22:50:34 +02:00
else:
2019-10-02 13:13:32 +02:00
apt_packages = apt_packages + WOVar.wo_nginx
2019-07-14 22:50:34 +02:00
else:
# Fix for Nginx white screen death
if not WOFileUtils.grep(self, '/etc/nginx/fastcgi_params',
'SCRIPT_FILENAME'):
with open('/etc/nginx/fastcgi_params', encoding='utf-8',
mode='a') as wo_nginx:
wo_nginx.write('fastcgi_param \tSCRIPT_FILENAME '
'\t$request_filename;\n')
if ((pargs.php and pargs.php73) or (pargs.php and pargs.php74) or
2022-01-23 15:14:24 -03:00
(pargs.php and pargs.php72) or (pargs.php and pargs.php80) or
(pargs.php and pargs.php81) or
(pargs.php73 and pargs.php74) or (pargs.php72 and pargs.php73) or
2022-01-23 15:14:24 -03:00
(pargs.php72 and pargs.php74) or (pargs.php73 and pargs.php80) or
(pargs.php74 and pargs.php80) or (pargs.php80 and pargs.php81) or
(pargs.php72 and pargs.php80) or (pargs.php72 and pargs.php81) or
(pargs.php73 and pargs.php81) or (pargs.php74 and pargs.php81) or
(pargs.php80 and pargs.php81)):
Log.error(
self, "Error: two different PHP versions cannot be "
"combined within the same WordOps site")
if ((not pargs.php72) and (not pargs.php73) and (not pargs.php74) and
2022-01-23 15:14:24 -03:00
(not pargs.php80) and (not pargs.php81) and
stype in ['php', 'mysql', 'wp', 'wpsubdir',
'wpsubdomain']):
Log.debug(self, "Setting apt_packages variable for PHP")
php_check = 'php7.3-fpm'
2020-05-04 16:36:12 +02:00
php_to_setup = WOVar.wo_php73
if self.app.config.has_section('php'):
config_php_ver = self.app.config.get(
'php', 'version')
if config_php_ver == '7.2':
php_check = 'php7.2-fpm'
php_to_setup = WOVar.wo_php72
elif config_php_ver == '7.3':
php_check = 'php7.3-fpm'
php_to_setup = WOVar.wo_php73
elif config_php_ver == '7.4':
php_check = 'php7.4-fpm'
php_to_setup = WOVar.wo_php74
2022-01-23 15:14:24 -03:00
elif config_php_ver == '8.0':
php_check = 'php8.0-fpm'
php_to_setup = WOVar.wo_php80
elif config_php_ver == '8.1':
php_check = 'php8.1-fpm'
php_to_setup = WOVar.wo_php81
else:
php_check = 'php7.3-fpm'
php_to_setup = WOVar.wo_php73
else:
php_check = 'php7.3-fpm'
php_to_setup = WOVar.wo_php73
2019-07-14 22:50:34 +02:00
if not (WOAptGet.is_installed(self, php_check)):
apt_packages = apt_packages + php_to_setup + WOVar.wo_php_extra
if pargs.php72 and stype in ['php72', 'mysql', 'wp',
'wpsubdir', 'wpsubdomain']:
2019-07-14 22:50:34 +02:00
Log.debug(self, "Setting apt_packages variable for PHP 7.2")
if not WOAptGet.is_installed(self, 'php7.2-fpm'):
2019-12-08 01:36:08 +01:00
apt_packages = apt_packages + WOVar.wo_php72 + WOVar.wo_php_extra
2019-07-14 22:50:34 +02:00
if pargs.php73 and stype in ['php73', 'mysql', 'wp',
2019-07-14 22:50:34 +02:00
'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting apt_packages variable for PHP 7.3")
if not WOAptGet.is_installed(self, 'php7.3-fpm'):
2019-12-08 01:36:08 +01:00
apt_packages = apt_packages + WOVar.wo_php73 + WOVar.wo_php_extra
if pargs.php74 and stype in ['php74', 'mysql', 'wp',
'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting apt_packages variable for PHP 7.4")
if not WOAptGet.is_installed(self, 'php7.4-fpm'):
2019-12-08 01:36:08 +01:00
apt_packages = apt_packages + WOVar.wo_php74 + WOVar.wo_php_extra
2019-07-14 22:50:34 +02:00
2022-01-23 15:14:24 -03:00
if pargs.php80 and stype in ['php80', 'mysql', 'wp',
'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting apt_packages variable for PHP 8.0")
if not WOAptGet.is_installed(self, 'php8.0-fpm'):
apt_packages = apt_packages + WOVar.wo_php80 + WOVar.wo_php_extra
if pargs.php81 and stype in ['php81', 'mysql', 'wp',
'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting apt_packages variable for PHP 8.1")
if not WOAptGet.is_installed(self, 'php8.1-fpm'):
apt_packages = apt_packages + WOVar.wo_php81 + WOVar.wo_php_extra
2019-07-14 22:50:34 +02:00
if stype in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting apt_packages variable for MySQL")
2019-07-28 21:23:07 +02:00
if not WOShellExec.cmd_exec(self, "/usr/bin/mysqladmin ping"):
apt_packages = apt_packages + WOVar.wo_mysql
2019-07-14 22:50:34 +02:00
if stype in ['wp', 'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting packages variable for WP-CLI")
if not WOAptGet.is_exec(self, "wp"):
2019-07-14 22:50:34 +02:00
packages = packages + [["https://github.com/wp-cli/wp-cli/"
"releases/download/v{0}/"
"wp-cli-{0}.phar"
2019-10-02 13:13:32 +02:00
.format(WOVar.wo_wp_cli),
2019-07-14 22:50:34 +02:00
"/usr/local/bin/wp", "WP-CLI"]]
if pargs.wpredis:
2019-07-14 22:50:34 +02:00
Log.debug(self, "Setting apt_packages variable for redis")
if not WOAptGet.is_installed(self, 'redis-server'):
apt_packages = apt_packages + WOVar.wo_redis
2019-07-14 22:50:34 +02:00
if pargs.ngxblocker:
2019-09-27 14:15:00 +02:00
if not os.path.isdir('/etc/nginx/bots.d'):
Log.debug(self, "Setting packages variable for ngxblocker")
packages = packages + \
[["https://raw.githubusercontent.com/"
"mitchellkrogza/nginx-ultimate-bad-bot-blocker"
"/master/install-ngxblocker",
"/usr/local/sbin/install-ngxblocker",
"ngxblocker"]]
2019-07-14 22:50:34 +02:00
return(stack.install(apt_packages=apt_packages, packages=packages,
disp_msg=False))
2019-08-16 00:26:08 +02:00
2019-07-14 22:50:34 +02:00
def updatewpuserpassword(self, wo_domain, wo_site_webroot):
wo_wp_user = ''
wo_wp_pass = ''
WOFileUtils.chdir(self, '{0}/htdocs/'.format(wo_site_webroot))
2019-09-24 19:10:13 +02:00
if not WOShellExec.cmd_exec(self, "wp --allow-root core"
" is-installed"):
# Exit if wo_domain is not wordpress install
2019-07-14 22:50:34 +02:00
Log.error(self, "{0} does not seem to be a WordPress site"
.format(wo_domain))
try:
wo_wp_user = input("Provide WordPress user name [admin]: ")
except Exception as e:
2019-09-02 18:56:34 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
Log.error(self, "\nCould not update password")
if wo_wp_user == "?":
Log.info(self, "Fetching WordPress user list")
try:
WOShellExec.cmd_exec(self, "wp --allow-root user list "
"--fields=user_login | grep -v user_login")
except CommandExecutionError as e:
Log.debug(self, "{0}".format(e))
2019-07-14 22:50:34 +02:00
raise SiteError("fetch wp userlist command failed")
if not wo_wp_user:
wo_wp_user = 'admin'
try:
is_user_exist = WOShellExec.cmd_exec(self, "wp --allow-root user list "
"--fields=user_login | grep {0}$ "
.format(wo_wp_user))
except CommandExecutionError as e:
Log.debug(self, "{0}".format(e))
2019-07-14 22:50:34 +02:00
raise SiteError("if wp user exists check command failed")
if is_user_exist:
try:
wo_wp_pass = getpass.getpass(prompt="Provide password for "
"{0} user: "
.format(wo_wp_user))
while not wo_wp_pass:
wo_wp_pass = getpass.getpass(prompt="Provide password for "
"{0} user: "
.format(wo_wp_user))
except Exception as e:
2019-09-02 18:56:34 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
raise SiteError("failed to read password input ")
try:
WOShellExec.cmd_exec(self, "wp --allow-root user update {0}"
" --user_pass={1}"
.format(wo_wp_user, wo_wp_pass))
except CommandExecutionError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
raise SiteError("wp user password update command failed")
Log.info(self, "Password updated successfully")
else:
Log.error(self, "Invalid WordPress user {0} for {1}."
.format(wo_wp_user, wo_domain))
def display_cache_settings(self, data):
if data['wpsc']:
if data['multisite']:
Log.info(self, "Configure WPSC:"
"\t\thttp://{0}/wp-admin/network/settings.php?"
"page=wpsupercache"
.format(data['site_name']))
else:
Log.info(self, "Configure WPSC:"
"\t\thttp://{0}/wp-admin/options-general.php?"
"page=wpsupercache"
.format(data['site_name']))
if data['wpredis']:
if data['multisite']:
Log.info(self, "Configure redis-cache:"
"\thttp://{0}/wp-admin/network/settings.php?"
"page=redis-cache".format(data['site_name']))
else:
Log.info(self, "Configure redis-cache:"
"\thttp://{0}/wp-admin/options-general.php?"
"page=redis-cache".format(data['site_name']))
Log.info(self, "Object Cache:\t\tEnable")
2019-08-27 15:12:01 +02:00
if data['wpfc']:
if data['multisite']:
Log.info(self, "Nginx-Helper configuration :"
"\thttp://{0}/wp-admin/network/settings.php?"
"page=nginx".format(data['site_name']))
else:
Log.info(self, "Nginx-Helper configuration :"
"\thttp://{0}/wp-admin/options-general.php?"
"page=nginx".format(data['site_name']))
if data['wpce']:
if data['multisite']:
2019-10-02 16:33:40 +02:00
Log.info(self, "Cache-Enabler configuration :"
"\thttp://{0}/wp-admin/network/settings.php?"
"page=cache-enabler".format(data['site_name']))
else:
2019-10-02 16:33:40 +02:00
Log.info(self, "Cache-Enabler configuration :"
"\thttp://{0}/wp-admin/options-general.php?"
"page=cache-enabler".format(data['site_name']))
2019-08-27 15:12:01 +02:00
2019-07-14 22:50:34 +02:00
def logwatch(self, logfiles):
import zlib
import base64
import time
2019-10-12 13:34:45 +02:00
from wo.core.logwatch import LogWatcher
2019-07-14 22:50:34 +02:00
def callback(filename, lines):
for line in lines:
if line.find(':::') == -1:
print(line)
else:
data = line.split(':::')
try:
print(data[0], data[1],
zlib.decompress(base64.decodestring(data[2])))
except Exception as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
Log.info(time.time(),
'caught exception rendering a new log line in %s'
% filename)
2019-10-12 13:34:45 +02:00
logl = LogWatcher(logfiles, callback)
logl.loop()
2019-07-14 22:50:34 +02:00
def detSitePar(opts):
"""
Takes dictionary of parsed arguments
1.returns sitetype and cachetype
2. raises RuntimeError when wrong combination is used like
"--wp --wpsubdir" or "--html --wp"
"""
sitetype, cachetype = '', ''
typelist = list()
cachelist = list()
for key, val in opts.items():
if val and key in ['html', 'php', 'mysql', 'wp',
'wpsubdir', 'wpsubdomain', 'php72',
2022-01-23 15:14:24 -03:00
'php73', 'php74', 'php80', 'php81']:
2019-07-14 22:50:34 +02:00
typelist.append(key)
2019-08-15 19:59:23 +02:00
elif val and key in ['wpfc', 'wpsc', 'wpredis', 'wprocket', 'wpce']:
2019-07-14 22:50:34 +02:00
cachelist.append(key)
if len(typelist) > 1 or len(cachelist) > 1:
if len(cachelist) > 1:
raise RuntimeError(
"Could not determine cache type."
"Multiple cache parameter entered")
elif False not in [x in ('php', 'mysql', 'html') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('php72', 'mysql', 'html') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2019-07-14 22:50:34 +02:00
elif False not in [x in ('php73', 'mysql', 'html') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('php74', 'mysql', 'html') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2022-01-23 15:14:24 -03:00
elif False not in [x in ('php80', 'mysql', 'html') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('php81', 'mysql', 'html') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2019-07-14 22:50:34 +02:00
elif False not in [x in ('php', 'mysql') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('php72', 'mysql') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2019-07-14 22:50:34 +02:00
elif False not in [x in ('php73', 'mysql') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('php74', 'mysql') for x in typelist]:
2019-07-14 22:50:34 +02:00
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2022-01-23 15:14:24 -03:00
elif False not in [x in ('php80', 'mysql') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('php81', 'mysql') for x in typelist]:
sitetype = 'mysql'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('html', 'mysql') for x in typelist]:
sitetype = 'mysql'
2019-07-14 22:50:34 +02:00
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('php', 'html') for x in typelist]:
sitetype = 'php'
2019-07-14 22:50:34 +02:00
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wp', 'wpsubdir') for x in typelist]:
sitetype = 'wpsubdir'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wp', 'wpsubdomain') for x in typelist]:
sitetype = 'wpsubdomain'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wp', 'php72') for x in typelist]:
sitetype = 'wp'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2019-07-14 22:50:34 +02:00
elif False not in [x in ('wp', 'php73') for x in typelist]:
sitetype = 'wp'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wp', 'php74') for x in typelist]:
sitetype = 'wp'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2022-01-23 15:14:24 -03:00
elif False not in [x in ('wp', 'php80') for x in typelist]:
sitetype = 'wp'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wp', 'php81') for x in typelist]:
sitetype = 'wp'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wpsubdir', 'php72') for x in typelist]:
sitetype = 'wpsubdir'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2019-07-14 22:50:34 +02:00
elif False not in [x in ('wpsubdir', 'php73') for x in typelist]:
sitetype = 'wpsubdir'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wpsubdir', 'php74') for x in typelist]:
sitetype = 'wpsubdir'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2022-01-23 15:14:24 -03:00
elif False not in [x in ('wpsubdir', 'php80') for x in typelist]:
sitetype = 'wpsubdir'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wpsubdir', 'php81') for x in typelist]:
sitetype = 'wpsubdir'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wpsubdomain', 'php72') for x in typelist]:
sitetype = 'wpsubdomain'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2019-07-14 22:50:34 +02:00
elif False not in [x in ('wpsubdomain', 'php73') for x in typelist]:
sitetype = 'wpsubdomain'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wpsubdomain', 'php74') for x in typelist]:
sitetype = 'wpsubdomain'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2022-01-23 15:14:24 -03:00
elif False not in [x in ('wpsubdomain', 'php80') for x in typelist]:
sitetype = 'wpsubdomain'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
elif False not in [x in ('wpsubdomain', 'php81') for x in typelist]:
sitetype = 'wpsubdomain'
if not cachelist:
cachetype = 'basic'
else:
cachetype = cachelist[0]
2019-07-14 22:50:34 +02:00
else:
raise RuntimeError("could not determine site and cache type")
else:
if not typelist and not cachelist:
sitetype = None
cachetype = None
elif (not typelist or "php72" in typelist) and cachelist:
sitetype = 'wp'
cachetype = cachelist[0]
2019-07-14 22:50:34 +02:00
elif (not typelist or "php73" in typelist) and cachelist:
sitetype = 'wp'
cachetype = cachelist[0]
elif (not typelist or "php74" in typelist) and cachelist:
sitetype = 'wp'
cachetype = cachelist[0]
2022-01-23 15:14:24 -03:00
elif (not typelist or "php80" in typelist) and cachelist:
sitetype = 'wp'
cachetype = cachelist[0]
elif (not typelist or "php81" in typelist) and cachelist:
sitetype = 'wp'
cachetype = cachelist[0]
2019-07-14 22:50:34 +02:00
elif typelist and (not cachelist):
sitetype = typelist[0]
cachetype = 'basic'
else:
sitetype = typelist[0]
cachetype = cachelist[0]
return (sitetype, cachetype)
def generate_random_pass():
2019-07-14 22:50:34 +02:00
wo_random10 = (''.join(random.sample(string.ascii_uppercase +
string.ascii_lowercase +
string.digits, 24)))
return wo_random10
def generate_random():
wo_random10 = (''.join(random.sample(string.ascii_uppercase +
string.ascii_lowercase +
string.digits, 4)))
return wo_random10
def generate_8_random():
wo_random8 = (''.join(random.sample(string.ascii_uppercase +
string.ascii_lowercase +
string.digits, 8)))
return wo_random8
2019-07-14 22:50:34 +02:00
def deleteDB(self, dbname, dbuser, dbhost, exit=True):
try:
# Check if Database exists
try:
if WOMysql.check_db_exists(self, dbname):
# Drop database if exists
Log.debug(self, "dropping database `{0}`".format(dbname))
WOMysql.execute(self,
"drop database `{0}`".format(dbname),
errormsg='Unable to drop database {0}'
.format(dbname))
except StatementExcecutionError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
Log.debug(self, "drop database failed")
Log.info(self, "Database {0} not dropped".format(dbname))
except MySQLConnectionError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
Log.debug(self, "Mysql Connection problem occured")
if dbuser != 'root':
Log.debug(self, "dropping user `{0}`".format(dbuser))
try:
WOMysql.execute(self,
"drop user `{0}`@`{1}`"
.format(dbuser, dbhost))
except StatementExcecutionError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
Log.debug(self, "drop database user failed")
Log.info(self, "Database {0} not dropped".format(dbuser))
try:
WOMysql.execute(self, "flush privileges")
except StatementExcecutionError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
Log.debug(self, "drop database failed")
Log.info(self, "Database {0} not dropped".format(dbname))
except Exception as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2019-07-14 22:50:34 +02:00
Log.error(self, "Error occured while deleting database", exit)
def deleteWebRoot(self, webroot):
# do some preprocessing before proceeding
webroot = webroot.strip()
if (webroot == "/var/www/" or webroot == "/var/www" or
webroot == "/var/www/.." or webroot == "/var/www/."):
Log.debug(self, "Tried to remove {0}, but didn't remove it"
.format(webroot))
return False
if os.path.isdir(webroot):
Log.debug(self, "Removing {0}".format(webroot))
WOFileUtils.rm(self, webroot)
return True
Log.debug(self, "{0} does not exist".format(webroot))
return False
def removeNginxConf(self, domain):
if os.path.isfile('/etc/nginx/sites-available/{0}'
.format(domain)):
Log.debug(self, "Removing Nginx configuration")
WOFileUtils.rm(self, '/etc/nginx/sites-enabled/{0}'
.format(domain))
WOFileUtils.rm(self, '/etc/nginx/sites-available/{0}'
.format(domain))
WOService.reload_service(self, 'nginx')
WOGit.add(self, ["/etc/nginx"],
msg="Deleted {0} "
.format(domain))
def doCleanupAction(self, domain='', webroot='', dbname='', dbuser='',
dbhost=''):
"""
Removes the nginx configuration and database for the domain provided.
doCleanupAction(self, domain='sitename', webroot='',
dbname='', dbuser='', dbhost='')
"""
if domain:
if os.path.isfile('/etc/nginx/sites-available/{0}'
.format(domain)):
removeNginxConf(self, domain)
2019-10-28 10:35:26 +01:00
WOAcme.removeconf(self, domain)
2019-07-14 22:50:34 +02:00
if webroot:
deleteWebRoot(self, webroot)
if dbname:
if not dbuser:
raise SiteError("dbuser not provided")
2019-09-02 18:56:34 +02:00
if not dbhost:
raise SiteError("dbhost not provided")
2019-07-14 22:50:34 +02:00
deleteDB(self, dbname, dbuser, dbhost)
# setup letsencrypt for domain + www.domain
2019-07-19 01:02:00 +02:00
# copy wildcard certificate to a subdomain
def copyWildcardCert(self, wo_domain_name, wo_root_domain):
if os.path.isfile("/var/www/{0}/conf/nginx/ssl.conf"
.format(wo_root_domain)):
try:
2019-08-30 10:49:55 +02:00
if not os.path.isdir("/etc/letsencrypt/shared"):
WOFileUtils.mkdir(self, "/etc/letsencrypt/shared")
if not os.path.isfile("/etc/letsencrypt/shared/{0}.conf"
.format(wo_root_domain)):
2019-08-30 10:51:00 +02:00
WOFileUtils.copyfile(self, "/var/www/{0}/conf/nginx/ssl.conf"
2019-08-30 10:49:55 +02:00
.format(wo_root_domain),
"/etc/letsencrypt/shared/{0}.conf"
.format(wo_root_domain))
WOFileUtils.create_symlink(self, ["/etc/letsencrypt/shared/"
"{0}.conf"
2019-08-30 08:24:02 +02:00
.format(wo_root_domain),
'/var/www/{0}/conf/nginx/'
'ssl.conf'
.format(wo_domain_name)])
except IOError as e:
Log.debug(self, str(e))
2019-08-30 07:24:08 +02:00
Log.debug(self, "Error occured while "
"creating symlink for ssl cert")
2019-07-14 22:50:34 +02:00
# letsencrypt cert renewal
def renewLetsEncrypt(self, wo_domain_name):
ssl = WOShellExec.cmd_exec(
self, "/etc/letsencrypt/acme.sh "
"--config-home "
"'/etc/letsencrypt/config' "
"--renew -d {0} --ecc --force"
.format(wo_domain_name))
# mail_list = ''
2019-07-14 22:50:34 +02:00
if not ssl:
Log.error(self, "ERROR : Let's Encrypt certificate renewal FAILED!",
False)
2019-09-04 03:07:24 +02:00
if (SSL.getexpirationdays(self, wo_domain_name) > 0):
2019-07-14 22:50:34 +02:00
Log.error(self, "Your current certificate will expire within " +
2019-09-04 03:07:24 +02:00
str(SSL.getexpirationdays(self, wo_domain_name)) +
2019-07-14 22:50:34 +02:00
" days.", False)
else:
Log.error(self, "Your current certificate already expired!", False)
# WOSendMail("wordops@{0}".format(wo_domain_name), wo_wp_email,
# "[FAIL] HTTPS cert renewal {0}".format(wo_domain_name),
# "Hi,\n\nHTTPS certificate renewal for https://{0}
# was unsuccessful.".format(wo_domain_name) +
# "\nPlease check the WordOps log for reason
# The current expiry date is : " +
# str(SSL.getExpirationDate(self, wo_domain_name)) +
# "\n\nFor support visit https://wordops.net/support .
# \n\nBest regards,\nYour WordOps Worker", files=mail_list,
# port=25, isTls=False)
Log.error(self, "Check the WO log for more details "
"`tail /var/log/wo/wordops.log`")
WOGit.add(self, ["/etc/letsencrypt"],
msg="Adding letsencrypt folder")
# WOSendMail("wordops@{0}".format(wo_domain_name), wo_wp_email,
# "[SUCCESS] Let's Encrypt certificate renewal {0}".format(wo_domain_name),
# "Hi,\n\nYour Let's Encrypt certificate has been renewed for
# https://{0} .".format(wo_domain_name) +
# "\nYour new certificate will expire on : " +
# str(SSL.getExpirationDate(self, wo_domain_name)) +
# "\n\nBest regards,\nYour WordOps Worker", files=mail_list,
# port=25, isTls=False)
# redirect= False to disable https redirection
2019-09-06 16:25:56 +02:00
def setuprocketchat(self):
2019-10-02 13:13:32 +02:00
if ((not WOVar.wo_platform_codename == 'bionic') and
(not WOVar.wo_platform_codename == 'xenial')):
2019-09-06 16:25:56 +02:00
Log.info(self, "Rocket.chat is only available on Ubuntu 16.04 "
2019-09-23 01:40:26 +02:00
"& 18.04 LTS")
2019-09-06 16:25:56 +02:00
return False
else:
if not WOAptGet.is_installed(self, 'snapd'):
WOAptGet.install(self, ["snapd"])
if WOShellExec.cmd_exec(self, "snap install rocketchat-server"):
return True
return False
2019-09-27 01:19:45 +02:00
2019-09-30 03:10:30 +02:00
def setupngxblocker(self, domain, block=True):
2019-10-29 18:47:52 +01:00
if block:
if os.path.isdir('/var/www/{0}/conf/nginx'.format(domain)):
if not os.path.isfile(
'/var/www/{0}/conf/nginx/ngxblocker.conf.disabled'
.format(domain)):
ngxconf = open(
"/var/www/{0}/conf/nginx/ngxblocker.conf"
.format(domain),
encoding='utf-8', mode='w')
ngxconf.write(
"# Bad Bot Blocker\n"
"include /etc/nginx/bots.d/ddos.conf;\n"
"include /etc/nginx/bots.d/blockbots.conf;\n")
ngxconf.close()
else:
WOFileUtils.mvfile(
self, '/var/www/{0}/conf/nginx/ngxblocker.conf.disabled'
.format(domain), '/var/www/{0}/conf/nginx/ngxblocker.conf'
.format(domain))
else:
if os.path.isfile('/var/www/{0}/conf/nginx/ngxblocker.conf'
.format(domain)):
2019-09-30 03:10:30 +02:00
WOFileUtils.mvfile(
2019-10-29 18:47:52 +01:00
self, '/var/www/{0}/conf/nginx/ngxblocker.conf'
.format(domain),
'/var/www/{0}/conf/nginx/ngxblocker.conf.disabled'
2019-09-30 03:10:30 +02:00
.format(domain))
2019-10-03 15:44:23 +02:00
return 0