From 5943b38a4052c94a6686ae8b5ecca7384f3a0e74 Mon Sep 17 00:00:00 2001 From: harshadyeola Date: Wed, 31 Dec 2014 15:01:09 +0530 Subject: [PATCH] ee mod setup database completed --- config/ee.conf | 6 +-- ee/cli/plugins/site.py | 3 +- ee/cli/plugins/site_functions.py | 87 +++++++++++++++++++++++++++----- ee/core/fileutils.py | 25 ++++----- 4 files changed, 91 insertions(+), 30 deletions(-) diff --git a/config/ee.conf b/config/ee.conf index dddf28f1..1f4c6727 100644 --- a/config/ee.conf +++ b/config/ee.conf @@ -46,13 +46,13 @@ grant-host = localhost -db-name = false +db-name = False -db-user = false +db-user = False [wordpress] -prefix = false +prefix = False user = diff --git a/ee/cli/plugins/site.py b/ee/cli/plugins/site.py index fd9d5bab..0487b720 100644 --- a/ee/cli/plugins/site.py +++ b/ee/cli/plugins/site.py @@ -4,7 +4,7 @@ from cement.core import handler, hook from ee.core.variables import EEVariables from ee.core.domainvalidate import validate_domain from ee.core.fileutils import EEFileUtils -from ee.cli.plugins.site_functions import setup_domain +from ee.cli.plugins.site_functions import setup_domain, setup_database import sys import os @@ -237,6 +237,7 @@ class EESiteCreateController(CementBaseController): wpsubdir=False, webroot=ee_site_webroot) setup_domain(self, data) + setup_database(self, data) class EESiteUpdateController(CementBaseController): diff --git a/ee/cli/plugins/site_functions.py b/ee/cli/plugins/site_functions.py index 11a12a28..881b488c 100644 --- a/ee/cli/plugins/site_functions.py +++ b/ee/cli/plugins/site_functions.py @@ -1,5 +1,10 @@ import os +import random +import string +import sys +import getpass from ee.core.fileutils import EEFileUtils +from ee.core.mysql import EEMysql def setup_domain(self, data): @@ -17,15 +22,17 @@ def setup_domain(self, data): ee_site_nginx_conf.close() except IOError as e: print("Unable to create nginx conf for {2} ({0}): {1}" - .format(e.errno, e.strerror)) + .format(e.errno, e.strerror, ee_domain_name)) + sys.exit(1) except Exception as e: print("{0}".format(e)) + sys.exit(1) # create symbolic link for - EEFileUtils.create_symlink(['/etc/nginx/sites-available/{0}.conf' - .format(ee_domain_name), - '/etc/nginx/sites-enabled/{0}.conf' - .format(ee_domain_name)]) + EEFileUtils.create_symlink(self, ['/etc/nginx/sites-available/{0}.conf' + .format(ee_domain_name), + '/etc/nginx/sites-enabled/{0}.conf' + .format(ee_domain_name)]) # Creating htdocs & logs directory try: @@ -35,19 +42,71 @@ def setup_domain(self, data): os.makedirs('{0}/logs'.format(ee_site_webroot)) except Exception as e: print("{0}".format(e)) + sys.exit(1) - EEFileUtils.create_symlink(['/var/log/nginx/{0}.access.log' - .format(ee_domain_name), - '{0}/logs/access.log' - .format(ee_site_webroot)]) - EEFileUtils.create_symlink(['/var/log/nginx/{0}.error.log' - .format(ee_domain_name), - '{0}/logs/error.log' - .format(ee_site_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)]) def setup_database(self, data): ee_domain_name = data['site_name'] ee_random = (''.join(random.sample(string.ascii_uppercase + - string.ascii_lowercase + string.digits, 64))) + 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') + print(ee_random) + + if prompt_dbname == 'True': + try: + ee_db_name = input('Enter the MySQL database name [{0}]:' + .format(ee_replace_dot)) + except EOFError as e: + print("{0} {1}".format(e.errorno, e.strerror)) + sys.exit(0) + + if not ee_db_name: + ee_db_name = ee_replace_dot + + if prompt_dbuser: + 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: + print("{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_name) > 16: + print('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 + 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_db_password)) diff --git a/ee/core/fileutils.py b/ee/core/fileutils.py index f9f9e698..c93edab2 100644 --- a/ee/core/fileutils.py +++ b/ee/core/fileutils.py @@ -1,6 +1,7 @@ """EasyEngine file utils core classes.""" import shutil import os +import sys import glob @@ -24,24 +25,24 @@ class EEFileUtils(): shutil.rmtree(file) self.app.log.info("Done") except shutil.Error as e: - self.app.log.error('Unable to Remove file' - + os.path.basename(file)+e.reason()) - self.app.log.info("Unable to remove file, [{err}]" - .format(err=str(e.reason))) - return False + self.app.log.error('Unable to Remove file {err}' + .format(err=str(e.reason))) + sys.exit(1) - def create_symlink(paths): + def create_symlink(self, paths): src = paths[0] dst = paths[1] try: os.symlink(src, dst) except Exception as e: - print("Unable to create symbolic link ...\n {0} " - .format(e.reason)) + self.app.log.error("Unable to create symbolic link ...\n {0} {1}" + .format(e.errno, e.strerror)) + sys.exit(1) - def remove_symlink(filepath): + def remove_symlink(self, filepath): try: - os.unlink(path) + os.unlink(filepath) except Exception as e: - print("Unable to reomove symbolic link ...\n {0} " - .format(e.reason)) + self.app.log.error("Unable to reomove symbolic link ...\n {0} {1}" + .format(e.errno, e.strerror)) + sys.exit(1)