From 7ad3db69ce33b06c55011d94f37a856815aca63c Mon Sep 17 00:00:00 2001 From: harshadyeola Date: Thu, 9 Apr 2015 19:06:06 +0530 Subject: [PATCH] added ee sync command --- config/ee.conf | 6 ++++ ee/cli/plugins/sync.py | 80 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 ee/cli/plugins/sync.py diff --git a/config/ee.conf b/config/ee.conf index ef568c5f..9d94df02 100644 --- a/config/ee.conf +++ b/config/ee.conf @@ -74,3 +74,9 @@ email = ### `ee.cli.plugins.example` or from the file path ### `/var/lib/ee/plugins/example.py` enable_plugin = true + +[sync] +### If enabled, load a plugin named `update` 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/ee/cli/plugins/sync.py b/ee/cli/plugins/sync.py new file mode 100644 index 00000000..eb0c748e --- /dev/null +++ b/ee/cli/plugins/sync.py @@ -0,0 +1,80 @@ +from cement.core.controller import CementBaseController, expose +from cement.core import handler, hook +from ee.core.fileutils import EEFileUtils +from ee.cli.plugins.sitedb import * +from ee.core.mysql import * +from ee.core.logging import Log + + +def ee_sync_hook(app): + # do something with the ``app`` object here. + pass + + +class EESyncController(CementBaseController): + class Meta: + label = 'sync' + stacked_on = 'base' + stacked_type = 'nested' + description = 'synchronize EasyEngine database' + + @expose(hide=True) + def default(self): + self.sync() + + @expose(hide=True) + def sync(self): + """ + 1. reads database information from wp/ee-config.php + 2. updates records into ee database accordingly. + """ + Log.info(self, "Synchronizing ee database, please wait ....") + 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('\'', '')) + + # Check if database really exist + try: + if not EEMysql.check_db_exists(self, ee_db_name): + # Mark it as deleted if not exist + ee_db_name = 'deleted' + except StatementExcecutionError as e: + Log.debug(self, str(e)) + except Exception as e: + Log.debug(self, str(e)) + + if site.db_name != ee_db_name: + # update records if any mismatch found + Log.debug(self, "Updating ee db record for {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) + + +def load(app): + # register the plugin class.. this only happens if the plugin is enabled + handler.register(EESyncController) + # register a hook (function) to run after arguments are parsed. + hook.register('post_argument_parsing', ee_sync_hook)