You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
16 KiB

import os
import random
import string
import sys
import getpass
from ee.cli.plugins.stack import EEStackController
from ee.core.fileutils import EEFileUtils
from ee.core.mysql import EEMysql
from ee.core.shellexec import EEShellExec
10 years ago
from ee.core.variables import EEVariables
from ee.core.aptget import EEAptGet
from ee.core.logging import Log
import glob
10 years ago
def setupDomain(self, data):
ee_domain_name = data['site_name']
ee_site_webroot = data['webroot']
10 years ago
Log.info(self, "Setting up NGINX configuration ", end='')
# write nginx config for file
try:
ee_site_nginx_conf = open('/etc/nginx/sites-available/{0}'
.format(ee_domain_name), 'w')
self.app.render((data), 'virtualconf.mustache',
out=ee_site_nginx_conf)
ee_site_nginx_conf.close()
except IOError as e:
10 years ago
Log.debug(self, "{0}".format(e))
Log.error(self, "\nUnable to create NGINX configuration")
except Exception as e:
10 years ago
Log.debug(self, "{0}".format(e))
Log.error(self, "\nUnable to create NGINX configuration")
Log.info(self, "[Done]")
# create symbolic link for
EEFileUtils.create_symlink(self, ['/etc/nginx/sites-available/{0}'
.format(ee_domain_name),
'/etc/nginx/sites-enabled/{0}'
.format(ee_domain_name)])
# Creating htdocs & logs directory
10 years ago
Log.info(self, "Setting up webroot ", end='')
try:
if not os.path.exists('{0}/htdocs'.format(ee_site_webroot)):
os.makedirs('{0}/htdocs'.format(ee_site_webroot))
if not os.path.exists('{0}/logs'.format(ee_site_webroot)):
os.makedirs('{0}/logs'.format(ee_site_webroot))
except Exception as e:
10 years ago
Log.debug(self, "{0}".format(e))
Log.error(self, "\nUnable to setup webroot")
EEFileUtils.create_symlink(self, ['/var/log/nginx/{0}.access.log'
.format(ee_domain_name),
'{0}/logs/access.log'
.format(ee_site_webroot)])
EEFileUtils.create_symlink(self, ['/var/log/nginx/{0}.error.log'
.format(ee_domain_name),
'{0}/logs/error.log'
.format(ee_site_webroot)])
10 years ago
Log.info(self, "[Done]")
10 years ago
def setupDatabase(self, data):
ee_domain_name = data['site_name']
ee_random = (''.join(random.sample(string.ascii_uppercase +
string.ascii_lowercase + string.digits, 15)))
ee_replace_dot = ee_domain_name.replace('.', '_')
prompt_dbname = self.app.config.get('mysql', 'db-name')
prompt_dbuser = self.app.config.get('mysql', 'db-user')
ee_mysql_host = self.app.config.get('mysql', 'grant-host')
ee_db_name = ''
10 years ago
ee_db_username = ''
ee_db_password = ''
10 years ago
if prompt_dbname == 'True' or prompt_dbname == 'true':
try:
ee_db_name = input('Enter the MySQL database name [{0}]:'
.format(ee_replace_dot))
except EOFError as e:
10 years ago
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to input database name")
if not ee_db_name:
ee_db_name = ee_replace_dot
10 years ago
if prompt_dbuser == 'True' or prompt_dbuser == 'true':
try:
ee_db_username = input('Enter the MySQL database user name [{0}]: '
.format(ee_replace_dot))
ee_db_password = input('Enter the MySQL database password [{0}]: '
.format(ee_random))
except EOFError as e:
10 years ago
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to input database credentials")
if not ee_db_username:
ee_db_username = ee_replace_dot
if not ee_db_password:
ee_db_password = ee_random
if len(ee_db_username) > 16:
10 years ago
Log.info(self, 'Autofix MySQL username (ERROR 1470 (HY000)),'
' please wait...')
ee_random10 = (''.join(random.sample(string.ascii_uppercase +
string.ascii_lowercase + string.digits, 10)))
ee_db_name = (ee_db_name[0:6] + ee_random10)
# create MySQL database
10 years ago
Log.info(self, "Setting Up Database ", end='')
Log.debug(self, "creating databse {0}".format(ee_db_name))
10 years ago
EEMysql.execute(self, "create database {0}"
.format(ee_db_name))
# Create MySQL User
10 years ago
Log.debug(self, "creating user {0}".format(ee_db_username))
EEMysql.execute(self,
10 years ago
"create user {0}@{1} identified by '{2}'"
.format(ee_db_username, ee_mysql_host, ee_db_password))
# Grant permission
10 years ago
Log.debug(self, "setting up user privileges")
EEMysql.execute(self,
10 years ago
"grant all privileges on {0}.* to {1}@{2}"
.format(ee_db_name, ee_db_username, ee_mysql_host))
10 years ago
Log.info(self, "[Done]")
data['ee_db_name'] = ee_db_name
data['ee_db_user'] = ee_db_username
data['ee_db_pass'] = ee_db_password
data['ee_db_host'] = ee_mysql_host
return(data)
10 years ago
def setupWordpress(self, data):
ee_domain_name = data['site_name']
ee_site_webroot = data['webroot']
prompt_wpprefix = self.app.config.get('wordpress', 'prefix')
ee_wp_user = self.app.config.get('wordpress', 'user')
ee_wp_pass = self.app.config.get('wordpress', 'password')
ee_wp_email = self.app.config.get('wordpress', 'email')
# Random characters
ee_random = (''.join(random.sample(string.ascii_uppercase +
string.ascii_lowercase + string.digits, 15)))
10 years ago
ee_wp_prefix = ''
ee_wp_user = ''
ee_wp_pass = ''
10 years ago
Log.info(self, "Downloading Wordpress ", end='')
EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))
EEShellExec.cmd_exec(self, "wp --allow-root core download")
10 years ago
Log.info(self, "[Done]")
if not (data['ee_db_name'] and data['ee_db_user'] and data['ee_db_pass']):
10 years ago
data = setupDatabase(self, data)
10 years ago
if prompt_wpprefix == 'True' or prompt_wpprefix == 'true':
try:
ee_wp_prefix = input('Enter the WordPress table prefix [wp_]: '
.format(ee_replace_dot))
while re.match('^[A-Za-z0-9_]*$', ee_wp_prefix):
10 years ago
Log.warn(self, "table prefix can only "
"contain numbers, letters, and underscores")
10 years ago
ee_wp_prefix = input('Enter the WordPress table prefix [wp_]: '
)
except EOFError as e:
10 years ago
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to input table prefix")
10 years ago
if not ee_wp_prefix:
ee_wp_prefix = 'wp_'
# Modify wp-config.php & move outside the webroot
EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))
10 years ago
Log.debug(self, "Setting up wp-config file")
if not data['multisite']:
10 years ago
Log.debug(self, "Generating wp-config for WordPress Single site")
10 years ago
EEShellExec.cmd_exec(self, "wp --allow-root core config "
+ "--dbname={0} --dbprefix={1} --dbuser={2} "
.format(data['ee_db_name'], ee_wp_prefix,
data['ee_db_user'])
+ "--dbpass={0}".format(data['ee_db_pass']))
else:
10 years ago
Log.debug(self, "Generating wp-config for WordPress multisite")
10 years ago
EEShellExec.cmd_exec(self, "php /usr/bin/wp --allow-root core config "
+ "--dbname={0} --dbprefix={1} "
.format(data['ee_db_name'], ee_wp_prefix)
+ "--dbuser={0} --dbpass={1} "
"--extra-php<<PHP \n {var1} {var2} \nPHP"
.format(data['ee_db_user'], data['ee_db_pass'],
var1=""
"\n define('WP_ALLOW_MULTISITE', true);",
var2=""
"\n define('WPMU_ACCEL_REDIRECT', true);")
)
10 years ago
EEFileUtils.mvfile(self, './wp-config.php', '../')
if not ee_wp_user:
ee_wp_user = EEVariables.ee_user
while not ee_wp_user:
10 years ago
Log.warn(self, "Usernames can have only alphanumeric"
"characters, spaces, underscores, hyphens,"
"periods and the @ symbol.")
try:
ee_wp_user = input('Enter WordPress username: ')
except EOFError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to input wp user name")
if not ee_wp_pass:
ee_wp_pass = ee_random
if not ee_wp_email:
ee_wp_email = EEVariables.ee_email
while not ee_wp_email:
10 years ago
try:
ee_wp_email = input('Enter WordPress email: ')
except EOFError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to input wp user email")
10 years ago
Log.debug(self, "setting up WordPress Tables")
if not data['multisite']:
10 years ago
Log.debug(self, "creating tables for WordPress Single site")
EEShellExec.cmd_exec(self, "php /usr/bin/wp --allow-root core install "
"--url={0} --title={0} --admin_name={1} "
.format(data['www_domain'], ee_wp_user)
+ "--admin_password={0} --admin_email={1}"
.format(ee_wp_pass, ee_wp_email),
errormsg="Unable to setup WordPress Tables")
else:
10 years ago
Log.debug(self, "creating tables for WordPress multisite")
EEShellExec.cmd_exec(self, "php /usr/bin/wp --allow-root "
"core multisite-install "
"--url={0} --title={0} --admin_name={1} "
.format(data['www_domain'], ee_wp_user)
+ "--admin_password={0} --admin_email={1} "
"{subdomains}"
.format(ee_wp_pass, ee_wp_email,
subdomains='--subdomains'
if not data['wpsubdir'] else ''),
errormsg="Unable to setup WordPress Tables")
10 years ago
Log.debug(self, "Updating WordPress permalink")
EEShellExec.cmd_exec(self, " php /usr/bin/wp --allow-root "
"rewrite structure "
"/%year%/%monthnum%/%day%/%postname%/",
errormsg="Unable to Update WordPress permalink")
"""Install nginx-helper plugin """
10 years ago
installWP_Plugin(self, 'nginx-helper', data)
"""Install Wp Super Cache"""
if data['wpsc']:
10 years ago
installWP_Plugin(self, 'wp-super-cache', data)
"""Install W3 Total Cache"""
if data['w3tc'] or data['wpfc']:
10 years ago
installWP_Plugin(self, 'w3-total-cache', data)
wp_creds = dict(wp_user=ee_wp_user, wp_pass=ee_wp_pass,
wp_email=ee_wp_email)
return(wp_creds)
10 years ago
def setupWordpressNetwork(self, data):
10 years ago
ee_site_webroot = data['webroot']
EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))
10 years ago
Log.info(self, "Setting up WordPress Network ", end='')
EEShellExec.cmd_exec(self, 'wp --allow-root core multisite-convert'
' --title={0} {subdomains}'
.format(data['www_domain'], subdomains='--subdomains'
if not data['wpsubdir'] else ''))
10 years ago
Log.info(self, "Done")
10 years ago
def installWP_Plugin(self, plugin_name, data):
10 years ago
ee_site_webroot = data['webroot']
10 years ago
Log.debug(self, "Installing plugin {0}".format(plugin_name))
EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))
EEShellExec.cmd_exec(self, "php /usr/bin/wp plugin --allow-root install "
"{0}".format(plugin_name),
errormsg="Unable to Install plugin {0}"
.format(plugin_name))
EEShellExec.cmd_exec(self, "php /usr/bin/wp plugin --allow-root activate "
"{0} {na}"
10 years ago
.format(plugin_name,
na='--network' if data['multisite'] else ''),
errormsg="Unable to Activate plugin {0}"
.format(plugin_name))
10 years ago
def uninstallWP_Plugin(self, plugin_name, data):
ee_site_webroot = data['webroot']
10 years ago
Log.debug(self, "Uninstalling plugin {0}".format(plugin_name))
EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))
EEShellExec.cmd_exec(self, "php /usr/bin/wp plugin --allow-root uninstall "
"{0}".format(plugin_name),
10 years ago
errormsg="Unable to UnInstall plugin {0}"
.format(plugin_name))
def SetWebrootPermissions(self, webroot):
10 years ago
Log.debug(self, "Setting Up Permissions...")
EEFileUtils.chown(self, webroot, EEVariables.ee_php_user,
EEVariables.ee_php_user, recursive=True)
def siteBackup(self, data):
ee_site_webroot = data['webroot']
backup_path = ee_site_webroot + '/backup/{0}'.format(EEVariables.ee_date)
if not EEFileUtils.isexist(self, backup_path):
EEFileUtils.mkdir(self, backup_path)
Log.info(self, "Backup Location : {0}".format(backup_path))
EEFileUtils.copyfile(self, '/etc/nginx/sites-available/{0}'
.format(data['site_name']), backup_path)
if data['currsitetype'] in ['html', 'php', 'mysql']:
10 years ago
Log.info(self, "Backing up Webroot ", end='')
EEFileUtils.mvfile(self, ee_site_webroot + '/htdocs', backup_path)
10 years ago
Log.info(self, "[Done]")
configfiles = glob.glob(ee_site_webroot + '/*-config.php')
10 years ago
if configfiles and EEFileUtils.isexist(self, configfiles[0]):
ee_db_name = (EEFileUtils.grep(self, configfiles[0],
'DB_NAME').split(',')[1]
.split(')')[0].strip().replace('\'', ''))
10 years ago
Log.info(self, 'Backing up Database ', end='')
EEShellExec.cmd_exec(self, "mysqldump {0} > {1}/{0}.sql"
.format(ee_db_name, backup_path),
10 years ago
errormsg="\nFailed: Backup Database")
Log.info(self, "[Done]")
# move wp-config.php/ee-config.php to backup
if data['currsitetype'] in ['mysql']:
EEFileUtils.mvfile(self, configfiles[0], backup_path)
else:
EEFileUtils.copyfile(self, configfiles[0], backup_path)
def site_package_check(self, stype):
apt_packages = []
packages = []
stack = EEStackController()
stack.app = self.app
if stype in ['html', 'php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting apt_packages variable for Nginx")
if not EEAptGet.is_installed(self, 'nginx-common'):
apt_packages = apt_packages + EEVariables.ee_nginx
if stype in ['php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting apt_packages variable for PHP")
if not EEAptGet.is_installed(self, 'php5-fpm'):
apt_packages = apt_packages + EEVariables.ee_php
if stype in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting apt_packages variable for MySQL")
if not EEShellExec.cmd_exec(self, "mysqladmin ping"):
apt_packages = apt_packages + EEVariables.ee_mysql
if stype in ['php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting apt_packages variable for PostFix")
if not EEAptGet.is_installed(self, 'postfix'):
apt_packages = apt_packages + EEVariables.ee_postfix
if stype in ['wp', 'wpsubdir', 'wpsubdomain']:
Log.debug(self, "Setting packages variable for WPCLI")
if not EEShellExec.cmd_exec(self, "which wp"):
packages = packages + [["https://github.com/wp-cli/wp-cli/"
"releases/download/v0.17.1/"
"wp-cli.phar", "/usr/bin/wp",
"WP_CLI"]]
stack.install(apt_packages=apt_packages, packages=packages)