Mini Shell

Direktori : /usr/share/l.v.e-manager/utils/
Upload File :
Current File : //usr/share/l.v.e-manager/utils/cloudlinux_cli.py

# coding:utf-8

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

# pylint: skip-file

from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import shutil
import sys
import json
import os
import subprocess
import traceback
from urllib.request import urlopen
import time
import logging

import cldetectlib as detect
from clcommon.cpapi import (
    getCPName,
    DIRECTADMIN_NAME,
    admins,
    is_admin
)
from libcagefs import CageFs
from libcloudlinux import CloudlinuxCliBase

from clcommon.lib.jwt_token import jwt_token_check
from clcommon.lib.cledition import (
    is_cl_solo_edition,
    get_cl_edition_readable
)
from clcommon.const import Feature
from clcommon.cpapi import get_supported_cl_features, is_panel_feature_supported

RESELLER_KERNEL_VERSION = '3.10.0-714.10.2.lve1.5.0.7'
DONE_FLAG = '/var/lve/wizard/done.flag'
FIRST_INSTALL_FLAG = '/var/lve/wizard/is_first_installation.flag'
WMT_API_CLI = '/usr/share/web-monitoring-tool/wmtbin/wmt-api'
SSA_CLI = '/usr/sbin/cloudlinux-ssa-manager'
XRAY_CLI = '/usr/sbin/cloudlinux-xray'
XRAY_AGENT_CLI = '/usr/sbin/cloudlinux-xray-manager'
XRAY_USER_AGENT_CLI = '/opt/alt/php-xray/cloudlinux-xray-user-manager'
XRAY_STAGING_ENABLED_FLAG = '/usr/share/alt-php-xray/staging_enabled'
AWP_PREMIUM_ENABLED_FLAG = '/var/lve/enable-wpos.flag'
SMART_ADVICE_CLI = '/usr/sbin/cl-smart-advice'
CL_LINK_TO_CLN_CLI = '/usr/sbin/cl-link-to-cln'
CL_AUTOTRACING_CLI = '/usr/sbin/cloudlinux-autotracing'
CL_WIZARD_CLI = '/usr/sbin/cloudlinux-wizard'
AWP_MIGRATION_NEEDED_FLAG = '/var/clwpos/admin/awp_migration_needed.v1'
AWP_CLI_ADMIN = '/usr/bin/cloudlinux-awp-admin'
AWP_INSTALLER_CLI = '/usr/sbin/cloudlinux-awp-installer'
AWP_PLUGIN_PATH = '/usr/share/cloudlinux-awp-plugin'
XRAY_PLUGIN_PATH = '/usr/share/lvemanager-xray'
CL_MANAGER_UI_SETTINGS = '/opt/cpvendor/config/cl-manager-ui-settings.json'

class UIFeature:
    """
    Enum of available CloudLinux features for UI
    """
    PHP_SELECTOR = 'php_selector'
    RUBY_SELECTOR = 'ruby_selector'
    PYTHON_SELECTOR = 'python_selector'
    NODEJS_SELECTOR = 'nodejs_selector'
    GOVERNOR = 'mysql_governor'
    LVE = 'lve'
    WIZARD = 'wizard'
    CAGEFS = 'cagefs'
    RESELLER_LIMITS = 'reseller_limits'
    XRAY = 'xray'
    ACCELERATE_WP = 'accelerate_wp'
    LSAPI = 'mod_lsapi'
    LINKING_TO_CLN = 'linking_to_cln'
    WEBSITE_MONITORING = 'website_monitoring'

class CloudlinuxCli(CloudlinuxCliBase):

    resellers = None
    RSS_NEWS_CACHE_FILE = '/var/lve/web-team/rss_news_cache.txt'
    RSS_REMOTE_URL = 'https://blog.cloudlinux.com/tag/technical-blog/rss.xml'
    RSS_CACHE_LIFETIME = 60
    IMUNIFY360_FILE = '/usr/bin/imunify360-agent'
    JWT_FILE = '/etc/sysconfig/rhn/jwt.token'

    def __init__(self):
        super(CloudlinuxCli, self).__init__()
        self.command_methods.update({
            'cloudlinux-limits': self.cl_limits,
            'cloudlinux-license': self.cl_license,
            'cloudlinux-config' : self.cl_config,
            'cloudlinux-packages': self.cl_packages,
            'cloudlinux-summary': self.cl_summary,
            'cloudlinux-wizard': self.cl_wizard,
            'cloudlinux-support': self.cl_support,
            'spa-get-admins': self.spa_get_admins,
            'cagefsctl': self.cagefsctl,             # cagefs
            'cldiag': self.cldiag,
            'spa-get-rss-news': self.spa_get_rss_news,
            'spa-check-imunify-av': self.spa_check_imunify_av,
            'lvectl': self.lvectl,
            'cloudlinux-xray-manager': self.agent,
            'cloudlinux-xray': self.cl_xray,
            'external-info': self.external_info,
            'cloudlinux-log': self.cl_log,
            'wmt-api': self.wmt_api,
            'cloudlinux-ssa-manager': self.cl_ssa,
            'cloudlinux-awp-admin': self.cloudlinux_awp_admin,
            'cloudlinux-autotracing': self.cl_autotracing,
            'cl-smart-advice': self.cl_smart_advice,
            'cl-link-to-cln': self.cl_link_to_cln,
            'cloudlinux-awp-installer': self.cl_awp_installer
        })

    def drop_permission(self):
        """
        Drop permission to users, if owner of script is user
        :return:
        """
        data = self.request_data
        if data['owner'] not in ['admin', 'reseller']:
            self.exit_with_error("User not allowed")
        super(CloudlinuxCli, self).drop_permission()
        if data['owner'] == 'reseller' and data.get('command') != 'external-info':
            if self.is_user_in_admin_list():
                self.exit_with_error(
                    code=503,
                    error_id='ERROR.login_by_admin',
                    icon='info')
            if self.get_lve_version() <= 9:
                if self.get_cloudlinux_version() in ['el7', 'el6h']:
                    self.exit_with_error(
                        code=503,
                        error_id='ERROR.not_supported_kernel',
                        context={'resellerKernelVersion': RESELLER_KERNEL_VERSION},
                        icon='info')
                else:
                    self.exit_with_error(
                        code=503,
                        error_id='ERROR.not_supported_OS',
                        context={'resellerKernelVersion': RESELLER_KERNEL_VERSION},
                        icon='info')
            if not self.is_activated_reseller():
                self.exit_with_error(
                    code=503,
                    error_id='ERROR.not_available_plugin',
                    context={'pluginName': 'CloudLinux Manager'},
                    icon='disabled')
        if os.getuid() != 0:
            os.setgroups([])
            os.setgid(0)
            os.setuid(0)

    def cl_summary(self):
        list_to_request = self.prepair_params_for_command()
        try:
            output = subprocess.check_output(['/usr/sbin/cloudlinux-summary'] + list_to_request,
                                             stderr=subprocess.STDOUT,
                                             shell=False, text=True)
        except subprocess.CalledProcessError as processError:
            output = processError.output
        try:
            result = json.loads(output)
            result['statistic_enabled'] = self._is_statistics_enabled_by_admin()
            result['ignore'] = True
            print(json.dumps(result))
        except:
            print(output)
        sys.exit(0)

    def cl_limits(self):
        from cllimits.lib.limits import CloudlinuxLimits

        list_to_request = self.prepair_params_for_command()
        cl = CloudlinuxLimits()
        cl.run(list_to_request)

    def cl_license(self):
        from cllicense.license import CloudlinuxLicense

        if self.request_data.get('params', {}).get('key') == '':
            self.request_data['params']['ip'] = self.request_data['params'].pop('key')
        list_to_request = self.prepair_params_for_command()
        cll = CloudlinuxLicense()
        cll.run(list_to_request)

    def cl_config(self):
        from clconfig.config import ClConfig
        """
        Main run function
        """
        list_to_request = self.prepair_params_for_command()
        cll = ClConfig()
        cll.run(list_to_request)

    def cl_packages(self):
        from clpackages.packages import CloudlinuxPackages

        # TODO: fix cloudlinux-packages and remove this ugly unicode->str->unicode->str transformation
        list_to_request = self.prepair_params_for_command(escaped_strings=True)
        cll = CloudlinuxPackages()
        cll.run(list_to_request)

    def spa_get_admins(self):
        if self.request_data['owner'] == 'admin' and getCPName() == DIRECTADMIN_NAME:
            print(json.dumps(
                {"result":"success", "list": list(admins())}))
            sys.exit(0)
        else:
            self.exit_with_error('Command is not available')

    def spa_get_rss_news(self):
        content = None
        cur_time = time.time()
        last_modified_time = os.path.getmtime(self.RSS_NEWS_CACHE_FILE) \
            if os.path.exists(self.RSS_NEWS_CACHE_FILE) else 0
        age_of_file = (cur_time - last_modified_time) / 60 # in minutes

        if age_of_file > self.RSS_CACHE_LIFETIME: # for file which older than hour or it is just created
           self._update_rss_news()

        try:
            with open(self.RSS_NEWS_CACHE_FILE, 'r') as f:
                content = f.read()
        except IOError:
            pass

        print(json.dumps({
            "result":"success",
            "content": content,
            "age" : age_of_file
        }))
        sys.exit(0)

    def _update_rss_news(self):
        if not os.path.exists(os.path.dirname(self.RSS_NEWS_CACHE_FILE)):
            os.makedirs(os.path.dirname(self.RSS_NEWS_CACHE_FILE))
        try:
            xmlf = urlopen(self.RSS_REMOTE_URL)
            with open(self.RSS_NEWS_CACHE_FILE, 'w+') as f:
                f.write(xmlf.read().decode('utf-8'))
        except IOError:
            pass

    def spa_check_imunify_av(self):
        result = 'installed' if os.path.exists(self.IMUNIFY360_FILE) else 'not_installed'
        print(json.dumps({"result": "success", "response": result }))

    def cagefsctl(self):
        if self.request_data['owner'] != 'admin':
            return
        cagefs = CageFs()
        method = self.request_data.get('method')
        if method:
            func = {
                'status': cagefs.status,
                'init': cagefs.init,
                'update': cagefs.update,
                'enable': lambda: cagefs.change_status('enable', self.request_data['params'].get('users')),
                'disable': lambda: cagefs.change_status('disable', self.request_data['params'].get('users')),
                'log': lambda: cagefs.get_log(self.request_data['params'].get('operation')),
                'download-log': lambda: cagefs.download_log(self.request_data['params'].get('operation')),
                'log_data': lambda: cagefs.cagefs_log_data(self.request_data['params'].get('operation')),
            }.get(method)
            print(json.dumps({"result": "success", "response": func()}))
        else:
            list_to_request = self.prepair_params_for_command(with_json=False)
            response = cagefs.run(list_to_request)
            print(json.dumps({"result": "success", "response": response}))
        sys.exit(0)

    def cldiag(self):
        params = ['/usr/bin/cldiag'] + self.prepair_params_for_command()
        if len(params) <= 2:
            params.append('--all')
        result = None
        try:
            with open(os.devnull, 'w') as devnull:
                output = subprocess.check_output(params, stderr=devnull, shell=False, text=True)
            result = json.loads(output)
        except subprocess.CalledProcessError as e:
            try:
                result = json.loads(e.output)
            except:
                result = e.output
        except OSError as e:
            self.exit_with_error('Can\'t call cldiag: ' + str(e))
        if result:
            print(json.dumps({
                'result': 'success',
                'data': result}))
        sys.exit(0)

    def cl_support(self):
        if self.request_data['owner'] != 'admin':
            return
        from libsupport import ZendeskAPIError, send_to_support
        try:
            ticket_url = send_to_support(self.request_data)
        except ZendeskAPIError as e:
            self.exit_with_error(e.message)
        print(json.dumps({
            "result": "success",
            "ticketUrl": ticket_url
        }))
        sys.exit(0)

    def cl_wizard(self):
        if self.request_data['owner'] != 'admin':
            return
        method = self.request_data.get('method')
        if not method:
            self.exit_with_error('Module unavailable', ignore_errors=True)
        elif method == 'status' and 'initial' not in self.request_data.get('params', {}):
            self.wizard_get_status()
        elif method == 'status' and 'initial' in self.request_data.get('params', {}):
            self.wizard_get_initial()
        elif method == 'log':
            self.wizard_log()
        elif method == 'log_data':
            self.wizard_log_data()
        elif method == 'finish':
            self.wizard_finish()
        else:
            list_to_request = self.prepair_params_for_command(with_json=False)
            try:
                print(self.run_util(CL_WIZARD_CLI, *list_to_request))
            except Exception as e:
                self.exit_with_error(traceback.format_exc())
        sys.exit(0)

    def spa_detect_first_installation(self):
        print(json.dumps({
            "result":"success",
            "is_first_installation": os.path.exists(FIRST_INSTALL_FLAG)
        }))
        sys.exit(0)

    def is_activated_reseller(self):
        username = self.user_info['username']
        if self.resellers is None:
            p = subprocess.Popen(
                ['lvectl', 'list-reseller', '--with-name', '--json'],
                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            (res_in_json, err) = p.communicate()
            if p.returncode != 0:
                self.check_license()
            try:
                self.resellers = [reseller.get('ID', '').split(':')[-1] \
                    for reseller in json.loads(res_in_json)['data']]
            except:
                self.resellers = []
        return username in self.resellers

    def is_user_in_admin_list(self):
        """
        Check user in admin list (disable reseller plugin for admin in DA)
        """
        if getCPName() == DIRECTADMIN_NAME:
            if is_admin(self.user_info['username']):
                return True
        return False

    def _is_statistics_enabled_by_admin(self):
        # FIXME: move to cllib
        return detect.get_boolean_param(detect.CL_CONFIG_FILE, 'cl_statistics_enabled')

    def checkIsFirstInstall(self):
        return  os.path.exists(FIRST_INSTALL_FLAG)

    def wizard_get_status(self):
        if not self.checkIsFirstInstall() or os.path.exists(DONE_FLAG):
            self.exit_with_success({'wizard_status': 'finish'})
        else:
            try:
                result = self.run_util(CL_WIZARD_CLI, 'status', ignore_errors=True)
                data = json.loads(result)
                if data.get('wizard_status') == 'idle':
                    self.exit_with_success({'wizard_status': 'first_install'})
                else:
                    print(result)
            except ValueError as e:
                print(result)
            except Exception as e:
                self.exit_with_error(traceback.format_exc())

    def wizard_get_initial(self):
        result = self.run_util(CL_WIZARD_CLI, 'status', '--initial', ignore_errors=True)
        data = json.loads(result)
        if 'RPM DB check error' in data.get('result'):
            self.exit_with_error('RPM DB is Corrupted', context={
                'message': data.get('result'),
            })
        else:
            print(result)
            sys.exit(0)

    def wizard_log(self):
        """
        Check log file exists and return filename to wrapper to seed
        """
        filepath = self._get_wizard_log_file()
        print(json.dumps({
            "result": "file",
            "filepath": filepath,
            "filesize": os.path.getsize(filepath)
        }))

    def wizard_log_data(self):
        """
        Check log file exists and return filename to wrapper to seed
        """
        filepath = self._get_wizard_log_file()
        with open(filepath) as log_file:
            shutil.copyfileobj(log_file, sys.stdout, 20*1024)
        sys.exit(0)

    def _get_wizard_log_file(self):
        module_name = self.request_data.get('params', {}).get('module')
        data = self.run_util(CL_WIZARD_CLI, 'status')
        data = json.loads(data)
        if module_name == 'crash':
            filepath = data.get('crash_log')
        else:
            filepath = next(
                (module.get('log_file')
                 for module in data.get('modules', [])
                 if module.get('name') == module_name
                ), None)
        if not filepath:
            self.exit_with_error("Log file {} not found".format(filepath))
        if not os.path.isfile(filepath):
            self.exit_with_error("File {} does not exists".format(filepath))
        if not os.access(filepath, os.R_OK):
            self.exit_with_error("File {} not available for reading".format(filepath))
        return filepath

    def wizard_finish(self):
        if os.path.exists(FIRST_INSTALL_FLAG):
            os.remove(FIRST_INSTALL_FLAG)
        print(self.run_util(CL_WIZARD_CLI, 'finish', ignore_errors=True))
        sys.exit(0)

    def lvectl(self):
        """run lvectl command with arguments"""
        POSITION_ARGUMENTS_LIST = ['lveid', 'username', 'package', 'pid']
        REPLACE_METHODS = {
            'apply-all': 'apply all',
            'destroy-all': 'destroy all',
        }
        position_arguments = []
        params = self.request_data.get('params', {})
        if params.get('lveid') is not None and params.get('username') is not None:
            print(json.dumps({'error': 'lveid and username should not be presented together'}))
            exit(0)
        if self.request_data.get('method') in REPLACE_METHODS:
            self.request_data['method'] = REPLACE_METHODS.get(self.request_data['method'])
        for argument in POSITION_ARGUMENTS_LIST:
            value = params.pop(argument, None)
            if value:
                position_arguments.append(value)
        if position_arguments:
            self.request_data['method'] = ' '.join([self.request_data.get('method', '')] + position_arguments)
        stdin = params.pop('stdin', None)
        list_to_request = self.prepair_params_for_command()
        try:
            print(self.run_util('/usr/sbin/lvectl', *list_to_request, stdin=stdin))
        except Exception as e:
            self.exit_with_error(traceback.format_exc())

    def agent(self):
        """
        Agnet requests
        :return:
        """

        def xray_error_checker(json_result):
            return json_result.get('status') != 'ok'

        list_to_request = self.prepair_params_for_command(with_json=False)
        try:
            response = self.run_util(XRAY_AGENT_CLI, *list_to_request, error_checker=xray_error_checker)
            response = json.loads(response)
            self.exit_with_success(response)
        except Exception as e:
            self.exit_with_error(traceback.format_exc())

    def cl_autotracing(self):
        """
        Run cloudlinux-autotracing
        :return: Any
        """
        list_to_request = self.prepair_params_for_command(with_json=False)
        try:
            response = self.run_util(CL_AUTOTRACING_CLI, *list_to_request)
            response = json.loads(response)
            self.exit_with_success(response)
        except Exception as e:
            self.exit_with_error(traceback.format_exc())

    def get_jwt(self):
        """
        Get JSON Web Token from file /etc/sysconfig/rhn/jwt.token
        :return: string
        """
        try:
            if self.request_data['owner'] == 'admin':
                with open(self.JWT_FILE, 'r') as f:
                    return f.read().strip()
            return ""
        except:
            return ""

    @staticmethod
    def compare_versions(ver1, ver2):
        """
        :param ver1: string
        :param ver2: string
        :return: True if first version is bigger or equal to the second
        """
        ver1_tuple = tuple(map(int, ver1.split(".")))
        ver2_tuple = tuple(map(int, ver2.split(".")))
        return ver1_tuple >= ver2_tuple

    def merge_dicts(self, dict1, dict2):
        """
        Merge two dicts and return merge result
        """
        result = dict1.copy()
        result.update(dict2)
        return result

    def read_json_config_file(self, config_path):
        """
        Read json file and return result
        """
        try:
            with open(config_path, 'r') as config_file:
                return json.load(config_file)
        except FileNotFoundError:
            return {}

    def cl_xray(self):
        """
        Check status and install xray utility
        Return status and server_id
        """

        list_to_request = self.prepair_params_for_command(with_json=False)
        util_response = json.loads(self.run_util(XRAY_CLI, *list_to_request))
        if util_response.get('result') == 'success':
            self.exit_with_success(util_response)
        else:
            self.exit_with_error(util_response.get('response'))

    def get_available_cl_features(self):
        supported_features = get_supported_cl_features()

        # Rewrite supported features for UI with custom config
        if os.path.isfile(CL_MANAGER_UI_SETTINGS):
            config = self.read_json_config_file(CL_MANAGER_UI_SETTINGS)
            features_from_config = config.get("features_override", {})
            return self.merge_dicts(supported_features, features_from_config)
        return supported_features

    def get_supported_features_for_ui(self):
        features = self.get_available_cl_features()
        return {
            UIFeature.PHP_SELECTOR: features.get(Feature.PHP_SELECTOR, False),  # TODO: Should be replaced to is_php_supported()
            UIFeature.RUBY_SELECTOR: features.get(Feature.RUBY_SELECTOR, False),
            UIFeature.PYTHON_SELECTOR: features.get(Feature.PYTHON_SELECTOR, False),
            UIFeature.NODEJS_SELECTOR: features.get(Feature.NODEJS_SELECTOR, False),
            UIFeature.GOVERNOR: self.is_governor_available(features),
            UIFeature.CAGEFS: features.get(Feature.CAGEFS, False),
            UIFeature.LVE: features.get(Feature.LVE, False),
            UIFeature.RESELLER_LIMITS: features.get(Feature.RESELLER_LIMITS, False),
            UIFeature.XRAY: features.get(Feature.XRAY, False),
            UIFeature.ACCELERATE_WP: features.get(Feature.WPOS, False),
            UIFeature.WIZARD: features.get(Feature.WIZARD, False),
            UIFeature.LSAPI: features.get(Feature.LSAPI, False),
            UIFeature.LINKING_TO_CLN: features.get(UIFeature.LINKING_TO_CLN, True),
            UIFeature.WEBSITE_MONITORING: features.get(UIFeature.WEBSITE_MONITORING, True),
        }

    def is_ssa_installed(self):
        """
        Check if Slow Site Analyzer availability
        """
        ssa_binary_present = os.path.exists(SSA_CLI)
        return ssa_binary_present

    def is_wmt_installed(self):
        """
        Check if Website Monitoring availability
        """
        return os.path.exists(WMT_API_CLI)

    def is_smart_advice_installed(self):
        """
        Check Smart Advice availability
        """
        smart_advice_binary_present = os.path.exists(SMART_ADVICE_CLI)
        return smart_advice_binary_present

    def is_xray_installed(self):
        """
        Check X-Ray availability
        """
        xray_binary_present = all([os.path.isfile(XRAY_AGENT_CLI) and os.path.isfile(XRAY_USER_AGENT_CLI)])
        return xray_binary_present

    def is_awp_installed(self):
        """
        Check AccelerateWP availability
        """
        awp_plugin_installed = os.path.exists(AWP_PLUGIN_PATH)
        return awp_plugin_installed

    def is_governor_available(self, features):
        """
        Check if Governor available in UI
        """
        is_governor = features.get(Feature.GOVERNOR, False)
        is_lve = features.get(Feature.LVE, False)
        return all([is_governor, is_lve])

    def external_info(self):
        """
        Return external info based on the owner type (admin or reseller).
        """
        if self.request_data['owner'] == 'admin':
            self._get_admin_external_info()
        elif self.request_data['owner'] == 'reseller':
            self._get_reseller_external_info()
        else:
            self.exit_with_error('User not allowed')

    def _get_admin_external_info(self):
        """
        Gather and return external info needed for admin.
        """
        from clcommon.utils import get_rhn_systemid_value
        system_id = get_rhn_systemid_value('system_id') or ''
        if system_id:
            system_id = system_id.split('-')[1]
        jwt = self.get_jwt()
        is_valid, _, _ = jwt_token_check()
        self.exit_with_success({
            'system_id': system_id,
            'jwt': jwt,
            'cl_plus': is_valid,
            'is_cl_solo_edition': is_cl_solo_edition(skip_jwt_check=True),
            'xray_staging_enabled': os.path.isfile(XRAY_STAGING_ENABLED_FLAG),
            'awp_premium': os.path.isfile(AWP_PREMIUM_ENABLED_FLAG),
            'awp_plugin_version': self.get_awp_plugin_version(),
            'xray_plugin_version': self.get_xray_plugin_version(),
            'plugin_installed': True,
            'autotracing': os.path.isfile(CL_AUTOTRACING_CLI),
            'awp_migration_needed': os.path.isfile(AWP_MIGRATION_NEEDED_FLAG),
            'cl_edition': get_cl_edition_readable(),
            'supported_cl_features': self.get_supported_features_for_ui(),
            'xray_installed': self.is_xray_installed(),
            'accelerate_wp_installed': self.is_awp_installed(),
            'ssa_installed': self.is_ssa_installed(),
            'wmt_installed': self.is_wmt_installed(),
            'awp_installed': self.is_awp_installed(),
            'smart_advice_installed': self.is_smart_advice_installed(),
            'server_ip': self.get_server_ip(),
        })

    def _get_reseller_external_info(self):
        """
        Gather and return external info needed for reseller.
        """
        self.exit_with_success({
            'is_cl_solo_edition': is_cl_solo_edition(skip_jwt_check=True),
            'cl_edition': get_cl_edition_readable(),
            'supported_cl_features': self.get_supported_features_for_ui(),
            'server_ip': self.get_server_ip(),
        })

    def wmt_api(self):
        list_to_request = self.prepair_params_for_command(with_json=False)
        util_response = json.loads(self.run_util(WMT_API_CLI, *list_to_request))
        if util_response.get('result') == 'success':
            self.exit_with_success(util_response)
        else:
            self.exit_with_error(util_response.get('response'))

    def cl_ssa(self):
        list_to_request = self.prepair_params_for_command(with_json=False)
        util_response = json.loads(self.run_util(SSA_CLI, *list_to_request))
        if util_response.get('result') == 'success':
            self.exit_with_success(util_response)
        else:
            self.exit_with_error(util_response.get('response'))

    def cl_log(self):
        """
        Writes log message to the log file
        :return:
        """
        message = self.request_data.get('params', {}).get('message')
        if message:
            logging.basicConfig(filename='/var/log/cloudlinux/lvemanager.log',
                                level=logging.INFO,
                                format='%(asctime)s %(message)s')
            logging.info(message)
        self.exit_with_success()

    def cloudlinux_awp_admin(self):
        """
        Run cloudlinux-awp-admin utility with args
        """
        list_to_request = self.prepair_params_for_command(with_json=False)
        util_response = json.loads(self.run_util(AWP_CLI_ADMIN, *list_to_request))
        if util_response.get('result') == 'success':
            self.exit_with_success(util_response)
        else:
            self.exit_with_error(util_response.get('response'))

    def cl_smart_advice(self):
        list_to_request = self.prepair_params_for_command(with_json=False)
        # Workaround to run the command in background
        if '--async' in list_to_request:
            subprocess.Popen([SMART_ADVICE_CLI, *list_to_request], stdin=subprocess.PIPE,stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE, text=True)
            self.exit_with_success()
        util_response = json.loads(self.run_util(SMART_ADVICE_CLI, *list_to_request))
        if util_response.get('result') == 'success':
            self.exit_with_success(util_response)
        else:
            self.exit_with_error(util_response.get('response'))

    def cl_link_to_cln(self):
        list_to_request = self.prepair_params_for_command(with_json=False)
        util_response = json.loads(self.run_util(CL_LINK_TO_CLN_CLI, *list_to_request))
        if util_response.get('result') == 'success':
            self.exit_with_success(util_response)
        else:
            self.exit_with_error(util_response.get('response'))

    def cl_awp_installer(self):
        """
        Run commands for AccelerateWP
        """
        list_to_request = self.prepair_params_for_command(with_json=False)
        util_response = json.loads(self.run_util(AWP_INSTALLER_CLI, *list_to_request))
        if util_response.get('result') == 'success':
            self.exit_with_success(util_response)
        else:
            self.exit_with_error(util_response.get('response'))

    def get_awp_plugin_version(self):
        """
        Get version of the AccelerateWP plugin
        """
        try:
            version_file = AWP_PLUGIN_PATH + '/version'
            with open(version_file, 'r') as f:
                return f.read().strip()
        except:
            return None

    def get_xray_plugin_version(self):
        """
        Get version of the X-Ray plugin
        """
        try:
            version_file = XRAY_PLUGIN_PATH + '/version'
            with open(version_file, 'r') as f:
                return f.read().strip()
        except:
            return None

Zerion Mini Shell 1.0