Beispiel #1
0
    def test_restore_generic_config(self, wp_generator_generic, wp_plugin_list):

        # First, uninstall from WP installation
        plugin_config = wp_plugin_list.plugins()['add-to-any']
        wp_plugin_config = WPPluginConfig(wp_generator_generic.wp_site, 'add-to-any', plugin_config)
        wp_plugin_config.uninstall()

        # Then, reinstall plugin and configure it
        wp_plugin_config.install()
        wp_plugin_config.configure(force=True)

        # Check plugin options
        wp_config = WPConfig(wp_generator_generic.wp_site)
        assert wp_config.run_wp_cli("option get addtoany_options") == 'test'
Beispiel #2
0
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())
Beispiel #3
0
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)
Beispiel #4
0
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)
Beispiel #5
0
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
Beispiel #6
0
    def locate_existing(self, path):
        """
        Locate all existing shortcodes in a given path. Go through all WordPress installs and parse pages to extract
        shortcode list.
        :param path: path where to start search
        :return:
        """

        for site_details in WPConfig.inventory(path):

            if site_details.valid == settings.WP_SITE_INSTALL_OK:

                logging.info("Checking %s...", site_details.url)

                try:

                    # Getting site posts
                    post_ids = Utils.run_command("wp post list --post_type=page --format=csv --fields=ID "
                                                 "--skip-plugins --skip-themes --path={}".format(site_details.path))

                except Exception as e:
                    logging.error("Error getting page list, skipping to next site: %s", str(e))
                    continue

                # Getting list of registered shortcodes to be sure to list only registered and not all strings
                # written between [ ]
                registered_shortcodes = self._get_site_registered_shortcodes(site_details.path)

                if not post_ids:
                    continue

                post_ids = post_ids.split('\n')[1:]

                logging.debug("%s pages to analyze...", len(post_ids))

                # Looping through posts
                for post_id in post_ids:
                    try:
                        content = Utils.run_command("wp post get {} --field=post_content --skip-plugins --skip-themes "
                                                    "--path={}".format(post_id, site_details.path))
                    except Exception as e:
                        logging.error("Error getting page, skipping to next page: %s", str(e))
                        continue

                    # Looking for all shortcodes in current post
                    for shortcode in re.findall(self.regex, content):

                        # This is not a registered shortcode
                        if shortcode not in registered_shortcodes:
                            continue

                        if shortcode not in self.list:
                            self.list[shortcode] = []

                        if site_details.path not in self.list[shortcode]:
                            self.list[shortcode].append(site_details.path)
Beispiel #7
0
    def test_restore_specific_config(self, wp_generator_generic,
                                     wp_plugin_list):

        # First, uninstall from WP installation
        plugin_config = wp_plugin_list.plugins(
            settings.TEST_SITE)['add-to-any']
        wp_plugin_config = WPPluginConfig(wp_generator_generic.wp_site,
                                          'add-to-any', plugin_config)
        wp_plugin_config.uninstall()

        # Then, reinstall and configure it
        wp_plugin_config.install()
        wp_plugin_config.configure()

        # Check plugin options
        wp_config = WPConfig(wp_generator_generic.wp_site)
        assert wp_config.run_wp_cli(
            "option get addtoany_options") == 'test_overload'
        assert wp_config.run_wp_cli("option get addtoany_dummy") == 'dummy'
Beispiel #8
0
def inventory(wp_env, path, **kwargs):
    logging.info("Building inventory...")
    print(";".join(
        ['path', 'valid', 'url', 'version', 'db_name', 'db_user', 'admins']))
    for site_details in WPConfig.inventory(wp_env, path):
        print(";".join([
            site_details.path, site_details.valid, site_details.url,
            site_details.version, site_details.db_name, site_details.db_user,
            site_details.admins
        ]))
    logging.info("Inventory made for %s", path)
Beispiel #9
0
    def get_details(self, path, shortcode_list):
        """
        Locate all instance of given shortcode in a given path. Go through all WordPress installs and parse pages to
        extract shortcode details
        :param path: path where to start search
        :param shortcode_list: list with shortcode name to look for
        :return: Dict - Key is WP site URL and value is a list of dict containing shortcode infos.
        """

        # Building "big" regex to match all given shortcodes
        regexes = []
        for shortcode in shortcode_list:
            regexes.append('\[{}\s?.*?\]'.format(shortcode))

        regex = re.compile('|'.join(regexes), re.VERBOSE)

        shortcode_details = {}

        for site_details in WPConfig.inventory(path):

            if site_details.valid == settings.WP_SITE_INSTALL_OK:

                logging.info("Checking %s...", site_details.url)

                try:
                    # Getting site posts
                    post_ids = Utils.run_command("wp post list --post_type=page --format=csv --fields=ID "
                                                 "--skip-plugins --skip-themes --path={}".format(site_details.path))

                    if not post_ids:
                        continue

                    post_ids = post_ids.split('\n')[1:]

                    # Looping through posts
                    for post_id in post_ids:
                        content = Utils.run_command("wp post get {} --field=post_content --skip-plugins --skip-themes "
                                                    "--path={}".format(post_id, site_details.path))

                        # Looking for given shortcode in current post
                        for shortcode_with_args in re.findall(regex, content):

                            if site_details.path not in shortcode_details:
                                shortcode_details[site_details.path] = []

                            post_url = '{}/wp-admin/post.php?post={}&action=edit'.format(site_details.url, post_id)
                            shortcode_details[site_details.path].append({'post_url': post_url,
                                                                         'shortcode_call': shortcode_with_args})

                except Exception as e:
                    logging.error("Error, skipping to next site: %s", str(e))
                    pass

        return shortcode_details
Beispiel #10
0
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)
Beispiel #11
0
 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)
Beispiel #12
0
    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