def test_valid_url_from_path(self): assert WPSite.from_path(settings.OPENSHIFT_ENV, ROOT_PATH) \ .url == "http://localhost/" assert WPSite.from_path(settings.OPENSHIFT_ENV, ROOT_PATH + "htdocs/folder") \ .url == "http://localhost/folder" assert WPSite.from_path(settings.OPENSHIFT_ENV, ROOT_PATH + "htdocs/folder/sub") \ .url == "http://localhost/folder/sub"
def test_name(self, wordpress): assert wordpress.name == "folder" assert WPSite( openshift_env=settings.OPENSHIFT_ENV, wp_site_url="http://localhost/") \ .name == "localhost" assert WPSite( openshift_env=settings.OPENSHIFT_ENV, wp_site_url="http://www.epfl.ch/") \ .name == "www"
def test_name(self, wordpress): assert wordpress.name == "folder" assert WPSite( openshift_env=settings.OPENSHIFT_ENV, wp_site_url="http://localhost/") \ .name == "localhost" assert WPSite( openshift_env=settings.OPENSHIFT_ENV, wp_site_url="http://www.epfl.ch/") \ .name == "www" assert WPSite.from_path(settings.OPENSHIFT_ENV, ROOT_PATH + "htdocs/folder") \ .name == "folder" assert WPSite.from_path(settings.OPENSHIFT_ENV, ROOT_PATH + "htdocs/folder/sub") \ .name == "sub"
def rotate_backup_inventory(path, dry_run=False, **kwargs): if dry_run: logging.info("! DRY RUN !") for site_details in WPConfig.inventory(path): if site_details.valid == settings.WP_SITE_INSTALL_OK: try: path = WPBackup( WPSite.openshift_env_from_path(site_details.path), site_details.url).path # rotate full backups first for pattern in [["*full.sql"], ["*full.tar", "*full.tar.gz"]]: RotateBackups(FULL_BACKUP_RETENTION_THEME, dry_run=dry_run, include_list=pattern).rotate_backups(path) # rotate incremental backups for pattern in [["*.list"], ["*inc.sql"], ["*inc.tar", "*inc.tar.gz"]]: RotateBackups(INCREMENTAL_BACKUP_RETENTION_THEME, dry_run=dry_run, include_list=pattern).rotate_backups(path) except: logging.error("Site %s - Error %s", site_details.url, sys.exc_info())
def backup_inventory(path, dry_run=False, **kwargs): """ Backup all WordPress instances found in a given path :param path: :param dry_run: :param kwargs: :return: """ if dry_run: logging.info("! DRY RUN !") logging.info("Backup from inventory...") for site_details in WPConfig.inventory(path): if site_details.valid == settings.WP_SITE_INSTALL_OK: logging.info("Running backup for %s", site_details.url) try: WPBackup(WPSite.openshift_env_from_path(site_details.path), site_details.url, dry_run=dry_run).backup() except: logging.error("Site %s - Error %s", site_details.url, sys.exc_info()) logging.info("All backups done for path: %s", path)
def update_plugins_inventory(path, plugin=None, force_plugin=False, force_options=False, strict_list=False, extra_config=None, **kwargs): logging.info("Update plugins from inventory...") for site_details in WPConfig.inventory(path): if site_details.valid == settings.WP_SITE_INSTALL_OK: logging.info("Updating plugins for %s", site_details.url) all_params = { 'openshift_env': WPSite.openshift_env_from_path(site_details.path), 'wp_site_url': site_details.url } # if we have extra configuration to load, if extra_config is not None: all_params = _add_extra_config(extra_config, all_params) WPGenerator(all_params).update_plugins( only_one=plugin, force_plugin=force_plugin, force_options=force_options, strict_plugin_list=strict_list) logging.info("All plugins updates done for path: %s", path)
def _check_site(wp_env, wp_url, **kwargs): """ Helper function to validate wp site given arguments """ wp_site = WPSite(wp_env, wp_url, wp_site_title=kwargs.get('wp_title')) wp_config = WPConfig(wp_site) if not wp_config.is_installed: raise SystemExit("No files found for {}".format(wp_site.url)) if not wp_config.is_config_valid: raise SystemExit("Configuration not valid for {}".format(wp_site.url)) return wp_config
def __init__(self, wp_env, wp_url): """ Constructor Arguments keyword: wp_env - OpenShift env wp_url - webSite URL """ # List of "defined" value to look for in "wp-config.php" file. WP_CONFIG_DEFINE_NAMES = [ 'DB_NAME', 'DB_USER', 'DB_PASSWORD', 'DB_HOST', 'DB_CHARSET' ] self.wp_site = WPSite(wp_env, wp_url) wp_config_file = os.path.join(self.wp_site.path, "wp-config.php") if not os.path.exists(wp_config_file): logging.error("WordPress config file not exists: %s", wp_config_file) wp_config_file = open(wp_config_file, 'r') wp_config_file_content = wp_config_file.read() wp_config_file.close() define_search_regex = re.compile( "define\(\s*'(.+)'\s*,\s*'(.+)'\s*\);") wp_table_prefix_search_regex = re.compile( "\$table_prefix\s*=\s*'(.+)'\s*;") self.wp_defined = {} # Extracting 'defined' values in WordPress config file and store in dict for define_name, define_value in define_search_regex.findall( wp_config_file_content): self.wp_defined[define_name] = define_value # Extracting table prefix result = wp_table_prefix_search_regex.findall(wp_config_file_content) if len(result) == 0: logging.error( "Missing information for $wp_table_prefix in WordPress config file" ) self.wp_table_prefix = result[0] # Check if we have all needed 'define' for define_name in WP_CONFIG_DEFINE_NAMES: if define_name not in self.wp_defined: logging.error( "Missing 'define' for '%s' in WordPress config file", define_name)
def backup_inventory(path, **kwargs): logging.info("Backup from inventory...") for site_details in WPConfig.inventory(path): if site_details.valid == settings.WP_SITE_INSTALL_OK: logging.info("Running backup for %s", site_details.url) try: WPBackup(WPSite.openshift_env_from_path(site_details.path), site_details.url).backup() except: logging.error("Site %s - Error %s", site_details.url, sys.exc_info()) logging.info("All backups done for path: %s", path)
def extract_plugin_config(wp_env, wp_url, output_file, **kwargs): wp_site = WPSite(wp_env, wp_url) ext = WPPluginConfigExtractor(wp_site) ext.extract_config(output_file)
def wp_config(self): wordpress = WPSite(openshift_env=settings.OPENSHIFT_ENV, wp_site_url="http://localhost/folder", wp_default_site_title="My test") return WPConfig(wordpress)
def test_failing_url_from_path(self): with pytest.raises(ValueError): WPSite.from_path("idontexistandneverwill", ROOT_PATH)
def test_valid_url_from_path(self): assert WPSite.from_path(ROOT_PATH + "htdocs/folder") \ .url == "https://localhost/folder" assert WPSite.from_path(ROOT_PATH + "htdocs/folder/sub") \ .url == "https://localhost/folder/sub"
def test_failing_url_from_path(self): with pytest.raises(ValueError): WPSite.from_path("/usr/folder")
def wordpress(self): return WPSite(openshift_env=settings.OPENSHIFT_ENV, wp_site_url="http://localhost/folder", wp_site_title="TST", wp_tagline="Test site")
def fix_site(self, openshift_env, wp_site_url, shortcode_name=None): """ Fix shortocdes in WP site :param openshift_env: openshift environment name :param wp_site_url: URL to website to fix. :param shortcode_name: fix site for this shortcode only :return: dictionnary with report. """ content_filename = Utils.generate_name(15, '/tmp/') wp_site = WPSite(openshift_env, wp_site_url) wp_config = WPConfig(wp_site) if not wp_config.is_installed: logging.info("No WP site found at given URL (%s)", wp_site_url) return self.report logging.info("Fixing %s...", wp_site.path) # Getting site posts post_ids = wp_config.run_wp_cli("post list --post_type=page --skip-plugins --skip-themes " "--format=csv --fields=ID") # Nothing to fix if not post_ids: logging.info("No page found, nothing to do!") return post_ids = post_ids.split('\n')[1:] # Looping through posts for post_id in post_ids: logging.info("Fixing page ID %s...", post_id) content = wp_config.run_wp_cli("post get {} --skip-plugins --skip-themes " "--field=post_content".format(post_id)) original_content = content # Step 1 - Fixing shortcodes # Looking for all shortcodes in current post for shortcode in list(set(re.findall(self.regex, content))): if shortcode_name is None or shortcode_name.startswith(shortcode): fix_func_name = "_fix_{}".format(shortcode.replace("-", "_")) if shortcode_name is not None and shortcode_name.endswith("_new_version"): fix_func_name += "_new_version" try: # Trying to get function to fix current shortcode fix_func = getattr(self, fix_func_name) except Exception as e: # "Fix" function not found, skipping to next shortcode continue logging.debug("Fixing shortcode %s...", shortcode) content = fix_func(content) # Step 2: Removing <div class="textbox"> to avoid display issues on 2018 theme soup = BeautifulSoup(content, 'html5lib') soup.body.hidden = True # Looking for all DIVs with "textBox" as class for div in soup.find_all('div', {'class': 'textBox'}): # Remove DIV but keep its content div.unwrap() content = str(soup.body) # If content changed for current page, if content != original_content: logging.debug("Content fixed, updating page...") for try_no in range(settings.WP_CLI_AND_API_NB_TRIES): try: # We use a temporary file to store page content to avoid to have problems with simple/double # quotes and content size with open(content_filename, 'wb') as content_file: content_file.write(content.encode()) wp_config.run_wp_cli("post update {} --skip-plugins --skip-themes {} ".format( post_id, content_filename)) except Exception as e: if try_no < settings.WP_CLI_AND_API_NB_TRIES - 1: logging.error("fix_site() error. Retry %s in %s sec...", try_no + 1, settings.WP_CLI_AND_API_NB_SEC_BETWEEN_TRIES) time.sleep(settings.WP_CLI_AND_API_NB_SEC_BETWEEN_TRIES) pass # Cleaning if os.path.exists(content_filename): os.remove(content_filename) return self.report