Viewing file: features.py (31.93 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
# -*- coding: utf-8 -*- # Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2020 All Rights Reserved # # Licensed under CLOUD LINUX LICENSE AGREEMENT # http://cloudlinux.com/docs/LICENSE.TXT
# TODO: convert this file into python package # and move logic of modules manipulations here import json import os import argparse from pathlib import Path from typing import Dict
import cpanel from clcommon.clwpos_lib import get_wp_cache_plugin from clwpos import gettext as _, constants from clwpos.cl_wpos_exceptions import WposError, WpCliCommandError from clwpos.constants import MINIMUM_SUPPORTED_PHP_OBJECT_CACHE, \ CL_DOC_USER_PLUGIN from clwpos.utils import is_conflict_modules_installed, PHP from cpanel import wordpress, WordpressError from dataclasses import dataclass, field, asdict from enum import Enum
class PluginStatus(Enum): UNINSTALLED = 'uninstalled' ACTIVE = 'active' INACTIVE = 'inactive'
@dataclass class Issue: """ Generic class for keeping compatibility/misconfiguration issues """ header: str description: str fix_tip: str
context: Dict[str, str] = field(default_factory=dict)
@property def dict_repr(self): return asdict(self)
class UniqueId: PHP_NOT_SUPPORTED = 'PHP_NOT_SUPPORTED' PLUGIN_CONFLICT = 'PLUGIN_CONFLICT' WORDPRESS_MULTISITE_ENABLED = 'WORDPRESS_MULTISITE_ENABLED' MISCONFIGURED_WORDPRESS = 'MISCONFIGURED_WORDPRESS'
WEBSERVER_NOT_SUPPORTED = 'WEBSERVER_NOT_SUPPORTED' PHP_MISCONFIGURATION = 'PHP_MISCONFIGURATION' UNCOMPATIBLE_WORDPRESS_VERSION = 'UNCOMPATIBLE_WORDPRESS_VERSION'
CLOUDLINUX_MODULE_ALREADY_ENABLED = 'CLOUDLINUX_MODULE_ALREADY_ENABLED'
@dataclass class CompatibilityIssue(Issue): """ For compatibility issues """ unique_id: str = None telemetry: Dict[str, str] = field(default_factory=dict)
type: str = 'incompatibility'
@property def dict_repr(self): representation = asdict(self) representation.pop('unique_id') representation.pop('telemetry') return representation
@dataclass class MisconfigurationIssue(Issue): """ For misconfiguration issues """ type: str = 'misconfiguration'
class Feature(str): """ Helper class which hides differences of optimization features behind abstract methods. """ NAME = '' WP_PLUGIN_NAME = ''
def __new__(cls, *args, **kwargs): if cls != Feature: return str.__new__(cls, *args)
classes = { "object_cache": _ObjectCache,
# yep, site_optimization and accelerate_wp names are same thing "site_optimization": _SiteOptimization, "accelerate_wp": _SiteOptimization }
try: return classes[args[0]](*args) except KeyError: raise argparse.ArgumentTypeError(f"No such feature: {args[0]}.")
@classmethod def optimization_feature(cls): return cls(cls.NAME.lower())
@classmethod def redis_daemon_required(cls): raise NotImplementedError
@classmethod def collect_docroot_issues(cls, wpos_user_obj, doc_root_info, allowed_modules=None): raise NotImplementedError
@classmethod def is_php_supported(cls, php_version: PHP): raise NotImplementedError
@classmethod def minimum_supported_wp_version(cls): raise NotImplementedError
@staticmethod def collect_wordpress_issues(self, wordpress: Dict, docroot: str, module_is_enabled: bool): raise NotImplementedError
@staticmethod def to_interface_name(): raise NotImplementedError
@staticmethod def get_wp_plugin_status(wordpress_abs_path, plugin_name) -> PluginStatus: """ Get information about WordPress plugin current status.
:param wordpress_abs_path: absolute path to wordpress installation :param plugin_name: name of plugin as it listed in plugins directory :return: PluginStatus """ plugins_data = wordpress(wordpress_abs_path, 'plugin', 'list', '--name=%s' % plugin_name, '--json') if isinstance(plugins_data, WordpressError): raise WposError(message=plugins_data.message, context=plugins_data.context)
try: response = json.loads(plugins_data) except (ValueError, TypeError, json.JSONDecodeError) as e: raise WposError( message=_( 'Malformed plugins information received from wp-cli. ' 'Raw response is %(response)s.'), context={'response': plugins_data}, details=str(e) )
# in case of missing plugin wp-cli returns empty dict if not response: return PluginStatus.UNINSTALLED
# in any other case we get list of one element with parameters return PluginStatus(response[0]['status'])
@classmethod def _get_wp_plugin_compatibility_issues(cls, docroot, wordpress): """ Get issues that relates to currently installed WP plugin or None if everything is ok """ try: plugin_status = cls.get_wp_plugin_status( wordpress_abs_path=os.path.join(docroot, wordpress["path"]), plugin_name=cls.WP_PLUGIN_NAME) except WposError as e: return CompatibilityIssue( header=_('Unexpected WordPress error'), description=_( 'Unable to detect the WordPress plugins ' 'due to unexpected error. ' '\n\n' 'Technical details:\n%(error_message)s.\n' '\nMost likely WordPress installation is not working properly.' ), fix_tip=_( 'Check that your website is working properly – ' 'try to run the specified command to find any obvious ' 'errors in the WordPress configuration. ' 'Otherwise, try to fix other issues first - ' 'it may help to resolve this issue as well.' ), context=dict( error_message=e.message % e.context ), unique_id=UniqueId.MISCONFIGURED_WORDPRESS, telemetry=dict( error_message=e.message % e.context ) )
return cls._get_issues_from_wp_plugin_status(plugin_status)
@classmethod def _get_issues_from_wp_plugin_status(cls, plugin_status): raise NotImplementedError
class _ObjectCache(Feature): """Implementation for object caching"""
NAME = 'OBJECT_CACHE' WP_PLUGIN_NAME = 'redis-cache'
@classmethod def redis_daemon_required(cls): return True
@staticmethod def to_interface_name(): return 'object_cache'
@classmethod def _get_issues_from_wp_plugin_status(cls, plugin_status): """ Get issue that relates to currently installed redis-cache plugin or None if everything is ok """ if plugin_status == PluginStatus.INACTIVE: return MisconfigurationIssue( header=_('"Redis Object Cache" plugin is deactivated'), description=_('Object caching is enabled, but the ' '"Redis Object Cache" plugin is deactivated in Wordpress admin page. Caching does not work'), fix_tip=_('Activate the Redis Object Cache plugin in the Wordpress admin page and ' 'enable Object Cache Drop-in in the Redis Object Cache plugin settings. ' 'As an alternative, rollback the feature and apply it again.') ) elif plugin_status == PluginStatus.ACTIVE: return MisconfigurationIssue( header=_('The Object Cache Drop-in not installed'), description=_('The Object Cache Drop-In is not enabled. Caching does not work'), fix_tip=_('Enable the Object Cache using the Redis Object Cache plugin ' 'settings page of Wordpress Admin. ' 'As an alternative, rollback the feature and apply it again.') ) elif plugin_status == PluginStatus.UNINSTALLED: return MisconfigurationIssue( header=_('"Redis Object Cache" plugin is not installed'), description=_('The "Redis Object Cache" WordPress plugin is not installed. ' 'Caching does not work'), fix_tip=_('Rollback the feature and apply it again. ' 'Contact your administrator if the issue persists.') ) else: raise WposError(_('Unexpected plugin status: %(status)s'), context=dict(status=plugin_status))
@classmethod def collect_docroot_issues(cls, wpos_user_obj, doc_root_info, allowed_modules=None): """ Collects incompatibilities related to docroot (non-supported handler, etc) for object caching. """ issues = [] php_version = doc_root_info['php_version'] is_modules_allowed = None supported_php_versions = wpos_user_obj.supported_php_versions[OBJECT_CACHE_FEATURE] header__, fix_tip__, description__, uniq_id__, telemetry__ = None, None, None, None, None
if allowed_modules is not None: is_modules_allowed = 'object_cache' in allowed_modules
if not cls.is_php_supported(php_version): header__ = _('PHP version is not supported') fix_tip__ = _('Please, set or ask your system administrator to set one of the ' 'supported PHP versions: %(compatible_versions)s') description__ = _('Non supported PHP version %(php_version)s currently is used.') uniq_id__ = UniqueId.PHP_NOT_SUPPORTED telemetry__ = dict( reason='PHP_VERSION_TOO_LOW', php_version=php_version, supported_php_versions=supported_php_versions )
elif php_version not in cpanel.get_cached_php_versions_with_redis_present(): header = _('Redis extension is not installed for selected php version') fix_tip = _('Please, install or ask your system administrator to install redis extension ' 'for current %(php_version)s version, or use one of the compatible php versions: ' '%(compatible_versions)s for the domain.') description = _('Redis PHP extension is required for optimization feature, but not installed for ' 'selected PHP version: %(php_version)s.') if not is_modules_allowed: issues.append(MisconfigurationIssue( header=header, fix_tip=fix_tip, description=description, context=dict(php_version=php_version, compatible_versions=supported_php_versions))) else: header__ = header fix_tip__ = fix_tip description__ = description uniq_id__ = UniqueId.PHP_NOT_SUPPORTED telemetry__ = dict( php_version=php_version, reason='PHP_REDIS_NOT_INSTALLED', supported_php_versions=supported_php_versions )
elif php_version not in cpanel.get_cached_php_versions_with_redis_loaded(): header = _('Redis extension is not loaded for selected php version') fix_tip = _('Please, load or ask your system administrator to load redis extension ' 'for current %(php_version)s version, or use one of the compatible php versions: ' '%(compatible_versions)s for the domain.') description = _('Redis PHP extension is required for optimization feature, but not loaded for ' 'selected PHP version: %(php_version)s.') if not is_modules_allowed: issues.append(MisconfigurationIssue( header=header, fix_tip=fix_tip, description=description, context=dict(php_version=php_version, compatible_versions=supported_php_versions))) else: header__ = header fix_tip__ = fix_tip description__ = description uniq_id__ = UniqueId.PHP_NOT_SUPPORTED telemetry__ = dict( php_version=php_version, reason='PHP_REDIS_NOT_LOADED', supported_php_versions=supported_php_versions )
if not supported_php_versions: fix_tip__ = _('Please, ask your system administrator to setup at least ' 'one of the recommended PHP version in accordance with docs (%(docs_url)s).')
if header__ is not None: issues.append( CompatibilityIssue( header=header__, description=description__, fix_tip=fix_tip__, context=dict(php_version=php_version, compatible_versions=', '.join(supported_php_versions), docs_url=constants.CL_DOC_USER_PLUGIN), unique_id=uniq_id__, telemetry=telemetry__ ) )
if doc_root_info["php_handler"] not in wpos_user_obj.supported_handlers: issues.append( CompatibilityIssue( header=_('Unsupported PHP handler'), description=_('Website uses unsupported PHP handler. Currently supported ' 'handler(s): %(supported_handlers)s.'), fix_tip=_('Please, set or ask your system administrator to set one of the ' 'supported PHP handlers for the domain: %(supported_handlers)s. ' 'Or keep watching our blog: %(blog_url)s for supported handlers list updates.'), context={ 'supported_handlers': ", ".join(wpos_user_obj.supported_handlers), 'blog_url': 'https://blog.cloudlinux.com/' }, unique_id=UniqueId.PHP_NOT_SUPPORTED, telemetry=dict( reason='PHP_UNSUPPORTED_HANDLER', handler=doc_root_info["php_handler"], supported_handlers=wpos_user_obj.supported_handlers, php_version=php_version ) ) )
incompatible_php_modules = {} incompatible_module = 'snuffleupagus' if incompatible_php_modules.get(php_version) == incompatible_module or \ is_conflict_modules_installed(php_version, incompatible_module): incompatible_php_modules[php_version] = incompatible_module issues.append( CompatibilityIssue( header=_('Unsupported PHP module is loaded'), description=_('Incompatible PHP module "%(incompatible_module)s" is currently used.'), fix_tip=_('Please, disable or remove "%(incompatible_module)s" PHP extension.'), context=dict(incompatible_module=incompatible_module), unique_id=UniqueId.PHP_NOT_SUPPORTED, telemetry=dict( handler=doc_root_info["php_handler"], supported_handlers=wpos_user_obj.supported_handlers, php_version=php_version ) ))
return issues
@classmethod def is_php_supported(cls, php_version: PHP): """ Check if passed php version >= minimum PHP version supported by object caching. """ return php_version.digits >= MINIMUM_SUPPORTED_PHP_OBJECT_CACHE
@classmethod def minimum_supported_wp_version(cls): return constants.MINIMUM_SUPPORTED_WP_OBJECT_CACHE
@classmethod def collect_wordpress_issues(cls, self, wordpress: Dict, docroot: str, module_is_enabled: bool): issues = [] wp_dir = Path(docroot).joinpath(wordpress["path"]) wp_content_dir = wp_dir.joinpath("wp-content") plugin_type = "object-cache" detected_object_cache_plugin = get_wp_cache_plugin(wp_dir, plugin_type)
if module_is_enabled: if detected_object_cache_plugin != "redis-cache": issue = cls._get_wp_plugin_compatibility_issues(docroot, wordpress) if issue: issues.append(issue)
if not self.is_redis_running: issues.append( MisconfigurationIssue( header=_('Redis is not running'), description=_('Object caching is enabled, but redis process is not running.'), fix_tip=_('Redis will start automatically in 5 minutes. ' 'If the issue persists - contact your system administrator and report this issue') ) ) try: cpanel.diagnose_redis_connection_constants(docroot, wordpress['path']) except WpCliCommandError as e: issues.append( MisconfigurationIssue( header=_('Unable to identify redis constants in wordpress config'), description=_('wp-cli utility returns malformed response, reason: "%(reason)s"'), fix_tip=_('Please, try to check executed command and fix possible issues with it. ' 'If issue persists - please, contact CloudLinux support.'), context=dict( reason=e.message % e.context ) ) ) except WposError as e: issues.append( MisconfigurationIssue( header=_('Missed redis constants in site config'), description=_('WordPress config does not have needed constants ' 'for redis connection establishment.\n' 'Details: %(reason)s'), fix_tip=_('Please, try to disable and enable plugin again. ' 'If issue persists - please, contact CloudLinux support.'), context=dict( reason=e.message % e.context ) ) ) if detected_object_cache_plugin == "Unknown": drop_in_file = wp_content_dir.joinpath(f'{plugin_type}.php') issues.append( CompatibilityIssue( header=_('Conflicting object caching plugin enabled'), description=_('Unknown custom object caching plugin is already enabled'), fix_tip=_(f'Remove the drop-in ({drop_in_file}) file from the WordPress ' f'instance because it conflicts with AccelerateWP object caching.'), unique_id=UniqueId.PLUGIN_CONFLICT, telemetry=dict( reason='OBJECT_CACHE_ALREADY_ENABLED', plugin=detected_object_cache_plugin ) )) elif detected_object_cache_plugin == "w3-total-cache": issues.append( CompatibilityIssue( header=_('Object Caching of W3 Total Cache plugin is incompatible'), description=_('WordPress website already has Object Caching feature enabled ' 'with caching backend configured by the the W3 Total Cache plugin.'), fix_tip=_('Deactivate Object Caching in W3 Total Cache plugin settings.'), context=dict(), unique_id=UniqueId.PLUGIN_CONFLICT, telemetry=dict( reason='OBJECT_CACHE_ALREADY_ENABLED', plugin=detected_object_cache_plugin ) )) elif detected_object_cache_plugin not in (None, "redis-cache"): issues.append( CompatibilityIssue( header=_('Conflicting object caching plugin enabled'), description=_('The "%(detected_wp_plugin)s" plugin conflicts with AccelerateWP object caching.'), fix_tip=_('Deactivate object caching in the plugin settings or completely uninstall' 'the conflicting plugin using the WordPress administration interface.'), context=dict(detected_wp_plugin=detected_object_cache_plugin), unique_id=UniqueId.PLUGIN_CONFLICT, telemetry=dict( reason='OBJECT_CACHE_ALREADY_ENABLED', plugin=detected_object_cache_plugin ) )) try: if not self.check_installed_roc_plugin(os.path.join(docroot, wordpress['path'])): issues.append( CompatibilityIssue( header=_('Another Redis Object Cache plugin is installed'), description=_('Non CloudLinux Redis Object Cache is installed for the website'), fix_tip=_('Uninstall Redis Object Cache plugin using WordPress administration page'), unique_id=UniqueId.PLUGIN_CONFLICT, telemetry=dict( reason='OBJECT_CACHE_ALREADY_ENABLED', plugin=detected_object_cache_plugin ) )) except WpCliCommandError as e: issues.append( MisconfigurationIssue( header=_('Unable to identify installed object cache plugin in WordPress'), description=_('wp-cli utility returns malformed response, reason: "%(reason)s"'), fix_tip=_('Please, try to check executed command and fix possible issues with it. ' 'If issue persists - please, contact CloudLinux support.'), context=dict( reason=e.message % e.context ) ) )
try: multisite = cpanel.is_multisite(os.path.join(docroot, wordpress["path"])) if multisite: issues.append( CompatibilityIssue( header=_('WordPress Multisite mode is enabled'), description=_('WordPress uses the Multisite mode which is currently not supported.'), fix_tip=_('Install or configure WordPress in the single-site mode.'), unique_id=UniqueId.WORDPRESS_MULTISITE_ENABLED, telemetry=dict() )) except WposError as e: issues.append( CompatibilityIssue( header=_('Unexpected WordPress error'), description=_('Unable to detect if the WordPress installation has the Multisite mode enabled ' 'mode due to unexpected error. ' '\n\n' 'Technical details:\n%(error_message)s.\n' '\nMost likely WordPress installation is not working properly.'), fix_tip=_('If this is only one issue, please check that your website is working properly – ' 'try to run the specified command to find any obvious ' 'errors in the WordPress configuration. ' 'Otherwise, try to fix other issues first - it may help to resolve this issue as well.'), context=dict( error_message=e.message % e.context ), unique_id=UniqueId.MISCONFIGURED_WORDPRESS, telemetry=dict( error_message=e.message % e.context ) )) return issues
class _SiteOptimization(Feature): """Implementation for site optimization feature"""
NAME = 'SITE_OPTIMIZATION' WP_PLUGIN_NAME = 'clsop'
@classmethod def redis_daemon_required(cls): return False
@staticmethod def to_interface_name(): return 'accelerate_wp'
@classmethod def collect_docroot_issues(cls, wpos_user_obj, doc_root_info, allowed_modules=None): """ Collects incompatibilities related to docroot (non-supported handler, etc) for site optimizatin module. """ issues = [] php_version = doc_root_info['php_version']
if not cls.is_php_supported(php_version): supported_php_versions = wpos_user_obj.supported_php_versions[SITE_OPTIMIZATION_FEATURE] issues.append( CompatibilityIssue( header=_('PHP version is not supported'), fix_tip=_('Please, set or ask your system administrator to set one of the ' 'supported PHP version: %(compatible_versions)s for the domain.'), description=_('Non supported PHP version %(php_version)s currently is used.'), context=dict(php_version=php_version, compatible_versions=', '.join(supported_php_versions), docs_url=CL_DOC_USER_PLUGIN), unique_id=UniqueId.PHP_NOT_SUPPORTED, telemetry=dict(reason='PHP_VERSION_TOO_LOW') ) )
return issues
@staticmethod def _requirements(): with open("/opt/cloudlinux-site-optimization-module/requirements.json", "r") as f: # { # "required_php_version": "7.0", # "required_wp_version": "5.4", # "incompatible_plugins": { # "w3-total-cache": "w3-total-cache/w3-total-cache.php", # "wp-super-cache": "wp-super-cache/wp-cache.php" # } # } return json.load(f)
@classmethod def is_php_supported(cls, php_version: PHP): """ Check if passed php version >= minimum PHP version supported by site optimization feature. """ return php_version.digits >= int(cls._requirements()["required_php_version"].replace(".", ""))
@classmethod def minimum_supported_wp_version(cls): return cls._requirements()["required_wp_version"]
@classmethod def collect_wordpress_issues(cls, self, wordpress_info: Dict, docroot: str, module_is_enabled: bool): issues = [] abs_wp_path = Path(docroot).joinpath(wordpress_info["path"]) wp_content_dir = abs_wp_path.joinpath("wp-content") plugin_type = "advanced-cache"
detected_advanced_cache_plugin = get_wp_cache_plugin(abs_wp_path, plugin_type)
plugins_data = wordpress(str(abs_wp_path), "plugin", "list", "--status=active", "--format=json")
if isinstance(plugins_data, WordpressError): found_plugins = set() else: try: found_plugins = {item["name"] for item in json.loads(plugins_data)} except (ValueError, TypeError, json.JSONDecodeError): issues.append( MisconfigurationIssue( header=_('Unable to identify module compatibility'), description=_('Malformed output received from the following command: <br> $/opt/clwpos/wp-cli plugin list --status=active --format=json' '<br><br>The raw command output is:<br> \"%(wp_cli_response)s\"'), fix_tip=_('Please, check the received command output and ensure it returns a valid JSON.'), context=dict( wp_cli_response=plugins_data ) ) ) found_plugins = set()
incompatible_plugins = set(cls._requirements()["incompatible_plugins"].keys()) result = found_plugins & incompatible_plugins if detected_advanced_cache_plugin: result.add(detected_advanced_cache_plugin)
# if our WP Rocket module is enabled it's not conflicting plugin if module_is_enabled: result.discard("WP Rocket") issue = cls._get_wp_plugin_compatibility_issues(docroot, wordpress_info) if issue: issues.append(issue)
# for more beautiful output if len(result) > 1: result.discard("Unknown")
result = list(result) if len(result) == 1 and result[0] == 'Unknown': drop_in_file = wp_content_dir.joinpath(f'{plugin_type}.php') issues.append( CompatibilityIssue( header=_("Conflicting advanced cache plugin enabled"), description=_("Unknown advanced cache plugin is already enabled."), fix_tip=_(f'Remove the drop-in ({drop_in_file}) file from the WordPress ' f'instance because it conflicts with AccelerateWP.'), context=dict(plugins=", ".join(result)), unique_id=UniqueId.PLUGIN_CONFLICT, telemetry=dict( reason='SOM_ALREADY_ENABLED', plugin=list(result) ) ) ) elif result: issues.append( CompatibilityIssue( header=_("Conflicting plugins are enabled"), description=_("Found conflicting plugins: %(plugins)s."), fix_tip=_("Deactivate and uninstall the conflicting plugin " "using the WordPress administration interface."), context=dict(plugins=", ".join(result)), unique_id=UniqueId.PLUGIN_CONFLICT, telemetry=dict( reason='SOM_ALREADY_ENABLED', plugin=list(result) ) ) ) return issues
@classmethod def _get_issues_from_wp_plugin_status(cls, plugin_status): """ Get issue that relates to currently installed redis-cache plugin or None if everything is ok """ if plugin_status == PluginStatus.INACTIVE: return MisconfigurationIssue( header=_('"AccelerateWP" plugin is deactivated'), description=_('AccelerateWP feature is enabled, but the ' '"AccelerateWP" plugin is deactivated in Wordpress admin page. Caching does not work'), fix_tip=_( 'Activate the "AccelerateWP" plugin in the Wordpress admin page. ' 'As an alternative, rollback the feature and apply it again.') ) elif plugin_status == PluginStatus.UNINSTALLED: return MisconfigurationIssue( header=_('"AccelerateWP" plugin is not installed'), description=_( 'The "AccelerateWP" WordPress plugin is not installed. ' 'Caching does not work'), fix_tip=_('Rollback the feature and apply it again. ' 'Contact your administrator if the issue persists.') )
OBJECT_CACHE_FEATURE = Feature("object_cache") SITE_OPTIMIZATION_FEATURE = Feature("site_optimization")
ALL_OPTIMIZATION_FEATURES = [ OBJECT_CACHE_FEATURE, SITE_OPTIMIZATION_FEATURE ]
|