Mini Shell

Direktori : /usr/share/l.v.e-manager/commons/hooks/
Upload File :
Current File : //usr/share/l.v.e-manager/commons/hooks/py_node_selector_hook.py

#!/opt/cloudlinux/venv/bin/python3 -sbb
# 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
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

import logging
from clcommon.cpapi import PLESK_NAME, UNKNOWN_CP_NAME, getCPName

from clselect import clselectctlpython

from clselect.clselectnodejs.apps_manager import ApplicationsManager as NodeJsAppsManager
from clselect.clselectpython.apps_manager import ApplicationsManager as PythonAppsManager
from clcommon.clpwd import drop_privileges

from clcommon.public_hooks.lib import ModifyDomainHook, ModifyUserHook

from clcommon.cpapi import Feature, is_panel_feature_supported


class PyNodeSelectorHook(ModifyDomainHook, ModifyUserHook):

    def _is_py_node_selector_available(self):
        """
        PyNode selector is not available on two control panels:
         - Plesk, which has it's own selector
         - Unknown aka without control panel at all
        Last one added "just in case"...
        :return: boolean if selector can work on current control panel
        """
        return getCPName() not in [PLESK_NAME, UNKNOWN_CP_NAME]

    def post_modify_domain(self, username, domain, new_domain=None, include_subdomains=None, **kwargs):
        if (not is_panel_feature_supported(Feature.NODEJS_SELECTOR)
                and not is_panel_feature_supported(Feature.PYTHON_SELECTOR)):
            not_supported_msg = 'Python and Node selectors are not supported in current edition or environment. ' +\
                'PyNodeSelectorHook.post_modify_domain skipped'
            print(not_supported_msg)
            logging.debug(not_supported_msg)
            return
        if not self._is_py_node_selector_available():
            logging.debug('Python & Node selectors are not available '
                          'for %s control panel. Skip and ignore')
            return

        if new_domain and domain != new_domain:
            self._post_rename_domain(
                username, old_domain=domain, new_domain=new_domain,
                include_subdomains=include_subdomains)

    def _post_rename_domain(self, username, old_domain, new_domain, include_subdomains, **kwargs):
        """
        Automatically update domain in selector configs when it is being renamed.
        """
        apps_managers = [NodeJsAppsManager(), PythonAppsManager()]

        with drop_privileges(username):
            for apps_manager in apps_managers:
                # TODO: where is config locking??
                apps_manager.replace_domain_in_configs(
                    username, old_domain, new_domain, include_subdomains)

    def post_modify_user(self, username, new_name=None, new_owner=None, **kwargs):
        if (not is_panel_feature_supported(Feature.NODEJS_SELECTOR)
                and not is_panel_feature_supported(Feature.PYTHON_SELECTOR)):
            not_supported_msg = 'Python and Node selectors are not supported in current edition or environment. ' +\
                'PyNodeSelectorHook.post_modify_user skipped'
            print(not_supported_msg)
            logging.debug(not_supported_msg)
            return
        if not self._is_py_node_selector_available():
            logging.debug('Python & Node selectors are not available '
                          'for %s control panel. Skip and ignore')
            return

        if new_name is not None:
            self._post_rename_user(username, new_name)

    def _post_rename_user(self, old_name, new_name, **kwargs):
        """Catch user rename event and update homedir"""
        # fix home dir for python selector while changing name of user
        # TODO: nodejs selector? home dir change?
        clselectctlpython.fix_homedir(new_name)

Zerion Mini Shell 1.0