|
| 1 | +from cement.core.controller import CementBaseController, expose |
| 2 | +from cement.core import handler, hook |
| 3 | +from ee.core.fileutils import EEFileUtils |
| 4 | +from ee.cli.plugins.sitedb import * |
| 5 | +from ee.core.mysql import * |
| 6 | +from ee.core.logging import Log |
| 7 | + |
| 8 | + |
| 9 | +def ee_sync_hook(app): |
| 10 | + # do something with the ``app`` object here. |
| 11 | + pass |
| 12 | + |
| 13 | + |
| 14 | +class EESyncController(CementBaseController): |
| 15 | + class Meta: |
| 16 | + label = 'sync' |
| 17 | + stacked_on = 'base' |
| 18 | + stacked_type = 'nested' |
| 19 | + description = 'synchronize EasyEngine database' |
| 20 | + |
| 21 | + @expose(hide=True) |
| 22 | + def default(self): |
| 23 | + self.sync() |
| 24 | + |
| 25 | + @expose(hide=True) |
| 26 | + def sync(self): |
| 27 | + """ |
| 28 | + 1. reads database information from wp/ee-config.php |
| 29 | + 2. updates records into ee database accordingly. |
| 30 | + """ |
| 31 | + Log.info(self, "Synchronizing ee database, please wait ....") |
| 32 | + sites = getAllsites(self) |
| 33 | + if not sites: |
| 34 | + pass |
| 35 | + for site in sites: |
| 36 | + if site.site_type in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']: |
| 37 | + ee_site_webroot = site.site_path |
| 38 | + # Read config files |
| 39 | + configfiles = glob.glob(ee_site_webroot + '/*-config.php') |
| 40 | + if configfiles: |
| 41 | + if EEFileUtils.isexist(self, configfiles[0]): |
| 42 | + ee_db_name = (EEFileUtils.grep(self, configfiles[0], |
| 43 | + 'DB_NAME').split(',')[1] |
| 44 | + .split(')')[0].strip().replace('\'', '')) |
| 45 | + ee_db_user = (EEFileUtils.grep(self, configfiles[0], |
| 46 | + 'DB_USER').split(',')[1] |
| 47 | + .split(')')[0].strip().replace('\'', '')) |
| 48 | + ee_db_pass = (EEFileUtils.grep(self, configfiles[0], |
| 49 | + 'DB_PASSWORD').split(',')[1] |
| 50 | + .split(')')[0].strip().replace('\'', '')) |
| 51 | + ee_db_host = (EEFileUtils.grep(self, configfiles[0], |
| 52 | + 'DB_HOST').split(',')[1] |
| 53 | + .split(')')[0].strip().replace('\'', '')) |
| 54 | + |
| 55 | + # Check if database really exist |
| 56 | + try: |
| 57 | + if not EEMysql.check_db_exists(self, ee_db_name): |
| 58 | + # Mark it as deleted if not exist |
| 59 | + ee_db_name = 'deleted' |
| 60 | + ee_db_user = 'deleted' |
| 61 | + ee_db_pass = 'deleted' |
| 62 | + except StatementExcecutionError as e: |
| 63 | + Log.debug(self, str(e)) |
| 64 | + except Exception as e: |
| 65 | + Log.debug(self, str(e)) |
| 66 | + |
| 67 | + if site.db_name != ee_db_name: |
| 68 | + # update records if any mismatch found |
| 69 | + Log.debug(self, "Updating ee db record for {0}" |
| 70 | + .format(site.sitename)) |
| 71 | + updateSiteInfo(self, site.sitename, |
| 72 | + db_name=ee_db_name, |
| 73 | + db_user=ee_db_user, |
| 74 | + db_password=ee_db_pass, |
| 75 | + db_host=ee_db_host) |
| 76 | + |
| 77 | + |
| 78 | +def load(app): |
| 79 | + # register the plugin class.. this only happens if the plugin is enabled |
| 80 | + handler.register(EESyncController) |
| 81 | + # register a hook (function) to run after arguments are parsed. |
| 82 | + hook.register('post_argument_parsing', ee_sync_hook) |
0 commit comments