%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /opt/cloudlinux/venv/lib64/python3.11/site-packages/clwpos/
Upload File :
Create Path :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/clwpos/wpos_hooks.py

#!/opt/cloudlinux/venv/bin/python3 -bb
# -*- coding: utf-8 -*-

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

import subprocess
import sys
from distutils.sysconfig import get_python_lib
from pathlib import Path

from clwpos.constants import (
    ALT_PHP_REDIS_ENABLE_UTILITY,
    INSTALL_CACHING_HOOKS_UTILITY,
)
from clwpos.optimization_features import OBJECT_CACHE_FEATURE
from clwpos.feature_suites import (
    any_suite_allowed_on_server,
    is_module_allowed_for_user
)
from clwpos.utils import is_wpos_supported

UNIVERSAL_HOOK_PATH_DNF = '/etc/dnf/universal-hooks/multi_pkgs/transaction'
UNIVERSAL_HOOK_PATH_YUM = '/etc/yum/universal-hooks/multi_pkgs/posttrans'
HOOKS_LISTENERS_DIR = '/usr/share/cloudlinux/hooks/listeners'

wpos_hooks = ('wpos_modify_user_hook.py',)


def get_yum_universal_hook_alt_php_path() -> Path:
    """
    Get path to yum universal hooks directory
    with alt-php*-pecl-ext hooks.
    """
    dir_name = 'alt-php__WILDCARD__-pecl-ext'
    if Path('/etc/dnf/').exists():
        return Path(UNIVERSAL_HOOK_PATH_DNF, dir_name)
    return Path(UNIVERSAL_HOOK_PATH_YUM, dir_name)


def install_yum_universal_hook_alt_php() -> None:
    """
    Install yum universal hook for configuring PHP redis
    after alt-php*-pecl-ext package is installed/updated.
    """
    hook_dir_path = get_yum_universal_hook_alt_php_path()
    hook_dir_path.mkdir(parents=True, exist_ok=True)
    hook_name = Path(ALT_PHP_REDIS_ENABLE_UTILITY).name
    hook_full_path = Path(hook_dir_path, hook_name)
    if not hook_full_path.exists():
        hook_full_path.symlink_to(ALT_PHP_REDIS_ENABLE_UTILITY)


def uninstall_yum_universal_hook_alt_php() -> None:
    """
    Remove yum universal hook for configuring PHP redis ext.
    """
    hook_dir_path = get_yum_universal_hook_alt_php_path()
    hook_name = Path(ALT_PHP_REDIS_ENABLE_UTILITY).name
    hook_full_path = Path(hook_dir_path, hook_name)
    # check is_symlink because we want to delete link even if it's broken
    if hook_full_path.is_symlink():
        hook_full_path.unlink()


def install_panel_hooks() -> None:
    """
    Install panel WPOS hooks.
    """
    subprocess.run([INSTALL_CACHING_HOOKS_UTILITY, '-i'], capture_output=True)

    lve_utils_hooks_dir = Path(get_python_lib(), 'clwpos', 'hooks')
    for hook in wpos_hooks:
        listeners_hook_path = Path(HOOKS_LISTENERS_DIR, hook)
        lve_utils_hook_path = Path(lve_utils_hooks_dir, hook)
        # remove old hook pointing to symlink to lve_utils
        if 'lve_utils' in str(listeners_hook_path.resolve()) or \
                'python3.7' in str(listeners_hook_path.resolve()):
            listeners_hook_path.unlink()
        if not listeners_hook_path.exists() and lve_utils_hook_path.exists():
            listeners_hook_path.symlink_to(lve_utils_hook_path)


def uninstall_panel_hooks() -> None:
    """
    Remove panel WPOS hooks.
    """
    subprocess.run([INSTALL_CACHING_HOOKS_UTILITY, '-d'], capture_output=True)

    for hook in wpos_hooks:
        listeners_hook_path = Path(HOOKS_LISTENERS_DIR, hook)
        if listeners_hook_path.is_symlink():
            listeners_hook_path.unlink()


def _install_hooks():
    if is_wpos_supported() and any_suite_allowed_on_server():
        if is_module_allowed_for_user(OBJECT_CACHE_FEATURE):
            install_yum_universal_hook_alt_php()
        install_panel_hooks()


def _uninstall_hooks():
    uninstall_yum_universal_hook_alt_php()
    uninstall_panel_hooks()


def main():
    """
    Install or uninstall panel and yum/dnf universal hooks.
    """
    if '--install' in sys.argv:
        _install_hooks()
    elif '--uninstall' in sys.argv:
        _uninstall_hooks()


if __name__ == "__main__":
    main()

Zerion Mini Shell 1.0