import os import random import string import sys import getpass from ee.core.fileutils import EEFileUtils from ee.core.mysql import EEMysql from ee.core.shellexec import EEShellExec from ee.core.variables import EEVariables from ee.core.logging import Log import glob def SetupDomain(self, data): ee_domain_name = data['site_name'] ee_site_webroot = data['webroot'] self.app.log.info("Creating {0} ...".format(ee_domain_name)) # 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: Log.error(self, "Unable to create nginx conf for {2} ({0}): {1}" .format(e.errno, e.strerror, ee_domain_name)) sys.exit(1) except Exception as e: Log.error(self, "{0}".format(e)) sys.exit(1) # 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 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: Log.error(self, "{0}".format(e)) sys.exit(1) 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)]) 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 = '' ee_db_username = '' ee_db_password = '' 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: Log.error(self, "{0} {1}".format(e.errorno, e.strerror)) sys.exit(0) if not ee_db_name: ee_db_name = ee_replace_dot 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: Log.error(self, "{0} {1}".format(e.errorno, e.strerror)) sys.exit(1) 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: self.app.log.info('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 self.app.log.info("Setting Up Database ...") EEMysql.execute(self, "create database {0}" .format(ee_db_name)) # Create MySQL User EEMysql.execute(self, "create user {0}@{1} identified by '{2}'" .format(ee_db_username, ee_mysql_host, ee_db_password)) # Grant permission EEMysql.execute(self, "grant all privileges on {0}.* to {1}@{2}" .format(ee_db_name, ee_db_username, ee_mysql_host)) 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) 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))) ee_wp_prefix = '' ee_wp_user = '' ee_wp_pass = '' self.app.log.info("Downloading Wordpress...") EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot)) EEShellExec.cmd_exec(self, "wp --allow-root core download") if not (data['ee_db_name'] and data['ee_db_user'] and data['ee_db_pass']): data = SetupDatabase(self, data) 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): self.app.log.warn("table prefix can only " "contain numbers, letters, and underscores") ee_wp_prefix = input('Enter the WordPress table prefix [wp_]: ' ) except EOFError as e: Log.error(self, "{0} {1}".format(e.errorno, e.strerror)) sys.exit(1) 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)) self.app.log.debug("Setting Up WordPress Configuration...") if not data['multisite']: 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: 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< {1}/{0}.sql" .format(ee_db_name, backup_path), "Failed: Backup Database") # move wp-config.php/ee-config.php to backup EEFileUtils.mvfile(self, file, backup_path)