ok

Mini Shell

Direktori : /opt/alt/python37/lib/python3.7/site-packages/clconfigure/cli/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/clconfigure/cli/cloudlinux-reconfigure.py

#!/opt/cloudlinux/venv/bin/python -bb
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# https://cloudlinux.com/docs/LICENCE.TXT
#
from __future__ import absolute_import
import argparse
import enum
import logging
import sys

from clconfigure import task
from clconfigure.limits import set_default_limits_state
from clconfigure.services import set_service_state, STATE_RUNNING, STATE_MASKED, STATE_STOPPED, STATE_UNMASKED
from clconfigure.packages import set_package_state, STATE_INSTALLED, STATE_REMOVED
from lve_utils.sentry import init_lve_utils_sentry_client
from clconfigure import setup_logger
from clcommon.lib import cledition

from clconfigure.awp import (
    enable_accelerate_wp_free
)

LOGS_PATH = '/var/log/cloudlinux/clcustomizer.log'

CONFLICTED_PACKAGES = (
    'mod_hostinglimits',
    'ea-apache24-mod_hostinglimits',
    'httpd24-mod_hostinglimits',
    'governor-mysql',
)


class TargetEdition(enum.Enum):
    ADMIN = 'admin'
    AUTO = 'auto'
    CONTAINER = 'container'
    SHARED = 'shared'
    SHARED_PRO = 'shared_pro'
    SOLO = 'solo'


def _disable_lve_services():
    """
    Turn off all lve-related services.
    """
    set_service_state(STATE_STOPPED, 'lve')
    set_service_state(STATE_STOPPED, 'lve_namespaces')
    set_service_state(STATE_STOPPED, 'lvestats')
    set_service_state(STATE_STOPPED, 'lvectl')
    set_service_state(STATE_MASKED, 'lve')
    set_service_state(STATE_MASKED, 'lve_namespaces')
    set_service_state(STATE_MASKED, 'lvestats')
    set_service_state(STATE_MASKED, 'lvectl')


def _enable_lve_services():
    set_service_state(STATE_UNMASKED, 'lve')
    set_service_state(STATE_UNMASKED, 'lve_namespaces')
    set_service_state(STATE_UNMASKED, 'lvestats')
    set_service_state(STATE_UNMASKED, 'lvectl')
    set_service_state(STATE_RUNNING, 'lve')
    set_service_state(STATE_RUNNING, 'lve_namespaces')
    set_service_state(STATE_RUNNING, 'lvectl')
    set_service_state(STATE_RUNNING, 'lvestats')


@task("Configuring environment for CloudLinux OS Solo: is_posttrans='{is_posttrans}'")
def install_cl_solo(is_posttrans):
    """
    Converts "normal" CloudLinux into CL Solo by disabling
    some services and installing some packages.
    :param is_posttrans:
        When true, we must do only actions that need to be done during
        %posttrans section of cloudlinux-solo-meta package
    """
    # it seems that install_cl_solo is never called with is_posttrans==False
    if not is_posttrans:
        for conflicted_package in CONFLICTED_PACKAGES:
            set_package_state(STATE_REMOVED, conflicted_package)
        set_package_state(STATE_INSTALLED, 'cloudlinux-solo-meta')
        enable_accelerate_wp_free()
    else:
        _disable_lve_services()


@task("Configuring environment for CloudLinux OS Shared (container): is_posttrans='{is_posttrans}'")
def install_cl_container(is_posttrans):
    """
    Converts "normal" CloudLinux Shared into container by disabling
    some services and installing some packages.
    :param is_posttrans:
        When true, we must do only actions that need to be done during
        %posttrans section of cloudlinux-container package
    """
    if not is_posttrans:
        set_package_state(STATE_INSTALLED, 'cloudlinux-container')
    else:
        _disable_lve_services()


@task("Configuring environment for CloudLinux OS Admin: is_posttrans='{is_posttrans}'")
def install_cl_admin(is_posttrans):
    """
    Converts "normal" CloudLinux into CL Admin by changing default limits
    and some other stuff that may be added later.
    :param is_posttrans:
        When true, we must do only actions that need to be done during
        %posttrans section of cloudlinux-solo-meta package
    """
    if not is_posttrans:
        # TODO: mysql-governor is not so easy to install and
        #       also we don't support shared -> admin migration in production
        #       so just omit this case for now
        set_package_state(STATE_INSTALLED, 'cloudlinux-admin-meta')
        enable_accelerate_wp_free()
    elif not cledition.is_container():
        set_default_limits_state('unlimited')


@task("Configuring environment for CloudLinux OS Shared PRO: is_posttrans='{is_posttrans}'")
def install_cl_shared_pro(is_posttrans):
    """
    Converts "normal" CloudLinux Shared into CL Shared PRO.
    :param is_posttrans:
        When true, we must do only actions that need to be done during
        %posttrans section of cloudlinux-solo-meta package
    """
    # cannot do that in posttrans because awp may be not installed
    # cron will handle that later
    if not is_posttrans:
        enable_accelerate_wp_free()


@task("Converting environment from CloudLinux OS Solo to CloudLinux OS Shared: is_posttrans='{is_posttrans}'")
def uninstall_cl_solo(is_posttrans):
    """
    Converts CL Solo into "normal" CloudLinux by enabling
    some services and installing some packages.
    :param is_posttrans:
        When true, we must do only actions that need to be done during
        %posttrans section of cloudlinux-solo-meta package
    """
    if not is_posttrans:
        set_package_state(STATE_REMOVED, 'cloudlinux-solo-meta')
        set_package_state(STATE_INSTALLED, 'cagefs')
    elif not cledition.is_container():
        _enable_lve_services()


def _resolve_auto_target():
    """
    Resolves edition for 'auto' target.
    """
    if cledition.is_cl_solo_edition():
        return TargetEdition.SOLO.value
    elif cledition.is_cl_admin_edition():
        return TargetEdition.ADMIN.value
    elif cledition.is_cl_shared_edition():
        return TargetEdition.SHARED.value
    elif cledition.is_cl_shared_pro_edition():
        return TargetEdition.SHARED_PRO.value
    else:
        raise NotImplementedError()


def main():
    """
    Entry point for this program.
    Parses arguments and calls needed methods.
    """
    setup_logger(None, LOGS_PATH)
    logging.debug('Executing "%s"', ' '.join(sys.argv))

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='action')
    reconfigure = subparsers.add_parser('reconfigure')
    reconfigure.add_argument('-t', '--target', choices=[e.value for e in TargetEdition], required=True)
    reconfigure.add_argument('--posttrans', default=False, action='store_true')

    args = parser.parse_args()
    if args.action is None:
        parser.print_help()
        return

    target = _resolve_auto_target() if args.target == 'auto' else args.target

    match target:
        case TargetEdition.CONTAINER.value:
            install_cl_container(args.posttrans)
        case TargetEdition.SOLO.value:
            install_cl_solo(args.posttrans)
        case TargetEdition.ADMIN.value:
            install_cl_admin(args.posttrans)
        case TargetEdition.SHARED.value:
            uninstall_cl_solo(args.posttrans)
        case TargetEdition.SHARED_PRO.value:
            install_cl_shared_pro(args.posttrans)
        case _:
            raise NotImplementedError()


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)

    init_lve_utils_sentry_client('clconfigure')
    main()

Zerion Mini Shell 1.0