PNG  IHDR  8] PLTE S =tRNS   PNG  IHDR  8] PLTE S =tRNS   REDROOM
PHP 7.4.33
Preview: multiphp_reconfigure.py Size: 7.70 KB
//opt/alt/alt-php-config/multiphp_reconfigure.py

#!/usr/bin/python3
# -*- mode:python; coding:utf-8; -*-
# author: Dmitriy Kasyanov <dkasyanov@cloudlinux.com>

import getopt
import glob
import logging
import os
import sys
import traceback
try:
        import configparser
except ImportError:
        import ConfigParser as configparser

try:
    import rpm
except:
    class rpm:
        @staticmethod
        def labelCompare(version1, version2):
            def index_exists(_list, i):
                return (0 <= i < len(_list)) or (-len(_list) <= i < 0)

            res = 0
            ver1 = version1[1].split('.')
            ver2 = version2[1].split('.')
            max_num = len(ver1)
            if len(ver2) > max_num:
                max_num = len(ver2)
            i = 0
            while i < max_num:
                if index_exists(ver1,i) and index_exists(ver2,i):
                    if ver1[i] == ver2[i]:
                        res=0
                    else:
                        return int(ver1[i]) - int(ver2[i])
                elif index_exists(ver1,i) and not index_exists(ver2,i):
                    return 1
                elif index_exists(ver2,i) and not index_exists(ver1,i):
                    return -1
                i += 1
            return 0

# Minimal version with MultiPHP support for alt-php
MIN_CPANEL_VERSION = '11.66.0.11'
SCL_PREFIX_PATH = '/etc/scl/prefixes'
CONFIG_PATH = '/opt/alt/alt-php-config/alt-php.cfg'


def configure_logging(verbose):
    """
    Logging configuration function.

    @type verbose:  bool
    @param verbose: Enable additional debug output if True, display only errors
        otherwise.
    """
    if verbose:
        level = logging.DEBUG
    else:
        level = logging.ERROR
    handler = logging.StreamHandler()
    handler.setLevel(level)
    log_format = "%(levelname)-8s: %(message)s"
    formatter = logging.Formatter(log_format, "%H:%M:%S %d.%m.%y")
    handler.setFormatter(formatter)
    logger = logging.getLogger()
    logger.addHandler(handler)
    logger.setLevel(level)
    return logger


def get_cpanel_version():
    """
    Returns cPanel version if cPanel installed or None othervise.

    @rtype: str or None
    @return: String with cPanel version or None if cPanel not installed.
    """
    if os.path.exists('/usr/local/cpanel/version'):
        with open('/usr/local/cpanel/version', 'r') as fd:
            version = fd.read()
            return version
    return None


def find_interpreter_versions():
    """
    Returns list of installed alt-php versions and their base directories.

    @rtype:  list
    @return: List of version (e.g. 44, 55) and base directory tuples.
    """
    int_versions = []
    base_path_regex = "/opt/alt/php[0-9][0-9]"
    for int_dir in glob.glob(base_path_regex):
        if os.path.exists(os.path.join(int_dir, "usr/bin/php")):
            int_versions.append((int_dir[-2:], int_dir))
    int_versions.sort()
    return int_versions


def delete_prefix(prefix_path):
    """
    Remove prefix file
    @type prefix_path: str
    @param prefix_path: Path to the prefix file, e.g. /etc/scl/prefix/alt-php70

    @rtype: bool
    @return: True if file was removed sucessfully, False otherwise
    """
    try:
        if os.path.exists(prefix_path):
            os.unlink(prefix_path)
        return True
    except OSError as e:
        logging.error(u"Couldn't remove prefix %s:\n%s" % (prefix_path, e))
        return False


def create_prefix(prefix_path, prefix_content):
    """
    Creates prefix with path to alt-php
    @type prefix_path: str
    @param prefix_path: Path to the prefix file, e.g. /etc/scl/prefix/alt-php70
    @type prefix_content: str
    @param prefix_content: SCL path, e.g. /opt/cloudlinux

    @rtype: bool
    @return: True if file was created sucessfully, False otherwise
    """
    try:
        with open(prefix_path, 'w') as fd:
            fd.write(prefix_content)
    except IOError as e:
        logging.error(u"Couldn't open file %s:\n%s" % (prefix_path, e))
        return False
    return True


def reconfigure(config, int_version):
    """

    @type config:
    @param config:
    @type int_version: str
    @param int_version: Interpreter version (44, 55, 72, etc.)
    @type int_path: str
    @param int_path: Interpreter directory on the disk (/opt/alt/php51, etc.)

    @rtype: bool
    @return: True if reconfiguration was successful, False otherwise
    """
    cp_version = get_cpanel_version()
    prefix_name = "alt-php%s" % int_version
    prefix_path = os.path.join(SCL_PREFIX_PATH, prefix_name)
    prefix_content = "/opt/cloudlinux\n"
    alt_php_enable_file = "/opt/cloudlinux/alt-php%s/enable" % int_version

    if cp_version and rpm.labelCompare(
            ('1', cp_version, '0'), ('1', MIN_CPANEL_VERSION, '0')) < 0:
        status = delete_prefix(prefix_path)

    else:
        try:
            int_enabled = config.getboolean("MultiPHP Manager", prefix_name)
        except configparser.NoOptionError as e:
            int_enabled = True
            logging.warning("Prefix %s doesn't exist in %s:\n" % (prefix_name,
                                                                  CONFIG_PATH))
            config.set("MultiPHP Manager", prefix_name, "yes")
            with open(CONFIG_PATH, 'w') as configfile:
                config.write(configfile)
        except configparser.NoSectionError as e:
            int_enabled = True
            config.add_section('MultiPHP Manager')
            config.set("MultiPHP Manager", prefix_name, "yes")
            with open(CONFIG_PATH, 'w') as configfile:
                config.write(configfile)
            print("Config %s is broken:\nCreated it.\n" % (CONFIG_PATH))

        if int_enabled and os.path.exists(alt_php_enable_file):
            status = create_prefix(prefix_path, prefix_content)
        elif not int_enabled and os.path.exists(prefix_path):
            status = delete_prefix(prefix_path)
        else:
            return True
    return status



def check_alt_path_exists(int_path, int_name, int_ver):
    """
    Check if alt-php path exist
    ----------
    @type int_path:  str or unicode
    @param int_path: Interpreter directory on the disk (/opt/alt/php51, etc.)
    @type int_name:  str or unicode
    @param int_name: Interpreter name (php, python)
    @type int_ver:   str or unicode
    @apram int_ver:  Interpreter version (44, 55, 72, etc.)

    @rtype: bool
    @return: True if interpreter path exists, False otherwise

    """
    if not os.path.isdir(int_path):
        sys.stderr.write("unknown {0} version {1}".format(int_name, int_ver))
        return False
    return True


def main(sys_args):
    try:
        opts, args = getopt.getopt(sys_args, "p:v", ["php=", "verbose"])
    except getopt.GetoptError as e:
        sys.stderr.write("cannot parse command line arguments: {0}".format(e))
        return 1
    verbose = False
    int_versions = []
    int_name = "php"
    for opt, arg in opts:
        if opt in ("-p", "--php"):
            int_name = "php"
            int_path = "/opt/alt/php%s" % arg
            if check_alt_path_exists(int_path, int_name, arg):
                int_versions.append((arg, int_path))
            else:
                return 1
        if opt in ("-v", "--verbose"):
            verbose = True
    log = configure_logging(verbose)
    config = configparser.ConfigParser()
    config.read(CONFIG_PATH)

    try:
        if not int_versions:
            int_versions = find_interpreter_versions()
        for int_ver, int_dir in int_versions:
            reconfigure(config, int_ver)
        return 0
    except Exception as e:
        log.error(u"cannot reconfigure alt-%s for SCL: %s. "
                  u"Traceback:\n%s" % (int_name, e,
                                       traceback.format_exc()))
        return 1


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

Directory Contents

Dirs: 1 × Files: 5

Name Size Perms Modified Actions
- drwxr-xr-x 2026-03-10 18:24:44
Edit Download
8.53 KB lrwxr-xr-x 2026-01-12 22:18:07
Edit Download
308 B lrw-r--r-- 2026-03-24 10:18:33
Edit Download
687 B lrwxr-xr-x 2026-01-12 22:18:07
Edit Download
530 B lrwxr-xr-x 2026-01-12 22:12:42
Edit Download
7.70 KB lrwxr-xr-x 2026-01-12 22:18:07
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).
 !"#$%&'(()*+,-./00123456789 t\ wIDATx ]ys  47Y ƒ -  "  Rv  < f{Ɛ $k l L > L  ~h^ 1  [  r G t& h  l F z3O Y ! p A(_g̷ E8 )S 8 c  Kb"z ~ 5 J xAL WU <  *  5 m;W a pB h ~P J 2 3 6 ҙ .Ƹ P i  4g F R L P ΪK/D  M v (a3 k J Œ4N5* SH ` SdJ z  O J Xՠ V>u ߱ BE&L b2 ?2` tX+  c CB A$ i b C ĀMB E : /  # Dx &l =q Ty  0 \p I ( L Ǎ { e 4k ;`u^ヲ eP!( d {  )T A 8 O;Ě n >;s6 !  :Nx `[S D HU ~ q›J F} a g*D 49 / pn k h (t 8NxƐF _!r չ7 ZR R׷ q/5") Ӎ NY 0 x sZ!   o  fu  ,  K"$ ? pg  㕣=  1» {h " fh7    y  } € +7  $ y " X —ą - G P u 4 m >J 5 L =V ' ^@I p ?MS xЌ XV P ! h "C NS9B8̢ ]!K  e   zA , ӏkbY  !< XQ ٿyS| *" f { w  4@[S <  # 0 ! js [m  =,~ o "ݎ DHf Wo $ g ! Vԅ t mB /y Wf V4񺍸 c+@x?  B ~u " xUN e 0 BĂ) ~J pz! 7y6]l Ԥ@ P a< O /DHC `≻  N m"$  0ObB }{ x AO FCG D R ^ "B  { WDH  UR l@ T #  +"d T ; 0 i  D}. 7 ` ' ] w rE &S i ƕiTD EL P _ u h $ Ա FG wVD G L R Zf ' .!] J /ZR oGЍs Mr Ĥ ʬ 3 Q [3 cL ` ^ p + ( F;# B 5 '  2Y f [  ϶R0e }  E 7 6M aۮ H <& n % L] E}Up x紉, Uw' Q  Ǯշo k ވۙ 0N94 VX5 xEDE l D #֤ } C o )W :  ^ s 9  bRf iX5u ཱི 4 :[  T 1. | [E 2ؽ Iy\ : o x K G 5 ylP ' uK E ftb/i[3 .g _  [3M n G, #NwQ5~  ؚ) | n =Ц"x qg gB ` 듘 ~ x w ? ? R~  _ u. &VQ K˻   H C ( TN˄+ `C dA nB׭ D 3"Z G ê ^k H_ /- ~ " R_  .8 Z_ 6@ o  xg  uP ? 3լ @7AM!  E7^ - =V L  x  g-D0  CtmW 7  O  G _ WD0 g C  w1 r d w : a | \  *" f nֳ ^ H# f L ` Z ۽hV  }S F r0Ù Bć5r] @! NL iQ]{s^=4 d  WD  "  "   M; t" 8 5 e dL| "-*st" ) SWD ?R[S e ooF  20.D ? bo =) A i o d ģ ZҰaO @E =) i D a &ܟa CϞ y6 ,<%{^x%{f8? `iw^ ?/ M * IEND B`