????

Your IP : 18.116.86.255


Current Path : /usr/sbin/
Upload File :
Current File : //usr/sbin/clwpos-uninstall-smart-advice-plugins

#!/opt/cloudlinux/venv/bin/python3 -sbb
"""
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

Temporary script used to turn off part of the features that were previously
installed automatically as part of new server provisioning.
"""
import argparse
import logging
import os.path
import subprocess

from clwpos.wpos_admin import disable_smart_advice_functionality
from clwpos.logsetup import init_wpos_sentry_safely, setup_logging
from clwpos.utils import is_wpos_supported

MARKER_FILE = "/opt/cloudlinux/flags/enabled-flags.d/smart-advice-plugin-install-reverted.flag"


def main(one_time: bool = False, force: bool = False):
    if _script_already_succeded() and one_time:
        logging.info("Skipping uninstallation of smart-advice because it already succeeded")
        return

    if not _enabled_automatically() and not force:
        logging.info("Skipping uninstallation of smart-advice because it was not installed automatically")
        return

    _turn_off_plugin_installations()
    _uninstall_smart_advice_plugins()

    _touch_success_marker()
    logging.info('Uninstallation complete')


def _turn_off_plugin_installations():
    logging.info('Turning off notifications and automatic WordPress plugin installation')
    disable_smart_advice_functionality()


def _uninstall_smart_advice_plugins():
    if not os.path.exists('/usr/sbin/cl-smart-advice'):
        logging.info('SmartAdvice is not installed, plugins removal skipped')
        return

    logging.info('Removing already installed SmartAdvice plugins')
    subprocess.run([
        '/usr/sbin/cl-smart-advice', 'wordpress-plugin-uninstall'
    ], check=True)


def _touch_success_marker():
    open(MARKER_FILE, 'a').close()


def _script_already_succeded():
    return os.path.exists(MARKER_FILE)


def _enabled_automatically():
    """
    Detect whether Accelerate WP was enabled during automatic reconfiguration.
    """
    log_file = '/var/log/cloudlinux/clcustomizer.log'
    expected_text = 'Enabling Accelerate WP'

    if not os.path.exists(log_file):
        return False

    with open(log_file, 'r') as f:
        for line in f:
            if expected_text in line:
                return True
        return False


if __name__ == '__main__':
    if not is_wpos_supported():
        exit(0)

    parser = argparse.ArgumentParser()

    parser.add_argument('--one-time', default=False, action='store_true')
    parser.add_argument('--force', default=False, action='store_true')
    parser.add_argument('--verbose', default=False, action='store_true')

    args = parser.parse_args()

    console_level = logging.DEBUG if args.verbose else logging.CRITICAL

    setup_logging(
        caller_name=None,
        console_level=console_level,
        file_level=logging.INFO,
        logfile_path='/var/log/clwpos/uninstall-smart-advice-plugins.log',
    )
    init_wpos_sentry_safely()

    main(args.one_time, args.force)