diff --git a/README.md b/README.md index e77d7b74..aec4ec8c 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,16 @@ sudo ee site create example.com --wp # Install required packages & setup ## Update EasyEngine -Update Procedure For EasyEngine to version 3.0.6 +Update procedure for EasyEngine to latest version +#### For current installed version prior to 3.0.6 ```bash wget -qO ee rt.cx/ee && sudo bash ee + +``` +#### Current version is 3.0.6 +``` +ee update ``` ## More Site Creation Commands @@ -85,7 +91,7 @@ ee site create example.com --mysql # create example.com with php & mysql supp ## Careers -We are looking for [Python Developers] (https://rtcamp.com/careers/python-developer/) to join our team. +We are looking for [Python Developers] (https://rtcamp.com/careers/python-developer/) to join our team. We offer work from home, so you can join EasyEngine team anywhere! _[Why Python?] (https://rtcamp.com/blog/easyengine-3-roadmap/#whypython)_ diff --git a/config/bash_completion.d/ee_auto.rc b/config/bash_completion.d/ee_auto.rc index 8f8608bd..e7b9459c 100644 --- a/config/bash_completion.d/ee_auto.rc +++ b/config/bash_completion.d/ee_auto.rc @@ -11,7 +11,7 @@ _ee_complete() # SETUP THE BASE LEVEL (everything after "ee") if [ $COMP_CWORD -eq 1 ]; then COMPREPLY=( $(compgen \ - -W "stack site debug clean secure import-slow-log log" \ + -W "stack site debug clean secure import-slow-log log update" \ -- $cur) ) @@ -74,7 +74,7 @@ _ee_complete() # HANDLE EVERYTHING AFTER THE THIRD LEVEL NAMESPACE "install" | "purge" | "remove" ) COMPREPLY=( $(compgen \ - -W "--web --admin --mail --nginx --php --mysql --postfix --wpcli --phpmyadmin --adminer --utils --memcache --dovecot --all" \ + -W "--web --admin --mail --nginx --php --mysql --postfix --wpcli --phpmyadmin --adminer --utils --memcache --dovecot --all --mailscanner" \ -- $cur) ) ;; "start" | "stop" | "reload" | "restart" | "status") diff --git a/config/plugins.d/update.conf b/config/plugins.d/update.conf new file mode 100644 index 00000000..27e9c53a --- /dev/null +++ b/config/plugins.d/update.conf @@ -0,0 +1,8 @@ +### Example Plugin Configuration for EasyEngine + +[update] + +### If enabled, load a plugin named `example` either from the Python module +### `ee.cli.plugins.example` or from the file path +### `/var/lib/ee/plugins/example.py` +enable_plugin = true diff --git a/docs/ee.8 b/docs/ee.8 index 68252f27..77e39715 100644 --- a/docs/ee.8 +++ b/docs/ee.8 @@ -17,15 +17,15 @@ ee site update example.com [ --php | --mysql] [[--wp | --wpsubdir | --wpsubdomai .TP ee site delete example.com [--db | --files | --all | --no-prompt ] .TP -ee debug [ -i | --nginx | --rewrite | --php | --fpm | --mysql ] [--stop ] +ee debug [ -i | --all=on/off |--nginx=on/off | --rewrite=on/off | --php=on/off | --fpm=on/off | --mysql=on/off ] .TP -ee debug example.com [ -i | --nginx | --rewrite | --wp ] [--stop ] +ee debug example.com [ -i | --all=on/off | --nginx=on/off | --rewrite=on/off | --wp=on/off ] .TP ee secure [ --auth | --port | --ip ] .SH DESCRIPTION EasyEngine aka ee is the opensource project developed with the purpose to automate web-server configuration. .br -EasyEngine is the collection of shell scripts that provides automation for the web-server +EasyEngine is the collection of python script that provides automation for the web-server .br installation, site creation, services debugging & monitoring. .SH OPTIONS @@ -95,7 +95,7 @@ Change directory to webroot of specified site in subshell. .br monitor access and error logs for site specified. .TP -.B list [ enable | available ] +.B list [ --enabled | --disabled ] .br Lists all available sites from /etc/nginx/sites-enabled/ .br @@ -143,21 +143,21 @@ Update site configuration according to specified options. .br Delete site i.e webroot, database, ad configuration permenantly. .TP -.B debug [ -i | --nginx | --php | --mysql | --rewrite | --fpm ] [ --start | --stop ] +.B debug [ -i | --nginx=on/off | --php=on/off | --mysql=on/off | --rewrite=on/off | --fpm=on/off ] .br -Starts server level debugging. If used without arguments starts debugging +Starts server level debugging. If this is used without arguments it will start debugging .br -all services, else debug only service provided with argument. Stop +all services.Else it will debug only service provided with argument.This will Stop .br -Debugging if used with --stop argument. +Debugging if used with --all=off argument. .TP -.B debug example.com [ -i | --nginx | --rewrite | --wp ] [ --start | --stop ] +.B debug example.com [ -i | --nginx=on/off | --rewrite=on/off | --wp=on/off | --all=on/off ] .br -Starts site level debugging. If used without arguments starts debugging all +Starts site level debugging. If this is used without arguments it will start debugging all .br -services, else debug only service provided with argument. Stop Debugging +services.Else it will debug only service provided with argument.This will Stop Debugging .br -if used with --stop argument. +if used with --all=off argument. .TP .B secure [ --auth | --port ] .br @@ -174,35 +174,35 @@ Clean NGINX fastCGI cache if no option specified. .br setup intractive mode while used with debug. .TP -.B --nginx +.B --nginx=on/off .br used with ee debug command. used to start or stop nginx debugging. .TP -.B --php +.B --php=on/off .br used with ee debug command. used to start or stop php debugging. .TP -.B --mysql +.B --mysql=on/off .br used with ee debug command. used to start or stop mysql debugging. .TP -.B --rewrite +.B --rewrite=on/off .br used with ee debug command. used to start or stop nginx rewrite rules debugging. .TP -.B --fpm +.B --fpm=on/off .br used with ee debug command. used to start or stop fpm debugging. .TP -.B --wp +.B --wp=on/off .br -used with ee debug command. used to start or stop wordpress site debugging. +used with ee debug command. used to start or stop wordpress site debugging. .TP -.B --start +.B --all=on/off .br used with ee debug command. used to stop debugging. .TP -.B --stop +.B --all=off .br used with ee debug command. used to stop debugging. .TP diff --git a/ee/cli/plugins/clean.py b/ee/cli/plugins/clean.py index 28c5a5ab..9f1ca087 100644 --- a/ee/cli/plugins/clean.py +++ b/ee/cli/plugins/clean.py @@ -10,7 +10,7 @@ import os import urllib.request -def clean_plugin_hook(app): +def ee_clean_hook(app): # do something with the ``app`` object here. pass @@ -31,6 +31,7 @@ class EECleanController(CementBaseController): (['--opcache'], dict(help='Clean OpCache', action='store_true')) ] + usage = "ee clean [options]" @expose(hide=True) def default(self): @@ -86,4 +87,4 @@ def load(app): # register the plugin class.. this only happens if the plugin is enabled handler.register(EECleanController) # register a hook (function) to run after arguments are parsed. - hook.register('post_argument_parsing', clean_plugin_hook) + hook.register('post_argument_parsing', ee_clean_hook) diff --git a/ee/cli/plugins/debug.py b/ee/cli/plugins/debug.py index 58c267b6..1f8a47ea 100644 --- a/ee/cli/plugins/debug.py +++ b/ee/cli/plugins/debug.py @@ -14,29 +14,8 @@ import glob import signal import subprocess -usage = """ -Usage: ee debug {} {arguments} - -arguments usage: ---all --all=on start debugging all server parameters. ---all=off stop debugging all server parameters ---nginx --nginx=on start debugging nginx server configuration for site ---nginx=off stop debugging nginx server configuration for site ---rewrite --rewrite=on start debugging nginx rewrite rules for site ---rewrite=off stop debugging nginx rewrite rules for site ---php --php=on start debugging server php configuration ---php=off stop debugging server php configuration ---fpm --fpm=on start debugging fastcgi configuration ---fpm=off stop debugging fastcgi configuration ---mysql --mysql=on start debugging mysql server ---mysql=off stop debugging mysql server ---wp --wp=on start wordpress debugging ---wp=off stop wordpress debugging - -""" - - -def debug_plugin_hook(app): + +def ee_debug_hook(app): # do something with the ``app`` object here. pass @@ -89,6 +68,7 @@ class EEDebugController(CementBaseController): (['site_name'], dict(help='Website Name', nargs='?', default=None)) ] + usage = "ee debug [] [options] " @expose(hide=True) def debug_nginx(self): @@ -493,9 +473,10 @@ class EEDebugController(CementBaseController): and (not self.app.pargs.all) and (not self.app.pargs.site_name)): if self.app.pargs.stop or self.app.pargs.start: - print("--start/stop option is deprecated in ee3.0.5", usage) + print("--start/stop option is deprecated since ee3.0.5") + self.app.args.print_help() else: - print(usage) + self.app.args.print_help() if self.app.pargs.all == 'on': if self.app.pargs.site_name: @@ -519,7 +500,7 @@ class EEDebugController(CementBaseController): and (not self.app.pargs.fpm) and (not self.app.pargs.mysql) and (not self.app.pargs.wp) and (not self.app.pargs.rewrite) and self.app.pargs.site_name): - print(usage) + self.app.args.print_help() # self.app.pargs.nginx = 'on' # self.app.pargs.wp = 'on' # self.app.pargs.rewrite = 'on' @@ -570,4 +551,4 @@ def load(app): # register the plugin class.. this only happens if the plugin is enabled handler.register(EEDebugController) # register a hook (function) to run after arguments are parsed. - hook.register('post_argument_parsing', debug_plugin_hook) + hook.register('post_argument_parsing', ee_debug_hook) diff --git a/ee/cli/plugins/import_slow_log.py b/ee/cli/plugins/import_slow_log.py index b52bf72b..4022d0e2 100644 --- a/ee/cli/plugins/import_slow_log.py +++ b/ee/cli/plugins/import_slow_log.py @@ -6,7 +6,7 @@ from ee.core.variables import EEVariables import os -def import_slow_log_plugin_hook(app): +def ee_import_slow_log_hook(app): pass @@ -16,6 +16,7 @@ class EEImportslowlogController(CementBaseController): stacked_on = 'base' stacked_type = 'nested' description = 'Import MySQL slow log to Anemometer database' + usage = "ee import-slow-log" @expose(hide=True) def default(self): @@ -67,4 +68,4 @@ def load(app): handler.register(EEImportslowlogController) # register a hook (function) to run after arguments are parsed. - hook.register('post_argument_parsing', import_slow_log_plugin_hook) + hook.register('post_argument_parsing', ee_import_slow_log_hook) diff --git a/ee/cli/plugins/info.py b/ee/cli/plugins/info.py index 4ec74db2..b8162cb3 100644 --- a/ee/cli/plugins/info.py +++ b/ee/cli/plugins/info.py @@ -10,7 +10,7 @@ import os import configparser -def info_plugin_hook(app): +def ee_info_hook(app): # do something with the ``app`` object here. pass @@ -33,6 +33,7 @@ class EEInfoController(CementBaseController): dict(help='Get Nginx configuration information', action='store_true')), ] + usage = "ee info [options]" @expose(hide=True) def info_nginx(self): @@ -199,4 +200,4 @@ def load(app): handler.register(EEInfoController) # register a hook (function) to run after arguments are parsed. - hook.register('post_argument_parsing', info_plugin_hook) + hook.register('post_argument_parsing', ee_info_hook) diff --git a/ee/cli/plugins/log.py b/ee/cli/plugins/log.py index 6b5b3038..e645d9fd 100644 --- a/ee/cli/plugins/log.py +++ b/ee/cli/plugins/log.py @@ -11,7 +11,7 @@ import os import glob -def log_plugin_hook(app): +def ee_log_hook(app): # do something with the ``app`` object here. pass @@ -43,6 +43,7 @@ class EELogController(CementBaseController): (['site_name'], dict(help='Website Name', nargs='?', default=None)) ] + usage = "ee log [] [options]" @expose(hide=True) def default(self): @@ -137,4 +138,4 @@ def load(app): # register the plugin class.. this only happens if the plugin is enabled handler.register(EELogController) # register a hook (function) to run after arguments are parsed. - hook.register('post_argument_parsing', log_plugin_hook) + hook.register('post_argument_parsing', ee_log_hook) diff --git a/ee/core/models.py b/ee/cli/plugins/models.py similarity index 66% rename from ee/core/models.py rename to ee/cli/plugins/models.py index 2592a26c..0593210b 100644 --- a/ee/core/models.py +++ b/ee/cli/plugins/models.py @@ -7,6 +7,7 @@ class SiteDB(Base): Databse model for site table """ __tablename__ = 'sites' + __table_args__ = {'extend_existing': True} id = Column(Integer, primary_key=True) sitename = Column(String, unique=True) @@ -23,10 +24,18 @@ class SiteDB(Base): is_ssl = Column(Boolean, unique=False, default=False) storage_fs = Column(String) storage_db = Column(String) + db_name = Column(String) + db_user = Column(String) + db_password = Column(String) + db_host = Column(String) + is_hhvm = Column(Boolean, unique=False, default=False) + is_pagespeed = Column(Boolean, unique=False, default=False) def __init__(self, sitename=None, site_type=None, cache_type=None, site_path=None, site_enabled=None, - is_ssl=None, storage_fs=None, storage_db=None): + is_ssl=None, storage_fs=None, storage_db=None, db_name=None, + db_user=None, db_password=None, db_host='localhost', + hhvm=None, pagespeed=None): self.sitename = sitename self.site_type = site_type self.cache_type = cache_type @@ -35,7 +44,12 @@ class SiteDB(Base): self.is_ssl = is_ssl self.storage_fs = storage_fs self.storage_db = storage_db - + self.db_name = db_name + self.db_user = db_user + self.db_password = db_password + self.db_host = db_host + self.is_hhvm = hhvm + self.is_pagespeed = pagespeed # def __repr__(self): # return '' % (self.site_type) # diff --git a/ee/cli/plugins/secure.py b/ee/cli/plugins/secure.py index 09df15f2..02c20bb3 100644 --- a/ee/cli/plugins/secure.py +++ b/ee/cli/plugins/secure.py @@ -12,7 +12,7 @@ import hashlib import getpass -def secure_plugin_hook(app): +def ee_secure_hook(app): # do something with the ``app`` object here. pass @@ -34,6 +34,7 @@ class EESecureController(CementBaseController): dict(help='user input', nargs='?', default=None)), (['user_pass'], dict(help='user pass', nargs='?', default=None))] + usage = "ee secure [options]" @expose(hide=True) def default(self): @@ -138,4 +139,4 @@ def load(app): # register the plugin class.. this only happens if the plugin is enabled handler.register(EESecureController) # register a hook (function) to run after arguments are parsed. - hook.register('post_argument_parsing', secure_plugin_hook) + hook.register('post_argument_parsing', ee_secure_hook) diff --git a/ee/cli/plugins/site.py b/ee/cli/plugins/site.py index 8e86dfb6..fc0db686 100644 --- a/ee/cli/plugins/site.py +++ b/ee/cli/plugins/site.py @@ -18,7 +18,9 @@ import subprocess def ee_site_hook(app): # do something with the ``app`` object here. from ee.core.database import init_db - init_db() + import ee.cli.plugins.models + init_db(app) + EESync(app).syncdbinfo() class EESiteController(CementBaseController): @@ -31,6 +33,7 @@ class EESiteController(CementBaseController): (['site_name'], dict(help='Website name', nargs='?')), ] + usage = "ee site (command) [options]" @expose(hide=True) def default(self): @@ -43,10 +46,16 @@ class EESiteController(CementBaseController): self.app.pargs.site_name = input('Enter site name : ') except IOError as e: Log.error(self, 'could not input site name') + + # validate domain name (ee_domain, ee_www_domain) = ValidateDomain(self.app.pargs.site_name) - Log.info(self, "Enable domain {0:10} \t".format(ee_domain), end='') + + # check if site exists + if not check_domain_exists(self, ee_domain): + Log.error(self, "site {0} does not exist".format(ee_domain)) if os.path.isfile('/etc/nginx/sites-available/{0}' .format(ee_domain)): + Log.info(self, "Enable domain {0:10} \t".format(ee_domain), end='') EEFileUtils.create_symlink(self, ['/etc/nginx/sites-available/{0}' .format(ee_domain), @@ -59,7 +68,8 @@ class EESiteController(CementBaseController): Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]") EEService.reload_service(self, 'nginx') else: - Log.error(self, "\nsite {0} does not exists".format(ee_domain)) + Log.error(self, "nginx configuration file does not exist" + .format(ee_domain)) @expose(help="Disable site example.com") def disable(self): @@ -69,9 +79,14 @@ class EESiteController(CementBaseController): except IOError as e: Log.error(self, 'could not input site name') (ee_domain, ee_www_domain) = ValidateDomain(self.app.pargs.site_name) - Log.info(self, "Disable domain {0:10} \t".format(ee_domain), end='') + # check if site exists + if not check_domain_exists(self, ee_domain): + Log.error(self, "site {0} does not exist".format(ee_domain)) + if os.path.isfile('/etc/nginx/sites-available/{0}' .format(ee_domain)): + Log.info(self, "Disable domain {0:10} \t" + .format(ee_domain), end='') if not os.path.isfile('/etc/nginx/sites-enabled/{0}' .format(ee_domain)): Log.debug(self, "Site {0} already disabled".format(ee_domain)) @@ -87,7 +102,8 @@ class EESiteController(CementBaseController): Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]") EEService.reload_service(self, 'nginx') else: - Log.error(self, " site {0} does not exists".format(ee_domain)) + Log.error(self, "nginx configuration file does not exist" + .format(ee_domain)) @expose(help="Get example.com information") def info(self): @@ -100,53 +116,45 @@ class EESiteController(CementBaseController): ee_db_name = '' ee_db_user = '' ee_db_pass = '' + + if not check_domain_exists(self, ee_domain): + Log.error(self, "site {0} does not exist".format(ee_domain)) if os.path.isfile('/etc/nginx/sites-available/{0}' .format(ee_domain)): - check_site = getSiteInfo(self, ee_domain) - if check_site is None: - Log.error(self, " Site {0} does not exist.".format(ee_domain)) - else: - sitetype = check_site.site_type - cachetype = check_site.cache_type + siteinfo = getSiteInfo(self, ee_domain) - ee_site_webroot = EEVariables.ee_webroot + ee_domain + sitetype = siteinfo.site_type + cachetype = siteinfo.cache_type + ee_site_webroot = siteinfo.site_path access_log = (ee_site_webroot + '/logs/access.log') error_log = (ee_site_webroot + '/logs/error.log') - configfiles = glob.glob(ee_site_webroot + '/*-config.php') - if configfiles: - if EEFileUtils.isexist(self, configfiles[0]): - ee_db_name = (EEFileUtils.grep(self, configfiles[0], - 'DB_NAME').split(',')[1] - .split(')')[0].strip().replace('\'', '')) - ee_db_user = (EEFileUtils.grep(self, configfiles[0], - 'DB_USER').split(',')[1] - .split(')')[0].strip().replace('\'', '')) - ee_db_pass = (EEFileUtils.grep(self, configfiles[0], - 'DB_PASSWORD').split(',')[1] - .split(')')[0].strip().replace('\'', '')) + ee_db_name = siteinfo.db_name + ee_db_user = siteinfo.db_user + ee_db_pass = siteinfo.db_password + ee_db_host = siteinfo.db_host data = dict(domain=ee_domain, webroot=ee_site_webroot, accesslog=access_log, errorlog=error_log, dbname=ee_db_name, dbuser=ee_db_user, dbpass=ee_db_pass, type=sitetype + " " + cachetype + " ({0})".format("enabled" - if check_site.is_enabled else + if siteinfo.is_enabled else "disabled")) self.app.render((data), 'siteinfo.mustache') else: - Log.error(self, " site {0} does not exists".format(ee_domain)) + Log.error(self, "nginx configuration file does not exist" + .format(ee_domain)) @expose(help="Monitor example.com logs") def log(self): (ee_domain, ee_www_domain) = ValidateDomain(self.app.pargs.site_name) - ee_site_webroot = EEVariables.ee_webroot + ee_domain + ee_site_webroot = getSiteInfo(self, ee_domain).site_path - if os.path.isfile('/etc/nginx/sites-available/{0}' - .format(ee_domain)): - logfiles = glob.glob(ee_site_webroot + '/logs/*.log') + if not check_domain_exists(self, ee_domain): + Log.error(self, "site {0} does not exist".format(ee_domain)) + logfiles = glob.glob(ee_site_webroot + '/logs/*.log') + if logfiles: logwatch(self, logfiles) - else: - Log.error(self, " site {0} does not exists".format(ee_domain)) @expose(help="Edit Nginx configuration of example.com") def edit(self): @@ -154,12 +162,18 @@ class EESiteController(CementBaseController): try: self.app.pargs.site_name = input('Enter site name : ') except IOError as e: - Log.error(self, 'could not input site name') + Log.error(self, 'Unable to read input, Please try again') (ee_domain, ee_www_domain) = ValidateDomain(self.app.pargs.site_name) + + if not check_domain_exists(self, ee_domain): + Log.error(self, "site {0} does not exist".format(ee_domain)) if os.path.isfile('/etc/nginx/sites-available/{0}' .format(ee_domain)): - EEShellExec.invoke_editor(self, '/etc/nginx/sites-available/{0}' - .format(ee_domain)) + try: + EEShellExec.invoke_editor(self, '/etc/nginx/sites-available/' + '{0}'.format(ee_domain)) + except CommandExecutionError as e: + Log.error(self, "Failed invoke editor") if (EEGit.checkfilestatus(self, "/etc/nginx", '/etc/nginx/sites-available/{0}'.format(ee_domain))): EEGit.add(self, ["/etc/nginx"], msg="Edit website: {0}" @@ -167,7 +181,8 @@ class EESiteController(CementBaseController): # Reload NGINX EEService.reload_service(self, 'nginx') else: - Log.error(self, " site {0} does not exists".format(ee_domain)) + Log.error(self, "nginx configuration file does not exists" + .format(ee_domain)) @expose(help="Display Nginx configuration of example.com") def show(self): @@ -178,6 +193,10 @@ class EESiteController(CementBaseController): Log.error(self, 'could not input site name') # TODO Write code for ee site edit command here (ee_domain, ee_www_domain) = ValidateDomain(self.app.pargs.site_name) + + if not check_domain_exists(self, ee_domain): + Log.error(self, "site {0} does not exist".format(ee_domain)) + if os.path.isfile('/etc/nginx/sites-available/{0}' .format(ee_domain)): Log.info(self, "Display NGINX configuration for {0}" @@ -188,7 +207,8 @@ class EESiteController(CementBaseController): Log.info(self, Log.ENDC + text) f.close() else: - Log.error(self, " site {0} does not exists".format(ee_domain)) + Log.error(self, "nginx configuration file does not exists" + .format(ee_domain)) @expose(help="Change directory to site webroot") def cd(self): @@ -196,17 +216,21 @@ class EESiteController(CementBaseController): try: self.app.pargs.site_name = input('Enter site name : ') except IOError as e: - Log.error(self, 'could not input site name') + Log.error(self, 'Unable to read input, please try again') + (ee_domain, ee_www_domain) = ValidateDomain(self.app.pargs.site_name) - if os.path.isfile('/etc/nginx/sites-available/{0}' - .format(ee_domain)): - ee_site_webroot = EEVariables.ee_webroot + ee_domain - EEFileUtils.chdir(self, ee_site_webroot) - try: - subprocess.call(['bash']) - except OSError as e: - Log.debug(self, "{0}{1}".format(e.errno, e.strerror)) - Log.error(self, " cannot change directory") + + if not check_domain_exists(self, ee_domain): + Log.error(self, "site {0} does not exist".format(ee_domain)) + + ee_site_webroot = getSiteInfo(self, ee_domain).site_path + EEFileUtils.chdir(self, ee_site_webroot) + + try: + subprocess.call(['bash']) + except OSError as e: + Log.debug(self, "{0}{1}".format(e.errno, e.strerror)) + Log.error(self, "unable to change directory") class EESiteCreateController(CementBaseController): @@ -250,59 +274,40 @@ class EESiteCreateController(CementBaseController): def default(self): # self.app.render((data), 'default.mustache') # Check domain name validation + data = dict() + try: + stype, cache = detSitePar(vars(self.app.pargs)) + except RuntimeError as e: + Log.debug(self, str(e)) + Log.error(self, "Please provide valid options to creating site") + if not self.app.pargs.site_name: try: self.app.pargs.site_name = input('Enter site name : ') except IOError as e: - Log.error(self, 'could not input site name') - if not (self.app.pargs.html or self.app.pargs.php or - self.app.pargs.mysql or self.app.pargs.wp or - self.app.pargs.w3tc or self.app.pargs.wpfc or - self.app.pargs.wpsc or self.app.pargs.wpsubdir or - self.app.pargs.wpsubdomain): - self.app.pargs.html = True - - data = '' + Log.debug(self, str(e)) + Log.error(self, "Unable to input site name, Please try again!") + (ee_domain, ee_www_domain) = ValidateDomain(self.app.pargs.site_name) ee_site_webroot = EEVariables.ee_webroot + ee_domain - # Check if doain previously exists or not - if os.path.isfile('/etc/nginx/sites-available/{0}' - .format(ee_domain)): - Log.error(self, " site {0} already exists" - .format(ee_domain)) - - # setup nginx configuration for site - # HTML - if (self.app.pargs.html and not (self.app.pargs.php or - self.app.pargs.mysql or self.app.pargs.wp or self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc or - self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)): + if check_domain_exists(self, ee_domain): + Log.error(self, "site {0} already exists".format(ee_domain)) + elif os.path.isfile('/etc/nginx/sites-available/{0}' + .format(ee_domain)): + Log.error(self, "Nginx configuration /etc/nginx/sites-available/" + "{0} already exists".format(ee_domain)) + if stype in ['html', 'php']: data = dict(site_name=ee_domain, www_domain=ee_www_domain, static=True, basic=False, wp=False, w3tc=False, wpfc=False, wpsc=False, multisite=False, wpsubdir=False, webroot=ee_site_webroot) - stype = 'html' - cache = 'basic' - # PHP - if (self.app.pargs.php and not (self.app.pargs.html or - self.app.pargs.mysql or self.app.pargs.wp or self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc or - self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=True, wp=False, w3tc=False, - wpfc=False, wpsc=False, multisite=False, - wpsubdir=False, webroot=ee_site_webroot) - stype = 'php' - cache = 'basic' - # MySQL - if (self.app.pargs.mysql and not (self.app.pargs.html or - self.app.pargs.php or self.app.pargs.wp or self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc or - self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)): + if stype == 'php': + data['static'] = False + data['basic'] = True + elif stype in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']: data = dict(site_name=ee_domain, www_domain=ee_www_domain, static=False, basic=True, wp=False, w3tc=False, @@ -310,166 +315,15 @@ class EESiteCreateController(CementBaseController): wpsubdir=False, webroot=ee_site_webroot, ee_db_name='', ee_db_user='', ee_db_pass='', ee_db_host='') - stype = 'mysql' - cache = 'basic' - # WP - if ((self.app.pargs.wp or self.app.pargs.w3tc or self.app.pargs.wpfc or - self.app.pargs.wpsc) and not (self.app.pargs.html or - self.app.pargs.php or self.app.pargs.mysql or - self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)): - - if (self.app.pargs.wp and not (self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=True, wp=True, w3tc=False, - wpfc=False, wpsc=False, multisite=False, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wp' - cache = 'basic' - - if (self.app.pargs.w3tc and not - (self.app.pargs.wpfc or self.app.pargs.wpsc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=True, - wpfc=False, wpsc=False, multisite=False, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wp' - cache = 'w3tc' - - if (self.app.pargs.wpfc and not - (self.app.pargs.wpsc or self.app.pargs.w3tc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=True, wpsc=False, multisite=False, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wp' - cache = 'wpfc' - - if (self.app.pargs.wpsc and not - (self.app.pargs.w3tc or self.app.pargs.wpfc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=False, wpsc=True, multisite=False, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wp' - cache = 'wpsc' - - # WPSUBDIR - if (self.app.pargs.wpsubdir and not (self.app.pargs.html or - self.app.pargs.php or self.app.pargs.mysql or - self.app.pargs.wpsubdomain or self.app.pargs.wp)): - - if (self.app.pargs.wpsubdir and not (self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc)): - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=True, wp=True, w3tc=False, - wpfc=False, wpsc=False, multisite=True, - wpsubdir=True, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wpsubdir' - cache = 'basic' - - if (self.app.pargs.w3tc and not - (self.app.pargs.wpfc or self.app.pargs.wpsc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=True, - wpfc=False, wpsc=False, multisite=True, - wpsubdir=True, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wpsubdir' - cache = 'w3tc' - - if (self.app.pargs.wpfc and not - (self.app.pargs.wpsc or self.app.pargs.w3tc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=True, wpsc=False, multisite=True, - wpsubdir=True, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wpsubdir' - cache = 'wpfc' - - if (self.app.pargs.wpsc and not - (self.app.pargs.w3tc or self.app.pargs.wpfc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=False, wpsc=True, multisite=True, - wpsubdir=True, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wpsubdir' - cache = 'wpsc' - - # WPSUBDOAIN - if (self.app.pargs.wpsubdomain and not (self.app.pargs.html or - self.app.pargs.php or self.app.pargs.mysql or - self.app.pargs.wpsubdir or self.app.pargs.wp)): - - if (self.app.pargs.wpsubdomain and not (self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=True, wp=True, w3tc=False, - wpfc=False, wpsc=False, multisite=True, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wpsubdomain' - cache = 'basic' - - if (self.app.pargs.w3tc and not - (self.app.pargs.wpfc or self.app.pargs.wpsc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=True, - wpfc=False, wpsc=False, multisite=True, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wpsubdomain' - cache = 'w3tc' - - if (self.app.pargs.wpfc and not - (self.app.pargs.wpsc or self.app.pargs.w3tc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=True, wpsc=False, multisite=True, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wpsubdomain' - cache = 'wpfc' - - if (self.app.pargs.wpsc and not - (self.app.pargs.w3tc or self.app.pargs.wpfc)): - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=False, wpsc=True, multisite=True, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='') - stype = 'wpsubdomain' - cache = 'wpsc' + + if stype in ['wp', 'wpsubdir', 'wpsubdomain']: + data['wp'] = True + data['basic'] = False + data[cache] = True + if stype in ['wpsubdir', 'wpsubdomain']: + data['multisite'] = True + if stype == 'wpsubdir': + data['wpsubdir'] = True if not data: self.app.args.print_help() @@ -477,54 +331,136 @@ class EESiteCreateController(CementBaseController): # Check rerequired packages are installed or not ee_auth = site_package_check(self, stype) - # setup NGINX configuration, and webroot - setupdomain(self, data) - # Setup database for MySQL site - if 'ee_db_name' in data.keys() and not data['wp']: - data = setupdatabase(self, data) + + try: try: - eedbconfig = open("{0}/ee-config.php".format(ee_site_webroot), - encoding='utf-8', mode='w') - eedbconfig.write("" - .format(data['ee_db_name'], - data['ee_db_user'], - data['ee_db_pass'], - data['ee_db_host'])) - eedbconfig.close() - stype = 'mysql' - except IOError as e: - Log.debug(self, "{2} ({0}): {1}" - .format(e.errno, e.strerror, ee_domain)) - Log.error(self, " Unable to create ee-config.php for ") + # setup NGINX configuration, and webroot + setupdomain(self, data) + except SiteError as e: + # call cleanup actions on failure + Log.info(self, Log.FAIL + "Oops Something went wrong !!") + Log.info(self, Log.FAIL + "Calling cleanup actions ...") + doCleanupAction(self, domain=ee_domain, + webroot=data['webroot']) + Log.debug(self, str(e)) + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") + + addNewSite(self, ee_domain, stype, cache, ee_site_webroot) + # Setup database for MySQL site + if 'ee_db_name' in data.keys() and not data['wp']: + try: + data = setupdatabase(self, data) + # Add database information for site into database + updateSiteInfo(self, ee_domain, db_name=data['ee_db_name'], + db_user=data['ee_db_user'], + db_password=data['ee_db_pass'], + db_host=data['ee_db_host']) + except SiteError as e: + # call cleanup actions on failure + Log.debug(self, str(e)) + Log.info(self, Log.FAIL + "Oops Something went wrong !!") + Log.info(self, Log.FAIL + "Calling cleanup actions ...") + doCleanupAction(self, domain=ee_domain, + webroot=data['webroot'], + dbname=data['ee_db_name'], + dbuser=data['ee_db_user'], + dbhost=data['ee_db_host']) + deleteSiteInfo(self, ee_domain) + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") + + try: + eedbconfig = open("{0}/ee-config.php" + .format(ee_site_webroot), + encoding='utf-8', mode='w') + eedbconfig.write("" + .format(data['ee_db_name'], + data['ee_db_user'], + data['ee_db_pass'], + data['ee_db_host'])) + eedbconfig.close() + stype = 'mysql' + except IOError as e: + Log.debug(self, str(e)) + Log.debug(self, "Error occured while generating " + "ee-config.php") + Log.info(self, Log.FAIL + "Oops Something went wrong !!") + Log.info(self, Log.FAIL + "Calling cleanup actions ...") + doCleanupAction(self, domain=ee_domain, + webroot=data['webroot'], + dbname=data['ee_db_name'], + dbuser=data['ee_db_user'], + dbhost=data['ee_db_host']) + deleteSiteInfo(self, ee_domain) + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") + + # Setup WordPress if Wordpress site + if data['wp']: + try: + ee_wp_creds = setupwordpress(self, data) + # Add database information for site into database + updateSiteInfo(self, ee_domain, db_name=data['ee_db_name'], + db_user=data['ee_db_user'], + db_password=data['ee_db_pass'], + db_host=data['ee_db_host']) + except SiteError as e: + # call cleanup actions on failure + Log.debug(self, str(e)) + Log.info(self, Log.FAIL + "Oops Something went wrong !!") + Log.info(self, Log.FAIL + "Calling cleanup actions ...") + doCleanupAction(self, domain=ee_domain, + webroot=data['webroot'], + dbname=data['ee_db_name'], + dbuser=data['ee_db_user'], + dbhost=data['ee_db_host']) + deleteSiteInfo(self, ee_domain) + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") + + # Service Nginx Reload + EEService.reload_service(self, 'nginx') - # Setup WordPress if Wordpress site - if data['wp']: - ee_wp_creds = setupwordpress(self, data) - # Service Nginx Reload - EEService.reload_service(self, 'nginx') + EEGit.add(self, ["/etc/nginx"], + msg="{0} created with {1} {2}" + .format(ee_www_domain, stype, cache)) + # Setup Permissions for webroot + try: + setwebrootpermissions(self, data['webroot']) + except SiteError as e: + Log.debug(self, str(e)) + Log.info(self, Log.FAIL + "Oops Something went wrong !!") + Log.info(self, Log.FAIL + "Calling cleanup actions ...") + doCleanupAction(self, domain=ee_domain, + webroot=data['webroot'], + dbname=data['ee_db_name'], + dbuser=data['ee_db_user'], + dbhost=data['ee_db_host']) + deleteSiteInfo(self, ee_domain) + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") - EEGit.add(self, ["/etc/nginx"], - msg="{0} created with {1} {2}" - .format(ee_www_domain, stype, cache)) - # Setup Permissions for webroot - setwebrootpermissions(self, data['webroot']) - if ee_auth and len(ee_auth): - for msg in ee_auth: - Log.info(self, Log.ENDC + msg, log=False) + if ee_auth and len(ee_auth): + for msg in ee_auth: + Log.info(self, Log.ENDC + msg, log=False) - if data['wp']: - Log.info(self, Log.ENDC + "WordPress admin user :" - " {0}".format(ee_wp_creds['wp_user']), log=False) - Log.info(self, Log.ENDC + "WordPress admin user password : {0}" - .format(ee_wp_creds['wp_pass']), log=False) + if data['wp']: + Log.info(self, Log.ENDC + "WordPress admin user :" + " {0}".format(ee_wp_creds['wp_user']), log=False) + Log.info(self, Log.ENDC + "WordPress admin user password : {0}" + .format(ee_wp_creds['wp_pass']), log=False) - display_cache_settings(self, data) - addNewSite(self, ee_domain, stype, cache, ee_site_webroot) - Log.info(self, "Successfully created site" - " http://{0}".format(ee_domain)) + display_cache_settings(self, data) + + Log.info(self, "Successfully created site" + " http://{0}".format(ee_domain)) + except SiteError as e: + Log.error(self, "Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") class EESiteUpdateController(CementBaseController): @@ -564,12 +500,21 @@ class EESiteUpdateController(CementBaseController): @expose(help="Update site type or cache") def default(self): + + data = dict() + try: + stype, cache = detSitePar(vars(self.app.pargs)) + except RuntimeError as e: + Log.debug(self, str(e)) + Log.error(self, "Please provide valid options combination for" + " site update") + if not self.app.pargs.site_name: try: self.app.pargs.site_name = input('Enter site name : ') except IOError as e: - Log.error(self, 'could not input site name') - data = '' + Log.error(self, 'Unable to input site name, Please try again!') + (ee_domain, ee_www_domain, ) = ValidateDomain(self.app.pargs.site_name) ee_site_webroot = EEVariables.ee_webroot + ee_domain @@ -586,310 +531,48 @@ class EESiteUpdateController(CementBaseController): self.app.pargs.php or self.app.pargs.mysql or self.app.pargs.wp or self.app.pargs.w3tc or self.app.pargs.wpfc or self.app.pargs.wpsc or self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)): - - updatewpuserpassword(self, ee_domain, ee_site_webroot) + try: + updatewpuserpassword(self, ee_domain, ee_site_webroot) + except SiteError as e: + Log.debug(self, str(e)) + Log.error(self, "Password Unchanged.") self.app.close(0) - if (self.app.pargs.html and not (self.app.pargs.php or - self.app.pargs.mysql or self.app.pargs.wp or self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc or - self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)): - Log.error(self, " Cannot update {0} {1} to html" - .format(ee_domain, oldsitetype)) - - # PHP - if (self.app.pargs.php and not (self.app.pargs.html or - self.app.pargs.mysql or self.app.pargs.wp or self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc or - self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)): - - if oldsitetype != 'html': - - Log.error(self, " Cannot update {0} {1} to php" - .format(ee_domain, oldsitetype)) - + if ((stype == 'php' and oldsitetype != 'html') or + (stype == 'mysql' and oldsitetype not in ['html', 'php']) or + (stype == 'wp' and oldsitetype not in ['html', 'php', 'mysql', + 'wp']) or + (stype == 'wpsubdir' and oldsitetype in ['wpsubdomain']) or + (stype == 'wpsubdomain' and oldsitetype in ['wpsubdir']) or + (stype == oldsitetype and cache == oldcachetype)): + Log.error(self, "can not update {0} {1} to {2} {3}". + format(oldsitetype, oldcachetype, stype, cache)) + + if stype == 'php': data = dict(site_name=ee_domain, www_domain=ee_www_domain, static=False, basic=True, wp=False, w3tc=False, wpfc=False, wpsc=False, multisite=False, wpsubdir=False, webroot=ee_site_webroot, currsitetype=oldsitetype, currcachetype=oldcachetype) - stype = 'php' - cache = 'basic' - - # MySQL - if (self.app.pargs.mysql and not (self.app.pargs.html or - self.app.pargs.php or self.app.pargs.wp or self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc or - self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)): - if oldsitetype not in ['html', 'php']: - Log.error(self, " Cannot update {0}, {1} to mysql" - .format(ee_domain, oldsitetype)) + elif stype in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']: data = dict(site_name=ee_domain, www_domain=ee_www_domain, static=False, basic=True, wp=False, w3tc=False, wpfc=False, wpsc=False, multisite=False, wpsubdir=False, webroot=ee_site_webroot, ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - stype = 'mysql' - cache = 'basic' - - # WP - if ((self.app.pargs.wp or self.app.pargs.w3tc or self.app.pargs.wpfc or - self.app.pargs.wpsc) and not (self.app.pargs.html or - self.app.pargs.php or self.app.pargs.mysql or - self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)): - if (self.app.pargs.wp and not (self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc)): - - if ((oldsitetype not in ['html', 'php', 'mysql', 'wp']) - or (oldsitetype == 'wp' and oldcachetype == 'basic')): - - Log.error(self, "Cannot update {0} {1} {2} to wp basic" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=True, wp=True, w3tc=False, - wpfc=False, wpsc=False, multisite=False, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - stype = 'wp' - cache = 'basic' - - if (self.app.pargs.w3tc and not - (self.app.pargs.wpfc or self.app.pargs.wpsc)): - - if (oldsitetype not in ['html', 'php', 'mysql', 'wp'] - or (oldsitetype == 'wp' and oldcachetype == 'w3tc')): - Log.error(self, "Cannot update {0}, {1} {2} to wp w3tc" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=True, - wpfc=False, wpsc=False, multisite=False, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - - stype = 'wp' - cache = 'w3tc' - - if (self.app.pargs.wpfc and not - (self.app.pargs.wpsc or self.app.pargs.w3tc)): - - if (oldsitetype not in ['html', 'php', 'mysql', 'wp'] - or (oldsitetype == 'wp' and oldcachetype == 'wpfc')): - Log.error(self, "Cannot update {0}, {1} {2} to wp wpfc" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=True, wpsc=False, multisite=False, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - stype = 'wp' - cache = 'wpfc' - - if (self.app.pargs.wpsc and not - (self.app.pargs.w3tc or self.app.pargs.wpfc)): - - if (oldsitetype not in ['html', 'php', 'mysql', 'wp'] - or (oldsitetype == 'wp' and oldcachetype == 'wpsc')): - Log.error(self, "Cannot update {0}, {1} {2} to wp wpsc" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=False, wpsc=True, multisite=False, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - stype = 'wp' - cache = 'wpsc' - - # WPSUBDIR - if (self.app.pargs.wpsubdir and not (self.app.pargs.html or - self.app.pargs.php or self.app.pargs.mysql or - self.app.pargs.wpsubdomain or self.app.pargs.wp)): - if (self.app.pargs.wpsubdir and not (self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc)): - - if (oldsitetype not in ['html', 'php', 'mysql', 'wp', - 'wpsubdir'] - or (oldsitetype == 'wpsubdir' and oldcachetype == 'basic')): - Log.error(self, " Cannot update {0}, {1} {2} " - "to wpsubdir basic" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=True, wp=True, w3tc=False, - wpfc=False, wpsc=False, multisite=True, - wpsubdir=True, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - stype = 'wpsubdir' - cache = 'basic' - - if (self.app.pargs.w3tc and not - (self.app.pargs.wpfc or self.app.pargs.wpsc)): - - if (oldsitetype not in ['html', 'php', 'mysql', 'wp', - 'wpsubdir'] - or (oldsitetype == 'wpsubdir' and oldcachetype == 'w3tc')): - Log.error(self, " Cannot update {0} {1} {2}" - "to wpsubdir w3tc" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=True, - wpfc=False, wpsc=False, multisite=True, - wpsubdir=True, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - - stype = 'wpsubdir' - cache = 'w3tc' - - if (self.app.pargs.wpfc and not - (self.app.pargs.wpsc or self.app.pargs.w3tc)): - - if (oldsitetype not in ['html', 'php', 'mysql', 'wp', - 'wpsubdir'] - or (oldsitetype == 'wpsubdir' and oldcachetype == 'wpfc')): - Log.error(self, "Cannot update {0} {1} {2}" - " to wpsubdir wpfc" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=True, wpsc=False, multisite=True, - wpsubdir=True, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - stype = 'wpsubdir' - cache = 'wpfc' - - if (self.app.pargs.wpsc and not - (self.app.pargs.w3tc or self.app.pargs.wpfc)): - - if (oldsitetype not in ['html', 'php', 'mysql', 'wp', - 'wpsubdir'] - or (oldsitetype == 'wpsubdir' and oldcachetype == 'wpsc')): - Log.error(self, " Cannot update {0} {1} {2}" - " to wpsubdir wpsc" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=False, wpsc=True, multisite=True, - wpsubdir=True, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - stype = 'wpsubdir' - cache = 'wpsc' - - if (self.app.pargs.wpsubdomain and not (self.app.pargs.html or - self.app.pargs.php or self.app.pargs.mysql or - self.app.pargs.wpsubdir or self.app.pargs.wp)): - if (self.app.pargs.wpsubdomain and not (self.app.pargs.w3tc - or self.app.pargs.wpfc or self.app.pargs.wpsc)): - if (oldsitetype not in ['html', 'php', 'mysql', 'wp', - 'wpsubdomain'] - or (oldsitetype == 'wpsubdomain' and - oldcachetype == 'basic')): - - Log.error(self, " Cannot update {0} {1} {2}" - " to wpsubdomain basic" - .format(ee_domain, oldsitetype, oldcachetype)) + ee_db_host='', + currsitetype=oldsitetype, currcachetype=oldcachetype) - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=True, wp=True, w3tc=False, - wpfc=False, wpsc=False, multisite=True, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - - stype = 'wpsubdomain' - cache = 'basic' - - if (self.app.pargs.w3tc and not - (self.app.pargs.wpfc or self.app.pargs.wpsc)): - - if (oldsitetype not in ['html', 'php', 'mysql', 'wp', - 'wpsubdomain'] or - (oldsitetype == 'wpsubdomain' and oldcachetype == 'w3tc')): - print(oldsitetype, oldcachetype) - Log.error(self, " Cannot update {0}, {1} {2}" - " to wpsubdomain w3tc" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=True, - wpfc=False, wpsc=False, multisite=True, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - - stype = 'wpsubdomain' - cache = 'w3tc' - - if (self.app.pargs.wpfc and not - (self.app.pargs.wpsc or self.app.pargs.w3tc)): - - if (oldsitetype not in ['html', 'php', 'mysql', 'wp', - 'wpsubdomain'] or - (oldsitetype == 'wpsubdomain' and oldcachetype == 'wpfc')): - print(oldsitetype, oldcachetype) - Log.error(self, " Cannot update {0}, {1} {2} " - "to wpsubdomain wpfc" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=True, wpsc=False, multisite=True, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - - stype = 'wpsubdomain' - cache = 'wpfc' - - if (self.app.pargs.wpsc and not - (self.app.pargs.w3tc or self.app.pargs.wpfc)): - - if (oldsitetype not in ['html', 'php', 'mysql', 'wp', - 'wpsubdomain'] or - (oldsitetype == 'wpsubdomain' and oldcachetype == 'wpsc')): - print(oldsitetype, oldcachetype) - Log.error(self, " Cannot update {0}, {1} {2}" - " to wpsubdomain wpsc" - .format(ee_domain, oldsitetype, oldcachetype)) - - data = dict(site_name=ee_domain, www_domain=ee_www_domain, - static=False, basic=False, wp=True, w3tc=False, - wpfc=False, wpsc=True, multisite=True, - wpsubdir=False, webroot=ee_site_webroot, - ee_db_name='', ee_db_user='', ee_db_pass='', - ee_db_host='', currsitetype=oldsitetype, - currcachetype=oldcachetype) - - stype = 'wpsubdomain' - cache = 'wpsc' + if stype in ['wp', 'wpsubdir', 'wpsubdomain']: + data['wp'] = True + data['basic'] = False + data[cache] = True + if stype in ['wpsubdir', 'wpsubdomain']: + data['multisite'] = True + if stype == 'wpsubdir': + data['wpsubdir'] = True if not data: Log.error(self, " Cannot update {0}, Invalid Options" @@ -899,10 +582,20 @@ class EESiteUpdateController(CementBaseController): sitebackup(self, data) # setup NGINX configuration, and webroot - setupdomain(self, data) + try: + setupdomain(self, data) + except SiteError as e: + Log.debug(self, str(e)) + Log.error(self, "Update site failed. Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") if 'ee_db_name' in data.keys() and not data['wp']: - data = setupdatabase(self, data) + try: + data = setupdatabase(self, data) + except SiteError as e: + Log.debug(self, str(e)) + Log.error(self, "Update site failed. Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") try: eedbconfig = open("{0}/ee-config.php".format(ee_site_webroot), encoding='utf-8', mode='w') @@ -916,8 +609,10 @@ class EESiteUpdateController(CementBaseController): data['ee_db_host'])) eedbconfig.close() except IOError as e: - Log.debug(self, "{0}".format(e)) - Log.error(self, " Unable to create ee-config.php.") + Log.debug(self, str(e)) + Log.debug(self, "creating ee-config.php failed.") + Log.error(self, "Update site failed. Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") if oldsitetype == 'mysql': config_file = (ee_site_webroot + '/backup/{0}/ee-config.php' @@ -941,28 +636,58 @@ class EESiteUpdateController(CementBaseController): # Setup WordPress if old sites are html/php/mysql sites if data['wp'] and oldsitetype in ['html', 'php', 'mysql']: - ee_wp_creds = setupwordpress(self, data) + try: + ee_wp_creds = setupwordpress(self, data) + except SiteError as e: + Log.debug(self, str(e)) + Log.error(self, "Update site failed. Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") # Uninstall unnecessary plugins if oldsitetype in ['wp', 'wpsubdir', 'wpsubdomain']: # Setup WordPress Network if update option is multisite # and oldsite is WordPress single site if data['multisite'] and oldsitetype == 'wp': - setupwordpressnetwork(self, data) + try: + setupwordpressnetwork(self, data) + except SiteError as e: + Log.debug(self, str(e)) + Log.error(self, "Update site failed. Check logs for reason" + " `tail /var/log/ee/ee.log` & Try Again!!!") if (oldcachetype == 'w3tc' or oldcachetype == 'wpfc' and not (data['w3tc'] or data['wpfc'])): - uninstallwp_plugin(self, 'w3-total-cache', data) + try: + uninstallwp_plugin(self, 'w3-total-cache', data) + except SiteError as e: + Log.debug(self, str(e)) + Log.error(self, "Update site failed. Check logs for reason" + " `tail /var/log/ee/ee.log` & Try Again!!!") if oldcachetype == 'wpsc' and not data['wpsc']: - uninstallwp_plugin(self, 'wp-super-cache', data) + try: + uninstallwp_plugin(self, 'wp-super-cache', data) + except SiteError as e: + Log.debug(self, str(e)) + Log.error(self, "Update site failed. Check logs for reason" + " `tail /var/log/ee/ee.log` & Try Again!!!") if (oldcachetype != 'w3tc' or oldcachetype != 'wpfc') and (data['w3tc'] or data['wpfc']): - installwp_plugin(self, 'w3-total-cache', data) + try: + installwp_plugin(self, 'w3-total-cache', data) + except SiteError as e: + Log.debug(self, str(e)) + Log.error(self, "Update site failed. Check logs for reason" + " `tail /var/log/ee/ee.log` & Try Again!!!") if oldcachetype != 'wpsc' and data['wpsc']: - installwp_plugin(self, 'wp-super-cache', data) + try: + installwp_plugin(self, 'wp-super-cache', data) + except SiteError as e: + Log.debug(self, str(e)) + Log.error(self, "Update site failed. Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") # Service Nginx Reload EEService.reload_service(self, 'nginx') @@ -971,7 +696,13 @@ class EESiteUpdateController(CementBaseController): msg="{0} updated with {1} {2}" .format(ee_www_domain, stype, cache)) # Setup Permissions for webroot - setwebrootpermissions(self, data['webroot']) + try: + setwebrootpermissions(self, data['webroot']) + except SiteError as e: + Log.debug(self, str(e)) + Log.error(self, "Update site failed. Check logs for reason " + "`tail /var/log/ee/ee.log` & Try Again!!!") + if ee_auth and len(ee_auth): for msg in ee_auth: Log.info(self, Log.ENDC + msg) @@ -982,7 +713,14 @@ class EESiteUpdateController(CementBaseController): " {0}".format(ee_wp_creds['wp_user'])) Log.info(self, Log.ENDC + "WordPress admin password : {0}" .format(ee_wp_creds['wp_pass']) + "\n\n") - updateSiteInfo(self, ee_domain, stype=stype, cache=cache) + if oldsitetype in ['html', 'php'] and stype != 'php': + updateSiteInfo(self, ee_domain, stype=stype, cache=cache, + db_name=data['ee_db_name'], + db_user=data['ee_db_user'], + db_password=data['ee_db_pass'], + db_host=data['ee_db_host']) + else: + updateSiteInfo(self, ee_domain, stype=stype, cache=cache) Log.info(self, "Successfully updated site" " http://{0}".format(ee_domain)) @@ -1019,107 +757,85 @@ class EESiteDeleteController(CementBaseController): ee_db_name = '' ee_prompt = '' ee_nginx_prompt = '' + mark_db_deleted = False + mark_webroot_deleted = False + if not check_domain_exists(self, ee_domain): + Log.error(self, "site {0} does not exist".format(ee_domain)) + if ((not self.app.pargs.db) and (not self.app.pargs.files) and (not self.app.pargs.all)): self.app.pargs.all = True - if os.path.isfile('/etc/nginx/sites-available/{0}' - .format(ee_domain)): - ee_site_webroot = EEVariables.ee_webroot + ee_domain - - if self.app.pargs.no_prompt: - ee_prompt = 'Y' - - if self.app.pargs.db: - if not ee_prompt: - ee_db_prompt = input('Do you want to delete database' - '[Y/N]: ') + # Gather information from ee-db for ee_domain + check_site = getSiteInfo(self, ee_domain) + ee_site_type = check_site.site_type + ee_site_webroot = check_site.site_path + if ee_site_webroot == 'deleted': + mark_webroot_deleted = True + if ee_site_type in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']: + ee_db_name = check_site.db_name + ee_db_user = check_site.db_user + ee_db_host = check_site.db_host + if ee_db_name == 'deleted': + mark_db_deleted = True + if self.app.pargs.all: + self.app.pargs.db = True + self.app.pargs.files = True + else: + if self.app.pargs.all: + mark_db_deleted = True + self.app.pargs.files = True + + # Delete website database + if self.app.pargs.db: + if ee_db_name != 'deleted': + if not self.app.pargs.no_prompt: + ee_db_prompt = input('Are you sure, you want to delete' + ' database [y/N]: ') else: ee_db_prompt = 'Y' if ee_db_prompt == 'Y' or ee_db_prompt == 'y': - self.deleteDB(ee_site_webroot) - - if self.app.pargs.files: - if not ee_prompt: - ee_web_prompt = input('Do you want to delete webroot' - '[Y/N]: ') + Log.info(self, "Deleting Database, {0}, user {1}" + .format(ee_db_name, ee_db_user)) + deleteDB(self, ee_db_name, ee_db_user, ee_db_host) + updateSiteInfo(self, ee_domain, + db_name='deleted', + db_user='deleted', + db_password='deleted') + mark_db_deleted = True + Log.info(self, "Deleted Database successfully.") + else: + mark_db_deleted = True + Log.info(self, "Database seems to be deleted.") + + # Delete webroot + if self.app.pargs.files: + if ee_site_webroot != 'deleted': + if not self.app.pargs.no_prompt: + ee_web_prompt = input('Are you sure, you want to delete ' + 'webroot [y/N]: ') else: ee_web_prompt = 'Y' if ee_web_prompt == 'Y' or ee_web_prompt == 'y': - self.deleteWebRoot(ee_site_webroot) - - if self.app.pargs.all: - if not ee_prompt: - ee_db_prompt = input('Do you want to delete database' - '[Y/N]: ' - ) - ee_web_prompt = input('Do you want to delete webroot' - '[Y/N]: ') - ee_nginx_prompt = input('Do you want to delete NGINX' - ' configuration [Y/N]: ') - else: - ee_db_prompt = 'Y' - ee_web_prompt = 'Y' - ee_nginx_prompt = 'Y' + Log.info(self, "Deleting Webroot, {0}" + .format(ee_site_webroot)) + deleteWebRoot(self, ee_site_webroot) + updateSiteInfo(self, ee_domain, webroot='deleted') + mark_webroot_deleted = True + Log.info(self, "Deleted webroot successfully") + else: + mark_webroot_deleted = True + Log.info(self, "Webroot seems to be already deleted") - if ee_db_prompt == 'Y' or ee_db_prompt == 'y': - self.deleteDB(ee_site_webroot) - if ee_web_prompt == 'Y' or ee_web_prompt == 'y': - self.deleteWebRoot(ee_site_webroot) - - if (ee_nginx_prompt == 'Y' or ee_nginx_prompt == 'y'): - Log.debug(self, "Removing Nginx configuration") - EEFileUtils.rm(self, '/etc/nginx/sites-enabled/{0}' - .format(ee_domain)) - EEFileUtils.rm(self, '/etc/nginx/sites-available/{0}' - .format(ee_domain)) - EEGit.add(self, ["/etc/nginx"], - msg="Deleted {0} " - .format(ee_domain)) + if (mark_webroot_deleted and mark_db_deleted): + # TODO Delete nginx conf + removeNginxConf(self, ee_domain) deleteSiteInfo(self, ee_domain) - - Log.info(self, "Deleted site {0}".format(ee_domain)) - else: - Log.error(self, " site {0} does not exists".format(ee_domain)) - - @expose(hide=True) - def deleteDB(self, webroot): - configfiles = glob.glob(webroot + '/*-config.php') - if configfiles: - if EEFileUtils.isexist(self, configfiles[0]): - ee_db_name = (EEFileUtils.grep(self, configfiles[0], - 'DB_NAME').split(',')[1] - .split(')')[0].strip().replace('\'', '')) - ee_db_user = (EEFileUtils.grep(self, configfiles[0], - 'DB_USER').split(',')[1] - .split(')')[0].strip().replace('\'', '')) - ee_db_pass = (EEFileUtils.grep(self, configfiles[0], - 'DB_PASSWORD').split(',')[1] - .split(')')[0].strip().replace('\'', '')) - ee_db_host = (EEFileUtils.grep(self, configfiles[0], - 'DB_HOST').split(',')[1] - .split(')')[0].strip().replace('\'', '')) - try: - Log.debug(self, "dropping database `{0}`".format(ee_db_name)) - EEMysql.execute(self, - "drop database `{0}`".format(ee_db_name), - errormsg='Unable to drop database {0}' - .format(ee_db_name)) - if ee_db_user != 'root': - Log.debug(self, "dropping user `{0}`".format(ee_db_user)) - EEMysql.execute(self, - "drop user `{0}`@`{1}`" - .format(ee_db_user, ee_db_host)) - EEMysql.execute(self, - "flush privileges") - except Exception as e: - Log.error(self, "Error occured while deleting database") - - @expose(hide=True) - def deleteWebRoot(self, webroot): - EEFileUtils.rm(self, webroot) + Log.info(self, "Deleted site {0}".format(ee_domain)) + # else: + # Log.error(self, " site {0} does not exists".format(ee_domain)) class EESiteListController(CementBaseController): diff --git a/ee/cli/plugins/site_functions.py b/ee/cli/plugins/site_functions.py index 5ab34e44..2ec7edf5 100644 --- a/ee/cli/plugins/site_functions.py +++ b/ee/cli/plugins/site_functions.py @@ -1,10 +1,14 @@ 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 +from ee.core.mysql import * +from ee.core.shellexec import * from ee.core.variables import EEVariables +from ee.cli.plugins.sitedb import * from ee.core.aptget import EEAptGet +from ee.core.git import EEGit from ee.core.logging import Log +import subprocess +from subprocess import CalledProcessError import os import random import string @@ -14,10 +18,32 @@ import glob import re +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 check_domain_exists(self, domain): + if getSiteInfo(self, domain): + return True + else: + return False + + def setupdomain(self, data): ee_domain_name = data['site_name'] ee_site_webroot = data['webroot'] + + # Check if nginx configuration already exists + # if os.path.isfile('/etc/nginx/sites-available/{0}' + # .format(ee_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: @@ -30,11 +56,24 @@ def setupdomain(self, data): ee_site_nginx_conf.close() except IOError as e: Log.debug(self, "{0}".format(e)) - Log.error(self, "\nUnable to create NGINX configuration") + raise SiteError("create nginx configuration failed for site") except Exception as e: Log.debug(self, "{0}".format(e)) - Log.error(self, "\nUnable to create NGINX configuration") - Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]") + 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 ...") + FNULL = open('/dev/null', 'w') + ret = subprocess.check_call(["nginx", "-t"], stdout=FNULL, + stderr=subprocess.STDOUT) + 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 EEFileUtils.create_symlink(self, ['/etc/nginx/sites-available/{0}' @@ -49,19 +88,26 @@ def setupdomain(self, data): 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)) + + 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)]) except Exception as e: 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)]) - Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]") + raise SiteError("setup webroot failed for site") + finally: + # TODO Check if directories are setup + if (os.path.exists('{0}/htdocs'.format(ee_site_webroot)) and + os.path.exists('{0}/logs'.format(ee_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): @@ -82,7 +128,7 @@ def setupdatabase(self, data): .format(ee_replace_dot)) except EOFError as e: Log.debug(self, "{0}".format(e)) - Log.error(self, "Unable to input database name") + raise SiteError("Unable to input database name") if not ee_db_name: ee_db_name = ee_replace_dot @@ -96,7 +142,7 @@ def setupdatabase(self, data): .format(ee_random)) except EOFError as e: Log.debug(self, "{0}".format(e)) - Log.error(self, "Unable to input database credentials") + raise SiteError("Unable to input database credentials") if not ee_db_username: ee_db_username = ee_replace_dot @@ -106,31 +152,49 @@ def setupdatabase(self, data): if len(ee_db_username) > 16: Log.debug(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_username = (ee_db_name[0:6] + ee_random10) + ee_db_username = (ee_db_name[0:6] + generate_random()) + # create MySQL database Log.info(self, "Setting up database\t\t", end='') - Log.debug(self, "Creating databse {0}".format(ee_db_name)) - EEMysql.execute(self, "create database `{0}`" - .format(ee_db_name), errormsg="Cannot create database") + Log.debug(self, "Creating database {0}".format(ee_db_name)) + try: + if EEMysql.check_db_exists(self, ee_db_name): + Log.debug(self, "Database already exists, Updating DB_NAME .. ") + ee_db_name = (ee_db_name[0:6] + generate_random()) + ee_db_username = (ee_db_name[0:6] + generate_random()) + except MySQLConnectionError as e: + raise SiteError("MySQL Connectivity problem occured") + try: + EEMysql.execute(self, "create database `{0}`" + .format(ee_db_name)) + except StatementExcecutionError as e: + 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(ee_db_username)) Log.debug(self, "create user `{0}`@`{1}` identified by ''" .format(ee_db_username, ee_mysql_grant_host)) - EEMysql.execute(self, - "create user `{0}`@`{1}` identified by '{2}'" - .format(ee_db_username, ee_mysql_grant_host, - ee_db_password), - errormsg="Cannot setup database user", log=False) + try: + EEMysql.execute(self, + "create user `{0}`@`{1}` identified by '{2}'" + .format(ee_db_username, ee_mysql_grant_host, + ee_db_password), log=False) + except StatementExcecutionError as e: + 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") - EEMysql.execute(self, - "grant all privileges on `{0}`.* to `{1}`@`{2}`" - .format(ee_db_name, ee_db_username, ee_mysql_grant_host), - errormsg="Cannot setup database user privileges") + try: + EEMysql.execute(self, + "grant all privileges on `{0}`.* to `{1}`@`{2}`" + .format(ee_db_name, + ee_db_username, ee_mysql_grant_host)) + except StatementExcecutionError as e: + 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['ee_db_name'] = ee_db_name @@ -156,7 +220,13 @@ def setupwordpress(self, data): Log.info(self, "Downloading Wordpress \t\t", end='') EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot)) - EEShellExec.cmd_exec(self, "wp --allow-root core download") + try: + EEShellExec.cmd_exec(self, "wp --allow-root core" + " download") + except CommandExecutionError as e: + Log.info(self, "[" + Log.ENDC + Log.FAIL + "Fail" + Log.OKBLUE + "]") + raise SiteError(self, "download wordpress core failed") + Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]") if not (data['ee_db_name'] and data['ee_db_user'] and data['ee_db_pass']): @@ -171,7 +241,7 @@ def setupwordpress(self, data): ) except EOFError as e: Log.debug(self, "{0}".format(e)) - Log.error(self, "Unable to input table prefix") + raise SiteError("input table prefix failed") if not ee_wp_prefix: ee_wp_prefix = 'wp_' @@ -192,20 +262,22 @@ def setupwordpress(self, data): "--extra-php< {1}/{0}.sql" - .format(ee_db_name, backup_path), - errormsg="\nFailed: Backup Database") + try: + EEShellExec.cmd_exec(self, "mysqldump {0} > {1}/{0}.sql" + .format(ee_db_name, backup_path)) + except CommandExecutionError as e: + 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/ee-config.php to backup if data['currsitetype'] in ['mysql']: @@ -464,10 +563,11 @@ def updatewpuserpassword(self, ee_domain, ee_site_webroot): EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot)) # Check if ee_domain is wordpress install - is_wp = EEShellExec.cmd_exec(self, "wp --allow-root core" - " version", - errormsg="{0} : Unable to check if wp install" - .format(ee_domain)) + try: + is_wp = EEShellExec.cmd_exec(self, "wp --allow-root core" + " version") + except CommandExecutionError as e: + raise SiteError("is wordpress site? check command failed ") # Exit if ee_domain is not wordpress install if not is_wp: @@ -482,16 +582,21 @@ def updatewpuserpassword(self, ee_domain, ee_site_webroot): if ee_wp_user == "?": Log.info(self, "Fetching WordPress user list") - EEShellExec.cmd_exec(self, "wp --allow-root user list " - "--fields=user_login | grep -v user_login", - errormsg="Unable to Fetch users list") + try: + EEShellExec.cmd_exec(self, "wp --allow-root user list " + "--fields=user_login | grep -v user_login") + except CommandExecutionError as e: + raise SiteError("fetch wp userlist command failed") if not ee_wp_user: ee_wp_user = 'admin' - is_user_exist = EEShellExec.cmd_exec(self, "wp --allow-root user list " - "--fields=user_login | grep {0}$ " - .format(ee_wp_user)) + try: + is_user_exist = EEShellExec.cmd_exec(self, "wp --allow-root user list " + "--fields=user_login | grep {0}$ " + .format(ee_wp_user)) + except CommandExecutionError as e: + raise SiteError("if wp user exists check command failed") if is_user_exist: try: @@ -500,16 +605,18 @@ def updatewpuserpassword(self, ee_domain, ee_site_webroot): .format(ee_wp_user)) except Exception as e: Log.debug(self, "{0}".format(e)) - Log.error(self, "Could not update password") + raise SiteError("failed to read password input ") - if len(ee_wp_pass) > 8: + try: EEShellExec.cmd_exec(self, "wp --allow-root user update {0}" " --user_pass={1}" .format(ee_wp_user, ee_wp_pass)) - Log.info(self, "Password updated successfully") - else: - Log.error(self, "Password Unchanged. Hint : Your password must be " - "8 characters long") + except CommandExecutionError as e: + raise SiteError("wp user password update command failed") + Log.info(self, "Password updated successfully") + if len(ee_wp_pass) < 8: + Log.warn(self, "Warning: You have provided a " + "weak password") else: Log.error(self, "Invalid WordPress user {0} for {1}." .format(ee_wp_user, ee_domain)) @@ -579,3 +686,125 @@ def logwatch(self, logfiles): l = logwatch.LogWatcher(logfiles, callback) l.loop() + + +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']: + typelist.append(key) + elif val and key in ['wpfc', 'wpsc', 'w3tc']: + cachelist.append(key) + + if len(typelist) > 1 or len(cachelist) > 1: + raise RuntimeError("could not determine site and cache type") + else: + if not typelist and not cachelist: + sitetype = 'html' + cachetype = 'basic' + elif (not typelist) and cachelist: + sitetype = 'wp' + cachetype = cachelist[0] + elif typelist and (not cachelist): + sitetype = typelist[0] + cachetype = 'basic' + else: + sitetype = typelist[0] + cachetype = cachelist[0] + return (sitetype, cachetype) + + +def generate_random(): + ee_random10 = (''.join(random.sample(string.ascii_uppercase + + string.ascii_lowercase + string.digits, 10))) + return ee_random10 + + +def deleteDB(self, dbname, dbuser, dbhost): + try: + # Check if Database exists + try: + if EEMysql.check_db_exists(self, dbname): + # Drop database if exists + Log.debug(self, "dropping database `{0}`".format(dbname)) + EEMysql.execute(self, + "drop database `{0}`".format(dbname), + errormsg='Unable to drop database {0}' + .format(dbname)) + except StatementExcecutionError as e: + Log.debug(self, "drop database failed") + Log.info(self, "Database {0} not dropped".format(dbname)) + + except MySQLConnectionError as e: + Log.debug(self, "Mysql Connection problem occured") + + if dbuser != 'root': + Log.debug(self, "dropping user `{0}`".format(dbuser)) + try: + EEMysql.execute(self, + "drop user `{0}`@`{1}`" + .format(dbuser, dbhost)) + except StatementExcecutionError as e: + Log.debug(self, "drop database user failed") + Log.info(self, "Database {0} not dropped".format(dbuser)) + try: + EEMysql.execute(self, "flush privileges") + except StatementExcecutionError as e: + Log.debug(self, "drop database failed") + Log.info(self, "Database {0} not dropped".format(dbname)) + except Exception as e: + Log.error(self, "Error occured while deleting database") + + +def deleteWebRoot(self, webroot): + if os.path.isdir(webroot): + Log.debug(self, "Removing {0}".format(webroot)) + EEFileUtils.rm(self, webroot) + return True + else: + 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") + EEFileUtils.rm(self, '/etc/nginx/sites-enabled/{0}' + .format(domain)) + EEFileUtils.rm(self, '/etc/nginx/sites-available/{0}' + .format(domain)) + EEGit.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) + if webroot: + deleteWebRoot(self, webroot) + + if dbname: + if not dbuser: + raise SiteError("dbuser not provided") + if not dbhost: + raise SiteError("dbhost not provided") + deleteDB(self, dbname, dbuser, dbhost) diff --git a/ee/cli/plugins/sitedb.py b/ee/cli/plugins/sitedb.py index 805b72bc..fe1d3d98 100644 --- a/ee/cli/plugins/sitedb.py +++ b/ee/cli/plugins/sitedb.py @@ -4,17 +4,22 @@ from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declarative_base from ee.core.logging import Log from ee.core.database import db_session -from ee.core.models import SiteDB +from ee.core.fileutils import EEFileUtils +from ee.cli.plugins.models import SiteDB import sys +import glob def addNewSite(self, site, stype, cache, path, - enabled=True, ssl=False, fs='ext4', db='mysql'): + enabled=True, ssl=False, fs='ext4', db='mysql', + db_name=None, db_user=None, db_password=None, + db_host='localhost'): """ Add New Site record information into ee database. """ try: - newRec = SiteDB(site, stype, cache, path, enabled, ssl, fs, db) + newRec = SiteDB(site, stype, cache, path, enabled, ssl, fs, db, + db_name, db_user, db_password, db_host) db_session.add(newRec) db_session.commit() except Exception as e: @@ -34,8 +39,9 @@ def getSiteInfo(self, site): Log.error(self, "Unable to query database for site info") -def updateSiteInfo(self, site, stype='', cache='', - enabled=True, ssl=False, fs='', db=''): +def updateSiteInfo(self, site, stype='', cache='', webroot='', + enabled=True, ssl=False, fs='', db='', db_name=None, + db_user=None, db_password=None, db_host=None): """updates site record in database""" try: q = SiteDB.query.filter(SiteDB.sitename == site).first() @@ -59,6 +65,21 @@ def updateSiteInfo(self, site, stype='', cache='', if ssl and q.is_ssl != ssl: q.is_ssl = ssl + if db_name and q.db_name != db_name: + q.db_name = db_name + + if db_user and q.db_user != db_user: + q.db_user = db_user + + if db_user and q.db_password != db_password: + q.db_password = db_password + + if db_host and q.db_host != db_host: + q.db_host = db_host + + if webroot and q.site_path != webroot: + q.site_path = webroot + try: q.created_on = func.now() db_session.commit() @@ -87,10 +108,56 @@ def deleteSiteInfo(self, site): def getAllsites(self): - + """ + 1. returns all records from ee database + """ try: q = SiteDB.query.all() return q except Exception as e: Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to query database") + + +class EESync(object): + + def __init__(self, app): + self.app = app + + def syncdbinfo(self): + """ + 1. reads database information from wp/ee-config.php + 2. updates records into ee database accordingly. + """ + sites = getAllsites(self) + if not sites: + pass + for site in sites: + if site.site_type in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']: + ee_site_webroot = site.site_path + # Read config files + configfiles = glob.glob(ee_site_webroot + '/*-config.php') + if configfiles: + if EEFileUtils.isexist(self, configfiles[0]): + ee_db_name = (EEFileUtils.grep(self, configfiles[0], + 'DB_NAME').split(',')[1] + .split(')')[0].strip().replace('\'', '')) + ee_db_user = (EEFileUtils.grep(self, configfiles[0], + 'DB_USER').split(',')[1] + .split(')')[0].strip().replace('\'', '')) + ee_db_pass = (EEFileUtils.grep(self, configfiles[0], + 'DB_PASSWORD').split(',')[1] + .split(')')[0].strip().replace('\'', '')) + ee_db_host = (EEFileUtils.grep(self, configfiles[0], + 'DB_HOST').split(',')[1] + .split(')')[0].strip().replace('\'', '')) + + if site.db_name != ee_db_name: + # update records if any mismatch found + Log.debug(self, "Updating {0}" + .format(site.sitename)) + updateSiteInfo(self, site.sitename, + db_name=ee_db_name, + db_user=ee_db_user, + db_password=ee_db_pass, + db_host=ee_db_host) diff --git a/ee/cli/plugins/stack.py b/ee/cli/plugins/stack.py index b2bfb952..6c104ddf 100644 --- a/ee/cli/plugins/stack.py +++ b/ee/cli/plugins/stack.py @@ -69,6 +69,7 @@ class EEStackController(CementBaseController): (['--utils'], dict(help='Install Utils stack', action='store_true')), ] + usage = "ee stack (command) [options]" @expose(hide=True) def default(self): @@ -80,11 +81,16 @@ class EEStackController(CementBaseController): """Pre settings to do before installation packages""" if set(EEVariables.ee_postfix).issubset(set(apt_packages)): Log.debug(self, "Pre-seeding Postfix") - EEShellExec.cmd_exec(self, "echo \"postfix postfix" - "/main_mailer_type string \'Internet Site\'\"" - " | debconf-set-selections") - EEShellExec.cmd_exec(self, "echo \"postfix postfix/mailname string" - " $(hostname -f)\" | debconf-set-selections") + try: + EEShellExec.cmd_exec(self, "echo \"postfix postfix" + "/main_mailer_type string \'Internet Site" + "\'\"" + " | debconf-set-selections") + EEShellExec.cmd_exec(self, "echo \"postfix postfix/mailname" + " string $(hostname -f)\" | " + "debconf-set-selections") + except CommandExecutionError as e: + Log.error(self, "Failed to intialize postfix package") if set(EEVariables.ee_mysql).issubset(set(apt_packages)): Log.info(self, "Adding repository for MySQL, please wait ...") @@ -104,20 +110,30 @@ class EEStackController(CementBaseController): "mysql-server/root_password " "password \" | " "debconf-set-selections") - EEShellExec.cmd_exec(self, "echo \"mariadb-server-10.0 " - "mysql-server/root_password " - "password {chars}\" | " - "debconf-set-selections".format(chars=chars), - log=False) + try: + EEShellExec.cmd_exec(self, "echo \"mariadb-server-10.0 " + "mysql-server/root_password " + "password {chars}\" | " + "debconf-set-selections" + .format(chars=chars), + log=False) + except CommandExecutionError as e: + Log.error("Failed to initialize MySQL package") + Log.debug(self, "echo \"mariadb-server-10.0 " "mysql-server/root_password_again " "password \" | " "debconf-set-selections") - EEShellExec.cmd_exec(self, "echo \"mariadb-server-10.0 " - "mysql-server/root_password_again " - "password {chars}\" | " - "debconf-set-selections".format(chars=chars), - log=False) + try: + EEShellExec.cmd_exec(self, "echo \"mariadb-server-10.0 " + "mysql-server/root_password_again " + "password {chars}\" | " + "debconf-set-selections" + .format(chars=chars), + log=False) + except CommandExecutionError as e: + Log.error("Failed to initialize MySQL package") + mysql_config = """ [client] user = root @@ -151,17 +167,16 @@ class EEStackController(CementBaseController): EERepo.add(self, ppa=EEVariables.ee_php_repo) if set(EEVariables.ee_mail).issubset(set(apt_packages)): - if EEVariables.ee_platform_codename == 'squeeze': - Log.info(self, "Adding repository for dovecot, " - "please wait ...") - EERepo.add(self, repo_url=EEVariables.ee_dovecot_repo) Log.debug(self, 'Executing the command debconf-set-selections.') - EEShellExec.cmd_exec(self, "echo \"dovecot-core dovecot-core/" - "create-ssl-cert boolean yes\" " - "| debconf-set-selections") - EEShellExec.cmd_exec(self, "echo \"dovecot-core dovecot-core" - "/ssl-cert-name string $(hostname -f)\"" - " | debconf-set-selections") + try: + EEShellExec.cmd_exec(self, "echo \"dovecot-core dovecot-core/" + "create-ssl-cert boolean yes\" " + "| debconf-set-selections") + EEShellExec.cmd_exec(self, "echo \"dovecot-core dovecot-core" + "/ssl-cert-name string $(hostname -f)\"" + " | debconf-set-selections") + except CommandExecutionError as e: + Log.error("Failed to initialize dovecot packages") @expose(hide=True) def post_pref(self, apt_packages, packages): @@ -173,7 +188,7 @@ class EEStackController(CementBaseController): EEService.reload_service(self, 'postfix') if set(EEVariables.ee_nginx).issubset(set(apt_packages)): - if ((not EEShellExec.cmd_exec(self, "grep -Hr EasyEngine " + if ((not EEShellExec.cmd_exec(self, "grep EasyEngine " "/etc/nginx")) and os.path.isfile('/etc/nginx/nginx.conf')): nc = NginxConfig() Log.debug(self, 'Loading file /etc/nginx/nginx.conf ') @@ -306,11 +321,15 @@ class EEStackController(CementBaseController): passwd = ''.join([random.choice (string.ascii_letters + string.digits) for n in range(6)]) - EEShellExec.cmd_exec(self, "printf \"easyengine:" - "$(openssl passwd -crypt " - "{password} 2> /dev/null)\n\"" - "> /etc/nginx/htpasswd-ee 2>/dev/null" - .format(password=passwd)) + try: + EEShellExec.cmd_exec(self, "printf \"easyengine:" + "$(openssl passwd -crypt " + "{password} 2> /dev/null)\n\"" + "> /etc/nginx/htpasswd-ee " + "2>/dev/null" + .format(password=passwd)) + except CommandExecutionError as e: + Log.error(self, "Failed to save HTTP Auth") # Create Symbolic link for 22222 EEFileUtils.create_symlink(self, ['/etc/nginx/' @@ -350,40 +369,46 @@ class EEStackController(CementBaseController): .format(EEVariables.ee_webroot)] ) - EEShellExec.cmd_exec(self, "openssl genrsa -out " - "{0}22222/cert/22222.key 2048" - .format(EEVariables.ee_webroot)) - EEShellExec.cmd_exec(self, "openssl req -new -batch -subj " - "/commonName=127.0.0.1/ -key " - "{0}22222/cert/22222.key " - "-out {0}22222/cert/" - "22222.csr" - .format(EEVariables.ee_webroot)) - - EEFileUtils.mvfile(self, "{0}22222/cert/22222.key" - .format(EEVariables.ee_webroot), + try: + EEShellExec.cmd_exec(self, "openssl genrsa -out " + "{0}22222/cert/22222.key 2048" + .format(EEVariables.ee_webroot)) + EEShellExec.cmd_exec(self, "openssl req -new -batch " + "-subj /commonName=127.0.0.1/ " + "-key {0}22222/cert/22222.key " + "-out {0}22222/cert/" + "22222.csr" + .format(EEVariables.ee_webroot)) + + EEFileUtils.mvfile(self, "{0}22222/cert/22222.key" + .format(EEVariables.ee_webroot), + "{0}22222/cert/" + "22222.key.org" + .format(EEVariables.ee_webroot)) + + EEShellExec.cmd_exec(self, "openssl rsa -in " "{0}22222/cert/" - "22222.key.org" - .format(EEVariables.ee_webroot)) + "22222.key.org -out " + "{0}22222/cert/22222.key" + .format(EEVariables.ee_webroot)) - EEShellExec.cmd_exec(self, "openssl rsa -in " - "{0}22222/cert/" - "22222.key.org -out " - "{0}22222/cert/22222.key" - .format(EEVariables.ee_webroot)) + EEShellExec.cmd_exec(self, "openssl x509 -req -days " + "3652 -in {0}22222/cert/" + "22222.csr -signkey {0}" + "22222/cert/22222.key -out " + "{0}22222/cert/22222.crt" + .format(EEVariables.ee_webroot)) + + except CommandExecutionError as e: + Log.error(self, "Failed to generate SSL for 22222") - EEShellExec.cmd_exec(self, "openssl x509 -req -days 3652 " - "-in {0}22222/cert/" - "22222.csr -signkey {0}" - "22222/cert/22222.key -out " - "{0}22222/cert/22222.crt" - .format(EEVariables.ee_webroot)) # Nginx Configation into GIT EEGit.add(self, ["/etc/nginx"], msg="Adding Nginx into Git") EEService.reload_service(self, 'nginx') self.msg = (self.msg + ["HTTP Auth User Name: easyengine"] + ["HTTP Auth Password : {0}".format(passwd)]) + if set(EEVariables.ee_php).issubset(set(apt_packages)): # Create log directories if not os.path.exists('/var/log/php5/'): @@ -513,37 +538,49 @@ class EEStackController(CementBaseController): # config.write(configfile) if not os.path.isfile("/etc/mysql/my.cnf"): config = ("[mysqld]\nwait_timeout = 30\n" - "interactive_timeout=60\nperformance_schema = 0") + "interactive_timeout=60\nperformance_schema = 0" + "\nquery_cache_type = 1") config_file = open("/etc/mysql/my.cnf", encoding='utf-8', mode='w') config_file.write(config) config_file.close() else: - EEShellExec.cmd_exec(self, "sed -i \"/#max_connections/a " - "wait_timeout = 30 \\n" - "interactive_timeout = 60 \\n" - "performance_schema = 0\" " - "/etc/mysql/my.cnf") + try: + EEShellExec.cmd_exec(self, "sed -i \"/#max_conn" + "ections/a wait_timeout = 30 \\n" + "interactive_timeout = 60 \\n" + "performance_schema = 0\\n" + "query_cache_type = 1 \" " + "/etc/mysql/my.cnf") + except CommandExecutionError as e: + Log.error(self, "Unable to update MySQL file") EEGit.add(self, ["/etc/mysql"], msg="Adding MySQL into Git") EEService.reload_service(self, 'mysql') if set(EEVariables.ee_mail).issubset(set(apt_packages)): Log.debug(self, "Adding user") - EEShellExec.cmd_exec(self, "adduser --uid 5000 --home /var" - "/vmail --disabled-password --gecos ''" - " vmail") - EEShellExec.cmd_exec(self, "openssl req -new -x509 -days 3650 " - "-nodes -subj /commonName={HOSTNAME}" - "/emailAddress={EMAIL} -out /etc/ssl" - "/certs/dovecot." - "pem -keyout /etc/ssl/private/dovecot.pem" - .format(HOSTNAME=EEVariables.ee_fqdn, - EMAIL=EEVariables.ee_email)) + try: + EEShellExec.cmd_exec(self, "adduser --uid 5000 --home /var" + "/vmail --disabled-password --gecos " + "'' vmail") + except CommandExecutionError as e: + Log.error(self, "Unable to add vmail user for mail server") + try: + EEShellExec.cmd_exec(self, "openssl req -new -x509 -days" + " 3650 " + "-nodes -subj /commonName={hostname}" + "/emailAddress={email} -out /etc/ssl" + "/certs/dovecot." + "pem -keyout " + "/etc/ssl/private/dovecot.pem" + .format(hostname=EEVariables.ee_fqdn, + email=EEVariables.ee_email)) + except CommandExecutionError as e: + Log.error(self, "Unable to generate PEM key for dovecot") Log.debug(self, "Setting Privileges to " "/etc/ssl/private/dovecot.pem file ") - EEShellExec.cmd_exec(self, "chmod 0600 /etc/ssl/private" - "/dovecot.pem") + EEFileUtils.chmod(self, "/etc/ssl/private/dovecot.pem", 0o600) # Custom Dovecot configuration by EasyEngine data = dict() @@ -562,82 +599,95 @@ class EEStackController(CementBaseController): encoding='utf-8', mode='w') self.app.render((data), 'dovecot.mustache', out=ee_dovecot) ee_dovecot.close() - - EEShellExec.cmd_exec(self, "sed -i \"s/\\!include " - "auth-system.conf.ext/#\\!include " - "auth-system.conf.ext/\" " - "/etc/dovecot/conf.d/10-auth.conf") - - EEShellExec.cmd_exec(self, "sed -i \"s\'/etc/dovecot/" - "dovecot.pem\'/etc/ssl/certs/dovecot.pem" - "\'\" /etc/dovecot/conf.d/10-ssl.conf") - EEShellExec.cmd_exec(self, "sed -i \"s\'/etc/dovecot/" - "private/dovecot.pem\'/etc/ssl/private" - "/dovecot.pem\'\" /etc/dovecot/conf.d/" - "10-ssl.conf") - - # Custom Postfix configuration needed with Dovecot - # Changes in master.cf - # TODO: Find alternative for sed in Python - EEShellExec.cmd_exec(self, "sed -i \'s/#submission/submission" - "/\'" - " /etc/postfix/master.cf") - EEShellExec.cmd_exec(self, "sed -i \'s/#smtps/smtps/\'" - " /etc/postfix/master.cf") - - EEShellExec.cmd_exec(self, "postconf -e \"smtpd_sasl_type = " - "dovecot\"") - EEShellExec.cmd_exec(self, "postconf -e \"smtpd_sasl_path = " - "private/auth\"") - EEShellExec.cmd_exec(self, "postconf -e \"" - "smtpd_sasl_auth_enable = " - "yes\"") - EEShellExec.cmd_exec(self, "postconf -e \"" - " smtpd_relay_restrictions =" - " permit_sasl_authenticated, " - " permit_mynetworks, " - " reject_unauth_destination\"") - - EEShellExec.cmd_exec(self, "postconf -e \"" - "smtpd_tls_mandatory_" - "protocols = !SSLv2,!SSLv3\"") - EEShellExec.cmd_exec(self, "postconf -e \"smtp_tls_mandatory_" - "protocols = !SSLv2,!SSLv3\"") - EEShellExec.cmd_exec(self, "postconf -e \"smtpd_tls_protocols " - " = !SSLv2,!SSLv3\"") - EEShellExec.cmd_exec(self, "postconf -e \"smtp_tls_protocols " - "= !SSLv2,!SSLv3\"") - EEShellExec.cmd_exec(self, "postconf -e \"mydestination " - "= localhost\"") - EEShellExec.cmd_exec(self, "postconf -e \"virtual_transport " - "= lmtp:unix:private/dovecot-lmtp\"") - EEShellExec.cmd_exec(self, "postconf -e \"virtual_uid_maps " - "= static:5000\"") - EEShellExec.cmd_exec(self, "postconf -e \"virtual_gid_maps " - "= static:5000\"") - EEShellExec.cmd_exec(self, "postconf -e \"" - " virtual_mailbox_domains = " - "mysql:/etc/postfix/mysql/virtual_" - "domains_maps.cf\"") - EEShellExec.cmd_exec(self, "postconf -e \"virtual_mailbox_maps" - " = mysql:/etc/postfix/mysql/virtual_" - "mailbox_maps.cf\"") - EEShellExec.cmd_exec(self, "postconf -e \"virtual_alias_maps " - "= mysql:/etc/postfix/mysql/virtual_" - "alias_maps.cf\"") - EEShellExec.cmd_exec(self, "openssl req -new -x509 -days " - " 3650 -nodes -subj /commonName=" - "{HOSTNAME}/emailAddress={EMAIL}" - " -out /etc/ssl/certs/postfix.pem" - " -keyout /etc/ssl/private/postfix.pem" - .format(HOSTNAME=EEVariables.ee_fqdn, - EMAIL=EEVariables.ee_email)) - EEShellExec.cmd_exec(self, "chmod 0600 /etc/ssl/private" - "/postfix.pem") - EEShellExec.cmd_exec(self, "postconf -e \"smtpd_tls_cert_file " - "= /etc/ssl/certs/postfix.pem\"") - EEShellExec.cmd_exec(self, "postconf -e \"smtpd_tls_key_file " - "= /etc/ssl/private/postfix.pem\"") + try: + EEShellExec.cmd_exec(self, "sed -i \"s/\\!include " + "auth-system.conf.ext/#\\!include " + "auth-system.conf.ext/\" " + "/etc/dovecot/conf.d/10-auth.conf") + + EEShellExec.cmd_exec(self, "sed -i \"s\'/etc/dovecot/" + "dovecot.pem\'/etc/ssl/certs/" + "dovecot.pem" + "\'\" /etc/dovecot/conf.d/" + "10-ssl.conf") + EEShellExec.cmd_exec(self, "sed -i \"s\'/etc/dovecot/" + "private/dovecot.pem\'/etc/ssl/" + "private" + "/dovecot.pem\'\" /etc/dovecot/" + "conf.d/" + "10-ssl.conf") + + # Custom Postfix configuration needed with Dovecot + # Changes in master.cf + # TODO: Find alternative for sed in Python + EEShellExec.cmd_exec(self, "sed -i \'s/#submission/" + "submission/\'" + " /etc/postfix/master.cf") + EEShellExec.cmd_exec(self, "sed -i \'s/#smtps/smtps/\'" + " /etc/postfix/master.cf") + + EEShellExec.cmd_exec(self, "postconf -e \"smtpd_sasl_type " + "= dovecot\"") + EEShellExec.cmd_exec(self, "postconf -e \"smtpd_sasl_path " + "= private/auth\"") + EEShellExec.cmd_exec(self, "postconf -e \"" + "smtpd_sasl_auth_enable = " + "yes\"") + EEShellExec.cmd_exec(self, "postconf -e \"" + " smtpd_relay_restrictions =" + " permit_sasl_authenticated, " + " permit_mynetworks, " + " reject_unauth_destination\"") + + EEShellExec.cmd_exec(self, "postconf -e \"" + "smtpd_tls_mandatory_" + "protocols = !SSLv2,!SSLv3\"") + EEShellExec.cmd_exec(self, "postconf -e \"smtp_tls_" + "mandatory_protocols = !SSLv2," + "!SSLv3\"") + EEShellExec.cmd_exec(self, "postconf -e \"smtpd_tls" + "_protocols = !SSLv2,!SSLv3\"") + EEShellExec.cmd_exec(self, "postconf -e \"smtp_tls" + "_protocols = !SSLv2,!SSLv3\"") + EEShellExec.cmd_exec(self, "postconf -e \"mydestination " + "= localhost\"") + EEShellExec.cmd_exec(self, "postconf -e \"virtual" + "_transport " + "= lmtp:unix:private/dovecot-lmtp\"") + EEShellExec.cmd_exec(self, "postconf -e \"virtual_uid_" + "maps = static:5000\"") + EEShellExec.cmd_exec(self, "postconf -e \"virtual_gid_" + "maps = static:5000\"") + EEShellExec.cmd_exec(self, "postconf -e \"" + " virtual_mailbox_domains = " + "mysql:/etc/postfix/mysql/virtual_" + "domains_maps.cf\"") + EEShellExec.cmd_exec(self, "postconf -e \"virtual_mailbox" + "_maps" + " = mysql:/etc/postfix/mysql/virtual_" + "mailbox_maps.cf\"") + EEShellExec.cmd_exec(self, "postconf -e \"virtual_alias" + "_maps " + "= mysql:/etc/postfix/mysql/virtual_" + "alias_maps.cf\"") + EEShellExec.cmd_exec(self, "openssl req -new -x509 -days " + " 3650 -nodes -subj /commonName=" + "{hostname}/emailAddress={email}" + " -out /etc/ssl/certs/postfix.pem" + " -keyout /etc/ssl/private/" + "postfix.pem" + .format(hostname=EEVariables.ee_fqdn, + email=EEVariables.ee_email)) + EEShellExec.cmd_exec(self, "chmod 0600 /etc/ssl/private" + "/postfix.pem") + EEShellExec.cmd_exec(self, "postconf -e \"smtpd_tls_cert_" + "file = /etc/ssl/certs/postfix.pem\"") + EEShellExec.cmd_exec(self, "postconf -e \"smtpd_tls_key_" + "file = /etc/ssl/private/" + "postfix.pem\"") + + except CommandExecutionError as e: + Log.Error(self, "Failed to update Dovecot configuration") # Sieve configuration if not os.path.exists('/var/lib/dovecot/sieve/'): @@ -661,8 +711,12 @@ class EEStackController(CementBaseController): # "/dovecot") EEFileUtils.chown(self, "/var/lib/dovecot", 'vmail', 'vmail', recursive=True) - EEShellExec.cmd_exec(self, "sievec /var/lib/dovecot/sieve/" - "default.sieve") + try: + EEShellExec.cmd_exec(self, "sievec /var/lib/dovecot/" + "/sieve/default.sieve") + except CommandExecutionError as e: + raise SiteError("Failed to compile default.sieve") + EEGit.add(self, ["/etc/postfix", "/etc/dovecot"], msg="Installed mail server") EEService.restart_service(self, 'dovecot') @@ -695,13 +749,17 @@ class EEStackController(CementBaseController): vm_config.close() # Amavis postfix configuration - EEShellExec.cmd_exec(self, "postconf -e \"content_filter = " - "smtp-amavis:[127.0.0.1]:10024\"") - EEShellExec.cmd_exec(self, "sed -i \"s/1 pickup/1 " - "pickup" - "\\n -o content_filter=\\n -o" - " receive_override_options=no_header_body" - "_checks/\" /etc/postfix/master.cf") + try: + EEShellExec.cmd_exec(self, "postconf -e \"content_filter =" + " smtp-amavis:[127.0.0.1]:10024\"") + EEShellExec.cmd_exec(self, "sed -i \"s/1 pickup/1 " + " pickup" + "\\n -o content_filter=\\n " + " -o receive_override_options=" + "no_header_body" + "_checks/\" /etc/postfix/master.cf") + except CommandExecutionError as e: + raise SiteError("Failed to update Amavis-Postfix config") amavis_master = ("""smtp-amavis unix - - n - 2 smtp -o smtp_data_done_timeout=1200 @@ -730,19 +788,22 @@ class EEStackController(CementBaseController): encoding='utf-8', mode='a') as am_config: am_config.write(amavis_master) - # Amavis ClamAV configuration - Log.debug(self, "Adding new user clamav amavis") - EEShellExec.cmd_exec(self, "adduser clamav amavis") - Log.debug(self, "Adding new user amavis clamav") - EEShellExec.cmd_exec(self, "adduser amavis clamav") - Log.debug(self, "Setting Privileges to /var/lib/amavis/tmp ") - EEShellExec.cmd_exec(self, "chmod -R 775 /var/lib/amavis/tmp") - - # Update ClamAV database - Log.debug(self, "Updating database") - EEShellExec.cmd_exec(self, "freshclam") - Log.debug(self, "Restarting clamav-daemon service") - EEShellExec.cmd_exec(self, "service clamav-daemon restart") + try: + # Amavis ClamAV configuration + Log.debug(self, "Adding new user clamav amavis") + EEShellExec.cmd_exec(self, "adduser clamav amavis") + Log.debug(self, "Adding new user amavis clamav") + EEShellExec.cmd_exec(self, "adduser amavis clamav") + Log.debug(self, "Setting Privileges to /var/lib/amavis" + "/tmp") + EEFileUtils.chmod(self, "/var/lib/amavis/tmp", 0o755) + + # Update ClamAV database + Log.debug(self, "Updating database") + EEShellExec.cmd_exec(self, "freshclam") + except CommandExecutionError as e: + raise SiteError(" Unable to update ClamAV-Amavis config") + EEGit.add(self, ["/etc/amavis"], msg="Adding Amavis into Git") EEService.restart_service(self, 'dovecot') EEService.reload_service(self, 'postfix') @@ -751,7 +812,8 @@ class EEStackController(CementBaseController): if len(packages): if any('/usr/bin/wp' == x[1] for x in packages): Log.debug(self, "Setting Privileges to /usr/bin/wp file ") - EEShellExec.cmd_exec(self, "chmod +x /usr/bin/wp") + EEFileUtils.chmod(self, "/usr/bin/wp", 0o775) + if any('/tmp/pma.tar.gz' == x[1] for x in packages): EEExtract.extract(self, '/tmp/pma.tar.gz', '/tmp/') @@ -834,9 +896,13 @@ class EEStackController(CementBaseController): '{0}22222/htdocs/db/anemometer' .format(EEVariables.ee_webroot)) chars = ''.join(random.sample(string.ascii_letters, 8)) - EEShellExec.cmd_exec(self, 'mysql < {0}22222/htdocs/db' - '/anemometer/install.sql' - .format(EEVariables.ee_webroot)) + try: + EEShellExec.cmd_exec(self, 'mysql < {0}22222/htdocs/db' + '/anemometer/install.sql' + .format(EEVariables.ee_webroot)) + except CommandExecutionError as e: + raise SiteError("Unable to import Anemometer database") + EEMysql.execute(self, 'grant select on *.* to \'anemometer\'' '@\'{0}\''.format(self.app.config.get('mysql', 'grant-host'))) @@ -863,8 +929,7 @@ class EEStackController(CementBaseController): if any('/usr/bin/pt-query-advisor' == x[1] for x in packages): - EEShellExec.cmd_exec(self, "chmod +x /usr/bin/pt-query" - "-advisor") + EEFileUtils.chmod(self, "/usr/bin/pt-query-advisor", 0o775) if any('/tmp/vimbadmin.tar.gz' == x[1] for x in packages): # Extract ViMbAdmin @@ -886,18 +951,23 @@ class EEStackController(CementBaseController): # Donwload composer and install ViMbAdmin Log.debug(self, "Downloading composer " "https://getcomposer.org/installer | php ") - EEShellExec.cmd_exec(self, "cd {0}22222/htdocs" - "/vimbadmin; curl" - " -sS https://getcomposer.org/installer |" - " php".format(EEVariables.ee_webroot)) - Log.debug(self, "installation of composer") - EEShellExec.cmd_exec(self, "cd {0}22222/htdocs" - "/vimbadmin && " - "php composer.phar install --prefer-dist" - " --no-dev && rm -f {1}22222/htdocs" - "/vimbadmin/composer.phar" - .format(EEVariables.ee_webroot, - EEVariables.ee_webroot)) + try: + EEShellExec.cmd_exec(self, "cd {0}22222/htdocs" + "/vimbadmin; curl" + " -sS https://getcomposer.org/" + "installer |" + " php".format(EEVariables.ee_webroot)) + Log.debug(self, "Installating of composer") + EEShellExec.cmd_exec(self, "cd {0}22222/htdocs" + "/vimbadmin && " + "php composer.phar install " + "--prefer-dist" + " --no-dev && rm -f {1}22222/htdocs" + "/vimbadmin/composer.phar" + .format(EEVariables.ee_webroot, + EEVariables.ee_webroot)) + except CommandExecutionError as e: + raise SiteError("Failed to setup ViMbAdmin") # Configure vimbadmin database vm_passwd = ''.join(random.sample(string.ascii_letters, 8)) @@ -942,9 +1012,13 @@ class EEStackController(CementBaseController): "{0}22222/htdocs/vimbadmin/bin" "/doctrine2-cli.php orm:schema-tool:" "create".format(EEVariables.ee_webroot)) - EEShellExec.cmd_exec(self, "{0}22222/htdocs/vimbadmin" - "/bin/doctrine2-cli.php orm:schema-tool:" - "create".format(EEVariables.ee_webroot)) + try: + EEShellExec.cmd_exec(self, "{0}22222/htdocs/vimbadmin" + "/bin/doctrine2-cli.php " + "orm:schema-tool:create" + .format(EEVariables.ee_webroot)) + except CommandExecutionError as e: + raise SiteError("Unable to create ViMbAdmin schema") EEFileUtils.chown(self, '{0}22222' .format(EEVariables.ee_webroot), diff --git a/ee/core/database.py b/ee/core/database.py index 573ee099..0b44eac1 100644 --- a/ee/core/database.py +++ b/ee/core/database.py @@ -13,12 +13,16 @@ Base = declarative_base() Base.query = db_session.query_property() -def init_db(): +def init_db(app): """ Initializes and creates all tables from models into the database """ # import all modules here that might define models so that # they will be registered properly on the metadata. Otherwise - # you will have to import them first before calling init_db() - import ee.core.models - Base.metadata.create_all(bind=engine) + # # you will have to import them first before calling init_db() + # import ee.core.models + try: + app.log.info("Initializing EasyEngine Database") + Base.metadata.create_all(bind=engine) + except Exception as e: + app.log.debug("{0}".format(e)) diff --git a/ee/core/logging.py b/ee/core/logging.py index 0cea61d9..b7acb692 100644 --- a/ee/core/logging.py +++ b/ee/core/logging.py @@ -36,6 +36,7 @@ class Log: """ Logs warning into log file """ + print(Log.WARNING + msg + Log.ENDC) self.app.log.warn(Log.BOLD + msg + Log.ENDC) def debug(self, msg): diff --git a/ee/core/mysql.py b/ee/core/mysql.py index 7c523bf1..88e5580a 100644 --- a/ee/core/mysql.py +++ b/ee/core/mysql.py @@ -1,5 +1,6 @@ """EasyEngine MySQL core classes.""" import pymysql +from pymysql import connections, DatabaseError, Error import configparser from os.path import expanduser import sys @@ -8,59 +9,71 @@ from ee.core.logging import Log from ee.core.variables import EEVariables +class MySQLConnectionError(Exception): + """Custom Exception when MySQL server Not Connected""" + pass + + +class StatementExcecutionError(Exception): + """Custom Exception when any Query Fails to execute""" + pass + + +class DatabaseNotExistsError(Exception): + """Custom Exception when Database not Exist""" + pass + + class EEMysql(): """Method for MySQL connection""" + def connect(self): + """Makes connection with MySQL server""" + try: + connection = pymysql.connect(read_default_file='~/.my.cnf') + return connection + except ValueError as e: + Log.debug(self, str(e)) + raise MySQLConnectionError + except pymysql.err.InternalError as e: + Log.debug(self, str(e)) + raise MySQLConnectionError + + def dbConnection(self, db_name): + try: + connection = pymysql.connect(db=db_name, + read_default_file='~/.my.cnf') + return connection + except DatabaseError as e: + if e.args[1] == '#42000Unknown database \'{0}\''.format(db_name): + raise DatabaseNotExistsError + else: + raise MySQLConnectionError + except pymysql.err.InternalError as e: + Log.debug(self, str(e)) + raise MySQLConnectionError + def execute(self, statement, errormsg='', log=True): """Get login details from ~/.my.cnf & Execute MySQL query""" - config = configparser.RawConfigParser() - cnfpath = expanduser("~")+"/.my.cnf" - if [cnfpath] == config.read(cnfpath): - user = config.get('client', 'user') - passwd = config.get('client', 'password') - try: - host = config.get('client', 'host') - except configparser.NoOptionError as e: - host = 'localhost' - - try: - port = config.get('client', 'port') - except configparser.NoOptionError as e: - port = '3306' - - try: - conn = pymysql.connect(host=host, port=int(port), - user=user, passwd=passwd) - cur = conn.cursor() - except Exception as e: - if errormsg: - Log.debug(self, '{0}' - .format(e)) - Log.error(self, '{0}' - .format(errormsg)) - else: - Log.debug(self, '{0}' - .format(e)) - Log.error(self, 'Unable to connect to database: {0}' - .format(e)) - - try: - if log: - Log.debug(self, "Executing MySQL statement: {0}" - .format(statement)) - - cur.execute(statement) - cur.close() - conn.close() - - except Exception as e: - cur.close() - conn.close() - Log.debug(self, "{0}".format(e)) - if not errormsg: - Log.error(self, 'Unable to execute statement') - else: - Log.error(self, '{0}'.format(errormsg)) + connection = EEMysql.connect(self) + log and Log.debug(self, "Exceuting MySQL Statement : {0}" + .format(statement)) + try: + cursor = connection.cursor() + sql = statement + cursor.execute(sql) + + # connection is not autocommit by default. + # So you must commit to save your changes. + connection.commit() + except AttributeError as e: + Log.debug(self, str(e)) + raise StatementExcecutionError + except Error as e: + Log.debug(self, str(e)) + raise StatementExcecutionError + finally: + connection.close() def backupAll(self): import subprocess @@ -101,3 +114,11 @@ class EEMysql(): except Exception as e: Log.error(self, "Error: process exited with status %s" % e) + + def check_db_exists(self, db_name): + try: + if EEMysql.dbConnection(self, db_name): + return True + except DatabaseNotExistsError as e: + Log.debug(self, str(e)) + return False diff --git a/ee/core/shellexec.py b/ee/core/shellexec.py index 0ba5cfa4..f82352fd 100644 --- a/ee/core/shellexec.py +++ b/ee/core/shellexec.py @@ -5,6 +5,11 @@ import sys import subprocess +class CommandExecutionError(Exception): + """custom Exception for command execution""" + pass + + class EEShellExec(): """Method to run shell commands""" def __init__(): @@ -13,8 +18,8 @@ class EEShellExec(): def cmd_exec(self, command, errormsg='', log=True): """Run shell command from Python""" try: - if log: - Log.debug(self, "Running command: {0}".format(command)) + log and Log.debug(self, "Running command: {0}".format(command)) + with subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) as proc: (cmd_stdout_bytes, cmd_stderr_bytes) = proc.communicate() @@ -26,25 +31,15 @@ class EEShellExec(): if proc.returncode == 0: return True else: - Log.debug(self, "Command Output: {0}, Command Error: {1}" + Log.debug(self, "Command Output: {0}, \nCommand Error: {1}" .format(cmd_stdout, cmd_stderr)) return False except OSError as e: - if errormsg: - Log.error(self, errormsg) - else: - Log.debug(self, "Unable to execute command {0}" - .format(command)) - Log.debug(self, "{0}".format(e)) - Log.error(self, "Error occured while executing command") + Log.debug(self, str(e)) + raise CommandExecutionError except Exception as e: - if errormsg: - Log.error(self, errormsg) - else: - Log.debug(self, "Unable to execute command {0}" - .format(command)) - Log.debug(self, "{0}".format(e)) - Log.error(self, "Error occurred while executing command") + Log.debug(self, str(e)) + raise CommandExecutionError def invoke_editor(self, filepath, errormsg=''): """ @@ -53,8 +48,5 @@ class EEShellExec(): try: subprocess.call(['sensible-editor', filepath]) except OSError as e: - if errormsg: - Log.error(self, errormsg) - else: Log.debug(self, "{0}{1}".format(e.errno, e.strerror)) - Log.error(self, "Unable to edit file {0}".format(filepath)) + raise CommandExecutionError diff --git a/ee/core/variables.py b/ee/core/variables.py index 2c2203b7..235699b7 100644 --- a/ee/core/variables.py +++ b/ee/core/variables.py @@ -12,12 +12,12 @@ class EEVariables(): """Intialization of core variables""" # EasyEngine version - ee_version = "3.0.6" + ee_version = "3.0.7" # EasyEngine packages versions ee_wp_cli = "0.18.0" ee_adminer = "4.1.0" - ee_roundcube = "1.1.0" + ee_roundcube = "1.1.1" ee_vimbadmin = "3.0.11" # Current date and time of System diff --git a/install b/install index 456c807a..8ebb8507 100644 --- a/install +++ b/install @@ -48,7 +48,7 @@ fi # Define variables for later use ee_branch=$1 readonly ee_version_old="2.2.3" -readonly ee_version_new="3.0.6" +readonly ee_version_new="3.0.7" readonly ee_log_dir=/var/log/ee/ readonly ee_install_log=/var/log/ee/install.log readonly ee_linux_distro=$(lsb_release -i | awk '{print $3}') @@ -102,9 +102,10 @@ function ee_install_dep() # which will be used by EasyEngine 3.x function ee_sync_db() { - mkdir -p /var/lib/ee + if [ ! -f /var/lib/ee/ee.db ]; then + mkdir -p /var/lib/ee - echo "CREATE TABLE sites ( + echo "CREATE TABLE sites ( id INTEGER PRIMARY KEY AUTOINCREMENT, sitename UNIQUE, site_type CHAR, @@ -117,87 +118,102 @@ function ee_sync_db() storage_db CHAR );" | sqlite3 /var/lib/ee/ee.db - # Check site is enable/live or disable - for site in $(ls /etc/nginx/sites-available/ | grep -v default); - do - if [ -f /etc/nginx/sites-enabled/$site ]; then - ee_site_status='1' - else - ee_site_status='0' - fi + # Check site is enable/live or disable + for site in $(ls /etc/nginx/sites-available/ | grep -v default); + do + if [ -f /etc/nginx/sites-enabled/$site ]; then + ee_site_status='1' + else + ee_site_status='0' + fi - # Find out information about current NGINX configuration - ee_site_current_type=$(head -n1 /etc/nginx/sites-available/$site | grep "NGINX CONFIGURATION" | rev | cut -d' ' -f3,4,5,6,7 | rev | cut -d ' ' -f2,3,4,5) - - # Detect current website type and cache - if [ "$ee_site_current_type" = "HTML" ]; then - ee_site_current="html" - ee_site_current_cache="basic" - elif [ "$ee_site_current_type" = "PHP" ]; then - ee_site_current="php" - ee_site_current_cache="basic" - elif [ "$ee_site_current_type" = "MYSQL" ]; then - ee_site_current="mysql" - ee_site_current_cache="basic" - # Single WordPress - elif [ "$ee_site_current_type" = "WPSINGLE BASIC" ]; then - ee_site_current="wp" - ee_site_current_cache="basic" - - elif [ "$ee_site_current_type" = "WPSINGLE WP SUPER CACHE" ]; then - ee_site_current="wp" - ee_site_current_cache="wpsc" - - elif [ "$ee_site_current_type" = "WPSINGLE W3 TOTAL CACHE" ]; then - ee_site_current="wp" - ee_site_current_cache="w3tc" - - elif [ "$ee_site_current_type" = "WPSINGLE FAST CGI" ] || [ "$ee_site_current_type" = "WPSINGLE FASTCGI" ]; then - ee_site_current="wp" - ee_site_current_cache="wpfc" - - # WordPress subdirectory - elif [ "$ee_site_current_type" = "WPSUBDIR BASIC" ]; then - ee_site_current="wpsubdir" - ee_site_current_cache="basic" - - elif [ "$ee_site_current_type" = "WPSUBDIR WP SUPER CACHE" ]; then - ee_site_current="wpsubdir" - ee_site_current_cache="wpsc" - - elif [ "$ee_site_current_type" = "WPSUBDIR W3 TOTAL CACHE" ]; then - ee_site_current="wpsubdir" - ee_site_current_cache="w3tc" - - elif [ "$ee_site_current_type" = "WPSUBDIR FAST CGI" ] || [ "$ee_site_current_type" = "WPSUBDIR FASTCGI" ]; then - ee_site_current="wpsubdir" - ee_site_current_cache="wpfc" - - # WordPress subdomain - elif [ "$ee_site_current_type" = "WPSUBDOMAIN BASIC" ]; then - ee_site_current="wpsubdomain" - ee_site_current_cache="basic" - - elif [ "$ee_site_current_type" = "WPSUBDOMAIN WP SUPER CACHE" ]; then - ee_site_current="wpsubdomain" - ee_site_current_cache="wpsc" - - elif [ "$ee_site_current_type" = "WPSUBDOMAIN W3 TOTAL CACHE" ]; then - ee_site_current="wpsubdomain" - ee_site_current_cache="w3tc" - - elif [ "$ee_site_current_type" = "WPSUBDOMAIN FAST CGI" ] || [ "$ee_site_current_type" = "WPSUBDOMAIN FASTCGI" ]; then - ee_site_current="wpsubdomain" - ee_site_current_cache="wpfc" - fi + # Find out information about current NGINX configuration + ee_site_current_type=$(head -n1 /etc/nginx/sites-available/$site | grep "NGINX CONFIGURATION" | rev | cut -d' ' -f3,4,5,6,7 | rev | cut -d ' ' -f2,3,4,5) + + # Detect current website type and cache + if [ "$ee_site_current_type" = "HTML" ]; then + ee_site_current="html" + ee_site_current_cache="basic" + elif [ "$ee_site_current_type" = "PHP" ]; then + ee_site_current="php" + ee_site_current_cache="basic" + elif [ "$ee_site_current_type" = "MYSQL" ]; then + ee_site_current="mysql" + ee_site_current_cache="basic" + # Single WordPress + elif [ "$ee_site_current_type" = "WPSINGLE BASIC" ]; then + ee_site_current="wp" + ee_site_current_cache="basic" + + elif [ "$ee_site_current_type" = "WPSINGLE WP SUPER CACHE" ]; then + ee_site_current="wp" + ee_site_current_cache="wpsc" + + elif [ "$ee_site_current_type" = "WPSINGLE W3 TOTAL CACHE" ]; then + ee_site_current="wp" + ee_site_current_cache="w3tc" + + elif [ "$ee_site_current_type" = "WPSINGLE FAST CGI" ] || [ "$ee_site_current_type" = "WPSINGLE FASTCGI" ]; then + ee_site_current="wp" + ee_site_current_cache="wpfc" + + # WordPress subdirectory + elif [ "$ee_site_current_type" = "WPSUBDIR BASIC" ]; then + ee_site_current="wpsubdir" + ee_site_current_cache="basic" + + elif [ "$ee_site_current_type" = "WPSUBDIR WP SUPER CACHE" ]; then + ee_site_current="wpsubdir" + ee_site_current_cache="wpsc" + + elif [ "$ee_site_current_type" = "WPSUBDIR W3 TOTAL CACHE" ]; then + ee_site_current="wpsubdir" + ee_site_current_cache="w3tc" + + elif [ "$ee_site_current_type" = "WPSUBDIR FAST CGI" ] || [ "$ee_site_current_type" = "WPSUBDIR FASTCGI" ]; then + ee_site_current="wpsubdir" + ee_site_current_cache="wpfc" + + # WordPress subdomain + elif [ "$ee_site_current_type" = "WPSUBDOMAIN BASIC" ]; then + ee_site_current="wpsubdomain" + ee_site_current_cache="basic" + + elif [ "$ee_site_current_type" = "WPSUBDOMAIN WP SUPER CACHE" ]; then + ee_site_current="wpsubdomain" + ee_site_current_cache="wpsc" + + elif [ "$ee_site_current_type" = "WPSUBDOMAIN W3 TOTAL CACHE" ]; then + ee_site_current="wpsubdomain" + ee_site_current_cache="w3tc" + + elif [ "$ee_site_current_type" = "WPSUBDOMAIN FAST CGI" ] || [ "$ee_site_current_type" = "WPSUBDOMAIN FASTCGI" ]; then + ee_site_current="wpsubdomain" + ee_site_current_cache="wpfc" + fi - ee_webroot="/var/www/$site" + ee_webroot="/var/www/$site" - # Insert query to insert old site information into ee.db - echo "INSERT INTO sites (sitename, site_type, cache_type, site_path, is_enabled, is_ssl, storage_fs, storage_db) - VALUES (\"$site\", \"$ee_site_current\", \"$ee_site_current_cache\", \"$ee_webroot\", \"$ee_site_status\", 0, 'ext4', 'mysql');" | sqlite3 /var/lib/ee/ee.db + # Insert query to insert old site information into ee.db + echo "INSERT INTO sites (sitename, site_type, cache_type, site_path, is_enabled, is_ssl, storage_fs, storage_db) + VALUES (\"$site\", \"$ee_site_current\", \"$ee_site_current_cache\", \"$ee_webroot\", \"$ee_site_status\", 0, 'ext4', 'mysql');" | sqlite3 /var/lib/ee/ee.db - done + done + else + ee_lib_echo "Updating EasyEngine Database" + echo "ALTER TABLE sites ADD COLUMN db_name varchar; \ + ALTER TABLE sites ADD COLUMN db_user varchar; \ + ALTER TABLE sites ADD COLUMN db_password varchar; \ + ALTER TABLE sites ADD COLUMN db_host varchar; \ + ALTER TABLE sites ADD COLUMN is_hhvm INT DEFAULT '0'; \ + ALTER TABLE sites ADD COLUMN is_pagespeed INT DEFAULT '0';" | sqlite3 /var/lib/ee/ee.db + fi +} + +function secure_ee_db() +{ + chown -R root:root /var/lib/ee/ + chmod -R 600 /var/lib/ee/ } @@ -305,19 +321,24 @@ if [ -f /usr/local/sbin/easyengine ]; then fi ee_install_dep | tee -ai $ee_install_log ee_sync_db | tee -ai $ee_install_log + secure_ee_db | tee -ai $EE_INSTALL_LOG ee_install | tee -ai $ee_install_log ee_update | tee -ai $ee_install_log ee_git_init | tee -ai $ee_install_log elif [ ! -f /usr/local/bin/ee ]; then ee_install_dep | tee -ai $ee_install_log ee_install | tee -ai $ee_install_log + secure_ee_db | tee -ai $EE_INSTALL_LOG ee_git_init | tee -ai $ee_install_log + else ee -v 2>&1 | grep $ee_version_new &>> /dev/null if [[ $? -ne 0 ]];then read -p "Update EasyEngine to $ee_version_new (y/n): " ee_ans if [ "$ee_ans" = "y" ] || [ "$ee_ans" = "Y" ]; then ee_install_dep | tee -ai $ee_install_log + sync_db | tee -ai $EE_INSTALL_LOG + secure_ee_db | tee -ai $EE_INSTALL_LOG ee_update_latest | tee -ai $ee_install_log ee_install | tee -ai $ee_install_log ee_git_init | tee -ai $ee_install_log diff --git a/setup.py b/setup.py index 29c8e5cc..4abd331a 100644 --- a/setup.py +++ b/setup.py @@ -54,7 +54,7 @@ except Exception as e: os.system("git config --global user.email {0}".format(ee_email)) setup(name='ee', - version='3.0.6', + version='3.0.7', description=long_description, long_description=long_description, classifiers=[],