%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /opt/cloudlinux/venv/lib/python3.11/site-packages/clwpos/php/
Upload File :
Create Path :
Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/clwpos/php/alt_php.py

from __future__ import absolute_import

import os
import re
import subprocess
from typing import List

from clwpos.php.base import PHP

SELECTOR_UTIL = "/usr/bin/selectorctl"

def create_generic_php(alt_php_identifier: str, root_path=None, base_cls = None):
    if root_path is None:
        # create alt versions by default
        php_root_dir = f'/opt/{alt_php_identifier.replace("-", "/")}/'
    else:
        php_root_dir = root_path

    base_cls = base_cls if base_cls else PHP
    return base_cls(
        identifier=alt_php_identifier,
        version=f'{alt_php_identifier[-2]}.{alt_php_identifier[-1]}',
        modules_dir=os.path.join(php_root_dir, 'usr/lib64/php/modules/'),
        dir=php_root_dir,
        bin=os.path.join(php_root_dir, 'usr/bin/php'),
        ini=os.path.join(php_root_dir, 'link/conf/default.ini')
    )


def get_alt_php_versions() -> List[PHP]:
    """
    Get list of installed alt-php versions.
    """
    alt_dir = '/opt/alt'
    pattern = re.compile(r'^php\d{2}$')
    alt_php_versions = [
        create_generic_php(f'alt-{dirname}') for dirname in os.listdir(alt_dir)
        if pattern.match(dirname)
    ]
    return alt_php_versions

def disable_selector_extensions(ext_list, version, logger):
    """
    To turn off extensions in selector for avoiding them loading in separate places!!!
    """
    target_users = []
    users_with_version = subprocess.run([SELECTOR_UTIL, "--list-users", "--version", version],
                                        capture_output=True,
                                        text=True).stdout
    if users_with_version:
        target_users = [line.strip() for line in users_with_version.strip().split(",") if line]
    if not target_users:
        logger.info("No users found using selector with version %s", version)
        return

    logger.info("Turning off those extensions=%s for version %s, for users=%s",
                 str(ext_list),
                 version,
                 str(target_users))
    for user in target_users:
        disable_result = subprocess.run([SELECTOR_UTIL, "--disable-user-extensions",
                                         ",".join(ext_list),
                                         "--version", version,
                                         "--user", user],
                                            capture_output=True,
                                            text=True)
        logger.info("Disabling extension result=%s for user=%s", str(disable_result), user)

def update_selector_built_in_extensions(logger):
    """
    To make extension non-editable in CloudLinux Selector (cannot turn it off manually)
    """
    update_res = subprocess.run(["/usr/share/l.v.e-manager/utils/cache_phpdata.py"],
                   capture_output=True,
                   text=True)
    logger.info("Selector built-in extensions updated=%s", str(update_res))

Zerion Mini Shell 1.0